From 3b53955b85d42c5b31f17b4796ad195432f8786d Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Tue, 7 Dec 2021 21:31:54 +0000 Subject: [PATCH] ARROW-14915: [C++] implement GcsFileSystem::DeleteDirContents --- cpp/src/arrow/filesystem/gcsfs.cc | 3 ++- cpp/src/arrow/filesystem/gcsfs_test.cc | 35 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/filesystem/gcsfs.cc b/cpp/src/arrow/filesystem/gcsfs.cc index 9c3ce804b09..c45d58b5fdb 100644 --- a/cpp/src/arrow/filesystem/gcsfs.cc +++ b/cpp/src/arrow/filesystem/gcsfs.cc @@ -493,7 +493,8 @@ Status GcsFileSystem::DeleteDir(const std::string& path) { } Status GcsFileSystem::DeleteDirContents(const std::string& path) { - return Status::NotImplemented("The GCS FileSystem is not fully implemented"); + ARROW_ASSIGN_OR_RAISE(auto p, GcsPath::FromString(path)); + return impl_->DeleteDirContents(p, io_context()); } Status GcsFileSystem::DeleteRootDirContents() { diff --git a/cpp/src/arrow/filesystem/gcsfs_test.cc b/cpp/src/arrow/filesystem/gcsfs_test.cc index 2703c31e591..3e4431cde81 100644 --- a/cpp/src/arrow/filesystem/gcsfs_test.cc +++ b/cpp/src/arrow/filesystem/gcsfs_test.cc @@ -476,6 +476,41 @@ TEST_F(GcsIntegrationTest, DeleteDirSuccess) { } } +TEST_F(GcsIntegrationTest, DeleteDirContentsSuccess) { + auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); + const char* const kTestFolders[] = { + "a/", "a/0/", "a/0/0/", "a/1/", "a/2/", + }; + for (auto const* f : kTestFolders) { + const auto folder = PreexistingBucketPath() + f; + ASSERT_OK(fs->CreateDir(folder, true)); + for (int i = 0; i != 64; ++i) { + const auto filename = folder + "test-file-" + std::to_string(i); + ASSERT_OK_AND_ASSIGN(auto w, fs->OpenOutputStream(filename, {})); + ASSERT_OK(w->Write(filename.data(), filename.size())); + ASSERT_OK(w->Close()); + } + } + + const auto folder = PreexistingBucketPath() + kTestFolders[0]; + ASSERT_OK(fs->DeleteDirContents(folder)); + arrow::fs::AssertFileInfo(fs.get(), folder, FileType::Directory); + arrow::fs::AssertFileInfo(fs.get(), PreexistingBucketPath(), FileType::Directory); + arrow::fs::AssertFileInfo(fs.get(), PreexistingObjectPath(), FileType::File); + + for (auto const* f : kTestFolders) { + const auto subfolder = PreexistingBucketPath() + f; + if (subfolder == folder) { + continue; + } + arrow::fs::AssertFileInfo(fs.get(), subfolder, FileType::NotFound); + for (int i = 0; i != 64; ++i) { + const auto filename = subfolder + "test-file-" + std::to_string(i); + arrow::fs::AssertFileInfo(fs.get(), filename, FileType::NotFound); + } + } +} + TEST_F(GcsIntegrationTest, DeleteRootDirContents) { auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); EXPECT_RAISES_WITH_MESSAGE_THAT(NotImplemented, HasSubstr("too dangerous"),