Move more file_util functions to base namespace.
This moves DevicePathToDriveLetterPath, NormalizeToNativeFilePath, IsLink, and GetFileInfo.
This also removes some explicit "base::" usage in base files I touched.
TBR=jam
Review URL: https://codereview.chromium.org/105293002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238763 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: 9eae4e686d49213ee7dba24cdf28f13d38b99741
diff --git a/base/file_util.cc b/base/file_util.cc
index 1198636..6140cea 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -167,7 +167,7 @@
bool GetFileSize(const FilePath& file_path, int64* file_size) {
PlatformFileInfo info;
- if (!file_util::GetFileInfo(file_path, &info))
+ if (!GetFileInfo(file_path, &info))
return false;
*file_size = info.size;
return true;
diff --git a/base/file_util.h b/base/file_util.h
index 8407f5a..5282888 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -272,34 +272,33 @@
// or if |real_path| would be longer than MAX_PATH characters.
BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
-} // namespace base
-
-// -----------------------------------------------------------------------------
-
-namespace file_util {
-
#if defined(OS_WIN)
// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
// return in |drive_letter_path| the equivalent path that starts with
// a drive letter ("C:\..."). Return false if no such path exists.
-BASE_EXPORT bool DevicePathToDriveLetterPath(const base::FilePath& device_path,
- base::FilePath* drive_letter_path);
+BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path,
+ FilePath* drive_letter_path);
// Given an existing file in |path|, set |real_path| to the path
// in native NT format, of the form "\Device\HarddiskVolumeXX\..".
// Returns false if the path can not be found. Empty files cannot
// be resolved with this function.
-BASE_EXPORT bool NormalizeToNativeFilePath(const base::FilePath& path,
- base::FilePath* nt_path);
+BASE_EXPORT bool NormalizeToNativeFilePath(const FilePath& path,
+ FilePath* nt_path);
#endif
// This function will return if the given file is a symlink or not.
-BASE_EXPORT bool IsLink(const base::FilePath& file_path);
+BASE_EXPORT bool IsLink(const FilePath& file_path);
// Returns information about the given file path.
-BASE_EXPORT bool GetFileInfo(const base::FilePath& file_path,
- base::PlatformFileInfo* info);
+BASE_EXPORT bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* info);
+
+} // namespace base
+
+// -----------------------------------------------------------------------------
+
+namespace file_util {
// Sets the time of the last access and the time of the last modification.
BASE_EXPORT bool TouchFile(const base::FilePath& path,
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index f3a7553..fdf196e 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -639,6 +639,56 @@
return true;
}
+// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
+// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
+bool IsLink(const FilePath& file_path) {
+ stat_wrapper_t st;
+ // If we can't lstat the file, it's safe to assume that the file won't at
+ // least be a 'followable' link.
+ if (CallLstat(file_path.value().c_str(), &st) != 0)
+ return false;
+
+ if (S_ISLNK(st.st_mode))
+ return true;
+ else
+ return false;
+}
+
+bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* results) {
+ stat_wrapper_t file_info;
+#if defined(OS_ANDROID)
+ if (file_path.IsContentUri()) {
+ int fd = OpenContentUriForRead(file_path);
+ if (fd < 0)
+ return false;
+ file_util::ScopedFD scoped_fd(&fd);
+ if (CallFstat(fd, &file_info) != 0)
+ return false;
+ } else {
+#endif // defined(OS_ANDROID)
+ if (CallStat(file_path.value().c_str(), &file_info) != 0)
+ return false;
+#if defined(OS_ANDROID)
+ }
+#endif // defined(OS_ANDROID)
+ results->is_directory = S_ISDIR(file_info.st_mode);
+ results->size = file_info.st_size;
+#if defined(OS_MACOSX)
+ results->last_modified = Time::FromTimeSpec(file_info.st_mtimespec);
+ results->last_accessed = Time::FromTimeSpec(file_info.st_atimespec);
+ results->creation_time = Time::FromTimeSpec(file_info.st_ctimespec);
+#elif defined(OS_ANDROID)
+ results->last_modified = Time::FromTimeT(file_info.st_mtime);
+ results->last_accessed = Time::FromTimeT(file_info.st_atime);
+ results->creation_time = Time::FromTimeT(file_info.st_ctime);
+#else
+ results->last_modified = Time::FromTimeSpec(file_info.st_mtim);
+ results->last_accessed = Time::FromTimeSpec(file_info.st_atim);
+ results->creation_time = Time::FromTimeSpec(file_info.st_ctim);
+#endif
+ return true;
+}
+
} // namespace base
// -----------------------------------------------------------------------------
@@ -673,56 +723,6 @@
return base::FilePath();
}
-// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
-// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
-bool IsLink(const FilePath& file_path) {
- stat_wrapper_t st;
- // If we can't lstat the file, it's safe to assume that the file won't at
- // least be a 'followable' link.
- if (CallLstat(file_path.value().c_str(), &st) != 0)
- return false;
-
- if (S_ISLNK(st.st_mode))
- return true;
- else
- return false;
-}
-
-bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) {
- stat_wrapper_t file_info;
-#if defined(OS_ANDROID)
- if (file_path.IsContentUri()) {
- int fd = OpenContentUriForRead(file_path);
- if (fd < 0)
- return false;
- ScopedFD scoped_fd(&fd);
- if (base::CallFstat(fd, &file_info) != 0)
- return false;
- } else {
-#endif // defined(OS_ANDROID)
- if (CallStat(file_path.value().c_str(), &file_info) != 0)
- return false;
-#if defined(OS_ANDROID)
- }
-#endif // defined(OS_ANDROID)
- results->is_directory = S_ISDIR(file_info.st_mode);
- results->size = file_info.st_size;
-#if defined(OS_MACOSX)
- results->last_modified = base::Time::FromTimeSpec(file_info.st_mtimespec);
- results->last_accessed = base::Time::FromTimeSpec(file_info.st_atimespec);
- results->creation_time = base::Time::FromTimeSpec(file_info.st_ctimespec);
-#elif defined(OS_ANDROID)
- results->last_modified = base::Time::FromTimeT(file_info.st_mtime);
- results->last_accessed = base::Time::FromTimeT(file_info.st_atime);
- results->creation_time = base::Time::FromTimeT(file_info.st_ctime);
-#else
- results->last_modified = base::Time::FromTimeSpec(file_info.st_mtim);
- results->last_accessed = base::Time::FromTimeSpec(file_info.st_atim);
- results->creation_time = base::Time::FromTimeSpec(file_info.st_ctim);
-#endif
- return true;
-}
-
bool GetInode(const FilePath& path, ino_t* inode) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to stat().
struct stat buffer;
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 0460c6e..38bfbc0 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -434,14 +434,13 @@
// Run DevicePathToDriveLetterPath() on the NT style path we got from
// QueryDosDevice(). Expect the drive letter we started with.
- ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path,
- &win32_path));
+ ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
ASSERT_EQ(real_drive_letter, win32_path.value());
// Add some directories to the path. Expect those extra path componenets
// to be preserved.
FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
- ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
+ ASSERT_TRUE(DevicePathToDriveLetterPath(
actual_device_path.Append(kRelativePath),
&win32_path));
EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
@@ -459,11 +458,10 @@
ASSERT_LT(0, new_length);
FilePath prefix_of_real_device_path(
actual_device_path.value().substr(0, new_length));
- ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
- prefix_of_real_device_path,
- &win32_path));
+ ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
+ &win32_path));
- ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
+ ASSERT_FALSE(DevicePathToDriveLetterPath(
prefix_of_real_device_path.Append(kRelativePath),
&win32_path));
@@ -478,11 +476,11 @@
FilePath real_device_path_plus_numbers(
actual_device_path.value() + kExtraChars);
- ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
+ ASSERT_FALSE(DevicePathToDriveLetterPath(
real_device_path_plus_numbers,
&win32_path));
- ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
+ ASSERT_FALSE(DevicePathToDriveLetterPath(
real_device_path_plus_numbers.Append(kRelativePath),
&win32_path));
}
@@ -696,14 +694,14 @@
<< "Failed to create symlink.";
// Make sure the symbolic link is exist.
- EXPECT_TRUE(file_util::IsLink(file_link));
+ EXPECT_TRUE(IsLink(file_link));
EXPECT_FALSE(PathExists(file_link));
// Delete the symbolic link.
EXPECT_TRUE(DeleteFile(file_link, false));
// Make sure the symbolic link is deleted.
- EXPECT_FALSE(file_util::IsLink(file_link));
+ EXPECT_FALSE(IsLink(file_link));
}
TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
@@ -1915,7 +1913,7 @@
ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
PlatformFileInfo file_info;
- ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
+ ASSERT_TRUE(GetFileInfo(foobar, &file_info));
EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
access_time.ToInternalValue());
EXPECT_EQ(file_info.last_modified.ToInternalValue(),
diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc
index 1e986e1..d5052e2 100644
--- a/base/files/file_path_watcher_linux.cc
+++ b/base/files/file_path_watcher_linux.cc
@@ -446,7 +446,7 @@
if (path_valid) {
watch_entry->watch_ = g_inotify_reader.Get().AddWatch(path, this);
if ((watch_entry->watch_ == InotifyReader::kInvalidWatch) &&
- file_util::IsLink(path)) {
+ base::IsLink(path)) {
FilePath link;
if (ReadSymbolicLink(path, &link)) {
if (!link.IsAbsolute())
diff --git a/base/files/file_util_proxy.cc b/base/files/file_util_proxy.cc
index a36328e..eefb7a1 100644
--- a/base/files/file_util_proxy.cc
+++ b/base/files/file_util_proxy.cc
@@ -111,7 +111,7 @@
error_ = PLATFORM_FILE_ERROR_NOT_FOUND;
return;
}
- if (!file_util::GetFileInfo(file_path, &file_info_))
+ if (!GetFileInfo(file_path, &file_info_))
error_ = PLATFORM_FILE_ERROR_FAILED;
}
diff --git a/base/files/file_util_proxy_unittest.cc b/base/files/file_util_proxy_unittest.cc
index 7691d44..fa4a78c 100644
--- a/base/files/file_util_proxy_unittest.cc
+++ b/base/files/file_util_proxy_unittest.cc
@@ -226,7 +226,7 @@
// Setup.
ASSERT_EQ(4, file_util::WriteFile(test_path(), "test", 4));
PlatformFileInfo expected_info;
- file_util::GetFileInfo(test_path(), &expected_info);
+ GetFileInfo(test_path(), &expected_info);
// Run.
FileUtilProxy::GetFileInfo(
@@ -249,7 +249,7 @@
// Setup.
ASSERT_TRUE(base::CreateDirectory(test_path()));
PlatformFileInfo expected_info;
- file_util::GetFileInfo(test_path(), &expected_info);
+ GetFileInfo(test_path(), &expected_info);
// Run.
FileUtilProxy::GetFileInfo(
@@ -342,7 +342,7 @@
EXPECT_EQ(PLATFORM_FILE_OK, error_);
PlatformFileInfo info;
- file_util::GetFileInfo(test_path(), &info);
+ GetFileInfo(test_path(), &info);
// The returned values may only have the seconds precision, so we cast
// the double values to int here.
@@ -357,7 +357,7 @@
const char kTestData[] = "0123456789";
ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
PlatformFileInfo info;
- file_util::GetFileInfo(test_path(), &info);
+ GetFileInfo(test_path(), &info);
ASSERT_EQ(10, info.size);
// Run.
@@ -369,7 +369,7 @@
MessageLoop::current()->Run();
// Verify.
- file_util::GetFileInfo(test_path(), &info);
+ GetFileInfo(test_path(), &info);
ASSERT_EQ(7, info.size);
char buffer[7];
@@ -384,7 +384,7 @@
const char kTestData[] = "9876543210";
ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
PlatformFileInfo info;
- file_util::GetFileInfo(test_path(), &info);
+ GetFileInfo(test_path(), &info);
ASSERT_EQ(10, info.size);
// Run.
@@ -396,7 +396,7 @@
MessageLoop::current()->Run();
// Verify.
- file_util::GetFileInfo(test_path(), &info);
+ GetFileInfo(test_path(), &info);
ASSERT_EQ(53, info.size);
char buffer[53];
diff --git a/base/nix/mime_util_xdg.cc b/base/nix/mime_util_xdg.cc
index a5a97f4..d695d15 100644
--- a/base/nix/mime_util_xdg.cc
+++ b/base/nix/mime_util_xdg.cc
@@ -32,13 +32,12 @@
// None of the XDG stuff is thread-safe, so serialize all access under
// this lock.
-base::LazyInstance<base::Lock>::Leaky
- g_mime_util_xdg_lock = LAZY_INSTANCE_INITIALIZER;
+LazyInstance<Lock>::Leaky g_mime_util_xdg_lock = LAZY_INSTANCE_INITIALIZER;
class MimeUtilConstants {
public:
typedef std::map<std::string, IconTheme*> IconThemeMap;
- typedef std::map<FilePath, base::Time> IconDirMtimeMap;
+ typedef std::map<FilePath, Time> IconDirMtimeMap;
typedef std::vector<std::string> IconFormats;
// Specified by XDG icon theme specs.
@@ -62,7 +61,7 @@
// The default theme.
IconTheme* default_themes_[kDefaultThemeNum];
- base::TimeTicks last_check_time_;
+ TimeTicks last_check_time_;
// The current icon theme, usually set through GTK theme integration.
std::string icon_theme_name_;
@@ -159,7 +158,7 @@
IconTheme::IconTheme(const std::string& name)
: index_theme_loaded_(false) {
- base::ThreadRestrictions::AssertIOAllowed();
+ ThreadRestrictions::AssertIOAllowed();
// Iterate on all icon directories to find directories of the specified
// theme and load the first encountered index.theme.
MimeUtilConstants::IconDirMtimeMap::iterator iter;
@@ -281,7 +280,7 @@
std::string key, value;
std::vector<std::string> r;
- base::SplitStringDontTrim(entry, '=', &r);
+ SplitStringDontTrim(entry, '=', &r);
if (r.size() < 2)
continue;
@@ -385,12 +384,11 @@
return true;
}
-bool CheckDirExistsAndGetMtime(const FilePath& dir,
- base::Time* last_modified) {
+bool CheckDirExistsAndGetMtime(const FilePath& dir, Time* last_modified) {
if (!DirectoryExists(dir))
return false;
- base::PlatformFileInfo file_info;
- if (!file_util::GetFileInfo(dir, &file_info))
+ PlatformFileInfo file_info;
+ if (!GetFileInfo(dir, &file_info))
return false;
*last_modified = file_info.last_modified;
return true;
@@ -398,7 +396,7 @@
// Make sure |dir| exists and add it to the list of icon directories.
void TryAddIconDir(const FilePath& dir) {
- base::Time last_modified;
+ Time last_modified;
if (!CheckDirExistsAndGetMtime(dir, &last_modified))
return;
MimeUtilConstants::GetInstance()->icon_dirs_[dir] = last_modified;
@@ -449,15 +447,15 @@
void EnsureUpdated() {
MimeUtilConstants* constants = MimeUtilConstants::GetInstance();
if (constants->last_check_time_.is_null()) {
- constants->last_check_time_ = base::TimeTicks::Now();
+ constants->last_check_time_ = TimeTicks::Now();
InitIconDir();
return;
}
// Per xdg theme spec, we should check the icon directories every so often
// for newly added icons.
- base::TimeDelta time_since_last_check =
- base::TimeTicks::Now() - constants->last_check_time_;
+ TimeDelta time_since_last_check =
+ TimeTicks::Now() - constants->last_check_time_;
if (time_since_last_check.InSeconds() > constants->kUpdateIntervalInSeconds) {
constants->last_check_time_ += time_since_last_check;
@@ -465,7 +463,7 @@
MimeUtilConstants::IconDirMtimeMap* icon_dirs = &constants->icon_dirs_;
MimeUtilConstants::IconDirMtimeMap::iterator iter;
for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
- base::Time last_modified;
+ Time last_modified;
if (!CheckDirExistsAndGetMtime(iter->first, &last_modified) ||
last_modified != iter->second) {
rescan_icon_dirs = true;
@@ -502,7 +500,7 @@
IconTheme** default_themes =
MimeUtilConstants::GetInstance()->default_themes_;
- scoped_ptr<base::Environment> env(base::Environment::Create());
+ scoped_ptr<Environment> env(Environment::Create());
base::nix::DesktopEnvironment desktop_env =
base::nix::GetDesktopEnvironment(env.get());
if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3 ||
@@ -577,14 +575,14 @@
std::string GetFileMimeType(const FilePath& filepath) {
if (filepath.empty())
return std::string();
- base::ThreadRestrictions::AssertIOAllowed();
- base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
+ ThreadRestrictions::AssertIOAllowed();
+ AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str());
}
std::string GetDataMimeType(const std::string& data) {
- base::ThreadRestrictions::AssertIOAllowed();
- base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
+ ThreadRestrictions::AssertIOAllowed();
+ AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL);
}
@@ -599,13 +597,13 @@
}
FilePath GetMimeIcon(const std::string& mime_type, size_t size) {
- base::ThreadRestrictions::AssertIOAllowed();
+ ThreadRestrictions::AssertIOAllowed();
std::vector<std::string> icon_names;
std::string icon_name;
FilePath icon_file;
if (!mime_type.empty()) {
- base::AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
+ AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
const char *icon = xdg_mime_get_icon(mime_type.c_str());
icon_name = std::string(icon ? icon : "");
}
diff --git a/base/sys_info_chromeos.cc b/base/sys_info_chromeos.cc
index e0f8b4f..7cf6975 100644
--- a/base/sys_info_chromeos.cc
+++ b/base/sys_info_chromeos.cc
@@ -56,14 +56,14 @@
is_running_on_chromeos_ = false;
std::string lsb_release, lsb_release_time_str;
- scoped_ptr<base::Environment> env(base::Environment::Create());
+ scoped_ptr<Environment> env(Environment::Create());
bool parsed_from_env =
env->GetVar(kLsbReleaseKey, &lsb_release) &&
env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
if (parsed_from_env) {
double us = 0;
if (StringToDouble(lsb_release_time_str, &us))
- lsb_release_time_ = base::Time::FromDoubleT(us);
+ lsb_release_time_ = Time::FromDoubleT(us);
} else {
// If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
// set, fall back to a blocking read of the lsb_release file. This should
@@ -71,8 +71,8 @@
ThreadRestrictions::ScopedAllowIO allow_io;
FilePath path(kLinuxStandardBaseReleaseFile);
ReadFileToString(path, &lsb_release);
- base::PlatformFileInfo fileinfo;
- if (file_util::GetFileInfo(path, &fileinfo))
+ PlatformFileInfo fileinfo;
+ if (GetFileInfo(path, &fileinfo))
lsb_release_time_ = fileinfo.creation_time;
}
ParseLsbRelease(lsb_release);
@@ -97,7 +97,7 @@
*bugfix_version = bugfix_version_;
}
- const base::Time& lsb_release_time() const { return lsb_release_time_; }
+ const Time& lsb_release_time() const { return lsb_release_time_; }
const SysInfo::LsbReleaseMap& lsb_release_map() const {
return lsb_release_map_;
}
@@ -109,7 +109,7 @@
// of entries so the overhead for this will be small, and it can be
// useful for debugging.
std::vector<std::pair<std::string, std::string> > pairs;
- base::SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
+ SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
for (size_t i = 0; i < pairs.size(); ++i) {
std::string key, value;
TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
@@ -151,7 +151,7 @@
}
}
- base::Time lsb_release_time_;
+ Time lsb_release_time_;
SysInfo::LsbReleaseMap lsb_release_map_;
int32 major_version_;
int32 minor_version_;
@@ -196,7 +196,7 @@
}
// static
-base::Time SysInfo::GetLsbReleaseTime() {
+Time SysInfo::GetLsbReleaseTime() {
return GetChromeOSVersionInfo().lsb_release_time();
}
@@ -208,10 +208,10 @@
// static
void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
const Time& lsb_release_time) {
- scoped_ptr<base::Environment> env(base::Environment::Create());
+ scoped_ptr<Environment> env(Environment::Create());
env->SetVar(kLsbReleaseKey, lsb_release);
env->SetVar(kLsbReleaseTimeKey,
- base::DoubleToString(lsb_release_time.ToDoubleT()));
+ DoubleToString(lsb_release_time.ToDoubleT()));
g_chrome_os_version_info.Get().Parse();
}