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