| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/file.h" | 5 #include "net/disk_cache/file.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
| 11 | 11 |
| 12 namespace disk_cache { | 12 namespace disk_cache { |
| 13 | 13 |
| 14 File::File(OSFile file) | 14 File::File(base::PlatformFile file) |
| 15 : init_(true), os_file_(file) { | 15 : init_(true), platform_file_(file) { |
| 16 } | 16 } |
| 17 | 17 |
| 18 bool File::Init(const std::wstring& name) { | 18 bool File::Init(const std::wstring& name) { |
| 19 if (init_) | 19 if (init_) |
| 20 return false; | 20 return false; |
| 21 | 21 |
| 22 os_file_ = CreateOSFile(name, OS_FILE_OPEN | OS_FILE_READ | OS_FILE_WRITE, | 22 int flags = base::PLATFORM_FILE_OPEN | |
| 23 NULL); | 23 base::PLATFORM_FILE_READ | |
| 24 if (os_file_ < 0) { | 24 base::PLATFORM_FILE_WRITE; |
| 25 os_file_ = 0; | 25 platform_file_ = base::CreatePlatformFile(name, flags, NULL); |
| 26 if (platform_file_ < 0) { |
| 27 platform_file_ = 0; |
| 26 return false; | 28 return false; |
| 27 } | 29 } |
| 28 | 30 |
| 29 init_ = true; | 31 init_ = true; |
| 30 return true; | 32 return true; |
| 31 } | 33 } |
| 32 | 34 |
| 33 File::~File() { | 35 File::~File() { |
| 34 if (os_file_) | 36 if (platform_file_) |
| 35 close(os_file_); | 37 close(platform_file_); |
| 36 } | 38 } |
| 37 | 39 |
| 38 OSFile File::os_file() const { | 40 base::PlatformFile File::platform_file() const { |
| 39 return os_file_; | 41 return platform_file_; |
| 40 } | 42 } |
| 41 | 43 |
| 42 bool File::IsValid() const { | 44 bool File::IsValid() const { |
| 43 if (!init_) | 45 if (!init_) |
| 44 return false; | 46 return false; |
| 45 return (INVALID_HANDLE_VALUE != os_file_); | 47 return (base::kInvalidPlatformFileValue != platform_file_); |
| 46 } | 48 } |
| 47 | 49 |
| 48 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { | 50 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { |
| 49 DCHECK(init_); | 51 DCHECK(init_); |
| 50 if (buffer_len > ULONG_MAX || offset > LONG_MAX) | 52 if (buffer_len > ULONG_MAX || offset > LONG_MAX) |
| 51 return false; | 53 return false; |
| 52 | 54 |
| 53 int ret = pread(os_file_, buffer, buffer_len, offset); | 55 int ret = pread(platform_file_, buffer, buffer_len, offset); |
| 54 return (static_cast<size_t>(ret) == buffer_len); | 56 return (static_cast<size_t>(ret) == buffer_len); |
| 55 } | 57 } |
| 56 | 58 |
| 57 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { | 59 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { |
| 58 DCHECK(init_); | 60 DCHECK(init_); |
| 59 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) | 61 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) |
| 60 return false; | 62 return false; |
| 61 | 63 |
| 62 int ret = pwrite(os_file_, buffer, buffer_len, offset); | 64 int ret = pwrite(platform_file_, buffer, buffer_len, offset); |
| 63 return (static_cast<size_t>(ret) == buffer_len); | 65 return (static_cast<size_t>(ret) == buffer_len); |
| 64 } | 66 } |
| 65 | 67 |
| 66 // We have to increase the ref counter of the file before performing the IO to | 68 // We have to increase the ref counter of the file before performing the IO to |
| 67 // prevent the completion to happen with an invalid handle (if the file is | 69 // prevent the completion to happen with an invalid handle (if the file is |
| 68 // closed while the IO is in flight). | 70 // closed while the IO is in flight). |
| 69 bool File::Read(void* buffer, size_t buffer_len, size_t offset, | 71 bool File::Read(void* buffer, size_t buffer_len, size_t offset, |
| 70 FileIOCallback* callback, bool* completed) { | 72 FileIOCallback* callback, bool* completed) { |
| 71 DCHECK(init_); | 73 DCHECK(init_); |
| 72 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) | 74 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 delete[] reinterpret_cast<const char*>(buffer); | 109 delete[] reinterpret_cast<const char*>(buffer); |
| 108 | 110 |
| 109 return ret; | 111 return ret; |
| 110 } | 112 } |
| 111 | 113 |
| 112 bool File::SetLength(size_t length) { | 114 bool File::SetLength(size_t length) { |
| 113 DCHECK(init_); | 115 DCHECK(init_); |
| 114 if (length > ULONG_MAX) | 116 if (length > ULONG_MAX) |
| 115 return false; | 117 return false; |
| 116 | 118 |
| 117 return 0 == ftruncate(os_file_, length); | 119 return 0 == ftruncate(platform_file_, length); |
| 118 } | 120 } |
| 119 | 121 |
| 120 size_t File::GetLength() { | 122 size_t File::GetLength() { |
| 121 DCHECK(init_); | 123 DCHECK(init_); |
| 122 size_t ret = lseek(os_file_, 0, SEEK_END); | 124 size_t ret = lseek(platform_file_, 0, SEEK_END); |
| 123 return ret; | 125 return ret; |
| 124 } | 126 } |
| 125 | 127 |
| 126 } // namespace disk_cache | 128 } // namespace disk_cache |
| OLD | NEW |