Move more file_util functions to base namespace.
TBR=jam
Review URL: https://codereview.chromium.org/189333004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256863 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: a26f4aec68d4e5637e19e156a4b34518c6a5804c
diff --git a/base/file_util.cc b/base/file_util.cc
index 87b9afb..c1bf50a 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -236,18 +236,8 @@
return true;
}
-} // namespace base
-
-// -----------------------------------------------------------------------------
-
-namespace file_util {
-
-using base::FilePath;
-using base::kMaxUniqueFiles;
-
-int GetUniquePathNumber(
- const FilePath& path,
- const FilePath::StringType& suffix) {
+int GetUniquePathNumber(const FilePath& path,
+ const FilePath::StringType& suffix) {
bool have_suffix = !suffix.empty();
if (!PathExists(path) &&
(!have_suffix || !PathExists(FilePath(path.value() + suffix)))) {
@@ -256,8 +246,7 @@
FilePath new_path;
for (int count = 1; count <= kMaxUniqueFiles; ++count) {
- new_path =
- path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count));
+ new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count));
if (!PathExists(new_path) &&
(!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
return count;
@@ -267,4 +256,4 @@
return -1;
}
-} // namespace file_util
+} // namespace base
diff --git a/base/file_util.h b/base/file_util.h
index 3c171f1..431569a 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -349,25 +349,12 @@
// Sets the current working directory for the process.
BASE_EXPORT bool SetCurrentDirectory(const FilePath& path);
-} // namespace base
-
-// -----------------------------------------------------------------------------
-
-namespace file_util {
-
// Attempts to find a number that can be appended to the |path| to make it
// unique. If |path| does not exist, 0 is returned. If it fails to find such
// a number, -1 is returned. If |suffix| is not empty, also checks the
// existence of it with the given suffix.
-BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path,
- const base::FilePath::StringType& suffix);
-
-#if defined(OS_POSIX)
-// Creates a directory with a guaranteed unique name based on |path|, returning
-// the pathname if successful, or an empty path if there was an error creating
-// the directory. Does not create parent directories.
-BASE_EXPORT base::FilePath MakeUniqueDirectory(const base::FilePath& path);
-#endif
+BASE_EXPORT int GetUniquePathNumber(const FilePath& path,
+ const FilePath::StringType& suffix);
#if defined(OS_POSIX)
// Test that |path| can only be changed by a given user and members of
@@ -402,6 +389,32 @@
// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path);
+#if defined(OS_LINUX)
+// Broad categories of file systems as returned by statfs() on Linux.
+enum FileSystemType {
+ FILE_SYSTEM_UNKNOWN, // statfs failed.
+ FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
+ FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
+ FILE_SYSTEM_NFS,
+ FILE_SYSTEM_SMB,
+ FILE_SYSTEM_CODA,
+ FILE_SYSTEM_MEMORY, // in-memory file system
+ FILE_SYSTEM_CGROUP, // cgroup control.
+ FILE_SYSTEM_OTHER, // any other value.
+ FILE_SYSTEM_TYPE_COUNT
+};
+
+// Attempts determine the FileSystemType for |path|.
+// Returns false if |path| doesn't exist.
+BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type);
+#endif
+
+} // namespace base
+
+// -----------------------------------------------------------------------------
+
+namespace file_util {
+
// Functor for |ScopedFILE| (below).
struct ScopedFILEClose {
inline void operator()(FILE* x) const {
@@ -439,27 +452,6 @@
typedef ScopedFD ScopedFDCloser;
#endif // OS_POSIX
-#if defined(OS_LINUX)
-// Broad categories of file systems as returned by statfs() on Linux.
-enum FileSystemType {
- FILE_SYSTEM_UNKNOWN, // statfs failed.
- FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
- FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
- FILE_SYSTEM_NFS,
- FILE_SYSTEM_SMB,
- FILE_SYSTEM_CODA,
- FILE_SYSTEM_MEMORY, // in-memory file system
- FILE_SYSTEM_CGROUP, // cgroup control.
- FILE_SYSTEM_OTHER, // any other value.
- FILE_SYSTEM_TYPE_COUNT
-};
-
-// Attempts determine the FileSystemType for |path|.
-// Returns false if |path| doesn't exist.
-BASE_EXPORT bool GetFileSystemType(const base::FilePath& path,
- FileSystemType* type);
-#endif
-
} // namespace file_util
// Internal --------------------------------------------------------------------
diff --git a/base/file_util_linux.cc b/base/file_util_linux.cc
index 14e801c..2910c9c 100644
--- a/base/file_util_linux.cc
+++ b/base/file_util_linux.cc
@@ -25,9 +25,9 @@
#define TMPFS_MAGIC 0x01021994
#endif
-namespace file_util {
+namespace base {
-bool GetFileSystemType(const base::FilePath& path, FileSystemType* type) {
+bool GetFileSystemType(const FilePath& path, FileSystemType* type) {
struct statfs statfs_buf;
if (statfs(path.value().c_str(), &statfs_buf) < 0) {
if (errno == ENOENT)
@@ -75,4 +75,4 @@
return true;
}
-} // namespace file_util
+} // namespace base
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index db84c3f..7e7dc05 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -772,44 +772,6 @@
return !ret;
}
-} // namespace base
-
-// -----------------------------------------------------------------------------
-
-namespace file_util {
-
-using base::stat_wrapper_t;
-using base::CallStat;
-using base::CallLstat;
-using base::CreateAndOpenFdForTemporaryFile;
-using base::DirectoryExists;
-using base::FileEnumerator;
-using base::FilePath;
-using base::MakeAbsoluteFilePath;
-using base::VerifySpecificPathControlledByUser;
-
-base::FilePath MakeUniqueDirectory(const base::FilePath& path) {
- const int kMaxAttempts = 20;
- for (int attempts = 0; attempts < kMaxAttempts; attempts++) {
- int uniquifier =
- GetUniquePathNumber(path, base::FilePath::StringType());
- if (uniquifier < 0)
- break;
- base::FilePath test_path = (uniquifier == 0) ? path :
- path.InsertBeforeExtensionASCII(
- base::StringPrintf(" (%d)", uniquifier));
- if (mkdir(test_path.value().c_str(), 0777) == 0)
- return test_path;
- else if (errno != EEXIST)
- break;
- }
- return base::FilePath();
-}
-
-FILE* OpenFile(const std::string& filename, const char* mode) {
- return OpenFile(FilePath(filename), mode);
-}
-
bool VerifyPathControlledByUser(const FilePath& base,
const FilePath& path,
uid_t owner_uid,
@@ -861,7 +823,7 @@
};
// Reading the groups database may touch the file system.
- base::ThreadRestrictions::AssertIOAllowed();
+ ThreadRestrictions::AssertIOAllowed();
std::set<gid_t> allowed_group_ids;
for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
@@ -881,13 +843,12 @@
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
int GetMaximumPathComponentLength(const FilePath& path) {
- base::ThreadRestrictions::AssertIOAllowed();
+ ThreadRestrictions::AssertIOAllowed();
return pathconf(path.value().c_str(), _PC_NAME_MAX);
}
-} // namespace file_util
+// -----------------------------------------------------------------------------
-namespace base {
namespace internal {
bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 5b8684f..96b5858 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -2127,23 +2127,23 @@
.AppendASCII("not")
.AppendASCII("exist");
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, does_not_exist, uid_, ok_gids_));
// |base| not a subpath of |path|.
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, base_dir_, uid_, ok_gids_));
// An empty base path will fail to be a prefix for any path.
FilePath empty;
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
empty, base_dir_, uid_, ok_gids_));
// Finding that a bad call fails proves nothing unless a good call succeeds.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
}
@@ -2156,10 +2156,10 @@
<< "Failed to create symlink.";
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, file_link, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
file_link, file_link, uid_, ok_gids_));
// Symlink from one directory to another within the path.
@@ -2171,16 +2171,16 @@
ASSERT_TRUE(PathExists(file_path_with_link));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, file_path_with_link, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
// Symlinks in parents of base path are allowed.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
file_path_with_link, file_path_with_link, uid_, ok_gids_));
}
@@ -2198,35 +2198,35 @@
// We control these paths.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Another user does not control these paths.
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, bad_uid, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, bad_uid, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, bad_uid, ok_gids_));
// Another group does not control the paths.
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
}
@@ -2241,36 +2241,36 @@
// Any group is okay because the path is not group-writable.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
// No group is okay, because we don't check the group
// if no group can write.
std::set<gid_t> no_gids; // Empty set of gids.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, no_gids));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, no_gids));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, no_gids));
@@ -2284,23 +2284,23 @@
// Now |ok_gids_| works, but |bad_gids_| fails.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
// Because any group in the group set is allowed,
@@ -2313,13 +2313,13 @@
std::inserter(multiple_gids, multiple_gids.begin()));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, multiple_gids));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, multiple_gids));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, multiple_gids));
}
@@ -2334,78 +2334,78 @@
// Initialy, we control all parts of the path.
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Make base_dir_ world-writable.
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Make sub_dir_ world writable.
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Make text_file_ world writable.
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Make sub_dir_ non-world writable.
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Make base_dir_ non-world-writable.
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
// Back to the initial state: Nothing is writable, so every path
@@ -2413,13 +2413,13 @@
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
- file_util::VerifyPathControlledByUser(
+ base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
}
diff --git a/base/files/file_path.h b/base/files/file_path.h
index 734c402..5695767 100644
--- a/base/files/file_path.h
+++ b/base/files/file_path.h
@@ -246,7 +246,7 @@
// TODO(davidben): Check all our extension-sensitive code to see if
// we can rename this to Extension() and the other to something like
// LongExtension(), defaulting to short extensions and leaving the
- // long "extensions" to logic like file_util::GetUniquePathNumber().
+ // long "extensions" to logic like base::GetUniquePathNumber().
StringType FinalExtension() const;
// Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
diff --git a/base/process/process_linux.cc b/base/process/process_linux.cc
index c5acf1b..d92d7c3 100644
--- a/base/process/process_linux.cc
+++ b/base/process/process_linux.cc
@@ -14,6 +14,8 @@
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
+namespace base {
+
namespace {
const int kForegroundPriority = 0;
@@ -46,13 +48,13 @@
base::FilePath(base::StringPrintf(kControlPath, kForeground));
background_file =
base::FilePath(base::StringPrintf(kControlPath, kBackground));
- file_util::FileSystemType foreground_type;
- file_util::FileSystemType background_type;
+ base::FileSystemType foreground_type;
+ base::FileSystemType background_type;
enabled =
- file_util::GetFileSystemType(foreground_file, &foreground_type) &&
- file_util::GetFileSystemType(background_file, &background_type) &&
- foreground_type == file_util::FILE_SYSTEM_CGROUP &&
- background_type == file_util::FILE_SYSTEM_CGROUP;
+ base::GetFileSystemType(foreground_file, &foreground_type) &&
+ base::GetFileSystemType(background_file, &background_type) &&
+ foreground_type == FILE_SYSTEM_CGROUP &&
+ background_type == FILE_SYSTEM_CGROUP;
}
};
@@ -62,8 +64,6 @@
#endif
}
-namespace base {
-
bool Process::IsProcessBackgrounded() const {
DCHECK(process_);