| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file.h" | 5 #include "base/files/file.h" |
| 6 | 6 |
| 7 #include <io.h> | 7 #include <io.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 // Make sure our Whence mappings match the system headers. | 16 // Make sure our Whence mappings match the system headers. |
| 17 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && | 17 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && |
| 18 File::FROM_CURRENT == FILE_CURRENT && | 18 File::FROM_CURRENT == FILE_CURRENT && |
| 19 File::FROM_END == FILE_END, whence_matches_system); | 19 File::FROM_END == FILE_END, whence_matches_system); |
| 20 | 20 |
| 21 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { | 21 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
| 22 base::ThreadRestrictions::AssertIOAllowed(); | 22 ThreadRestrictions::AssertIOAllowed(); |
| 23 DCHECK(!IsValid()); | 23 DCHECK(!IsValid()); |
| 24 | 24 |
| 25 DWORD disposition = 0; | 25 DWORD disposition = 0; |
| 26 | 26 |
| 27 if (flags & FLAG_OPEN) | 27 if (flags & FLAG_OPEN) |
| 28 disposition = OPEN_EXISTING; | 28 disposition = OPEN_EXISTING; |
| 29 | 29 |
| 30 if (flags & FLAG_CREATE) { | 30 if (flags & FLAG_CREATE) { |
| 31 DCHECK(!disposition); | 31 DCHECK(!disposition); |
| 32 disposition = CREATE_NEW; | 32 disposition = CREATE_NEW; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 PlatformFile File::GetPlatformFile() const { | 109 PlatformFile File::GetPlatformFile() const { |
| 110 return file_.Get(); | 110 return file_.Get(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 PlatformFile File::TakePlatformFile() { | 113 PlatformFile File::TakePlatformFile() { |
| 114 return file_.Take(); | 114 return file_.Take(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void File::Close() { | 117 void File::Close() { |
| 118 if (file_.IsValid()) { | 118 if (file_.IsValid()) { |
| 119 base::ThreadRestrictions::AssertIOAllowed(); | 119 ThreadRestrictions::AssertIOAllowed(); |
| 120 file_.Close(); | 120 file_.Close(); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 int64 File::Seek(Whence whence, int64 offset) { | 124 int64 File::Seek(Whence whence, int64 offset) { |
| 125 base::ThreadRestrictions::AssertIOAllowed(); | 125 ThreadRestrictions::AssertIOAllowed(); |
| 126 DCHECK(IsValid()); | 126 DCHECK(IsValid()); |
| 127 | 127 |
| 128 LARGE_INTEGER distance, res; | 128 LARGE_INTEGER distance, res; |
| 129 distance.QuadPart = offset; | 129 distance.QuadPart = offset; |
| 130 DWORD move_method = static_cast<DWORD>(whence); | 130 DWORD move_method = static_cast<DWORD>(whence); |
| 131 if (!SetFilePointerEx(file_.Get(), distance, &res, move_method)) | 131 if (!SetFilePointerEx(file_.Get(), distance, &res, move_method)) |
| 132 return -1; | 132 return -1; |
| 133 return res.QuadPart; | 133 return res.QuadPart; |
| 134 } | 134 } |
| 135 | 135 |
| 136 int File::Read(int64 offset, char* data, int size) { | 136 int File::Read(int64 offset, char* data, int size) { |
| 137 base::ThreadRestrictions::AssertIOAllowed(); | 137 ThreadRestrictions::AssertIOAllowed(); |
| 138 DCHECK(IsValid()); | 138 DCHECK(IsValid()); |
| 139 DCHECK(!async_); | 139 DCHECK(!async_); |
| 140 if (size < 0) | 140 if (size < 0) |
| 141 return -1; | 141 return -1; |
| 142 | 142 |
| 143 LARGE_INTEGER offset_li; | 143 LARGE_INTEGER offset_li; |
| 144 offset_li.QuadPart = offset; | 144 offset_li.QuadPart = offset; |
| 145 | 145 |
| 146 OVERLAPPED overlapped = {0}; | 146 OVERLAPPED overlapped = {0}; |
| 147 overlapped.Offset = offset_li.LowPart; | 147 overlapped.Offset = offset_li.LowPart; |
| 148 overlapped.OffsetHigh = offset_li.HighPart; | 148 overlapped.OffsetHigh = offset_li.HighPart; |
| 149 | 149 |
| 150 DWORD bytes_read; | 150 DWORD bytes_read; |
| 151 if (::ReadFile(file_.Get(), data, size, &bytes_read, &overlapped)) | 151 if (::ReadFile(file_.Get(), data, size, &bytes_read, &overlapped)) |
| 152 return bytes_read; | 152 return bytes_read; |
| 153 if (ERROR_HANDLE_EOF == GetLastError()) | 153 if (ERROR_HANDLE_EOF == GetLastError()) |
| 154 return 0; | 154 return 0; |
| 155 | 155 |
| 156 return -1; | 156 return -1; |
| 157 } | 157 } |
| 158 | 158 |
| 159 int File::ReadAtCurrentPos(char* data, int size) { | 159 int File::ReadAtCurrentPos(char* data, int size) { |
| 160 base::ThreadRestrictions::AssertIOAllowed(); | 160 ThreadRestrictions::AssertIOAllowed(); |
| 161 DCHECK(IsValid()); | 161 DCHECK(IsValid()); |
| 162 DCHECK(!async_); | 162 DCHECK(!async_); |
| 163 if (size < 0) | 163 if (size < 0) |
| 164 return -1; | 164 return -1; |
| 165 | 165 |
| 166 DWORD bytes_read; | 166 DWORD bytes_read; |
| 167 if (::ReadFile(file_.Get(), data, size, &bytes_read, NULL)) | 167 if (::ReadFile(file_.Get(), data, size, &bytes_read, NULL)) |
| 168 return bytes_read; | 168 return bytes_read; |
| 169 if (ERROR_HANDLE_EOF == GetLastError()) | 169 if (ERROR_HANDLE_EOF == GetLastError()) |
| 170 return 0; | 170 return 0; |
| 171 | 171 |
| 172 return -1; | 172 return -1; |
| 173 } | 173 } |
| 174 | 174 |
| 175 int File::ReadNoBestEffort(int64 offset, char* data, int size) { | 175 int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
| 176 return Read(offset, data, size); | 176 return Read(offset, data, size); |
| 177 } | 177 } |
| 178 | 178 |
| 179 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { | 179 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
| 180 return ReadAtCurrentPos(data, size); | 180 return ReadAtCurrentPos(data, size); |
| 181 } | 181 } |
| 182 | 182 |
| 183 int File::Write(int64 offset, const char* data, int size) { | 183 int File::Write(int64 offset, const char* data, int size) { |
| 184 base::ThreadRestrictions::AssertIOAllowed(); | 184 ThreadRestrictions::AssertIOAllowed(); |
| 185 DCHECK(IsValid()); | 185 DCHECK(IsValid()); |
| 186 DCHECK(!async_); | 186 DCHECK(!async_); |
| 187 | 187 |
| 188 LARGE_INTEGER offset_li; | 188 LARGE_INTEGER offset_li; |
| 189 offset_li.QuadPart = offset; | 189 offset_li.QuadPart = offset; |
| 190 | 190 |
| 191 OVERLAPPED overlapped = {0}; | 191 OVERLAPPED overlapped = {0}; |
| 192 overlapped.Offset = offset_li.LowPart; | 192 overlapped.Offset = offset_li.LowPart; |
| 193 overlapped.OffsetHigh = offset_li.HighPart; | 193 overlapped.OffsetHigh = offset_li.HighPart; |
| 194 | 194 |
| 195 DWORD bytes_written; | 195 DWORD bytes_written; |
| 196 if (::WriteFile(file_.Get(), data, size, &bytes_written, &overlapped)) | 196 if (::WriteFile(file_.Get(), data, size, &bytes_written, &overlapped)) |
| 197 return bytes_written; | 197 return bytes_written; |
| 198 | 198 |
| 199 return -1; | 199 return -1; |
| 200 } | 200 } |
| 201 | 201 |
| 202 int File::WriteAtCurrentPos(const char* data, int size) { | 202 int File::WriteAtCurrentPos(const char* data, int size) { |
| 203 base::ThreadRestrictions::AssertIOAllowed(); | 203 ThreadRestrictions::AssertIOAllowed(); |
| 204 DCHECK(IsValid()); | 204 DCHECK(IsValid()); |
| 205 DCHECK(!async_); | 205 DCHECK(!async_); |
| 206 if (size < 0) | 206 if (size < 0) |
| 207 return -1; | 207 return -1; |
| 208 | 208 |
| 209 DWORD bytes_written; | 209 DWORD bytes_written; |
| 210 if (::WriteFile(file_.Get(), data, size, &bytes_written, NULL)) | 210 if (::WriteFile(file_.Get(), data, size, &bytes_written, NULL)) |
| 211 return bytes_written; | 211 return bytes_written; |
| 212 | 212 |
| 213 return -1; | 213 return -1; |
| 214 } | 214 } |
| 215 | 215 |
| 216 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { | 216 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
| 217 return WriteAtCurrentPos(data, size); | 217 return WriteAtCurrentPos(data, size); |
| 218 } | 218 } |
| 219 | 219 |
| 220 int64 File::GetLength() { | 220 int64 File::GetLength() { |
| 221 base::ThreadRestrictions::AssertIOAllowed(); | 221 ThreadRestrictions::AssertIOAllowed(); |
| 222 DCHECK(IsValid()); | 222 DCHECK(IsValid()); |
| 223 LARGE_INTEGER size; | 223 LARGE_INTEGER size; |
| 224 if (!::GetFileSizeEx(file_.Get(), &size)) | 224 if (!::GetFileSizeEx(file_.Get(), &size)) |
| 225 return -1; | 225 return -1; |
| 226 | 226 |
| 227 return static_cast<int64>(size.QuadPart); | 227 return static_cast<int64>(size.QuadPart); |
| 228 } | 228 } |
| 229 | 229 |
| 230 bool File::SetLength(int64 length) { | 230 bool File::SetLength(int64 length) { |
| 231 base::ThreadRestrictions::AssertIOAllowed(); | 231 ThreadRestrictions::AssertIOAllowed(); |
| 232 DCHECK(IsValid()); | 232 DCHECK(IsValid()); |
| 233 | 233 |
| 234 // Get the current file pointer. | 234 // Get the current file pointer. |
| 235 LARGE_INTEGER file_pointer; | 235 LARGE_INTEGER file_pointer; |
| 236 LARGE_INTEGER zero; | 236 LARGE_INTEGER zero; |
| 237 zero.QuadPart = 0; | 237 zero.QuadPart = 0; |
| 238 if (!::SetFilePointerEx(file_.Get(), zero, &file_pointer, FILE_CURRENT)) | 238 if (!::SetFilePointerEx(file_.Get(), zero, &file_pointer, FILE_CURRENT)) |
| 239 return false; | 239 return false; |
| 240 | 240 |
| 241 LARGE_INTEGER length_li; | 241 LARGE_INTEGER length_li; |
| 242 length_li.QuadPart = length; | 242 length_li.QuadPart = length; |
| 243 // If length > file size, SetFilePointerEx() should extend the file | 243 // If length > file size, SetFilePointerEx() should extend the file |
| 244 // with zeroes on all Windows standard file systems (NTFS, FATxx). | 244 // with zeroes on all Windows standard file systems (NTFS, FATxx). |
| 245 if (!::SetFilePointerEx(file_.Get(), length_li, NULL, FILE_BEGIN)) | 245 if (!::SetFilePointerEx(file_.Get(), length_li, NULL, FILE_BEGIN)) |
| 246 return false; | 246 return false; |
| 247 | 247 |
| 248 // Set the new file length and move the file pointer to its old position. | 248 // Set the new file length and move the file pointer to its old position. |
| 249 // This is consistent with ftruncate()'s behavior, even when the file | 249 // This is consistent with ftruncate()'s behavior, even when the file |
| 250 // pointer points to a location beyond the end of the file. | 250 // pointer points to a location beyond the end of the file. |
| 251 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not | 251 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not |
| 252 // promised by the interface (nor was promised by PlatformFile). See if this | 252 // promised by the interface (nor was promised by PlatformFile). See if this |
| 253 // implementation detail can be removed. | 253 // implementation detail can be removed. |
| 254 return ((::SetEndOfFile(file_.Get()) != FALSE) && | 254 return ((::SetEndOfFile(file_.Get()) != FALSE) && |
| 255 (::SetFilePointerEx(file_.Get(), file_pointer, NULL, FILE_BEGIN) != | 255 (::SetFilePointerEx(file_.Get(), file_pointer, NULL, FILE_BEGIN) != |
| 256 FALSE)); | 256 FALSE)); |
| 257 } | 257 } |
| 258 | 258 |
| 259 bool File::SetTimes(Time last_access_time, Time last_modified_time) { | 259 bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
| 260 base::ThreadRestrictions::AssertIOAllowed(); | 260 ThreadRestrictions::AssertIOAllowed(); |
| 261 DCHECK(IsValid()); | 261 DCHECK(IsValid()); |
| 262 | 262 |
| 263 FILETIME last_access_filetime = last_access_time.ToFileTime(); | 263 FILETIME last_access_filetime = last_access_time.ToFileTime(); |
| 264 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); | 264 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); |
| 265 return (::SetFileTime(file_.Get(), NULL, &last_access_filetime, | 265 return (::SetFileTime(file_.Get(), NULL, &last_access_filetime, |
| 266 &last_modified_filetime) != FALSE); | 266 &last_modified_filetime) != FALSE); |
| 267 } | 267 } |
| 268 | 268 |
| 269 bool File::GetInfo(Info* info) { | 269 bool File::GetInfo(Info* info) { |
| 270 base::ThreadRestrictions::AssertIOAllowed(); | 270 ThreadRestrictions::AssertIOAllowed(); |
| 271 DCHECK(IsValid()); | 271 DCHECK(IsValid()); |
| 272 | 272 |
| 273 BY_HANDLE_FILE_INFORMATION file_info; | 273 BY_HANDLE_FILE_INFORMATION file_info; |
| 274 if (!GetFileInformationByHandle(file_.Get(), &file_info)) | 274 if (!GetFileInformationByHandle(file_.Get(), &file_info)) |
| 275 return false; | 275 return false; |
| 276 | 276 |
| 277 LARGE_INTEGER size; | 277 LARGE_INTEGER size; |
| 278 size.HighPart = file_info.nFileSizeHigh; | 278 size.HighPart = file_info.nFileSizeHigh; |
| 279 size.LowPart = file_info.nFileSizeLow; | 279 size.LowPart = file_info.nFileSizeLow; |
| 280 info->size = size.QuadPart; | 280 info->size = size.QuadPart; |
| 281 info->is_directory = | 281 info->is_directory = |
| 282 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 282 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 283 info->is_symbolic_link = false; // Windows doesn't have symbolic links. | 283 info->is_symbolic_link = false; // Windows doesn't have symbolic links. |
| 284 info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); | 284 info->last_modified = Time::FromFileTime(file_info.ftLastWriteTime); |
| 285 info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime); | 285 info->last_accessed = Time::FromFileTime(file_info.ftLastAccessTime); |
| 286 info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime); | 286 info->creation_time = Time::FromFileTime(file_info.ftCreationTime); |
| 287 return true; | 287 return true; |
| 288 } | 288 } |
| 289 | 289 |
| 290 File::Error base::File::Lock() { | 290 File::Error File::Lock() { |
| 291 DCHECK(IsValid()); | 291 DCHECK(IsValid()); |
| 292 BOOL result = LockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); | 292 BOOL result = LockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); |
| 293 if (!result) | 293 if (!result) |
| 294 return OSErrorToFileError(GetLastError()); | 294 return OSErrorToFileError(GetLastError()); |
| 295 return FILE_OK; | 295 return FILE_OK; |
| 296 } | 296 } |
| 297 | 297 |
| 298 File::Error File::Unlock() { | 298 File::Error File::Unlock() { |
| 299 DCHECK(IsValid()); | 299 DCHECK(IsValid()); |
| 300 BOOL result = UnlockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); | 300 BOOL result = UnlockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 case ERROR_DISK_CORRUPT: | 356 case ERROR_DISK_CORRUPT: |
| 357 return FILE_ERROR_IO; | 357 return FILE_ERROR_IO; |
| 358 default: | 358 default: |
| 359 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", | 359 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", |
| 360 last_error); | 360 last_error); |
| 361 return FILE_ERROR_FAILED; | 361 return FILE_ERROR_FAILED; |
| 362 } | 362 } |
| 363 } | 363 } |
| 364 | 364 |
| 365 bool File::DoFlush() { | 365 bool File::DoFlush() { |
| 366 base::ThreadRestrictions::AssertIOAllowed(); | 366 ThreadRestrictions::AssertIOAllowed(); |
| 367 DCHECK(IsValid()); | 367 DCHECK(IsValid()); |
| 368 return ::FlushFileBuffers(file_.Get()) != FALSE; | 368 return ::FlushFileBuffers(file_.Get()) != FALSE; |
| 369 } | 369 } |
| 370 | 370 |
| 371 void File::SetPlatformFile(PlatformFile file) { | 371 void File::SetPlatformFile(PlatformFile file) { |
| 372 file_.Set(file); | 372 file_.Set(file); |
| 373 } | 373 } |
| 374 | 374 |
| 375 } // namespace base | 375 } // namespace base |
| OLD | NEW |