diff options
author | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 00:58:39 +0000 |
---|---|---|
committer | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 00:58:39 +0000 |
commit | 9e51af9018550b6b23802f66469310f5d1790ab9 (patch) | |
tree | 9ab6f3e0532f6d23c2af1d0acc5605ad00876418 /base/file_util_posix.cc | |
parent | 4c7ca4b8c2ceb6f823474744f035672c8501d23b (diff) | |
download | chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.zip chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.tar.gz chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.tar.bz2 |
Properly honor base::SharedMemory semantics for name="" to mean
new/private shared memory on POSIX. Transition base::SharedMemory
implementation to file/mmap() to prevent leaking of wired kernel
resources and allow easier cleanup. Enable one more shared_memory
unit test for POSIX. Enable stats_table_unittest.cc for Mac, and
modify it so it cleans up.
Review URL: http://codereview.chromium.org/19724
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9114 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_posix.cc')
-rw-r--r-- | base/file_util_posix.cc | 62 |
1 files changed, 48 insertions, 14 deletions
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index d44b743..6d3984e 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -38,7 +38,7 @@ std::wstring GetDirectoryFromPath(const std::wstring& path) { return UTF8ToWide(dirname(full_path)); } } - + bool AbsolutePath(FilePath* path) { char full_path[PATH_MAX]; if (realpath(path->value().c_str(), full_path) == NULL) @@ -57,7 +57,7 @@ bool Delete(const FilePath& path, bool recursive) { int test = stat64(path_str, &file_info); if (test != 0) { // The Windows version defines this condition as success. - bool ret = (errno == ENOENT || errno == ENOTDIR); + bool ret = (errno == ENOENT || errno == ENOTDIR); return ret; } if (!S_ISDIR(file_info.st_mode)) @@ -251,45 +251,79 @@ bool GetFileCreationLocalTimeFromHandle(int fd, LPSYSTEMTIME creation_time) { if (!file_handle) return false; - + FILETIME utc_filetime; if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) return false; - + FILETIME local_filetime; if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) return false; - + return !!FileTimeToSystemTime(&local_filetime, creation_time); } bool GetFileCreationLocalTime(const std::string& filename, LPSYSTEMTIME creation_time) { ScopedHandle file_handle( - CreateFile(filename.c_str(), GENERIC_READ, + CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); } #endif -bool CreateTemporaryFileName(FilePath* path) { - if (!GetTempDir(path)) - return false; - - *path = path->Append(kTempFileName); - std::string tmpdir_string = path->value(); +// Creates and opens a temporary file in |directory|, returning the +// file descriptor. |path| is set to the temporary file path. +// Note TODO(erikkay) comment in header for BlahFileName() calls; the +// intent is to rename these files BlahFile() (since they create +// files, not filenames). This function does NOT unlink() the file. +int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { + *path = directory.Append(kTempFileName); + const 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); + return mkstemp(buffer); +} + +bool CreateTemporaryFileName(FilePath* path) { + FilePath directory; + if (!GetTempDir(&directory)) + return false; + int fd = CreateAndOpenFdForTemporaryFile(directory, path); if (fd < 0) return false; - close(fd); 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); + if (fd < 0) + return NULL; + + FILE *fp = fdopen(fd, "a+"); + return fp; +} + bool CreateTemporaryFileNameInDir(const std::wstring& dir, std::wstring* temp_file) { // Not implemented yet. |