Skip to content

Commit

Permalink
Move temp file functions to base namespace.
Browse files Browse the repository at this point in the history
BUG=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238427 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Dec 3, 2013
1 parent e7a32a1 commit 03d9afc
Show file tree
Hide file tree
Showing 134 changed files with 304 additions and 324 deletions.
8 changes: 4 additions & 4 deletions apps/app_restore_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
base::ScopedTempDir temp_directory;
ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
base::FilePath temp_file;
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
&temp_file));
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.path(),
&temp_file));

FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&temp_file);
Expand Down Expand Up @@ -108,8 +108,8 @@ IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
base::ScopedTempDir temp_directory;
ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
base::FilePath temp_file;
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
&temp_file));
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.path(),
&temp_file));

FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&temp_file);
Expand Down
2 changes: 1 addition & 1 deletion base/debug/trace_event_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class TraceEventWinTest: public testing::Test {
}

// Create the log file.
ASSERT_TRUE(file_util::CreateTemporaryFile(&log_file_));
ASSERT_TRUE(base::CreateTemporaryFile(&log_file_));

// Create a private log session on the file.
EtwTraceProperties prop;
Expand Down
16 changes: 8 additions & 8 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ bool IsDirectoryEmpty(const FilePath& dir_path) {
return false;
}

FILE* CreateAndOpenTemporaryFile(FilePath* path) {
FilePath directory;
if (!GetTempDir(&directory))
return NULL;

return CreateAndOpenTemporaryFileInDir(directory, path);
}

} // namespace base

// -----------------------------------------------------------------------------
Expand All @@ -163,14 +171,6 @@ using base::FileEnumerator;
using base::FilePath;
using base::kMaxUniqueFiles;

FILE* CreateAndOpenTemporaryFile(FilePath* path) {
FilePath directory;
if (!GetTempDir(&directory))
return NULL;

return CreateAndOpenTemporaryFileInDir(directory, path);
}

bool CreateDirectory(const base::FilePath& full_path) {
return CreateDirectoryAndGetError(full_path, NULL);
}
Expand Down
40 changes: 20 additions & 20 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,49 +212,49 @@ BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
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
// be empty and all handles closed after this function returns.
BASE_EXPORT bool CreateTemporaryFile(base::FilePath* path);
BASE_EXPORT bool CreateTemporaryFile(FilePath* path);

// Same as CreateTemporaryFile but the file is created in |dir|.
BASE_EXPORT bool CreateTemporaryFileInDir(const base::FilePath& dir,
base::FilePath* temp_file);
BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir,
FilePath* temp_file);

// Create and open a temporary file. File is opened for read/write.
// The full path is placed in |path|.
// Returns a handle to the opened file or NULL if an error occurred.
BASE_EXPORT FILE* CreateAndOpenTemporaryFile(base::FilePath* path);
BASE_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path);

// Like above but for shmem files. Only useful for POSIX.
// The executable flag says the file needs to support using
// mprotect with PROT_EXEC after mapping.
BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(base::FilePath* path,
BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(FilePath* path,
bool executable);

// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const base::FilePath& dir,
base::FilePath* path);
BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
FilePath* path);

// Create a new directory. If prefix is provided, the new directory name is in
// the format of prefixyyyy.
// NOTE: prefix is ignored in the POSIX implementation.
// If success, return true and output the full path of the directory created.
BASE_EXPORT bool CreateNewTempDirectory(
const base::FilePath::StringType& prefix,
base::FilePath* new_temp_path);
BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix,
FilePath* new_temp_path);

// Create a directory within another directory.
// Extra characters will be appended to |prefix| to ensure that the
// new directory does not have the same name as an existing directory.
BASE_EXPORT bool CreateTemporaryDirInDir(
const base::FilePath& base_dir,
const base::FilePath::StringType& prefix,
base::FilePath* new_dir);
BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir,
const FilePath::StringType& prefix,
FilePath* new_dir);

} // namespace base

// -----------------------------------------------------------------------------

namespace file_util {

// Creates a directory, as well as creating any parent directories, if they
// don't exist. Returns 'true' on successful creation, or if the directory
Expand Down
44 changes: 22 additions & 22 deletions base/file_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,25 +513,8 @@ FilePath GetHomeDir() {
}
#endif // !defined(OS_MACOSX)

} // 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::RealPath;
using base::VerifySpecificPathControlledByUser;

bool CreateTemporaryFile(FilePath* path) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to close().
ThreadRestrictions::AssertIOAllowed(); // For call to close().
FilePath directory;
if (!GetTempDir(&directory))
return false;
Expand Down Expand Up @@ -562,15 +545,15 @@ FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
}

bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to close().
ThreadRestrictions::AssertIOAllowed(); // For call to close().
int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
}

static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
const FilePath::StringType& name_tmpl,
FilePath* new_dir) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
<< "Directory name template must contain \"XXXXXX\".";

Expand Down Expand Up @@ -602,10 +585,27 @@ bool CreateNewTempDirectory(const FilePath::StringType& prefix,
if (!GetTempDir(&tmpdir))
return false;

return CreateTemporaryDirInDirImpl(tmpdir, base::TempFileName(),
new_temp_path);
return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
}


} // 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::RealPath;
using base::VerifySpecificPathControlledByUser;

bool CreateDirectoryAndGetError(const FilePath& full_path,
base::PlatformFileError* error) {
base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
Expand Down
13 changes: 6 additions & 7 deletions base/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());

FilePath temp_file;
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file));
ASSERT_TRUE(base::CreateTemporaryFileInDir(short_test_dir, &temp_file));
EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
EXPECT_TRUE(PathExists(temp_file));

Expand All @@ -548,7 +548,7 @@ TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));

