diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-11 00:01:38 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-11 00:01:38 +0000 |
commit | 392264c76c724e3c31273ad1aa932a041cc12a7e (patch) | |
tree | fca9dca526bda74fa05a791b179e35e86e5c9755 | |
parent | 27961c57893201b2914f173cb517e33f37d0ae58 (diff) | |
download | chromium_src-392264c76c724e3c31273ad1aa932a041cc12a7e.zip chromium_src-392264c76c724e3c31273ad1aa932a041cc12a7e.tar.gz chromium_src-392264c76c724e3c31273ad1aa932a041cc12a7e.tar.bz2 |
Added CreateTemporaryFileName that takes a FilePath argument.
Review URL: http://codereview.chromium.org/9752
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5142 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/file_util.cc | 7 | ||||
-rw-r--r-- | base/file_util.h | 5 | ||||
-rw-r--r-- | base/file_util_posix.cc | 23 | ||||
-rw-r--r-- | base/file_util_unittest.cc | 4 | ||||
-rw-r--r-- | base/file_util_win.cc | 11 |
5 files changed, 32 insertions, 18 deletions
diff --git a/base/file_util.cc b/base/file_util.cc index f4d0504..4fdc128 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -343,6 +343,13 @@ bool ContentsEqual(const std::wstring& filename1, bool CreateDirectory(const std::wstring& full_path) { return CreateDirectory(FilePath::FromWStringHack(full_path)); } +bool CreateTemporaryFileName(std::wstring* temp_file) { + FilePath temp_file_path; + if (!CreateTemporaryFileName(&temp_file_path)) + return false; + *temp_file = temp_file_path.ToWStringHack(); + return true; +} bool GetCurrentDirectory(std::wstring* path_str) { FilePath path; if (!GetCurrentDirectory(&path)) diff --git a/base/file_util.h b/base/file_util.h index 2318ec7..3df31e4 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -237,16 +237,17 @@ bool IsDirectoryEmpty(const std::wstring& dir_path); #endif - // Get the temporary directory provided by the system. bool GetTempDir(FilePath* path); // Deprecated temporary compatibility function. bool GetTempDir(std::wstring* path); -// Creates a temporary file. The full path is placed in 'temp_file', and the +// 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. // TODO(erikkay): rename this function and track down all of the callers. +bool CreateTemporaryFileName(FilePath* path); +// Deprecated temporary compatibility function. bool CreateTemporaryFileName(std::wstring* temp_file); // Same as CreateTemporaryFileName but the file is created in |dir|. diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index fd012b4..a0c6cb6 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -24,7 +24,7 @@ namespace file_util { -static const wchar_t* kTempFileName = L"com.google.chrome.XXXXXX"; +static const char* kTempFileName = "com.google.chrome.XXXXXX"; std::wstring GetDirectoryFromPath(const std::wstring& path) { if (EndsWithSeparator(path)) { @@ -249,19 +249,20 @@ bool GetFileCreationLocalTime(const std::string& filename, } #endif -bool CreateTemporaryFileName(std::wstring* temp_file) { - std::wstring tmpdir; - if (!GetTempDir(&tmpdir)) +bool CreateTemporaryFileName(FilePath* path) { + if (!GetTempDir(path)) return false; - AppendToPath(&tmpdir, kTempFileName); - std::string tmpdir_string = WideToUTF8(tmpdir); + + *path = path->Append(kTempFileName); + 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()); + int fd = mkstemp(buffer); if (fd < 0) return false; - *temp_file = UTF8ToWide(buffer); - close(fd); + + close(fd); return true; } @@ -274,11 +275,11 @@ bool CreateTemporaryFileNameInDir(const std::wstring& dir, bool CreateNewTempDirectory(const std::wstring& prefix, std::wstring* new_temp_path) { - std::wstring tmpdir; + FilePath tmpdir; if (!GetTempDir(&tmpdir)) return false; - AppendToPath(&tmpdir, kTempFileName); - std::string tmpdir_string = WideToUTF8(tmpdir); + tmpdir = tmpdir.Append(kTempFileName); + std::string tmpdir_string = tmpdir.value(); // this should be OK since mkdtemp just replaces characters in place char* buffer = const_cast<char*>(tmpdir_string.c_str()); char* dtemp = mkdtemp(buffer); diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc index 441c903..d2cfcfb 100644 --- a/base/file_util_unittest.cc +++ b/base/file_util_unittest.cc @@ -689,14 +689,14 @@ TEST_F(FileUtilTest, CreateShortcutTest) { TEST_F(FileUtilTest, CreateTemporaryFileNameTest) { std::wstring temp_file; - file_util::CreateTemporaryFileName(&temp_file); + ASSERT_TRUE(file_util::CreateTemporaryFileName(&temp_file)); EXPECT_TRUE(file_util::PathExists(temp_file)); EXPECT_TRUE(file_util::Delete(temp_file, false)); } TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { std::wstring temp_dir; - file_util::CreateNewTempDirectory(std::wstring(), &temp_dir); + ASSERT_TRUE(file_util::CreateNewTempDirectory(std::wstring(), &temp_dir)); EXPECT_TRUE(file_util::PathExists(temp_dir)); EXPECT_TRUE(file_util::Delete(temp_dir, false)); } diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 6e6f6b0..355bdfd 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -393,13 +393,18 @@ bool GetTempDir(FilePath* path) { return true; } -bool CreateTemporaryFileName(std::wstring* temp_file) { - std::wstring temp_path; +bool CreateTemporaryFileName(FilePath* path) { + std::wstring temp_path, temp_file; if (!GetTempDir(&temp_path)) return false; - return CreateTemporaryFileNameInDir(temp_path, temp_file); + if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) { + *path = FilePath(temp_file); + return true; + } + + return false; } bool CreateTemporaryFileNameInDir(const std::wstring& dir, |