You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by jd...@apache.org on 2017/10/18 20:56:08 UTC
[2/3] kudu git commit: fs: move IsDirectoryEmpty to env_util
fs: move IsDirectoryEmpty to env_util
I thought I'd need this in a follow-on patch. I didn't, but I still had the
unit tests written, and I think env_util is a better home for this.
Change-Id: I6f6324ac7507801e5ba8c6ed87c9bb3a3a9ffc1a
Reviewed-on: http://gerrit.cloudera.org:8080/8289
Tested-by: Adar Dembo <ad...@cloudera.com>
Reviewed-by: Andrew Wong <aw...@cloudera.com>
Reviewed-by: Todd Lipcon <to...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/7a460f04
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/7a460f04
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/7a460f04
Branch: refs/heads/master
Commit: 7a460f0455e64ca001994f66e0ce0142fc6f1980
Parents: 27a62b0
Author: Adar Dembo <ad...@cloudera.com>
Authored: Fri Oct 13 16:17:26 2017 -0700
Committer: Todd Lipcon <to...@apache.org>
Committed: Wed Oct 18 19:33:54 2017 +0000
----------------------------------------------------------------------
src/kudu/fs/fs_manager.cc | 17 +----------------
src/kudu/fs/fs_manager.h | 6 ------
src/kudu/util/env_util-test.cc | 16 ++++++++++++++++
src/kudu/util/env_util.cc | 14 ++++++++++++++
src/kudu/util/env_util.h | 6 ++++++
5 files changed, 37 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kudu/blob/7a460f04/src/kudu/fs/fs_manager.cc
----------------------------------------------------------------------
diff --git a/src/kudu/fs/fs_manager.cc b/src/kudu/fs/fs_manager.cc
index 670c397..bf88c63 100644
--- a/src/kudu/fs/fs_manager.cc
+++ b/src/kudu/fs/fs_manager.cc
@@ -371,7 +371,7 @@ Status FsManager::CreateInitialFileSystemLayout(boost::optional<string> uuid) {
continue;
}
bool is_empty;
- RETURN_NOT_OK_PREPEND(IsDirectoryEmpty(root.path, &is_empty),
+ RETURN_NOT_OK_PREPEND(env_util::IsDirectoryEmpty(env_, root.path, &is_empty),
"Unable to check if FSManager root is empty");
if (!is_empty) {
return Status::AlreadyPresent(
@@ -490,21 +490,6 @@ Status FsManager::WriteInstanceMetadata(const InstanceMetadataPB& metadata,
return Status::OK();
}
-Status FsManager::IsDirectoryEmpty(const string& path, bool* is_empty) {
- vector<string> children;
- RETURN_NOT_OK(env_->GetChildren(path, &children));
- for (const string& child : children) {
- if (child == "." || child == "..") {
- continue;
- } else {
- *is_empty = false;
- return Status::OK();
- }
- }
- *is_empty = true;
- return Status::OK();
-}
-
Status FsManager::CreateDirIfMissing(const string& path, bool* created) {
return env_util::CreateDirIfMissing(env_, path, created);
}
http://git-wip-us.apache.org/repos/asf/kudu/blob/7a460f04/src/kudu/fs/fs_manager.h
----------------------------------------------------------------------
diff --git a/src/kudu/fs/fs_manager.h b/src/kudu/fs/fs_manager.h
index f5ec089..b0488a8 100644
--- a/src/kudu/fs/fs_manager.h
+++ b/src/kudu/fs/fs_manager.h
@@ -255,12 +255,6 @@ class FsManager {
Status WriteInstanceMetadata(const InstanceMetadataPB& metadata,
const std::string& root);
- // Checks if 'path' is an empty directory.
- //
- // Returns an error if it's not a directory. Otherwise, sets 'is_empty'
- // accordingly.
- Status IsDirectoryEmpty(const std::string& path, bool* is_empty);
-
// ==========================================================================
// file-system helpers
// ==========================================================================
http://git-wip-us.apache.org/repos/asf/kudu/blob/7a460f04/src/kudu/util/env_util-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/env_util-test.cc b/src/kudu/util/env_util-test.cc
index b919b4a..9c8266f 100644
--- a/src/kudu/util/env_util-test.cc
+++ b/src/kudu/util/env_util-test.cc
@@ -172,5 +172,21 @@ TEST_F(EnvUtilTest, TestDeleteExcessFilesByPattern) {
ASSERT_EQ(expected_set, children_set) << children;
}
+TEST_F(EnvUtilTest, TestIsDirectoryEmpty) {
+ const string kDir = JoinPathSegments(test_dir_, "foo");
+ const string kFile = JoinPathSegments(kDir, "bar");
+
+ bool is_empty;
+ ASSERT_TRUE(env_util::IsDirectoryEmpty(env_, kDir, &is_empty).IsNotFound());
+ ASSERT_OK(env_->CreateDir(kDir));
+ ASSERT_OK(env_util::IsDirectoryEmpty(env_, kDir, &is_empty));
+ ASSERT_TRUE(is_empty);
+
+ unique_ptr<WritableFile> file;
+ ASSERT_OK(env_->NewWritableFile(WritableFileOptions(), kFile, &file));
+ ASSERT_OK(env_util::IsDirectoryEmpty(env_, kDir, &is_empty));
+ ASSERT_FALSE(is_empty);
+}
+
} // namespace env_util
} // namespace kudu
http://git-wip-us.apache.org/repos/asf/kudu/blob/7a460f04/src/kudu/util/env_util.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/env_util.cc b/src/kudu/util/env_util.cc
index ac46b01..1a7a5bc 100644
--- a/src/kudu/util/env_util.cc
+++ b/src/kudu/util/env_util.cc
@@ -267,5 +267,19 @@ Status DeleteTmpFilesRecursively(Env* env, const string& path) {
return env->Walk(path, Env::PRE_ORDER, Bind(&DeleteTmpFilesRecursivelyCb, env));
}
+Status IsDirectoryEmpty(Env* env, const string& path, bool* is_empty) {
+ vector<string> children;
+ RETURN_NOT_OK(env->GetChildren(path, &children));
+ for (const auto& c : children) {
+ if (c == "." || c == "..") {
+ continue;
+ }
+ *is_empty = false;
+ return Status::OK();
+ }
+ *is_empty = true;
+ return Status::OK();
+}
+
} // namespace env_util
} // namespace kudu
http://git-wip-us.apache.org/repos/asf/kudu/blob/7a460f04/src/kudu/util/env_util.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/env_util.h b/src/kudu/util/env_util.h
index 442596f..faba197 100644
--- a/src/kudu/util/env_util.h
+++ b/src/kudu/util/env_util.h
@@ -88,6 +88,12 @@ Status DeleteExcessFilesByPattern(Env* env, const std::string& pattern, int max_
// Deletion errors generate warnings but do not halt the traversal.
Status DeleteTmpFilesRecursively(Env* env, const std::string& path);
+// Checks if 'path' is an empty directory.
+//
+// Returns an error if it's not a directory. Otherwise, sets 'is_empty'
+// accordingly.
+Status IsDirectoryEmpty(Env* env, const std::string& path, bool* is_empty);
+
} // namespace env_util
} // namespace kudu