Skip to content

Commit

Permalink
Tidy path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Newton committed Oct 15, 2023
1 parent 4c652a9 commit a872890
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions cpp/src/arrow/filesystem/azurefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct AzurePath {
// http://127.0.0.1/accountName/pathToBlob
if (internal::IsLikelyUri(s)) {
return Status::Invalid(
"Expected an Azure object path of the form 'container/key...', got a URI: '", s, "'");
"Expected an Azure object path of the form 'container/key...', got a URI: '", s,
"'");
}
auto src = internal::RemoveTrailingSlash(s);
if (arrow::internal::StartsWith(src, "https://127.0.0.1") ||
Expand Down Expand Up @@ -163,6 +164,25 @@ struct AzurePath {
}
};

Status PathNotFound(const AzurePath& path) {
return ::arrow::fs::internal::PathNotFound(path.full_path);
}

Status NotAFile(const AzurePath& path) {
return ::arrow::fs::internal::NotAFile(path.full_path);
}

Status ValidateFilePath(const AzurePath& path) {
if (path.container.empty()) {
return PathNotFound(path);
}

if (path.container.empty() || path.path_to_file.empty()) {
return NotAFile(path);
}
return Status::OK();
}

template <typename ObjectResult>
std::shared_ptr<const KeyValueMetadata> GetObjectMetadata(const ObjectResult& result) {
auto md = std::make_shared<KeyValueMetadata>();
Expand Down Expand Up @@ -204,7 +224,7 @@ class ObjectInputFile final : public io::RandomAccessFile {
return Status::OK();
} catch (const Azure::Storage::StorageException& exception) {
if (exception.StatusCode == Azure::Core::Http::HttpStatusCode::NotFound) {
// Could be either container or blob not found.
// Could be either container or blob not found.
return ::arrow::fs::internal::PathNotFound(path_.full_path);
}
return Status::IOError(exception.RawResponse->GetReasonPhrase());
Expand Down Expand Up @@ -346,12 +366,7 @@ class AzureFileSystem::Impl {
Result<std::shared_ptr<ObjectInputFile>> OpenInputFile(const std::string& s,
AzureFileSystem* fs) {
ARROW_ASSIGN_OR_RAISE(auto path, AzurePath::FromString(s));

// TODO: Return NotAFile if path.path_to_file is empty. Return PathNotFound if account
// name or container name is empty.
if (path.empty()) {
return ::arrow::fs::internal::PathNotFound(path.full_path);
}
ValidateFilePath(path);

auto blob_client = std::make_shared<Azure::Storage::Blobs::BlobClient>(
std::move(service_client_->GetBlobContainerClient(path.container)
Expand All @@ -372,6 +387,7 @@ class AzureFileSystem::Impl {
}

ARROW_ASSIGN_OR_RAISE(auto path, AzurePath::FromString(info.path()));
ValidateFilePath(path);

auto blob_client = std::make_shared<Azure::Storage::Blobs::BlobClient>(
std::move(service_client_->GetBlobContainerClient(path.container)
Expand Down

0 comments on commit a872890

Please sign in to comment.