| 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 "base/platform_file.h" | 5 #include "base/platform_file.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/thread_restrictions.h" |
| 9 | 10 |
| 10 namespace base { | 11 namespace base { |
| 11 | 12 |
| 12 PlatformFile CreatePlatformFile(const FilePath& name, | 13 PlatformFile CreatePlatformFile(const FilePath& name, |
| 13 int flags, | 14 int flags, |
| 14 bool* created, | 15 bool* created, |
| 15 PlatformFileError* error_code) { | 16 PlatformFileError* error_code) { |
| 17 base::ThreadRestrictions::AssertIOAllowed(); |
| 18 |
| 16 DWORD disposition = 0; | 19 DWORD disposition = 0; |
| 17 | 20 |
| 18 if (flags & PLATFORM_FILE_OPEN) | 21 if (flags & PLATFORM_FILE_OPEN) |
| 19 disposition = OPEN_EXISTING; | 22 disposition = OPEN_EXISTING; |
| 20 | 23 |
| 21 if (flags & PLATFORM_FILE_CREATE) { | 24 if (flags & PLATFORM_FILE_CREATE) { |
| 22 DCHECK(!disposition); | 25 DCHECK(!disposition); |
| 23 disposition = CREATE_NEW; | 26 disposition = CREATE_NEW; |
| 24 } | 27 } |
| 25 | 28 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return file; | 104 return file; |
| 102 } | 105 } |
| 103 | 106 |
| 104 PlatformFile CreatePlatformFile(const std::wstring& name, int flags, | 107 PlatformFile CreatePlatformFile(const std::wstring& name, int flags, |
| 105 bool* created) { | 108 bool* created) { |
| 106 return CreatePlatformFile(FilePath::FromWStringHack(name), flags, | 109 return CreatePlatformFile(FilePath::FromWStringHack(name), flags, |
| 107 created, NULL); | 110 created, NULL); |
| 108 } | 111 } |
| 109 | 112 |
| 110 bool ClosePlatformFile(PlatformFile file) { | 113 bool ClosePlatformFile(PlatformFile file) { |
| 114 base::ThreadRestrictions::AssertIOAllowed(); |
| 111 return (CloseHandle(file) != 0); | 115 return (CloseHandle(file) != 0); |
| 112 } | 116 } |
| 113 | 117 |
| 114 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { | 118 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { |
| 119 base::ThreadRestrictions::AssertIOAllowed(); |
| 115 if (file == kInvalidPlatformFileValue) | 120 if (file == kInvalidPlatformFileValue) |
| 116 return -1; | 121 return -1; |
| 117 | 122 |
| 118 LARGE_INTEGER offset_li; | 123 LARGE_INTEGER offset_li; |
| 119 offset_li.QuadPart = offset; | 124 offset_li.QuadPart = offset; |
| 120 | 125 |
| 121 OVERLAPPED overlapped = {0}; | 126 OVERLAPPED overlapped = {0}; |
| 122 overlapped.Offset = offset_li.LowPart; | 127 overlapped.Offset = offset_li.LowPart; |
| 123 overlapped.OffsetHigh = offset_li.HighPart; | 128 overlapped.OffsetHigh = offset_li.HighPart; |
| 124 | 129 |
| 125 DWORD bytes_read; | 130 DWORD bytes_read; |
| 126 if (::ReadFile(file, data, size, &bytes_read, &overlapped) != 0) | 131 if (::ReadFile(file, data, size, &bytes_read, &overlapped) != 0) |
| 127 return bytes_read; | 132 return bytes_read; |
| 128 else if (ERROR_HANDLE_EOF == GetLastError()) | 133 else if (ERROR_HANDLE_EOF == GetLastError()) |
| 129 return 0; | 134 return 0; |
| 130 | 135 |
| 131 return -1; | 136 return -1; |
| 132 } | 137 } |
| 133 | 138 |
| 134 int WritePlatformFile(PlatformFile file, int64 offset, | 139 int WritePlatformFile(PlatformFile file, int64 offset, |
| 135 const char* data, int size) { | 140 const char* data, int size) { |
| 141 base::ThreadRestrictions::AssertIOAllowed(); |
| 136 if (file == kInvalidPlatformFileValue) | 142 if (file == kInvalidPlatformFileValue) |
| 137 return -1; | 143 return -1; |
| 138 | 144 |
| 139 LARGE_INTEGER offset_li; | 145 LARGE_INTEGER offset_li; |
| 140 offset_li.QuadPart = offset; | 146 offset_li.QuadPart = offset; |
| 141 | 147 |
| 142 OVERLAPPED overlapped = {0}; | 148 OVERLAPPED overlapped = {0}; |
| 143 overlapped.Offset = offset_li.LowPart; | 149 overlapped.Offset = offset_li.LowPart; |
| 144 overlapped.OffsetHigh = offset_li.HighPart; | 150 overlapped.OffsetHigh = offset_li.HighPart; |
| 145 | 151 |
| 146 DWORD bytes_written; | 152 DWORD bytes_written; |
| 147 if (::WriteFile(file, data, size, &bytes_written, &overlapped) != 0) | 153 if (::WriteFile(file, data, size, &bytes_written, &overlapped) != 0) |
| 148 return bytes_written; | 154 return bytes_written; |
| 149 | 155 |
| 150 return -1; | 156 return -1; |
| 151 } | 157 } |
| 152 | 158 |
| 153 bool TruncatePlatformFile(PlatformFile file, int64 length) { | 159 bool TruncatePlatformFile(PlatformFile file, int64 length) { |
| 160 base::ThreadRestrictions::AssertIOAllowed(); |
| 154 if (file == kInvalidPlatformFileValue) | 161 if (file == kInvalidPlatformFileValue) |
| 155 return false; | 162 return false; |
| 156 | 163 |
| 157 // Get the current file pointer. | 164 // Get the current file pointer. |
| 158 LARGE_INTEGER file_pointer; | 165 LARGE_INTEGER file_pointer; |
| 159 LARGE_INTEGER zero; | 166 LARGE_INTEGER zero; |
| 160 zero.QuadPart = 0; | 167 zero.QuadPart = 0; |
| 161 if (::SetFilePointerEx(file, zero, &file_pointer, FILE_CURRENT) == 0) | 168 if (::SetFilePointerEx(file, zero, &file_pointer, FILE_CURRENT) == 0) |
| 162 return false; | 169 return false; |
| 163 | 170 |
| 164 LARGE_INTEGER length_li; | 171 LARGE_INTEGER length_li; |
| 165 length_li.QuadPart = length; | 172 length_li.QuadPart = length; |
| 166 // If length > file size, SetFilePointerEx() should extend the file | 173 // If length > file size, SetFilePointerEx() should extend the file |
| 167 // with zeroes on all Windows standard file systems (NTFS, FATxx). | 174 // with zeroes on all Windows standard file systems (NTFS, FATxx). |
| 168 if (!::SetFilePointerEx(file, length_li, NULL, FILE_BEGIN)) | 175 if (!::SetFilePointerEx(file, length_li, NULL, FILE_BEGIN)) |
| 169 return false; | 176 return false; |
| 170 | 177 |
| 171 // Set the new file length and move the file pointer to its old position. | 178 // Set the new file length and move the file pointer to its old position. |
| 172 // This is consistent with ftruncate()'s behavior, even when the file | 179 // This is consistent with ftruncate()'s behavior, even when the file |
| 173 // pointer points to a location beyond the end of the file. | 180 // pointer points to a location beyond the end of the file. |
| 174 return ((::SetEndOfFile(file) != 0) && | 181 return ((::SetEndOfFile(file) != 0) && |
| 175 (::SetFilePointerEx(file, file_pointer, NULL, FILE_BEGIN) != 0)); | 182 (::SetFilePointerEx(file, file_pointer, NULL, FILE_BEGIN) != 0)); |
| 176 } | 183 } |
| 177 | 184 |
| 178 bool FlushPlatformFile(PlatformFile file) { | 185 bool FlushPlatformFile(PlatformFile file) { |
| 186 base::ThreadRestrictions::AssertIOAllowed(); |
| 179 return ((file != kInvalidPlatformFileValue) && ::FlushFileBuffers(file)); | 187 return ((file != kInvalidPlatformFileValue) && ::FlushFileBuffers(file)); |
| 180 } | 188 } |
| 181 | 189 |
| 182 bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time, | 190 bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time, |
| 183 const base::Time& last_modified_time) { | 191 const base::Time& last_modified_time) { |
| 192 base::ThreadRestrictions::AssertIOAllowed(); |
| 184 if (file == kInvalidPlatformFileValue) | 193 if (file == kInvalidPlatformFileValue) |
| 185 return false; | 194 return false; |
| 186 | 195 |
| 187 FILETIME last_access_filetime = last_access_time.ToFileTime(); | 196 FILETIME last_access_filetime = last_access_time.ToFileTime(); |
| 188 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); | 197 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); |
| 189 return (::SetFileTime(file, NULL, &last_access_filetime, | 198 return (::SetFileTime(file, NULL, &last_access_filetime, |
| 190 &last_modified_filetime) != 0); | 199 &last_modified_filetime) != 0); |
| 191 } | 200 } |
| 192 | 201 |
| 193 bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { | 202 bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { |
| 203 base::ThreadRestrictions::AssertIOAllowed(); |
| 194 if (!info) | 204 if (!info) |
| 195 return false; | 205 return false; |
| 196 | 206 |
| 197 BY_HANDLE_FILE_INFORMATION file_info; | 207 BY_HANDLE_FILE_INFORMATION file_info; |
| 198 if (GetFileInformationByHandle(file, &file_info) == 0) | 208 if (GetFileInformationByHandle(file, &file_info) == 0) |
| 199 return false; | 209 return false; |
| 200 | 210 |
| 201 LARGE_INTEGER size; | 211 LARGE_INTEGER size; |
| 202 size.HighPart = file_info.nFileSizeHigh; | 212 size.HighPart = file_info.nFileSizeHigh; |
| 203 size.LowPart = file_info.nFileSizeLow; | 213 size.LowPart = file_info.nFileSizeLow; |
| 204 info->size = size.QuadPart; | 214 info->size = size.QuadPart; |
| 205 info->is_directory = | 215 info->is_directory = |
| 206 file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0; | 216 file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0; |
| 207 info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); | 217 info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); |
| 208 info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime); | 218 info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime); |
| 209 info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime); | 219 info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime); |
| 210 return true; | 220 return true; |
| 211 } | 221 } |
| 212 | 222 |
| 213 } // namespace disk_cache | 223 } // namespace disk_cache |
| OLD | NEW |