From 442911be1b16773cef5c40219983741a71f56dc4 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Fri, 19 Jan 2024 15:22:21 -0300 Subject: [PATCH 1/3] Make DeleteDirOnFileSystem useful in more contexts and also simpler --- cpp/src/arrow/filesystem/azurefs.cc | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cpp/src/arrow/filesystem/azurefs.cc b/cpp/src/arrow/filesystem/azurefs.cc index 730adabd48be..259bd449d163 100644 --- a/cpp/src/arrow/filesystem/azurefs.cc +++ b/cpp/src/arrow/filesystem/azurefs.cc @@ -1649,23 +1649,29 @@ class AzureFileSystem::Impl { /// \pre location.container is not empty. /// \pre location.path is not empty. Status DeleteDirOnFileSystem(const DataLake::DataLakeFileSystemClient& adlfs_client, - const AzureLocation& location) { + const AzureLocation& location, bool recursive, + bool require_dir_to_exist) { DCHECK(!location.container.empty()); DCHECK(!location.path.empty()); auto directory_client = adlfs_client.GetDirectoryClient(location.path); - // XXX: should "directory not found" be considered an error? try { - auto response = directory_client.DeleteRecursive(); - if (response.Value.Deleted) { + auto response = + recursive ? directory_client.DeleteRecursive() : directory_client.DeleteEmpty(); + // Only the "*IfExists" functions ever set Deleted to false. + // All the others either succeed or throw an exception. + DCHECK(response.Value.Deleted); + } catch (const Storage::StorageException& exception) { + if (exception.ErrorCode == "FilesystemNotFound" || + exception.ErrorCode == "PathNotFound") { + if (require_dir_to_exist) { + return PathNotFound(location); + } return Status::OK(); - } else { - return StatusFromErrorResponse(directory_client.GetUrl(), *response.RawResponse, - "Failed to delete a directory: " + location.path); } - } catch (const Storage::StorageException& exception) { return ExceptionToStatus(exception, "Failed to delete a directory: ", location.path, ": ", directory_client.GetUrl()); } + return Status::OK(); } /// \pre location.container is not empty. @@ -1855,7 +1861,8 @@ Status AzureFileSystem::DeleteDir(const std::string& path) { return PathNotFound(location); } if (hns_support == HNSSupport::kEnabled) { - return impl_->DeleteDirOnFileSystem(adlfs_client, location); + return impl_->DeleteDirOnFileSystem(adlfs_client, location, /*recursive=*/true, + /*require_dir_to_exist=*/true); } DCHECK_EQ(hns_support, HNSSupport::kDisabled); auto container_client = impl_->GetBlobContainerClient(location.container); From 0371a54e99b100115119e7b4adfcbdca1f895cdc Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Fri, 19 Jan 2024 15:21:45 -0300 Subject: [PATCH 2/3] Simplify code to not need StatusFromErrorResponse() --- cpp/src/arrow/filesystem/azurefs.cc | 35 ++++------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/cpp/src/arrow/filesystem/azurefs.cc b/cpp/src/arrow/filesystem/azurefs.cc index 259bd449d163..a2022fd6e155 100644 --- a/cpp/src/arrow/filesystem/azurefs.cc +++ b/cpp/src/arrow/filesystem/azurefs.cc @@ -305,30 +305,6 @@ Status ValidateFileLocation(const AzureLocation& location) { return internal::AssertNoTrailingSlash(location.path); } -std::string_view BodyTextView(const Http::RawResponse& raw_response) { - const auto& body = raw_response.GetBody(); -#ifndef NDEBUG - auto& headers = raw_response.GetHeaders(); - auto content_type = headers.find("Content-Type"); - if (content_type != headers.end()) { - DCHECK_EQ(std::string_view{content_type->second}.substr(5), "text/"); - } -#endif - return std::string_view{reinterpret_cast(body.data()), body.size()}; -} - -Status StatusFromErrorResponse(const std::string& url, - const Http::RawResponse& raw_response, - const std::string& context) { - // There isn't an Azure specification that response body on error - // doesn't contain any binary data but we assume it. We hope that - // error response body has useful information for the error. - auto body_text = BodyTextView(raw_response); - return Status::IOError(context, ": ", url, ": ", raw_response.GetReasonPhrase(), " (", - static_cast(raw_response.GetStatusCode()), - "): ", body_text); -} - bool IsContainerNotFound(const Storage::StorageException& e) { if (e.ErrorCode == "ContainerNotFound" || e.ReasonPhrase == "The specified container does not exist." || @@ -1515,13 +1491,9 @@ class AzureFileSystem::Impl { DCHECK(location.path.empty()); try { auto response = container_client.Delete(); - if (response.Value.Deleted) { - return Status::OK(); - } else { - return StatusFromErrorResponse( - container_client.GetUrl(), *response.RawResponse, - "Failed to delete a container: " + location.container); - } + // Only the "*IfExists" functions ever set Deleted to false. + // All the others either succeed or throw an exception. + DCHECK(response.Value.Deleted); } catch (const Storage::StorageException& exception) { if (IsContainerNotFound(exception)) { return PathNotFound(location); @@ -1530,6 +1502,7 @@ class AzureFileSystem::Impl { "Failed to delete a container: ", location.container, ": ", container_client.GetUrl()); } + return Status::OK(); } /// Deletes contents of a directory and possibly the directory itself From b48b5299b552f89702c6dd34c44bcd85d6d3fead Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Fri, 19 Jan 2024 15:12:41 -0300 Subject: [PATCH 3/3] Add comment to IsContainerNotFound() --- cpp/src/arrow/filesystem/azurefs.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/src/arrow/filesystem/azurefs.cc b/cpp/src/arrow/filesystem/azurefs.cc index a2022fd6e155..a5179c22190e 100644 --- a/cpp/src/arrow/filesystem/azurefs.cc +++ b/cpp/src/arrow/filesystem/azurefs.cc @@ -306,6 +306,8 @@ Status ValidateFileLocation(const AzureLocation& location) { } bool IsContainerNotFound(const Storage::StorageException& e) { + // In some situations, only the ReasonPhrase is set and the + // ErrorCode is empty, so we check both. if (e.ErrorCode == "ContainerNotFound" || e.ReasonPhrase == "The specified container does not exist." || e.ReasonPhrase == "The specified filesystem does not exist.") {