diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.h | 14 | ||||
-rw-r--r-- | base/file_util_posix.cc | 39 |
2 files changed, 41 insertions, 12 deletions
diff --git a/base/file_util.h b/base/file_util.h index 9a590a4..8654e62 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -28,6 +28,10 @@ #include "base/string16.h" #include "base/time.h" +#if defined(OS_POSIX) +#include "base/file_descriptor_posix.h" +#endif + namespace base { class Time; } @@ -486,6 +490,10 @@ class MemoryMappedFile { // the file does not exist, or the memory mapping fails, it will return false. // Later we may want to allow the user to specify access. bool Initialize(const FilePath& file_name); +#if defined(OS_POSIX) + // As above, but works with an alreay-opened file. + bool Initialize(const base::FileDescriptor& fd); +#endif const uint8* data() const { return data_; } size_t length() const { return length_; } @@ -498,6 +506,10 @@ class MemoryMappedFile { // success, false on any kind of failure. This is a helper for Initialize(). bool MapFileToMemory(const FilePath& file_name); +#if defined(OS_POSIX) + bool MapFileToMemoryInternal(); +#endif + // Closes all open handles. Later we may want to make this public. void CloseHandles(); @@ -506,7 +518,7 @@ class MemoryMappedFile { HANDLE file_mapping_; #elif defined(OS_POSIX) // The file descriptor. - int file_; + base::FileDescriptor file_; #endif uint8* data_; size_t length_; diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index 6c5b86a..1a05d79 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -667,30 +667,47 @@ bool FileEnumerator::ReadDirectory(std::vector<DirectoryEntryInfo>* entries, // MemoryMappedFile MemoryMappedFile::MemoryMappedFile() - : file_(-1), - data_(NULL), + : data_(NULL), length_(0) { } +bool MemoryMappedFile::Initialize(const base::FileDescriptor& fd) { + if (IsValid()) + return false; + + file_ = fd; + + if (!MapFileToMemoryInternal()) { + CloseHandles(); + return false; + } + + return true; +} + bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { - file_ = open(file_name.value().c_str(), O_RDONLY); + file_ = base::FileDescriptor(open(file_name.value().c_str(), O_RDONLY), true); - if (file_ == -1) { + if (file_.fd == -1) { LOG(ERROR) << "Couldn't open " << file_name.value(); return false; } + return MapFileToMemoryInternal(); +} + +bool MemoryMappedFile::MapFileToMemoryInternal() { struct stat file_stat; - if (fstat(file_, &file_stat) == -1) { - LOG(ERROR) << "Couldn't fstat " << file_name.value() << ", errno " << errno; + if (fstat(file_.fd, &file_stat) == -1) { + LOG(ERROR) << "Couldn't fstat " << file_.fd << ", errno " << errno; return false; } length_ = file_stat.st_size; data_ = static_cast<uint8*>( - mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); + mmap(NULL, length_, PROT_READ, MAP_SHARED, file_.fd, 0)); if (data_ == MAP_FAILED) - LOG(ERROR) << "Couldn't mmap " << file_name.value() << ", errno " << errno; + LOG(ERROR) << "Couldn't mmap " << file_.fd << ", errno " << errno; return data_ != MAP_FAILED; } @@ -698,12 +715,12 @@ bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { void MemoryMappedFile::CloseHandles() { if (data_ != NULL) munmap(data_, length_); - if (file_ != -1) - close(file_); + if (file_.auto_close && file_.fd != -1) + close(file_.fd); data_ = NULL; length_ = 0; - file_ = -1; + file_ = base::FileDescriptor(); } } // namespace file_util |