From 6faa0e0d23ca6fc27ae603063ce23eb018a670cd Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Tue, 28 Apr 2009 06:50:36 +0000 Subject: ImportantFileWriter Introducing a class for writing important files, preventing their corruption during writing. Switched PrefService to use it. Other classes will be switched in future changesets. TEST=This may affect things using preferences. Make sure that changes in preferences don't get lost, and that you don't get excessive disk activity when changing preferences. http://crbug.com/10618 Review URL: http://codereview.chromium.org/83001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14717 0039d316-1c4b-4281-b951-d872f2087c98 --- base/file_util.cc | 8 ++++++++ base/file_util.h | 3 +++ base/file_util_posix.cc | 22 ++++++---------------- base/file_util_win.cc | 20 ++++++++++++-------- 4 files changed, 29 insertions(+), 24 deletions(-) (limited to 'base') diff --git a/base/file_util.cc b/base/file_util.cc index f7a0411..8eec3ac 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -188,6 +188,14 @@ bool ReadFileToString(const FilePath& path, std::string* contents) { return true; } +FILE* CreateAndOpenTemporaryFile(FilePath* path) { + FilePath directory; + if (!GetTempDir(&directory)) + return false; + + return CreateAndOpenTemporaryFileInDir(directory, path); +} + bool GetFileSize(const FilePath& file_path, int64* file_size) { FileInfo info; if (!GetFileInfo(file_path, &info)) diff --git a/base/file_util.h b/base/file_util.h index 489cbcc..47254ab 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -296,6 +296,9 @@ FILE* CreateAndOpenTemporaryFile(FilePath* path); // Like above but for shmem files. Only useful for POSIX. FILE* CreateAndOpenTemporaryShmemFile(FilePath* path); +// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. +FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path); + // Same as CreateTemporaryFileName but the file is created in |dir|. bool CreateTemporaryFileNameInDir(const std::wstring& dir, std::wstring* temp_file); diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index f218460..1835d3a 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -369,30 +369,20 @@ bool CreateTemporaryFileName(FilePath* path) { return true; } -FILE* CreateAndOpenTemporaryFile(FilePath* path) { - FilePath directory; - if (!GetTempDir(&directory)) - return false; - - int fd = CreateAndOpenFdForTemporaryFile(directory, path); - if (fd < 0) - return NULL; - - FILE *fp = fdopen(fd, "a+"); - return fp; -} - FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { FilePath directory; if (!GetShmemTempDir(&directory)) return false; - int fd = CreateAndOpenFdForTemporaryFile(directory, path); + return CreateAndOpenTemporaryFileInDir(directory, path); +} + +FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { + int fd = CreateAndOpenFdForTemporaryFile(dir, path); if (fd < 0) return NULL; - FILE *fp = fdopen(fd, "a+"); - return fp; + return fdopen(fd, "a+"); } bool CreateTemporaryFileNameInDir(const std::wstring& dir, diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 8b3d4f5..8964fbf 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -446,20 +446,24 @@ bool CreateTemporaryFileName(FilePath* path) { return false; } +FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { + return CreateAndOpenTemporaryFile(path); +} + // On POSIX we have semantics to create and open a temporary file // atomically. // TODO(jrg): is there equivalent call to use on Windows instead of // going 2-step? -FILE* CreateAndOpenTemporaryFile(FilePath* path) { - - if (!CreateTemporaryFileName(path)) { +FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { + std::wstring wstring_path; + if (!CreateTemporaryFileNameInDir(dir.value(), &wstring_path)) { return NULL; } - return OpenFile(*path, "w+"); -} - -FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { - return CreateAndOpenTemporaryFile(path); + *path = FilePath(wstring_path); + // 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+"); } bool CreateTemporaryFileNameInDir(const std::wstring& dir, -- cgit v1.1