Skip to content

Commit

Permalink
Move some more file utils to the base namespace.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brettw@chromium.org committed Dec 2, 2013
1 parent 072b8d4 commit fb4bcfa
Show file tree
Hide file tree
Showing 47 changed files with 219 additions and 206 deletions.
2 changes: 1 addition & 1 deletion base/base_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool PathProvider(int key, FilePath* result) {
cur = cur.DirName();
break;
case DIR_TEMP:
if (!file_util::GetTempDir(&cur))
if (!base::GetTempDir(&cur))
return false;
break;
case DIR_TEST_DATA:
Expand Down
2 changes: 1 addition & 1 deletion base/base_paths_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool PathProviderAndroid(int key, FilePath* result) {
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);
Expand Down
2 changes: 1 addition & 1 deletion base/base_paths_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool PathProviderPosix(int key, FilePath* result) {
return true;
}
case base::DIR_HOME:
*result = file_util::GetHomeDir();
*result = GetHomeDir();
return true;
}
return false;
Expand Down
16 changes: 8 additions & 8 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ bool ReadFileToString(const FilePath& path, std::string* contents) {
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

// -----------------------------------------------------------------------------
Expand All @@ -155,14 +163,6 @@ using base::FileEnumerator;
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))
Expand Down
44 changes: 30 additions & 14 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,42 @@ BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode);

#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);
// 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: 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.
//
// 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(base::FilePath* path, bool executable);
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.
BASE_EXPORT base::FilePath GetHomeDir();
//
// 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 {

// 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
Expand Down
6 changes: 3 additions & 3 deletions base/file_util_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions base/file_util_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
}

} // namespace internal
} // namepsace base

namespace file_util {

bool GetTempDir(base::FilePath* path) {
NSString* tmp = NSTemporaryDirectory();
Expand All @@ -35,8 +32,8 @@ bool GetTempDir(base::FilePath* path) {
return true;
}

bool GetShmemTempDir(base::FilePath* path, bool executable) {
bool GetShmemTempDir(bool executable, base::FilePath* path) {
return GetTempDir(path);
}

} // namespace
} // namespace base
Loading

0 comments on commit fb4bcfa

Please sign in to comment.