From a872890d874c25a2729aca4c1e3ffb301a33e259 Mon Sep 17 00:00:00 2001 From: Thomas Newton Date: Sun, 15 Oct 2023 14:58:38 +0100 Subject: [PATCH] Tidy path validation --- cpp/src/arrow/filesystem/azurefs.cc | 32 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/filesystem/azurefs.cc b/cpp/src/arrow/filesystem/azurefs.cc index baad308af557d..64dfb321e4d53 100644 --- a/cpp/src/arrow/filesystem/azurefs.cc +++ b/cpp/src/arrow/filesystem/azurefs.cc @@ -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") || @@ -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 std::shared_ptr GetObjectMetadata(const ObjectResult& result) { auto md = std::make_shared(); @@ -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()); @@ -346,12 +366,7 @@ class AzureFileSystem::Impl { Result> 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( std::move(service_client_->GetBlobContainerClient(path.container) @@ -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( std::move(service_client_->GetBlobContainerClient(path.container)