|
| 1 | +#include "archive_contents.hpp" |
| 2 | +#include "archive_file_system.hpp" |
| 3 | + |
| 4 | +#include "duckdb/common/exception.hpp" |
| 5 | +#include "duckdb/common/numeric_utils.hpp" |
| 6 | +#include "duckdb/common/file_opener.hpp" |
| 7 | +#include "duckdb/function/scalar/string_common.hpp" |
| 8 | +#include "duckdb/main/client_context.hpp" |
| 9 | + |
| 10 | +#ifdef ENABLE_LIBARCHIVE |
| 11 | + |
| 12 | +namespace duckdb { |
| 13 | + |
| 14 | +struct ReadArchiveFunctionData : public GlobalTableFunctionState { |
| 15 | + ReadArchiveFunctionData() : finished(false) {} |
| 16 | + bool finished; |
| 17 | +}; |
| 18 | + |
| 19 | +struct ReadArchiveFunctionBindData : public TableFunctionData { |
| 20 | + string file_path; |
| 21 | +}; |
| 22 | + |
| 23 | +void ReadArchiveFunction(ClientContext &context, TableFunctionInput &data, |
| 24 | + DataChunk &output) { |
| 25 | + auto bind_data = data.bind_data->Cast<ReadArchiveFunctionBindData>(); |
| 26 | + auto &global_data = data.global_state->Cast<ReadArchiveFunctionData>(); |
| 27 | + if (global_data.finished) { |
| 28 | + return; |
| 29 | + } |
| 30 | + auto &zip_path = bind_data.file_path; |
| 31 | + |
| 32 | + auto &fs = FileSystem::GetFileSystem(context); |
| 33 | + if (!fs.FileExists(zip_path)) { |
| 34 | + throw IOException("Archive file does not exist: %s", zip_path); |
| 35 | + } |
| 36 | + |
| 37 | + auto handle = fs.OpenFile(zip_path, FileOpenFlags::FILE_FLAGS_READ); |
| 38 | + if (!handle) { |
| 39 | + throw IOException("Failed to open file: %s", zip_path); |
| 40 | + } |
| 41 | + |
| 42 | + if (!handle->CanSeek()) { |
| 43 | + throw IOException("Cannot seek"); |
| 44 | + } |
| 45 | + |
| 46 | + idx_t size = handle->GetFileSize(); |
| 47 | + idx_t count = 0; |
| 48 | + |
| 49 | + struct archive *archive = archive_read_new(); |
| 50 | + try { |
| 51 | + if (archive_read_support_filter_all(archive)) { |
| 52 | + throw IOException("Failed to init libarchive (filter all): %s", |
| 53 | + archive_error_string(archive)); |
| 54 | + } |
| 55 | + if (archive_read_support_format_all(archive)) { |
| 56 | + throw IOException("Failed to init libarchive (format all): %s", |
| 57 | + archive_error_string(archive)); |
| 58 | + } |
| 59 | + unique_ptr<LibArchiveHandle> zipHandle = |
| 60 | + make_uniq<LibArchiveHandle>(std::move(handle)); |
| 61 | + // TODO: Add skip? |
| 62 | + if (archive_read_set_seek_callback(archive, FileSystemZipSeekFunc)) { |
| 63 | + throw IOException("Failed to init libarchive (seek callback): %s", |
| 64 | + archive_error_string(archive)); |
| 65 | + } |
| 66 | + if (archive_read_open(archive, zipHandle.get(), &FileSystemZipOpenFunc, |
| 67 | + &FileSystemZipReadFunc, &FileSystemZipCloseFunc)) { |
| 68 | + throw IOException("Failed to init libarchive (read callback): %s", |
| 69 | + archive_error_string(archive)); |
| 70 | + } |
| 71 | + struct archive_entry *entry = archive_entry_new2(archive); |
| 72 | + try { |
| 73 | + while (archive_read_next_header2(archive, entry) == ARCHIVE_OK) { |
| 74 | + auto pathName = archive_entry_pathname(entry); |
| 75 | + auto fileSize = archive_entry_size(entry); |
| 76 | + auto fileType = archive_entry_filetype(entry); |
| 77 | + auto isDir = fileType == AE_IFDIR; |
| 78 | + |
| 79 | + idx_t col = 0; |
| 80 | + output.SetValue(col++, count, Value(pathName)); |
| 81 | + output.SetValue(col++, count, |
| 82 | + Value::UBIGINT(NumericCast<uint64_t>(fileSize))); |
| 83 | + output.SetValue(col++, count, Value::BOOLEAN(isDir)); |
| 84 | + |
| 85 | + count++; |
| 86 | + } |
| 87 | + |
| 88 | + archive_entry_free(entry); |
| 89 | + archive_read_free(archive); |
| 90 | + } catch (Exception &ex2) { |
| 91 | + archive_entry_free(entry); |
| 92 | + throw; |
| 93 | + } |
| 94 | + } catch (IOException &ex) { |
| 95 | + archive_read_free(archive); |
| 96 | + throw; |
| 97 | + } catch (Exception &ex) { |
| 98 | + archive_read_free(archive); |
| 99 | + throw; |
| 100 | + } |
| 101 | + |
| 102 | + output.SetCardinality(count); |
| 103 | + global_data.finished = true; |
| 104 | +} |
| 105 | + |
| 106 | +unique_ptr<FunctionData> |
| 107 | +ReadArchiveFunctionBind(ClientContext &context, TableFunctionBindInput &input, |
| 108 | + vector<LogicalType> &return_types, |
| 109 | + vector<string> &names) { |
| 110 | + auto result = make_uniq<ReadArchiveFunctionBindData>(); |
| 111 | + result->file_path = input.inputs[0].GetValue<string>(); |
| 112 | + |
| 113 | + return_types.push_back(LogicalType::VARCHAR); |
| 114 | + names.emplace_back("file_name"); |
| 115 | + |
| 116 | + return_types.push_back(LogicalType::UBIGINT); |
| 117 | + names.emplace_back("file_size"); |
| 118 | + |
| 119 | + return_types.push_back(LogicalType::BOOLEAN); |
| 120 | + names.emplace_back("is_directory"); |
| 121 | + |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +unique_ptr<GlobalTableFunctionState> |
| 126 | +ReadArchiveFunctionInit(ClientContext &context, TableFunctionInitInput &input) { |
| 127 | + return std::move(make_uniq<ReadArchiveFunctionData>()); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace duckdb |
| 131 | + |
| 132 | +#endif // ENABLE_LIBARCHIVE |
0 commit comments