summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/file_util.cc11
-rw-r--r--base/file_util.h8
-rw-r--r--base/file_util_unittest.cc16
-rw-r--r--base/file_util_win.cc9
-rw-r--r--chrome/installer/setup/uninstall.cc5
5 files changed, 32 insertions, 17 deletions
diff --git a/base/file_util.cc b/base/file_util.cc
index 5bc73ce..aea6e62 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -194,6 +194,15 @@ bool ReadFileToString(const FilePath& path, std::string* contents) {
return true;
}
+bool IsDirectoryEmpty(const FilePath& dir_path) {
+ FileEnumerator files(dir_path, false,
+ static_cast<FileEnumerator::FILE_TYPE>(
+ FileEnumerator::FILES | FileEnumerator::DIRECTORIES));
+ if (files.Next().value().empty())
+ return true;
+ return false;
+}
+
FILE* CreateAndOpenTemporaryFile(FilePath* path) {
FilePath directory;
if (!GetTempDir(&directory))
diff --git a/base/file_util.h b/base/file_util.h
index 16f18f1..414f775 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -212,16 +212,16 @@ bool TaskbarPinShortcutLink(const wchar_t* shortcut);
// already be pinned to the taskbar.
bool TaskbarUnpinShortcutLink(const wchar_t* shortcut);
-// Return true if the given directory is empty
-bool IsDirectoryEmpty(const std::wstring& dir_path);
-
// Copy from_path to to_path recursively and then delete from_path recursively.
// Returns true if all operations succeed.
// This function simulates Move(), but unlike Move() it works across volumes.
// This fuction is not transactional.
bool CopyAndDeleteDirectory(const FilePath& from_path,
const FilePath& to_path);
-#endif
+#endif // defined(OS_WIN)
+
+// Return true if the given directory is empty
+bool IsDirectoryEmpty(const FilePath& dir_path);
// Get the temporary directory provided by the system.
bool GetTempDir(FilePath* path);
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index efe79c9..682376a 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1622,4 +1622,20 @@ TEST_F(FileUtilTest, LastModified) {
ASSERT_TRUE(file_info.last_modified == modification_time);
}
+TEST_F(FileUtilTest, IsDirectoryEmpty) {
+ FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
+
+ ASSERT_FALSE(file_util::PathExists(empty_dir));
+
+ ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
+
+ EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
+
+ FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
+ std::string bar("baz");
+ ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
+
+ EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
+}
+
} // namespace
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index fb65feb..fcad39c 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -431,15 +431,6 @@ bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) {
return result > 32;
}
-bool IsDirectoryEmpty(const std::wstring& dir_path) {
- FileEnumerator files(FilePath(dir_path), false,
- static_cast<FileEnumerator::FILE_TYPE>(
- FileEnumerator::FILES | FileEnumerator::DIRECTORIES));
- if (files.Next().value().empty())
- return true;
- return false;
-}
-
bool GetTempDir(FilePath* path) {
wchar_t temp_path[MAX_PATH + 1];
DWORD path_len = ::GetTempPath(MAX_PATH, temp_path);
diff --git a/chrome/installer/setup/uninstall.cc b/chrome/installer/setup/uninstall.cc
index 4338cb6..fe3754b 100644
--- a/chrome/installer/setup/uninstall.cc
+++ b/chrome/installer/setup/uninstall.cc
@@ -142,15 +142,14 @@ bool ScheduleParentAndGrandparentForDeletion(const FilePath& path) {
bool DeleteEmptyParentDir(const FilePath& path) {
bool ret = true;
FilePath parent_dir = path.DirName();
- if (!parent_dir.empty() && file_util::IsDirectoryEmpty(parent_dir.value())) {
+ if (!parent_dir.empty() && file_util::IsDirectoryEmpty(parent_dir)) {
if (!file_util::Delete(parent_dir, true)) {
ret = false;
LOG(ERROR) << "Failed to delete folder: " << parent_dir.value();
}
parent_dir = parent_dir.DirName();
- if (!parent_dir.empty() &&
- file_util::IsDirectoryEmpty(parent_dir.value())) {
+ if (!parent_dir.empty() && file_util::IsDirectoryEmpty(parent_dir)) {
if (!file_util::Delete(parent_dir, true)) {
ret = false;
LOG(ERROR) << "Failed to delete folder: " << parent_dir.value();