diff options
author | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-03 17:18:14 +0000 |
---|---|---|
committer | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-03 17:18:14 +0000 |
commit | 21da6eb1f8a9740de03cb1435bf935f5a3609a37 (patch) | |
tree | 47e6bdd8db72ee4b66f526bbe79cbca74ef85560 /net/disk_cache | |
parent | 0a173a23af355f6b4eceeb18f28b453063e4287c (diff) | |
download | chromium_src-21da6eb1f8a9740de03cb1435bf935f5a3609a37.zip chromium_src-21da6eb1f8a9740de03cb1435bf935f5a3609a37.tar.gz chromium_src-21da6eb1f8a9740de03cb1435bf935f5a3609a37.tar.bz2 |
* Add write and read/write support to FileStream (renamed from FileInputStream).
* Moved net/disk_cache/os_file to base/platform_file.
Review URL: http://codereview.chromium.org/8843
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4454 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r-- | net/disk_cache/backend_impl.cc | 16 | ||||
-rw-r--r-- | net/disk_cache/block_files.cc | 8 | ||||
-rw-r--r-- | net/disk_cache/disk_cache_test_util.cc | 8 | ||||
-rw-r--r-- | net/disk_cache/file.h | 10 | ||||
-rw-r--r-- | net/disk_cache/file_posix.cc | 32 | ||||
-rw-r--r-- | net/disk_cache/file_win.cc | 55 | ||||
-rw-r--r-- | net/disk_cache/mapped_file_posix.cc | 2 | ||||
-rw-r--r-- | net/disk_cache/mapped_file_win.cc | 2 | ||||
-rw-r--r-- | net/disk_cache/os_file.h | 40 | ||||
-rw-r--r-- | net/disk_cache/os_file_posix.cc | 61 | ||||
-rw-r--r-- | net/disk_cache/os_file_win.cc | 57 |
11 files changed, 76 insertions, 215 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index da11f98..20e6348 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -550,9 +550,12 @@ bool BackendImpl::CreateExternalFile(Addr* address) { continue; } std::wstring name = GetFileName(file_address); - scoped_refptr<disk_cache::File> file( - new disk_cache::File(CreateOSFile(name.c_str(), OS_FILE_READ | - OS_FILE_WRITE |OS_FILE_SHARE_READ | OS_FILE_CREATE, NULL))); + int flags = base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_WRITE | + base::PLATFORM_FILE_CREATE | + base::PLATFORM_FILE_EXCLUSIVE_WRITE; + scoped_refptr<disk_cache::File> file(new disk_cache::File( + base::CreatePlatformFile(name.c_str(), flags, NULL))); if (!file->IsValid()) continue; @@ -748,9 +751,12 @@ bool BackendImpl::InitBackingStore(bool* file_created) { std::wstring index_name(path_); file_util::AppendToPath(&index_name, kIndexName); + int flags = base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_WRITE | + base::PLATFORM_FILE_OPEN_ALWAYS | + base::PLATFORM_FILE_EXCLUSIVE_WRITE; scoped_refptr<disk_cache::File> file(new disk_cache::File( - CreateOSFile(index_name.c_str(), OS_FILE_READ | OS_FILE_WRITE | - OS_FILE_SHARE_READ | OS_FILE_OPEN_ALWAYS, file_created))); + base::CreatePlatformFile(index_name.c_str(), flags, file_created))); if (!file->IsValid()) return false; diff --git a/net/disk_cache/block_files.cc b/net/disk_cache/block_files.cc index 87f2dc2..9389b61 100644 --- a/net/disk_cache/block_files.cc +++ b/net/disk_cache/block_files.cc @@ -196,10 +196,12 @@ std::wstring BlockFiles::Name(int index) { bool BlockFiles::CreateBlockFile(int index, FileType file_type, bool force) { std::wstring name = Name(index); - int flags = force ? OS_FILE_CREATE_ALWAYS : OS_FILE_CREATE; - flags |= OS_FILE_WRITE | OS_FILE_SHARE_READ; + int flags = + force ? base::PLATFORM_FILE_CREATE_ALWAYS : base::PLATFORM_FILE_CREATE; + flags |= base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE; - scoped_refptr<File> file(new File(CreateOSFile(name.c_str(), flags, NULL))); + scoped_refptr<File> file(new File( + base::CreatePlatformFile(name.c_str(), flags, NULL))); if (!file->IsValid()) return false; diff --git a/net/disk_cache/disk_cache_test_util.cc b/net/disk_cache/disk_cache_test_util.cc index 3db5c6f..4920e54 100644 --- a/net/disk_cache/disk_cache_test_util.cc +++ b/net/disk_cache/disk_cache_test_util.cc @@ -50,10 +50,12 @@ std::wstring GetCachePath() { bool CreateCacheTestFile(const wchar_t* name) { using namespace disk_cache; - int flags = OS_FILE_CREATE_ALWAYS | OS_FILE_READ | OS_FILE_WRITE | - OS_FILE_SHARE_READ | OS_FILE_SHARE_WRITE; + int flags = base::PLATFORM_FILE_CREATE_ALWAYS | + base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_WRITE; - scoped_refptr<File> file(new File(CreateOSFile(name, flags, NULL))); + scoped_refptr<File> file(new File( + base::CreatePlatformFile(name, flags, NULL))); if (!file->IsValid()) return false; diff --git a/net/disk_cache/file.h b/net/disk_cache/file.h index cdfdcf8..b457e88 100644 --- a/net/disk_cache/file.h +++ b/net/disk_cache/file.h @@ -9,8 +9,8 @@ #include <string> +#include "base/platform_file.h" #include "base/ref_counted.h" -#include "net/disk_cache/os_file.h" namespace disk_cache { @@ -35,14 +35,14 @@ class File : public base::RefCounted<File> { // Initializes the object to use the passed in file instead of opening it with // the Init() call. No asynchronous operations can be performed with this // object. - explicit File(OSFile file); + explicit File(base::PlatformFile file); // Initializes the object to point to a given file. The file must aready exist // on disk, and allow shared read and write. bool Init(const std::wstring& name); // Returns the handle or file descriptor. - OSFile os_file() const; + base::PlatformFile platform_file() const; // Returns true if the file was opened properly. bool IsValid() const; @@ -78,8 +78,8 @@ class File : public base::RefCounted<File> { private: bool init_; bool mixed_; - OSFile os_file_; // Regular, asynchronous IO handle. - OSFile sync_os_file_; // Synchronous IO hanlde. + base::PlatformFile platform_file_; // Regular, asynchronous IO handle. + base::PlatformFile sync_platform_file_; // Synchronous IO handle. DISALLOW_COPY_AND_ASSIGN(File); }; diff --git a/net/disk_cache/file_posix.cc b/net/disk_cache/file_posix.cc index 1dcd141..8985c0d 100644 --- a/net/disk_cache/file_posix.cc +++ b/net/disk_cache/file_posix.cc @@ -11,18 +11,20 @@ namespace disk_cache { -File::File(OSFile file) - : init_(true), os_file_(file) { +File::File(base::PlatformFile file) + : init_(true), platform_file_(file) { } bool File::Init(const std::wstring& name) { if (init_) return false; - os_file_ = CreateOSFile(name, OS_FILE_OPEN | OS_FILE_READ | OS_FILE_WRITE, - NULL); - if (os_file_ < 0) { - os_file_ = 0; + int flags = base::PLATFORM_FILE_OPEN | + base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_WRITE; + platform_file_ = base::CreatePlatformFile(name, flags, NULL); + if (platform_file_ < 0) { + platform_file_ = 0; return false; } @@ -31,18 +33,18 @@ bool File::Init(const std::wstring& name) { } File::~File() { - if (os_file_) - close(os_file_); + if (platform_file_) + close(platform_file_); } -OSFile File::os_file() const { - return os_file_; +base::PlatformFile File::platform_file() const { + return platform_file_; } bool File::IsValid() const { if (!init_) return false; - return (INVALID_HANDLE_VALUE != os_file_); + return (base::kInvalidPlatformFileValue != platform_file_); } bool File::Read(void* buffer, size_t buffer_len, size_t offset) { @@ -50,7 +52,7 @@ bool File::Read(void* buffer, size_t buffer_len, size_t offset) { if (buffer_len > ULONG_MAX || offset > LONG_MAX) return false; - int ret = pread(os_file_, buffer, buffer_len, offset); + int ret = pread(platform_file_, buffer, buffer_len, offset); return (static_cast<size_t>(ret) == buffer_len); } @@ -59,7 +61,7 @@ bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { if (buffer_len > ULONG_MAX || offset > ULONG_MAX) return false; - int ret = pwrite(os_file_, buffer, buffer_len, offset); + int ret = pwrite(platform_file_, buffer, buffer_len, offset); return (static_cast<size_t>(ret) == buffer_len); } @@ -114,12 +116,12 @@ bool File::SetLength(size_t length) { if (length > ULONG_MAX) return false; - return 0 == ftruncate(os_file_, length); + return 0 == ftruncate(platform_file_, length); } size_t File::GetLength() { DCHECK(init_); - size_t ret = lseek(os_file_, 0, SEEK_END); + size_t ret = lseek(platform_file_, 0, SEEK_END); return ret; } diff --git a/net/disk_cache/file_win.cc b/net/disk_cache/file_win.cc index c0ad795..0e8e3f3 100644 --- a/net/disk_cache/file_win.cc +++ b/net/disk_cache/file_win.cc @@ -88,9 +88,9 @@ void CALLBACK IoCompletion(DWORD error, DWORD actual_bytes, } } -File::File(OSFile file) - : init_(true), mixed_(true), os_file_(INVALID_HANDLE_VALUE), - sync_os_file_(file) { +File::File(base::PlatformFile file) + : init_(true), mixed_(true), platform_file_(INVALID_HANDLE_VALUE), + sync_platform_file_(file) { } bool File::Init(const std::wstring& name) { @@ -98,23 +98,23 @@ bool File::Init(const std::wstring& name) { if (init_) return false; - os_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, + platform_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - if (INVALID_HANDLE_VALUE == os_file_) + if (INVALID_HANDLE_VALUE == platform_file_) return false; init_ = true; if (mixed_) { - sync_os_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, + sync_platform_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - if (INVALID_HANDLE_VALUE == sync_os_file_) + if (INVALID_HANDLE_VALUE == sync_platform_file_) return false; } else { - sync_os_file_ = INVALID_HANDLE_VALUE; + sync_platform_file_ = INVALID_HANDLE_VALUE; } return true; @@ -124,22 +124,23 @@ File::~File() { if (!init_) return; - if (INVALID_HANDLE_VALUE != os_file_) - CloseHandle(os_file_); - if (mixed_ && INVALID_HANDLE_VALUE != sync_os_file_) - CloseHandle(sync_os_file_); + if (INVALID_HANDLE_VALUE != platform_file_) + CloseHandle(platform_file_); + if (mixed_ && INVALID_HANDLE_VALUE != sync_platform_file_) + CloseHandle(sync_platform_file_); } -OSFile File::os_file() const { +base::PlatformFile File::platform_file() const { DCHECK(init_); - return (INVALID_HANDLE_VALUE == os_file_) ? sync_os_file_ : os_file_; + return (INVALID_HANDLE_VALUE == platform_file_) ? sync_platform_file_ : + platform_file_; } bool File::IsValid() const { if (!init_) return false; - return (INVALID_HANDLE_VALUE != os_file_ || - INVALID_HANDLE_VALUE != sync_os_file_); + return (INVALID_HANDLE_VALUE != platform_file_ || + INVALID_HANDLE_VALUE != sync_platform_file_); } bool File::Read(void* buffer, size_t buffer_len, size_t offset) { @@ -147,14 +148,16 @@ bool File::Read(void* buffer, size_t buffer_len, size_t offset) { if (!mixed_ || buffer_len > ULONG_MAX || offset > LONG_MAX) return false; - DWORD ret = SetFilePointer(sync_os_file_, static_cast<LONG>(offset), NULL, + DWORD ret = SetFilePointer(sync_platform_file_, + static_cast<LONG>(offset), + NULL, FILE_BEGIN); if (INVALID_SET_FILE_POINTER == ret) return false; DWORD actual; DWORD size = static_cast<DWORD>(buffer_len); - if (!ReadFile(sync_os_file_, buffer, size, &actual, NULL)) + if (!ReadFile(sync_platform_file_, buffer, size, &actual, NULL)) return false; return actual == size; } @@ -164,14 +167,16 @@ bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { if (!mixed_ || buffer_len > ULONG_MAX || offset > ULONG_MAX) return false; - DWORD ret = SetFilePointer(sync_os_file_, static_cast<LONG>(offset), NULL, + DWORD ret = SetFilePointer(sync_platform_file_, + static_cast<LONG>(offset), + NULL, FILE_BEGIN); if (INVALID_SET_FILE_POINTER == ret) return false; DWORD actual; DWORD size = static_cast<DWORD>(buffer_len); - if (!WriteFile(sync_os_file_, buffer, size, &actual, NULL)) + if (!WriteFile(sync_platform_file_, buffer, size, &actual, NULL)) return false; return actual == size; } @@ -196,7 +201,8 @@ bool File::Read(void* buffer, size_t buffer_len, size_t offset, DWORD size = static_cast<DWORD>(buffer_len); AddRef(); - if (!ReadFileEx(os_file_, buffer, size, &data->overlapped, &IoCompletion)) { + if (!ReadFileEx(platform_file_, buffer, size, &data->overlapped, + &IoCompletion)) { Release(); delete data; return false; @@ -260,7 +266,8 @@ bool File::AsyncWrite(const void* buffer, size_t buffer_len, size_t offset, DWORD size = static_cast<DWORD>(buffer_len); AddRef(); - if (!WriteFileEx(os_file_, buffer, size, &data->overlapped, &IoCompletion)) { + if (!WriteFileEx(platform_file_, buffer, size, &data->overlapped, + &IoCompletion)) { Release(); delete data; return false; @@ -295,7 +302,7 @@ bool File::SetLength(size_t length) { return false; DWORD size = static_cast<DWORD>(length); - HANDLE file = os_file(); + HANDLE file = platform_file(); if (INVALID_SET_FILE_POINTER == SetFilePointer(file, size, NULL, FILE_BEGIN)) return false; @@ -305,7 +312,7 @@ bool File::SetLength(size_t length) { size_t File::GetLength() { DCHECK(init_); LARGE_INTEGER size; - HANDLE file = os_file(); + HANDLE file = platform_file(); if (!GetFileSizeEx(file, &size)) return 0; if (size.HighPart) diff --git a/net/disk_cache/mapped_file_posix.cc b/net/disk_cache/mapped_file_posix.cc index fbcdbaa..46147ac 100644 --- a/net/disk_cache/mapped_file_posix.cc +++ b/net/disk_cache/mapped_file_posix.cc @@ -20,7 +20,7 @@ void* MappedFile::Init(const std::wstring name, size_t size) { size = GetLength(); buffer_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, - os_file(), 0); + platform_file(), 0); init_ = true; DCHECK(reinterpret_cast<int>(buffer_) != -1); if (reinterpret_cast<int>(buffer_) == -1) diff --git a/net/disk_cache/mapped_file_win.cc b/net/disk_cache/mapped_file_win.cc index 21e95aa..6407332 100644 --- a/net/disk_cache/mapped_file_win.cc +++ b/net/disk_cache/mapped_file_win.cc @@ -15,7 +15,7 @@ void* MappedFile::Init(const std::wstring name, size_t size) { buffer_ = NULL; init_ = true; - section_ = CreateFileMapping(os_file(), NULL, PAGE_READWRITE, 0, + section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0, static_cast<DWORD>(size), NULL); if (!section_) return NULL; diff --git a/net/disk_cache/os_file.h b/net/disk_cache/os_file.h deleted file mode 100644 index 2ef95e0..0000000 --- a/net/disk_cache/os_file.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef NET_DISK_CACHE_OS_FILE_H_ -#define NET_DISK_CACHE_OS_FILE_H_ - -#include <string> - -#include "build/build_config.h" - -namespace disk_cache { - -#if defined(OS_WIN) -#include <windows.h> -typedef HANDLE OSFile; -#elif defined(OS_POSIX) -typedef int OSFile; -const OSFile INVALID_HANDLE_VALUE = -1; -#endif - -enum OSFileFlags { - OS_FILE_OPEN = 1, - OS_FILE_CREATE = 2, - OS_FILE_OPEN_ALWAYS = 4, // May create a new file. - OS_FILE_CREATE_ALWAYS = 8, // May overwrite an old file. - OS_FILE_READ = 16, - OS_FILE_WRITE = 32, - OS_FILE_SHARE_READ = 64, - OS_FILE_SHARE_WRITE = 128 -}; - -// Creates or open the given file. If OS_FILE_OPEN_ALWAYS is used, and |created| -// is provided, |created| will be set to true if the file was created or to -// false in case the file was just opened. -OSFile CreateOSFile(const std::wstring& name, int flags, bool* created); - -} // namespace disk_cache - -#endif // NET_DISK_CACHE_OS_FILE_H_ diff --git a/net/disk_cache/os_file_posix.cc b/net/disk_cache/os_file_posix.cc deleted file mode 100644 index 8209e37..0000000 --- a/net/disk_cache/os_file_posix.cc +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "net/disk_cache/os_file.h" - -#include <fcntl.h> -#include <errno.h> - -#include "base/logging.h" -#include "base/string_util.h" - -namespace disk_cache { - -OSFile CreateOSFile(const std::wstring& name, int flags, bool* created) { - int open_flags = 0; - if (flags & OS_FILE_CREATE) - open_flags = O_CREAT | O_EXCL; - - if (flags & OS_FILE_CREATE_ALWAYS) { - DCHECK(!open_flags); - open_flags = O_CREAT | O_TRUNC; - } - - if (!open_flags && !(flags & OS_FILE_OPEN) && - !(flags & OS_FILE_OPEN_ALWAYS)) { - NOTREACHED(); - errno = ENOTSUP; - return INVALID_HANDLE_VALUE; - } - - if (flags & OS_FILE_WRITE && flags & OS_FILE_READ) { - open_flags |= O_RDWR; - } else if (flags & OS_FILE_WRITE) { - open_flags |= O_WRONLY; - } else if (!(flags & OS_FILE_READ)) { - NOTREACHED(); - } - - DCHECK(O_RDONLY == 0); - - int descriptor = open(WideToUTF8(name).c_str(), open_flags, - S_IRUSR | S_IWUSR); - - if (flags & OS_FILE_OPEN_ALWAYS) { - if (descriptor > 0) { - if (created) - *created = false; - } else { - open_flags |= O_CREAT; - descriptor = open(WideToUTF8(name).c_str(), open_flags, - S_IRUSR | S_IWUSR); - if (created && descriptor > 0) - *created = true; - } - } - - return descriptor; -} - -} // namespace disk_cache diff --git a/net/disk_cache/os_file_win.cc b/net/disk_cache/os_file_win.cc deleted file mode 100644 index 8c10bdd..0000000 --- a/net/disk_cache/os_file_win.cc +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "net/disk_cache/os_file.h" - -#include "base/logging.h" - -namespace disk_cache { - -OSFile CreateOSFile(const std::wstring& name, int flags, bool* created) { - DWORD disposition = 0; - - if (flags & OS_FILE_OPEN) - disposition = OPEN_EXISTING; - - if (flags & OS_FILE_CREATE) { - DCHECK(!disposition); - disposition = CREATE_NEW; - } - - if (flags & OS_FILE_OPEN_ALWAYS) { - DCHECK(!disposition); - disposition = OPEN_ALWAYS; - } - - if (flags & OS_FILE_CREATE_ALWAYS) { - DCHECK(!disposition); - disposition = CREATE_ALWAYS; - } - - if (!disposition) { - NOTREACHED(); - return NULL; - } - - DWORD access = (flags & OS_FILE_READ) ? GENERIC_READ : 0; - if (flags & OS_FILE_WRITE) - access |= GENERIC_WRITE; - - DWORD sharing = (flags & OS_FILE_SHARE_READ) ? FILE_SHARE_READ : 0; - if (flags & OS_FILE_SHARE_WRITE) - access |= FILE_SHARE_WRITE; - - HANDLE file = CreateFile(name.c_str(), access, sharing, NULL, disposition, 0, - NULL); - - if ((flags & OS_FILE_OPEN_ALWAYS) && created && - INVALID_HANDLE_VALUE != file) { - *created = (ERROR_ALREADY_EXISTS != GetLastError()); - } - - return file; -} - -} // namespace disk_cache - |