Move IsDir() to utils.
This patch moves the generic IsDir() function to the utils.cc file
where other similar functions reside. It also adds unit tests for it.
BUG=None
TEST=Added unittests.
Change-Id: Iba05059f72a1156bb5f19ae0624ae7025af4c522
Reviewed-on: https://chromium-review.googlesource.com/200691
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/payload_generator/generate_delta_main.cc b/payload_generator/generate_delta_main.cc
index e9f09f7..a07df67 100644
--- a/payload_generator/generate_delta_main.cc
+++ b/payload_generator/generate_delta_main.cc
@@ -113,12 +113,6 @@
namespace {
-bool IsDir(const char* path) {
- struct stat stbuf;
- TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
- return S_ISDIR(stbuf.st_mode);
-}
-
void ParseSignatureSizes(const string& signature_sizes_flag,
vector<int>* signature_sizes) {
signature_sizes->clear();
@@ -366,7 +360,8 @@
LOG(INFO) << "Generating delta update";
CHECK(!FLAGS_old_dir.empty());
CHECK(!FLAGS_new_dir.empty());
- if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
+ if (!utils::IsDir(FLAGS_old_dir.c_str()) ||
+ !utils::IsDir(FLAGS_new_dir.c_str())) {
LOG(FATAL) << "old_dir or new_dir not directory";
}
} else {
diff --git a/utils.cc b/utils.cc
index 6d2abac..70bcd34 100644
--- a/utils.cc
+++ b/utils.cc
@@ -520,6 +520,12 @@
return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0;
}
+bool IsDir(const char* path) {
+ struct stat stbuf;
+ TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
+ return S_ISDIR(stbuf.st_mode);
+}
+
std::string TempFilename(string path) {
static const string suffix("XXXXXX");
CHECK(StringHasSuffix(path, suffix));
diff --git a/utils.h b/utils.h
index 2961edf..e83db5c 100644
--- a/utils.h
+++ b/utils.h
@@ -105,6 +105,9 @@
// Returns true if |path| exists and is a symbolic link.
bool IsSymlink(const char* path);
+// Returns true if |path| exists and is a directory.
+bool IsDir(const char* path);
+
// The last 6 chars of path must be XXXXXX. They will be randomly changed
// and a non-existent path will be returned. Intentionally makes a copy
// of the string passed in.
diff --git a/utils_unittest.cc b/utils_unittest.cc
index 77a7d9b..3103349 100644
--- a/utils_unittest.cc
+++ b/utils_unittest.cc
@@ -198,6 +198,20 @@
EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
}
+TEST(UtilsTest, IsDirTest) {
+ string temp_dir;
+ EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
+ string temp_file = temp_dir + "temp-file";
+ EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
+ string temp_symlink = temp_dir + "temp-symlink";
+ EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
+ EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
+ EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
+ EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
+ EXPECT_FALSE(utils::IsDir("/non/existent/path"));
+ ASSERT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
+}
+
TEST(UtilsTest, TempFilenameTest) {
const string original = "/foo.XXXXXX";
const string result = utils::TempFilename(original);