Skip to content

Check the file is accessible before trying to read it #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/motherduck_destination_server.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -85,12 +86,23 @@ get_encryption_key(const std::string &filename,
return encryption_key_it->second;
}

void validate_file(const std::string &filename) {
std::ifstream fs(filename.c_str());
if (fs.good()) {
fs.close();
return;
}
throw std::invalid_argument("File <" + filename +
"> is missing or inaccessible");
}

void process_file(
duckdb::Connection &con, const std::string &filename,
const std::string &decryption_key, std::vector<std::string> &utf8_columns,
const std::string &null_value,
const std::function<void(const std::string &view_name)> &process_view) {

validate_file(filename);
auto table = decryption_key.empty()
? read_unencrypted_csv(filename, utf8_columns, null_value)
: read_encrypted_csv(filename, decryption_key, utf8_columns,
Expand Down
34 changes: 30 additions & 4 deletions test/integration/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ bool NO_FAIL(const grpc::Status &status) {
return status.ok();
}

bool IS_FAIL(const grpc::Status &status, const std::string &expected_error) {
if (!status.ok() && status.error_message() != expected_error) {
fprintf(stderr, "Query failed with unexpected message: %s\n",
status.error_message().c_str());
bool IS_FAIL(const grpc::Status &status,
const std::string &expected_error_snippet) {
if (!status.ok() && status.error_message().find(expected_error_snippet) ==
std::string::npos) {
UNSCOPED_INFO("Query failed with unexpected message: " +
status.error_message());
return false;
}
return !status.ok();
}
Expand Down Expand Up @@ -818,4 +821,27 @@ TEST_CASE("Truncate fails if synced_column is missing") {
auto status = service.Truncate(nullptr, &request, &response);

REQUIRE_FAIL(status, "Synced column is required");
}

TEST_CASE("reading inaccessible or nonexistent files fails with obvious error "
"message") {
DestinationSdkImpl service;

const std::string bad_file_name = TEST_RESOURCES_DIR + "nonexistent.csv";
::fivetran_sdk::WriteBatchRequest request;

auto token = std::getenv("motherduck_token");
REQUIRE(token);
(*request.mutable_configuration())["motherduck_token"] = token;
(*request.mutable_configuration())["motherduck_database"] = "fivetran_test";
request.mutable_csv()->set_encryption(::fivetran_sdk::Encryption::AES);
request.mutable_csv()->set_compression(::fivetran_sdk::Compression::ZSTD);
define_test_table(request, "unused_table");

request.add_replace_files(bad_file_name);
(*request.mutable_keys())[bad_file_name] = "whatever";

::fivetran_sdk::WriteBatchResponse response;
auto status = service.WriteBatch(nullptr, &request, &response);
REQUIRE_FAIL(status, "is missing or inaccessible");
}