Move PathIsWritable, DirectoryExists, ContentsEqual, and TextContentsEqual to the base namespace.

TBR=sky

Review URL: https://codereview.chromium.org/19052005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@211675 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: dcd16611096dcc5e7651763ff888135e0fb305fc
diff --git a/base/file_util.cc b/base/file_util.cc
index 4e1163a..2623b64 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -56,17 +56,6 @@
   return internal::CopyFileUnsafe(from_path, to_path);
 }
 
-}  // namespace base
-
-// -----------------------------------------------------------------------------
-
-namespace file_util {
-
-using base::FileEnumerator;
-using base::FilePath;
-using base::kExtensionSeparator;
-using base::kMaxUniqueFiles;
-
 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
   // We open the file in binary format even if they are text files because
   // we are just comparing that bytes are exactly same in both files and not
@@ -141,6 +130,17 @@
   return true;
 }
 
+}  // namespace base
+
+// -----------------------------------------------------------------------------
+
+namespace file_util {
+
+using base::FileEnumerator;
+using base::FilePath;
+using base::kExtensionSeparator;
+using base::kMaxUniqueFiles;
+
 bool ReadFileToString(const FilePath& path, std::string* contents) {
   if (path.ReferencesParent())
     return false;
diff --git a/base/file_util.h b/base/file_util.h
index 095734b..e6d7ac8 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -116,28 +116,28 @@
 // false otherwise.
 BASE_EXPORT bool PathExists(const FilePath& path);
 
+// Returns true if the given path is writable by the user, false otherwise.
+BASE_EXPORT bool PathIsWritable(const FilePath& path);
+
+// Returns true if the given path exists and is a directory, false otherwise.
+BASE_EXPORT bool DirectoryExists(const FilePath& path);
+
+// Returns true if the contents of the two files given are equal, false
+// otherwise.  If either file can't be read, returns false.
+BASE_EXPORT bool ContentsEqual(const FilePath& filename1,
+                               const FilePath& filename2);
+
+// Returns true if the contents of the two text files given are equal, false
+// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
+BASE_EXPORT bool TextContentsEqual(const FilePath& filename1,
+                                   const FilePath& filename2);
+
 }  // namespace base
 
 // -----------------------------------------------------------------------------
 
 namespace file_util {
 
-// Returns true if the given path is writable by the user, false otherwise.
-BASE_EXPORT bool PathIsWritable(const base::FilePath& path);
-
-// Returns true if the given path exists and is a directory, false otherwise.
-BASE_EXPORT bool DirectoryExists(const base::FilePath& path);
-
-// Returns true if the contents of the two files given are equal, false
-// otherwise.  If either file can't be read, returns false.
-BASE_EXPORT bool ContentsEqual(const base::FilePath& filename1,
-                               const base::FilePath& filename2);
-
-// Returns true if the contents of the two text files given are equal, false
-// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
-BASE_EXPORT bool TextContentsEqual(const base::FilePath& filename1,
-                                   const base::FilePath& filename2);
-
 // Read the file at |path| into |contents|, returning true on success.
 // This function fails if the |path| contains path traversal components ('..').
 // |contents| may be NULL, in which case this function is useful for its
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index f438253..c368534 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -314,6 +314,19 @@
   return access(path.value().c_str(), F_OK) == 0;
 }
 
+bool PathIsWritable(const FilePath& path) {
+  ThreadRestrictions::AssertIOAllowed();
+  return access(path.value().c_str(), W_OK) == 0;
+}
+
+bool DirectoryExists(const FilePath& path) {
+  ThreadRestrictions::AssertIOAllowed();
+  stat_wrapper_t file_info;
+  if (CallStat(path.value().c_str(), &file_info) == 0)
+    return S_ISDIR(file_info.st_mode);
+  return false;
+}
+
 }  // namespace base
 
 // -----------------------------------------------------------------------------
@@ -323,25 +336,13 @@
 using base::stat_wrapper_t;
 using base::CallStat;
 using base::CallLstat;
+using base::DirectoryExists;
 using base::FileEnumerator;
 using base::FilePath;
 using base::MakeAbsoluteFilePath;
 using base::RealPath;
 using base::VerifySpecificPathControlledByUser;
 
