|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/io/fs_file_io.h" |
| 21 | + |
| 22 | +#include <fcntl.h> |
| 23 | + |
| 24 | +#include <cassert> |
| 25 | +#include <filesystem> |
| 26 | +#include <format> |
| 27 | + |
| 28 | +#include <sys/stat.h> |
| 29 | + |
| 30 | +#include "iceberg/exception.h" |
| 31 | +#include "iceberg/io/fs_file_reader.h" |
| 32 | +#include "iceberg/io/fs_file_writer.h" |
| 33 | + |
| 34 | +namespace iceberg::io { |
| 35 | + |
| 36 | +bool FsInputFile::exists() const { return std::filesystem::exists(location()); } |
| 37 | + |
| 38 | +int64_t FsInputFile::getLength() const { |
| 39 | + struct stat stat_buffer; |
| 40 | + if (stat(location().c_str(), &stat_buffer) != 0) { |
| 41 | + throw IcebergError(std::format( |
| 42 | + "Failed to get file length. File does not exist or is inaccessible: {}", |
| 43 | + location_)); |
| 44 | + } |
| 45 | + return stat_buffer.st_size; |
| 46 | +} |
| 47 | + |
| 48 | +std::unique_ptr<Reader> FsInputFile::newReader() { |
| 49 | + return std::make_unique<FsFileReader>(location_); |
| 50 | +} |
| 51 | + |
| 52 | +void FsOutputFile::create() { |
| 53 | + // Check if the file already exists |
| 54 | + std::ifstream existing_file(location_); |
| 55 | + bool file_exists = existing_file.good(); |
| 56 | + existing_file.close(); |
| 57 | + |
| 58 | + if (file_exists) { |
| 59 | + throw IcebergError(std::format("File already exists: {}", location_)); |
| 60 | + } |
| 61 | + |
| 62 | + // Create or overwrite the file by opening it in truncating mode |
| 63 | + std::ofstream new_file(location_, std::ios::binary | std::ios::out | std::ios::trunc); |
| 64 | + if (!new_file.is_open()) { |
| 65 | + throw IcebergError(std::format("Failed to create or overwrite file: {}", location_)); |
| 66 | + } |
| 67 | + new_file.close(); |
| 68 | +} |
| 69 | + |
| 70 | +std::unique_ptr<Writer> FsOutputFile::newWriter() { |
| 71 | + return std::make_unique<FsFileWriter>(location_); |
| 72 | +} |
| 73 | + |
| 74 | +std::shared_ptr<InputFile> FsFileIO::newInputFile(const std::string& location) { |
| 75 | + // Check if the file exists |
| 76 | + if (!fileExists(location)) { |
| 77 | + throw IcebergError(std::format("InputFile does not exist: {}", location)); |
| 78 | + } |
| 79 | + |
| 80 | + // Create and return an FsInputFile instance |
| 81 | + return std::make_shared<FsInputFile>(location); |
| 82 | +} |
| 83 | + |
| 84 | +std::shared_ptr<OutputFile> FsFileIO::newOutputFile(const std::string& location) { |
| 85 | + return std::make_shared<FsOutputFile>(location); |
| 86 | +} |
| 87 | + |
| 88 | +void FsFileIO::DeleteFile(const std::string& location) { |
| 89 | + // Check if the file exists |
| 90 | + if (!fileExists(location)) { |
| 91 | + throw IcebergError(std::format("InputFile does not exist: {}", location)); |
| 92 | + } |
| 93 | + std::error_code ec; |
| 94 | + if (std::filesystem::remove(location, ec) == false) { |
| 95 | + throw IcebergError( |
| 96 | + std::format("Failed to delete file: {}, error code: {}", location, ec.message())); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +bool FsFileIO::fileExists(const std::string& location) { |
| 101 | + std::ifstream file(location); |
| 102 | + return file.good(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace iceberg::io |
0 commit comments