Move some more file utils to the base namespace.
This also swaps the order of the parameters to GetShmemTempDir so the out
parameter is last, and enhances some documentation.
Review URL: https://codereview.chromium.org/93263002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238144 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: fb4bcfa38b2cdd740fd112fabeb5d45add27354b
diff --git a/base/base_paths.cc b/base/base_paths.cc
index 9f2b250..b4fc28b 100644
--- a/base/base_paths.cc
+++ b/base/base_paths.cc
@@ -24,7 +24,7 @@
cur = cur.DirName();
break;
case DIR_TEMP:
- if (!file_util::GetTempDir(&cur))
+ if (!base::GetTempDir(&cur))
return false;
break;
case DIR_TEST_DATA:
diff --git a/base/base_paths_android.cc b/base/base_paths_android.cc
index d0a709f..68cc5a7 100644
--- a/base/base_paths_android.cc
+++ b/base/base_paths_android.cc
@@ -48,7 +48,7 @@
case base::DIR_ANDROID_APP_DATA:
return base::android::GetDataDirectory(result);
case base::DIR_HOME:
- *result = file_util::GetHomeDir();
+ *result = GetHomeDir();
return true;
case base::DIR_ANDROID_EXTERNAL_STORAGE:
return base::android::GetExternalStorageDirectory(result);
diff --git a/base/base_paths_posix.cc b/base/base_paths_posix.cc
index 6b26ba8..b4147e9 100644
--- a/base/base_paths_posix.cc
+++ b/base/base_paths_posix.cc
@@ -110,7 +110,7 @@
return true;
}
case base::DIR_HOME:
- *result = file_util::GetHomeDir();
+ *result = GetHomeDir();
return true;
}
return false;
diff --git a/base/file_util.cc b/base/file_util.cc
index 5e9790e..1f8ba81 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -145,6 +145,14 @@
return true;
}
+bool IsDirectoryEmpty(const FilePath& dir_path) {
+ FileEnumerator files(dir_path, false,
+ FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
+ if (files.Next().empty())
+ return true;
+ return false;
+}
+
} // namespace base
// -----------------------------------------------------------------------------
@@ -155,14 +163,6 @@
using base::FilePath;
using base::kMaxUniqueFiles;
-bool IsDirectoryEmpty(const FilePath& dir_path) {
- FileEnumerator files(dir_path, false,
- FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
- if (files.Next().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 ac9f67d..35225c7 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -181,27 +181,43 @@
#endif // OS_POSIX
+// Returns true if the given directory is empty
+BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path);
+
+// Get the temporary directory provided by the system.
+//
+// WARNING: In general, you should use CreateTemporaryFile variants below
+// instead of this function. Those variants will ensure that the proper
+// permissions are set so that other users on the system can't edit them while
+// they're open (which can lead to security issues).
+BASE_EXPORT bool GetTempDir(FilePath* path);
+
+// Get a temporary directory for shared memory files. The directory may depend
+// on whether the destination is intended for executable files, which in turn
+// depends on how /dev/shmem was mounted. As a result, you must supply whether
+// you intend to create executable shmem segments so this function can find
+// an appropriate location.
+//
+// Only useful on POSIX; redirects to GetTempDir() on Windows.
+BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
+
+#if defined(OS_POSIX)
+// Get the home directory. This is more complicated than just getenv("HOME")
+// as it knows to fall back on getpwent() etc.
+//
+// This function is not currently implemented on Windows or Mac because we
+// don't use it. Generally you would use one of PathService's APP_DATA
+// directories on those platforms. If we need it, this could be implemented
+// there to return the appropriate directory.
+BASE_EXPORT FilePath GetHomeDir();
+#endif // OS_POSIX
+
} // namespace base
// -----------------------------------------------------------------------------
namespace file_util {
-// Return true if the given directory is empty
-BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path);
-
-// Get the temporary directory provided by the system.
-// WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
-// the functions below.
-BASE_EXPORT bool GetTempDir(base::FilePath* path);
-// Get a temporary directory for shared memory files.
-// Only useful on POSIX; redirects to GetTempDir() on Windows.
-BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable);
-
-// Get the home directory. This is more complicated than just getenv("HOME")
-// as it knows to fall back on getpwent() etc.
-BASE_EXPORT base::FilePath GetHomeDir();
-
// Creates a temporary file. The full path is placed in |path|, and the
// function returns true if was successful in creating the file. The file will
// be empty and all handles closed after this function returns.
diff --git a/base/file_util_android.cc b/base/file_util_android.cc
index 6ac9def..def4d7c 100644
--- a/base/file_util_android.cc
+++ b/base/file_util_android.cc
@@ -7,10 +7,10 @@
#include "base/files/file_path.h"
#include "base/path_service.h"
-namespace file_util {
+namespace base {
-bool GetShmemTempDir(base::FilePath* path, bool executable) {
+bool GetShmemTempDir(bool executable, base::FilePath* path) {
return PathService::Get(base::DIR_CACHE, path);
}
-} // namespace file_util
+} // namespace base
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 027337f..b6a4945 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -150,6 +150,47 @@
#endif
}
+// Creates and opens a temporary file in |directory|, returning the
+// file descriptor. |path| is set to the temporary file path.
+// This function does NOT unlink() the file.
+int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
+ ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
+ *path = directory.Append(base::TempFileName());
+ const std::string& tmpdir_string = path->value();
+ // this should be OK since mkstemp just replaces characters in place
+ char* buffer = const_cast<char*>(tmpdir_string.c_str());
+
+ return HANDLE_EINTR(mkstemp(buffer));
+}
+
+#if defined(OS_LINUX)
+// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
+// This depends on the mount options used for /dev/shm, which vary among
+// different Linux distributions and possibly local configuration. It also
+// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
+// but its kernel allows mprotect with PROT_EXEC anyway.
+bool DetermineDevShmExecutable() {
+ bool result = false;
+ FilePath path;
+ int fd = CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path);
+ if (fd >= 0) {
+ file_util::ScopedFD shm_fd_closer(&fd);
+ DeleteFile(path, false);
+ long sysconf_result = sysconf(_SC_PAGESIZE);
+ CHECK_GE(sysconf_result, 0);
+ size_t pagesize = static_cast<size_t>(sysconf_result);
+ CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
+ void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
+ if (mapping != MAP_FAILED) {
+ if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
+ result = true;
+ munmap(mapping, pagesize);
+ }
+ }
+ return result;
+}
+#endif // defined(OS_LINUX)
+
} // namespace
FilePath MakeAbsoluteFilePath(const FilePath& input) {
@@ -405,6 +446,73 @@
return true;
}
+#if !defined(OS_MACOSX)
+// This is implemented in file_util_mac.mm for Mac.
+bool GetTempDir(FilePath* path) {
+ const char* tmp = getenv("TMPDIR");
+ if (tmp) {
+ *path = FilePath(tmp);
+ } else {
+#if defined(OS_ANDROID)
+ return PathService::Get(base::DIR_CACHE, path);
+#else
+ *path = FilePath("/tmp");
+#endif
+ }
+ return true;
+}
+#endif // !defined(OS_MACOSX)
+
+#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
+// This is implemented in file_util_mac.mm and file_util_android.cc for those
+// platforms.
+bool GetShmemTempDir(bool executable, FilePath* path) {
+#if defined(OS_LINUX)
+ bool use_dev_shm = true;
+ if (executable) {
+ static const bool s_dev_shm_executable = DetermineDevShmExecutable();
+ use_dev_shm = s_dev_shm_executable;
+ }
+ if (use_dev_shm) {
+ *path = FilePath("/dev/shm");
+ return true;
+ }
+#endif
+ return GetTempDir(path);
+}
+#endif // !defined(OS_MACOSX) && !defined(OS_ANDROID)
+
+#if !defined(OS_MACOSX)
+FilePath GetHomeDir() {
+#if defined(OS_CHROMEOS)
+ if (SysInfo::IsRunningOnChromeOS())
+ return FilePath("/home/chronos/user");
+#endif
+
+ const char* home_dir = getenv("HOME");
+ if (home_dir && home_dir[0])
+ return FilePath(home_dir);
+
+#if defined(OS_ANDROID)
+ DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
+#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
+ // g_get_home_dir calls getpwent, which can fall through to LDAP calls.
+ ThreadRestrictions::AssertIOAllowed();
+
+ home_dir = g_get_home_dir();
+ if (home_dir && home_dir[0])
+ return FilePath(home_dir);
+#endif
+
+ FilePath rv;
+ if (GetTempDir(&rv))
+ return rv;
+
+ // Last resort.
+ return FilePath("/tmp");
+}
+#endif // !defined(OS_MACOSX)
+
} // namespace base
// -----------------------------------------------------------------------------
@@ -414,6 +522,7 @@
using base::stat_wrapper_t;
using base::CallStat;
using base::CallLstat;
+using base::CreateAndOpenFdForTemporaryFile;
using base::DirectoryExists;
using base::FileEnumerator;
using base::FilePath;
@@ -421,19 +530,6 @@
using base::RealPath;
using base::VerifySpecificPathControlledByUser;
-// Creates and opens a temporary file in |directory|, returning the
-// file descriptor. |path| is set to the temporary file path.
-// This function does NOT unlink() the file.
-int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
- base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
- *path = directory.Append(base::TempFileName());
- const std::string& tmpdir_string = path->value();
- // this should be OK since mkstemp just replaces characters in place
- char* buffer = const_cast<char*>(tmpdir_string.c_str());
-
- return HANDLE_EINTR(mkstemp(buffer));
-}
-
bool CreateTemporaryFile(FilePath* path) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to close().
FilePath directory;
@@ -448,7 +544,7 @@
FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) {
FilePath directory;
- if (!GetShmemTempDir(&directory, executable))
+ if (!GetShmemTempDir(executable, &directory))
return NULL;
return CreateAndOpenTemporaryFileInDir(directory, path);
@@ -725,101 +821,6 @@
return true;
}
-#if !defined(OS_MACOSX)
-bool GetTempDir(FilePath* path) {
- const char* tmp = getenv("TMPDIR");
- if (tmp)
- *path = FilePath(tmp);
- else
-#if defined(OS_ANDROID)
- return PathService::Get(base::DIR_CACHE, path);
-#else
- *path = FilePath("/tmp");
-#endif
- return true;
-}
-
-#if !defined(OS_ANDROID)
-
-#if defined(OS_LINUX)
-// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
-// This depends on the mount options used for /dev/shm, which vary among
-// different Linux distributions and possibly local configuration. It also
-// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
-// but its kernel allows mprotect with PROT_EXEC anyway.
-
-namespace {
-
-bool DetermineDevShmExecutable() {
- bool result = false;
- FilePath path;
- int fd = CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path);
- if (fd >= 0) {
- ScopedFD shm_fd_closer(&fd);
- DeleteFile(path, false);
- long sysconf_result = sysconf(_SC_PAGESIZE);
- CHECK_GE(sysconf_result, 0);
- size_t pagesize = static_cast<size_t>(sysconf_result);
- CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
- void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
- if (mapping != MAP_FAILED) {
- if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
- result = true;
- munmap(mapping, pagesize);
- }
- }
- return result;
-}
-
-}; // namespace
-#endif // defined(OS_LINUX)
-
-bool GetShmemTempDir(FilePath* path, bool executable) {
-#if defined(OS_LINUX)
- bool use_dev_shm = true;
- if (executable) {
- static const bool s_dev_shm_executable = DetermineDevShmExecutable();
- use_dev_shm = s_dev_shm_executable;
- }
- if (use_dev_shm) {
- *path = FilePath("/dev/shm");
- return true;
- }
-#endif
- return GetTempDir(path);
-}
-#endif // !defined(OS_ANDROID)
-
-FilePath GetHomeDir() {
-#if defined(OS_CHROMEOS)
- if (base::SysInfo::IsRunningOnChromeOS())
- return FilePath("/home/chronos/user");
-#endif
-
- const char* home_dir = getenv("HOME");
- if (home_dir && home_dir[0])
- return FilePath(home_dir);
-
-#if defined(OS_ANDROID)
- DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
-#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
- // g_get_home_dir calls getpwent, which can fall through to LDAP calls.
- base::ThreadRestrictions::AssertIOAllowed();
-
- home_dir = g_get_home_dir();
- if (home_dir && home_dir[0])
- return FilePath(home_dir);
-#endif
-
- FilePath rv;
- if (file_util::GetTempDir(&rv))
- return rv;
-
- // Last resort.
- return FilePath("/tmp");
-}
-#endif // !defined(OS_MACOSX)
-
bool VerifyPathControlledByUser(const FilePath& base,
const FilePath& path,
uid_t owner_uid,
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index ebccfd4..968da5b 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1564,7 +1564,7 @@
for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
FilePath path;
::_tputenv_s(kTmpKey, kTmpValues[i]);
- file_util::GetTempDir(&path);
+ base::GetTempDir(&path);
EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
" result=" << path.value();
}
@@ -1637,7 +1637,7 @@
TEST_F(FileUtilTest, GetShmemTempDirTest) {
FilePath dir;
- EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
+ EXPECT_TRUE(GetShmemTempDir(false, &dir));
EXPECT_TRUE(DirectoryExists(dir));
}
@@ -1933,13 +1933,13 @@
ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
- EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
+ EXPECT_TRUE(base::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));
+ EXPECT_FALSE(base::IsDirectoryEmpty(empty_dir));
}
#if defined(OS_POSIX)
diff --git a/base/files/file_util_proxy.cc b/base/files/file_util_proxy.cc
index 5f6d405..965b489 100644
--- a/base/files/file_util_proxy.cc
+++ b/base/files/file_util_proxy.cc
@@ -213,7 +213,7 @@
return PLATFORM_FILE_ERROR_NOT_FOUND;
}
if (!base::DeleteFile(file_path, recursive)) {
- if (!recursive && !file_util::IsDirectoryEmpty(file_path)) {
+ if (!recursive && !base::IsDirectoryEmpty(file_path)) {
return PLATFORM_FILE_ERROR_NOT_EMPTY;
}
return PLATFORM_FILE_ERROR_FAILED;
diff --git a/base/files/scoped_temp_dir_unittest.cc b/base/files/scoped_temp_dir_unittest.cc
index 0c9131c..02b910c 100644
--- a/base/files/scoped_temp_dir_unittest.cc
+++ b/base/files/scoped_temp_dir_unittest.cc
@@ -55,7 +55,7 @@
test_path = dir.path();
EXPECT_TRUE(DirectoryExists(test_path));
FilePath tmp_dir;
- EXPECT_TRUE(file_util::GetTempDir(&tmp_dir));
+ EXPECT_TRUE(base::GetTempDir(&tmp_dir));
EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
}
EXPECT_FALSE(DirectoryExists(test_path));
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_posix.cc
index bb3c280..8bc7da8 100644
--- a/base/memory/shared_memory_posix.cc
+++ b/base/memory/shared_memory_posix.cc
@@ -399,7 +399,7 @@
DCHECK_EQ(std::string::npos, mem_name.find('\0'));
FilePath temp_dir;
- if (!file_util::GetShmemTempDir(&temp_dir, false))
+ if (!GetShmemTempDir(false, &temp_dir))
return false;
#if !defined(OS_MACOSX)
diff --git a/base/nix/mime_util_xdg.cc b/base/nix/mime_util_xdg.cc
index 1a41394..a5a97f4 100644
--- a/base/nix/mime_util_xdg.cc
+++ b/base/nix/mime_util_xdg.cc
@@ -414,7 +414,7 @@
// Add all the xdg icon directories.
void InitIconDir() {
- FilePath home = file_util::GetHomeDir();
+ FilePath home = GetHomeDir();
if (!home.empty()) {
FilePath legacy_data_dir(home);
legacy_data_dir = legacy_data_dir.AppendASCII(".icons");
diff --git a/base/nix/xdg_util.cc b/base/nix/xdg_util.cc
index b3caf2a..4c1510f 100644
--- a/base/nix/xdg_util.cc
+++ b/base/nix/xdg_util.cc
@@ -31,7 +31,7 @@
if (env->GetVar(env_name, &env_value) && !env_value.empty())
path = FilePath(env_value);
else
- path = file_util::GetHomeDir().Append(fallback_dir);
+ path = GetHomeDir().Append(fallback_dir);
return path.StripTrailingSeparators();
}
@@ -42,7 +42,7 @@
path = FilePath(xdg_dir);
free(xdg_dir);
} else {
- path = file_util::GetHomeDir().Append(fallback_dir);
+ path = GetHomeDir().Append(fallback_dir);
}
return path.StripTrailingSeparators();
}
diff --git a/base/os_compat_android_unittest.cc b/base/os_compat_android_unittest.cc
index c749b6a..a1d1fb1 100644
--- a/base/os_compat_android_unittest.cc
+++ b/base/os_compat_android_unittest.cc
@@ -17,7 +17,7 @@
// passes.
TEST_F(OsCompatAndroidTest, DISABLED_TestMkdTemp) {
FilePath tmp_dir;
- EXPECT_TRUE(file_util::GetTempDir(&tmp_dir));
+ EXPECT_TRUE(base::GetTempDir(&tmp_dir));
// Not six XXXXXX at the suffix of the path.
FilePath sub_dir = tmp_dir.Append("XX");
diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc
index eacf76c..93eff77 100644
--- a/base/sys_info_unittest.cc
+++ b/base/sys_info_unittest.cc
@@ -34,7 +34,7 @@
TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {
// We aren't actually testing that it's correct, just that it's sane.
FilePath tmp_path;
- ASSERT_TRUE(file_util::GetTempDir(&tmp_path));
+ ASSERT_TRUE(base::GetTempDir(&tmp_path));
EXPECT_GT(base::SysInfo::AmountOfFreeDiskSpace(tmp_path), 0)
<< tmp_path.value();
}