-bool PathIsWritable(const FilePath& path) {
-  base::ThreadRestrictions::AssertIOAllowed();
-  return access(path.value().c_str(), W_OK) == 0;
-}
-
-bool DirectoryExists(const FilePath& path) {
-  base::ThreadRestrictions::AssertIOAllowed();
-  stat_wrapper_t file_info;
-  if (CallStat(path.value().c_str(), &file_info) == 0)
-    return S_ISDIR(file_info.st_mode);
-  return false;
-}
-
 bool ReadFromFD(int fd, char* buffer, size_t bytes) {
   size_t total_read = 0;
   while (total_read < bytes) {
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index c73d079..e0007d5 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -35,8 +35,11 @@
 // This macro helps avoid wrapped lines in the test structs.
 #define FPL(x) FILE_PATH_LITERAL(x)
 
+using base::DirectoryExists;
 using base::FileEnumerator;
 using base::FilePath;
+using base::PathIsWritable;
+using base::TextContentsEqual;
 
 namespace {
 
@@ -862,7 +865,7 @@
   int mode = 0;
   EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode));
   EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER);
-  EXPECT_TRUE(file_util::PathIsWritable(file_name));
+  EXPECT_TRUE(PathIsWritable(file_name));
 
   // Get rid of the write permission.
   EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u));
@@ -871,7 +874,7 @@
   // Make sure the file can't be write.
   EXPECT_EQ(-1,
             file_util::WriteFile(file_name, kData.data(), kData.length()));
-  EXPECT_FALSE(file_util::PathIsWritable(file_name));
+  EXPECT_FALSE(PathIsWritable(file_name));
 
   // Give read permission.
   EXPECT_TRUE(file_util::SetPosixFilePermissions(
@@ -882,7 +885,7 @@
   // Make sure the file can be write.
   EXPECT_EQ(static_cast<int>(kData.length()),
             file_util::WriteFile(file_name, kData.data(), kData.length()));
-  EXPECT_TRUE(file_util::PathIsWritable(file_name));
+  EXPECT_TRUE(PathIsWritable(file_name));
 
   // Delete the file.
   EXPECT_TRUE(base::Delete(file_name, false));
@@ -1557,20 +1560,19 @@
   FilePath binary_file_diff =
       data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
 
-  EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
-  EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
-  EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
-  EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
-  EXPECT_FALSE(file_util::ContentsEqual(
-      FilePath(FILE_PATH_LITERAL("bogusname")),
-      FilePath(FILE_PATH_LITERAL("bogusname"))));
-  EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
-  EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
-  EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
-  EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
-  EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
-  EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
-  EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
+  EXPECT_TRUE(ContentsEqual(original_file, original_file));
+  EXPECT_TRUE(ContentsEqual(original_file, same_file));
+  EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
+  EXPECT_FALSE(ContentsEqual(original_file, different_file));
+  EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
+                             FilePath(FILE_PATH_LITERAL("bogusname"))));
+  EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
+  EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
+  EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
+  EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
+  EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
+  EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
+  EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
 }
 
 TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
@@ -1606,19 +1608,16 @@
   FilePath blank_line_crlf_file =
       data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
 
-  EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
-  EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(original_file,
-                                            different_first_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(original_file,
-                                            different_last_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
-  EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
-  EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
-  EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
-                                           blank_line_crlf_file));
+  EXPECT_TRUE(TextContentsEqual(original_file, same_file));
+  EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
+  EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
+  EXPECT_FALSE(TextContentsEqual(original_file, different_file));
+  EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
+  EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
+  EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
+  EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
+  EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
+  EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
 }
 
 // We don't need equivalent functionality outside of Windows.
@@ -1688,7 +1687,7 @@
   for (int i = 0; i < 3; i++) {
     ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
     EXPECT_TRUE(base::PathExists(temp_files[i]));
-    EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
+    EXPECT_FALSE(DirectoryExists(temp_files[i]));
   }
   for (int i = 0; i < 3; i++)
     EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