// Use the short form of the directory to create a temporary filename.
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
ASSERT_TRUE(base::CreateTemporaryFileInDir(
short_test_dir.Append(kTestSubDirName), &temp_file));
EXPECT_TRUE(PathExists(temp_file));
EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
Expand Down Expand Up @@ -1582,7 +1582,7 @@ TEST_F(FileUtilTest, GetTempDirTest) {
TEST_F(FileUtilTest, CreateTemporaryFileTest) {
FilePath temp_files[3];
for (int i = 0; i < 3; i++) {
ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
ASSERT_TRUE(base::CreateTemporaryFile(&(temp_files[i])));
EXPECT_TRUE(PathExists(temp_files[i]));
EXPECT_FALSE(DirectoryExists(temp_files[i]));
}
Expand All @@ -1599,7 +1599,7 @@ TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {

// Create; make sure they are open and exist.
for (i = 0; i < 3; ++i) {
fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
fps[i] = base::CreateAndOpenTemporaryFile(&(names[i]));
ASSERT_TRUE(fps[i]);
EXPECT_TRUE(PathExists(names[i]));
}
Expand All @@ -1618,15 +1618,14 @@ TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {

TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
FilePath temp_dir;
ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
&temp_dir));
ASSERT_TRUE(base::CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
EXPECT_TRUE(PathExists(temp_dir));
EXPECT_TRUE(DeleteFile(temp_dir, false));
}

TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
FilePath new_dir;
ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
ASSERT_TRUE(base::CreateTemporaryDirInDir(
temp_dir_.path(),
FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
&new_dir));
Expand Down
44 changes: 22 additions & 22 deletions base/file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,8 @@ bool GetShmemTempDir(bool executable, FilePath* path) {
return GetTempDir(path);
}

} // namespace base

// -----------------------------------------------------------------------------

namespace file_util {

using base::DirectoryExists;
using base::FilePath;
using base::kFileShareAll;

bool CreateTemporaryFile(FilePath* path) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();

FilePath temp_file;

Expand All @@ -258,7 +248,7 @@ bool CreateTemporaryFile(FilePath* path) {
}

FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();
return CreateAndOpenTemporaryFile(path);
}

Expand All @@ -267,24 +257,24 @@ FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) {
// TODO(jrg): is there equivalent call to use on Windows instead of
// going 2-step?
FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();
if (!CreateTemporaryFileInDir(dir, path)) {
return NULL;
}
// Open file in binary mode, to avoid problems with fwrite. On Windows
// it replaces \n's with \r\n's, which may surprise you.
// Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
return OpenFile(*path, "wb+");
return file_util::OpenFile(*path, "wb+");
}

bool CreateTemporaryFileInDir(const FilePath& dir,
FilePath* temp_file) {
base::ThreadRestrictions::AssertIOAllowed();
bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
ThreadRestrictions::AssertIOAllowed();

wchar_t temp_name[MAX_PATH + 1];

if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) {
DPLOG(WARNING) << "Failed to get temporary file name in " << dir.value();
DPLOG(WARNING) << "Failed to get temporary file name in "
<< UTF16ToUTF8(dir.value());
return false;
}

Expand All @@ -305,7 +295,7 @@ bool CreateTemporaryFileInDir(const FilePath& dir,
bool CreateTemporaryDirInDir(const FilePath& base_dir,
const FilePath::StringType& prefix,
FilePath* new_dir) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();

FilePath path_to_create;

Expand All @@ -314,9 +304,9 @@ bool CreateTemporaryDirInDir(const FilePath& base_dir,
// the one exists, keep trying another path name until we reach some limit.
string16 new_dir_name;
new_dir_name.assign(prefix);
new_dir_name.append(base::IntToString16(::base::GetCurrentProcId()));
new_dir_name.append(IntToString16(GetCurrentProcId()));
new_dir_name.push_back('_');
new_dir_name.append(base::IntToString16(base::RandInt(0, kint16max)));
new_dir_name.append(IntToString16(RandInt(0, kint16max)));

path_to_create = base_dir.Append(new_dir_name);
if (::CreateDirectory(path_to_create.value().c_str(), NULL)) {
Expand All @@ -330,7 +320,7 @@ bool CreateTemporaryDirInDir(const FilePath& base_dir,

bool CreateNewTempDirectory(const FilePath::StringType& prefix,
FilePath* new_temp_path) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();

FilePath system_temp_dir;
if (!GetTempDir(&system_temp_dir))
Expand All @@ -339,6 +329,16 @@ bool CreateNewTempDirectory(const FilePath::StringType& prefix,
return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path);
}

} // namespace base

// -----------------------------------------------------------------------------

namespace file_util {

using base::DirectoryExists;
using base::FilePath;
using base::kFileShareAll;

bool CreateDirectoryAndGetError(const FilePath& full_path,
base::PlatformFileError* error) {
base::ThreadRestrictions::AssertIOAllowed();
Expand Down
2 changes: 1 addition & 1 deletion base/files/file_util_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CreateTemporaryHelper {
void RunWork(int additional_file_flags) {
// TODO(darin): file_util should have a variant of CreateTemporaryFile
// that returns a FilePath and a PlatformFile.
file_util::CreateTemporaryFile(&file_path_);
base::CreateTemporaryFile(&file_path_);

int file_flags =
PLATFORM_FILE_WRITE |
Expand Down
2 changes: 1 addition & 1 deletion base/files/important_file_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
// as target file, so it can be moved in one step, and that the temp file
// is securely created.
FilePath tmp_file_path;
if (!file_util::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
if (!base::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
LogFailure(path, FAILED_CREATING, "could not create temporary file");
return false;
}
Expand Down
Loading

0 comments on commit 03d9afc

Please sign in to comment.