@@ -1742,7 +1741,7 @@
 TEST_F(FileUtilTest, GetShmemTempDirTest) {
   FilePath dir;
   EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
-  EXPECT_TRUE(file_util::DirectoryExists(dir));
+  EXPECT_TRUE(DirectoryExists(dir));
 }
 
 TEST_F(FileUtilTest, CreateDirectoryTest) {
@@ -1776,13 +1775,12 @@
   // Verify assumptions made by the Windows implementation:
   // 1. The current directory always exists.
   // 2. The root directory always exists.
-  ASSERT_TRUE(file_util::DirectoryExists(
-      FilePath(FilePath::kCurrentDirectory)));
+  ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
   FilePath top_level = test_root;
   while (top_level != top_level.DirName()) {
     top_level = top_level.DirName();
   }
-  ASSERT_TRUE(file_util::DirectoryExists(top_level));
+  ASSERT_TRUE(DirectoryExists(top_level));
 
   // Given these assumptions hold, it should be safe to
   // test that "creating" these directories succeeds.
@@ -1807,14 +1805,14 @@
   EXPECT_FALSE(base::PathExists(test_root));
   EXPECT_TRUE(file_util::CreateDirectory(test_root));
   EXPECT_TRUE(base::PathExists(test_root));
-  EXPECT_TRUE(file_util::DirectoryExists(test_root));
+  EXPECT_TRUE(DirectoryExists(test_root));
   // Check a file
   FilePath test_path =
       test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
   EXPECT_FALSE(base::PathExists(test_path));
   CreateTextFile(test_path, L"test file");
   EXPECT_TRUE(base::PathExists(test_path));
-  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+  EXPECT_FALSE(DirectoryExists(test_path));
   EXPECT_TRUE(base::Delete(test_path, false));
 
   EXPECT_TRUE(base::Delete(test_root, true));
diff --git a/base/files/file_util_proxy.cc b/base/files/file_util_proxy.cc
index 721f671..fee97fb 100644
--- a/base/files/file_util_proxy.cc
+++ b/base/files/file_util_proxy.cc
@@ -197,7 +197,7 @@
     PlatformFile* file_handle, bool* created) {
   DCHECK(file_handle);
   DCHECK(created);
-  if (!file_util::DirectoryExists(file_path.DirName())) {
+  if (!DirectoryExists(file_path.DirName())) {
     // If its parent does not exist, should return NOT_FOUND error.
     return PLATFORM_FILE_ERROR_NOT_FOUND;
   }
diff --git a/base/files/scoped_temp_dir.cc b/base/files/scoped_temp_dir.cc
index 5666ce1..3e90349 100644
--- a/base/files/scoped_temp_dir.cc
+++ b/base/files/scoped_temp_dir.cc
@@ -52,8 +52,7 @@
   if (!path_.empty())
     return false;
 
-  if (!file_util::DirectoryExists(path) &&
-      !file_util::CreateDirectory(path))
+  if (!DirectoryExists(path) && !file_util::CreateDirectory(path))
     return false;
 
   path_ = path;
@@ -80,7 +79,7 @@
 }
 
 bool ScopedTempDir::IsValid() const {
-  return !path_.empty() && file_util::DirectoryExists(path_);
+  return !path_.empty() && DirectoryExists(path_);
 }
 
 }  // namespace base
diff --git a/base/files/scoped_temp_dir_unittest.cc b/base/files/scoped_temp_dir_unittest.cc
index 7acec84..75afa61 100644
--- a/base/files/scoped_temp_dir_unittest.cc
+++ b/base/files/scoped_temp_dir_unittest.cc
@@ -17,32 +17,32 @@
                                     &test_path);
 
   // Against an existing dir, it should get destroyed when leaving scope.
-  EXPECT_TRUE(file_util::DirectoryExists(test_path));
+  EXPECT_TRUE(DirectoryExists(test_path));
   {
     ScopedTempDir dir;
     EXPECT_TRUE(dir.Set(test_path));
     EXPECT_TRUE(dir.IsValid());
   }
-  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+  EXPECT_FALSE(DirectoryExists(test_path));
 
   {
     ScopedTempDir dir;
     EXPECT_TRUE(dir.Set(test_path));
     // Now the dir doesn't exist, so ensure that it gets created.
-    EXPECT_TRUE(file_util::DirectoryExists(test_path));
+    EXPECT_TRUE(DirectoryExists(test_path));
     // When we call Release(), it shouldn't get destroyed when leaving scope.
     FilePath path = dir.Take();
     EXPECT_EQ(path.value(), test_path.value());
     EXPECT_FALSE(dir.IsValid());
   }
-  EXPECT_TRUE(file_util::DirectoryExists(test_path));
+  EXPECT_TRUE(DirectoryExists(test_path));
 
   // Clean up.
   {
     ScopedTempDir dir;
     EXPECT_TRUE(dir.Set(test_path));
   }
-  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+  EXPECT_FALSE(DirectoryExists(test_path));
 }
 
 TEST(ScopedTempDir, TempDir) {
@@ -53,12 +53,12 @@
     ScopedTempDir dir;
     EXPECT_TRUE(dir.CreateUniqueTempDir());
     test_path = dir.path();
-    EXPECT_TRUE(file_util::DirectoryExists(test_path));
+    EXPECT_TRUE(DirectoryExists(test_path));
     FilePath tmp_dir;
     EXPECT_TRUE(file_util::GetTempDir(&tmp_dir));
     EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
   }
-  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+  EXPECT_FALSE(DirectoryExists(test_path));
 }
 
 TEST(ScopedTempDir, UniqueTempDirUnderPath) {
@@ -72,11 +72,11 @@
     ScopedTempDir dir;
     EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
     test_path = dir.path();
-    EXPECT_TRUE(file_util::DirectoryExists(test_path));
+    EXPECT_TRUE(DirectoryExists(test_path));
     EXPECT_TRUE(base_path.IsParent(test_path));
     EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
   }
-  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+  EXPECT_FALSE(DirectoryExists(test_path));
   base::Delete(base_path, true);
 }
 
diff --git a/base/json/json_value_serializer_unittest.cc b/base/json/json_value_serializer_unittest.cc
index ce7129f..519af84 100644
--- a/base/json/json_value_serializer_unittest.cc
+++ b/base/json/json_value_serializer_unittest.cc
@@ -423,8 +423,7 @@
   ASSERT_TRUE(PathExists(written_file_path));
 
   // Now compare file contents.
-  EXPECT_TRUE(file_util::TextContentsEqual(original_file_path,
-                                           written_file_path));
+  EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
   EXPECT_TRUE(base::Delete(written_file_path, false));
 }
 
@@ -451,8 +450,7 @@
   ASSERT_TRUE(PathExists(written_file_path));
 
   // Now compare file contents.
-  EXPECT_TRUE(file_util::TextContentsEqual(original_file_path,
-                                           written_file_path));
+  EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
   EXPECT_TRUE(base::Delete(written_file_path, false));
 }
 
diff --git a/base/nix/mime_util_xdg.cc b/base/nix/mime_util_xdg.cc
index 7c1d660..1a41394 100644
--- a/base/nix/mime_util_xdg.cc
+++ b/base/nix/mime_util_xdg.cc
@@ -168,7 +168,7 @@
       &MimeUtilConstants::GetInstance()->icon_dirs_;
   for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
     theme_path = iter->first.Append(name);
-    if (!file_util::DirectoryExists(theme_path))
+    if (!DirectoryExists(theme_path))
       continue;
     FilePath theme_index = theme_path.Append("index.theme");
     if (!index_theme_loaded_ && PathExists(theme_index)) {
@@ -387,7 +387,7 @@
 
 bool CheckDirExistsAndGetMtime(const FilePath& dir,
                                base::Time* last_modified) {
-  if (!file_util::DirectoryExists(dir))
+  if (!DirectoryExists(dir))
     return false;
   base::PlatformFileInfo file_info;
   if (!file_util::GetFileInfo(dir, &file_info))
@@ -406,7 +406,7 @@
 
 // For a xdg directory |dir|, add the appropriate icon sub-directories.
 void AddXDGDataDir(const FilePath& dir) {
-  if (!file_util::DirectoryExists(dir))
+  if (!DirectoryExists(dir))
     return;
   TryAddIconDir(dir.Append("icons"));
   TryAddIconDir(dir.Append("pixmaps"));
@@ -418,7 +418,7 @@
   if (!home.empty()) {
       FilePath legacy_data_dir(home);
       legacy_data_dir = legacy_data_dir.AppendASCII(".icons");
-      if (file_util::DirectoryExists(legacy_data_dir))
+      if (DirectoryExists(legacy_data_dir))
         TryAddIconDir(legacy_data_dir);
   }
   const char* env = getenv("XDG_DATA_HOME");
diff --git a/base/prefs/json_pref_store_unittest.cc b/base/prefs/json_pref_store_unittest.cc
index 5c506d4..dcfea71 100644
--- a/base/prefs/json_pref_store_unittest.cc
+++ b/base/prefs/json_pref_store_unittest.cc
@@ -81,8 +81,7 @@
   EXPECT_FALSE(PathExists(invalid_file));
   base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
   EXPECT_TRUE(PathExists(moved_aside));
-  EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
-                                           moved_aside));
+  EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside));
 }
 
 // This function is used to avoid code duplication while testing synchronous and
@@ -147,7 +146,7 @@
   ASSERT_TRUE(PathExists(golden_output_file));
   pref_store->CommitPendingWrite();
   RunLoop().RunUntilIdle();
-  EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
+  EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file));
   ASSERT_TRUE(base::Delete(output_file, false));
 }
 
@@ -282,7 +281,7 @@
   base::FilePath golden_output_file =
       data_dir_.AppendASCII("write.golden.need_empty_value.json");
   ASSERT_TRUE(PathExists(golden_output_file));
-  EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
+  EXPECT_TRUE(TextContentsEqual(golden_output_file, pref_file));
 }
 
 }  // namespace base
diff --git a/base/test/test_launcher.cc b/base/test/test_launcher.cc
index 78f5ed3..aae48a5 100644
--- a/base/test/test_launcher.cc
+++ b/base/test/test_launcher.cc
@@ -162,7 +162,7 @@
   if (path.value().empty())
     path = FilePath(kDefaultOutputFile);
   FilePath dir_name = path.DirName();
-  if (!file_util::DirectoryExists(dir_name)) {
+  if (!DirectoryExists(dir_name)) {
     LOG(WARNING) << "The output directory does not exist. "
                  << "Creating the directory: " << dir_name.value();
     // Create the directory if necessary (because the gtest does the same).