[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/platform_file.h" |
| 6 | |
[email protected] | ca8e74e | 2013-02-25 23:33:34 | [diff] [blame] | 7 | #include <io.h> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | baeb657b | 2013-04-11 02:43:59 | [diff] [blame] | 11 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 12 | #include "base/threading/thread_restrictions.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 13 | |
| 14 | namespace base { |
[email protected] | 9fea5a9 | 2013-01-09 00:38:59 | [diff] [blame] | 15 | PlatformFile CreatePlatformFileUnsafe(const FilePath& name, |
| 16 | int flags, |
| 17 | bool* created, |
| 18 | PlatformFileError* error) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 19 | base::ThreadRestrictions::AssertIOAllowed(); |
| 20 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 21 | DWORD disposition = 0; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 22 | if (created) |
| 23 | *created = false; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 24 | |
| 25 | if (flags & PLATFORM_FILE_OPEN) |
| 26 | disposition = OPEN_EXISTING; |
| 27 | |
| 28 | if (flags & PLATFORM_FILE_CREATE) { |
| 29 | DCHECK(!disposition); |
| 30 | disposition = CREATE_NEW; |
| 31 | } |
| 32 | |
| 33 | if (flags & PLATFORM_FILE_OPEN_ALWAYS) { |
| 34 | DCHECK(!disposition); |
| 35 | disposition = OPEN_ALWAYS; |
| 36 | } |
| 37 | |
| 38 | if (flags & PLATFORM_FILE_CREATE_ALWAYS) { |
| 39 | DCHECK(!disposition); |
| 40 | disposition = CREATE_ALWAYS; |
| 41 | } |
| 42 | |
[email protected] | b2f2308d | 2011-05-23 22:00:04 | [diff] [blame] | 43 | if (flags & PLATFORM_FILE_OPEN_TRUNCATED) { |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 44 | DCHECK(!disposition); |
| 45 | DCHECK(flags & PLATFORM_FILE_WRITE); |
| 46 | disposition = TRUNCATE_EXISTING; |
| 47 | } |
| 48 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 49 | if (!disposition) { |
| 50 | NOTREACHED(); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 54 | DWORD access = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 55 | if (flags & PLATFORM_FILE_WRITE) |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 56 | access = GENERIC_WRITE; |
| 57 | if (flags & PLATFORM_FILE_APPEND) { |
| 58 | DCHECK(!access); |
| 59 | access = FILE_APPEND_DATA; |
| 60 | } |
| 61 | if (flags & PLATFORM_FILE_READ) |
| 62 | access |= GENERIC_READ; |
[email protected] | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame] | 63 | if (flags & PLATFORM_FILE_WRITE_ATTRIBUTES) |
| 64 | access |= FILE_WRITE_ATTRIBUTES; |
[email protected] | 33b9ffd0 | 2013-04-04 14:12:27 | [diff] [blame] | 65 | if (flags & PLATFORM_FILE_EXECUTE) |
| 66 | access |= GENERIC_EXECUTE; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 67 | |
| 68 | DWORD sharing = (flags & PLATFORM_FILE_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; |
| 69 | if (!(flags & PLATFORM_FILE_EXCLUSIVE_WRITE)) |
| 70 | sharing |= FILE_SHARE_WRITE; |
[email protected] | ad0f81dd | 2011-06-22 19:29:23 | [diff] [blame] | 71 | if (flags & PLATFORM_FILE_SHARE_DELETE) |
| 72 | sharing |= FILE_SHARE_DELETE; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 73 | |
| 74 | DWORD create_flags = 0; |
| 75 | if (flags & PLATFORM_FILE_ASYNC) |
| 76 | create_flags |= FILE_FLAG_OVERLAPPED; |
[email protected] | 017022b | 2009-07-27 23:06:34 | [diff] [blame] | 77 | if (flags & PLATFORM_FILE_TEMPORARY) |
| 78 | create_flags |= FILE_ATTRIBUTE_TEMPORARY; |
| 79 | if (flags & PLATFORM_FILE_HIDDEN) |
| 80 | create_flags |= FILE_ATTRIBUTE_HIDDEN; |
| 81 | if (flags & PLATFORM_FILE_DELETE_ON_CLOSE) |
| 82 | create_flags |= FILE_FLAG_DELETE_ON_CLOSE; |
[email protected] | 307a825a | 2012-11-01 11:48:52 | [diff] [blame] | 83 | if (flags & PLATFORM_FILE_BACKUP_SEMANTICS) |
| 84 | create_flags |= FILE_FLAG_BACKUP_SEMANTICS; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 85 | |
[email protected] | f294da7 | 2009-10-12 21:39:37 | [diff] [blame] | 86 | HANDLE file = CreateFile(name.value().c_str(), access, sharing, NULL, |
| 87 | disposition, create_flags, NULL); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 88 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 89 | if (created && (INVALID_HANDLE_VALUE != file)) { |
[email protected] | fd55c28 | 2010-10-15 00:37:34 | [diff] [blame] | 90 | if (flags & (PLATFORM_FILE_OPEN_ALWAYS)) |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 91 | *created = (ERROR_ALREADY_EXISTS != GetLastError()); |
[email protected] | fd55c28 | 2010-10-15 00:37:34 | [diff] [blame] | 92 | else if (flags & (PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_CREATE)) |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 93 | *created = true; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 9fea5a9 | 2013-01-09 00:38:59 | [diff] [blame] | 96 | if (error) { |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 97 | if (file != kInvalidPlatformFileValue) |
[email protected] | 9fea5a9 | 2013-01-09 00:38:59 | [diff] [blame] | 98 | *error = PLATFORM_FILE_OK; |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 99 | else |
| 100 | *error = LastErrorToPlatformFileError(GetLastError()); |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 103 | return file; |
| 104 | } |
| 105 | |
[email protected] | ca8e74e | 2013-02-25 23:33:34 | [diff] [blame] | 106 | FILE* FdopenPlatformFile(PlatformFile file, const char* mode) { |
| 107 | if (file == kInvalidPlatformFileValue) |
| 108 | return NULL; |
| 109 | int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0); |
| 110 | if (fd < 0) |
| 111 | return NULL; |
| 112 | return _fdopen(fd, mode); |
| 113 | } |
| 114 | |
[email protected] | ee8d4c8 | 2009-08-28 21:58:28 | [diff] [blame] | 115 | bool ClosePlatformFile(PlatformFile file) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 116 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 117 | return (CloseHandle(file) != 0); |
| 118 | } |
| 119 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 120 | int64 SeekPlatformFile(PlatformFile file, |
| 121 | PlatformFileWhence whence, |
| 122 | int64 offset) { |
| 123 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 4b74a4b | 2013-08-12 22:34:06 | [diff] [blame] | 124 | if (file == kInvalidPlatformFileValue || offset < 0) |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 125 | return -1; |
| 126 | |
| 127 | LARGE_INTEGER distance, res; |
| 128 | distance.QuadPart = offset; |
| 129 | DWORD move_method = static_cast<DWORD>(whence); |
| 130 | if (!SetFilePointerEx(file, distance, &res, move_method)) |
| 131 | return -1; |
| 132 | return res.QuadPart; |
| 133 | } |
| 134 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 135 | int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 136 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 137 | if (file == kInvalidPlatformFileValue) |
| 138 | return -1; |
| 139 | |
| 140 | LARGE_INTEGER offset_li; |
| 141 | offset_li.QuadPart = offset; |
| 142 | |
| 143 | OVERLAPPED overlapped = {0}; |
| 144 | overlapped.Offset = offset_li.LowPart; |
| 145 | overlapped.OffsetHigh = offset_li.HighPart; |
| 146 | |
| 147 | DWORD bytes_read; |
| 148 | if (::ReadFile(file, data, size, &bytes_read, &overlapped) != 0) |
| 149 | return bytes_read; |
| 150 | else if (ERROR_HANDLE_EOF == GetLastError()) |
| 151 | return 0; |
| 152 | |
| 153 | return -1; |
| 154 | } |
| 155 | |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 156 | int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) { |
| 157 | return ReadPlatformFile(file, 0, data, size); |
| 158 | } |
| 159 | |
| 160 | int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, char* data, |
| 161 | int size) { |
[email protected] | a37b56a | 2011-09-01 23:04:07 | [diff] [blame] | 162 | return ReadPlatformFile(file, offset, data, size); |
| 163 | } |
| 164 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 165 | int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, |
| 166 | char* data, int size) { |
| 167 | return ReadPlatformFile(file, 0, data, size); |
| 168 | } |
| 169 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 170 | int WritePlatformFile(PlatformFile file, int64 offset, |
| 171 | const char* data, int size) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 172 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 173 | if (file == kInvalidPlatformFileValue) |
| 174 | return -1; |
| 175 | |
| 176 | LARGE_INTEGER offset_li; |
| 177 | offset_li.QuadPart = offset; |
| 178 | |
| 179 | OVERLAPPED overlapped = {0}; |
| 180 | overlapped.Offset = offset_li.LowPart; |
| 181 | overlapped.OffsetHigh = offset_li.HighPart; |
| 182 | |
| 183 | DWORD bytes_written; |
| 184 | if (::WriteFile(file, data, size, &bytes_written, &overlapped) != 0) |
| 185 | return bytes_written; |
| 186 | |
| 187 | return -1; |
| 188 | } |
| 189 | |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 190 | int WritePlatformFileAtCurrentPos(PlatformFile file, const char* data, |
| 191 | int size) { |
| 192 | return WritePlatformFile(file, 0, data, size); |
| 193 | } |
| 194 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 195 | int WritePlatformFileCurPosNoBestEffort(PlatformFile file, |
| 196 | const char* data, int size) { |
| 197 | return WritePlatformFile(file, 0, data, size); |
| 198 | } |
| 199 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 200 | bool TruncatePlatformFile(PlatformFile file, int64 length) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 201 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 202 | if (file == kInvalidPlatformFileValue) |
| 203 | return false; |
| 204 | |
| 205 | // Get the current file pointer. |
| 206 | LARGE_INTEGER file_pointer; |
| 207 | LARGE_INTEGER zero; |
| 208 | zero.QuadPart = 0; |
| 209 | if (::SetFilePointerEx(file, zero, &file_pointer, FILE_CURRENT) == 0) |
| 210 | return false; |
| 211 | |
| 212 | LARGE_INTEGER length_li; |
| 213 | length_li.QuadPart = length; |
| 214 | // If length > file size, SetFilePointerEx() should extend the file |
| 215 | // with zeroes on all Windows standard file systems (NTFS, FATxx). |
| 216 | if (!::SetFilePointerEx(file, length_li, NULL, FILE_BEGIN)) |
| 217 | return false; |
| 218 | |
| 219 | // Set the new file length and move the file pointer to its old position. |
| 220 | // This is consistent with ftruncate()'s behavior, even when the file |
| 221 | // pointer points to a location beyond the end of the file. |
| 222 | return ((::SetEndOfFile(file) != 0) && |
| 223 | (::SetFilePointerEx(file, file_pointer, NULL, FILE_BEGIN) != 0)); |
| 224 | } |
| 225 | |
| 226 | bool FlushPlatformFile(PlatformFile file) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 227 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 228 | return ((file != kInvalidPlatformFileValue) && ::FlushFileBuffers(file)); |
| 229 | } |
| 230 | |
| 231 | bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time, |
| 232 | const base::Time& last_modified_time) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 233 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 234 | if (file == kInvalidPlatformFileValue) |
| 235 | return false; |
| 236 | |
| 237 | FILETIME last_access_filetime = last_access_time.ToFileTime(); |
| 238 | FILETIME last_modified_filetime = last_modified_time.ToFileTime(); |
| 239 | return (::SetFileTime(file, NULL, &last_access_filetime, |
| 240 | &last_modified_filetime) != 0); |
| 241 | } |
| 242 | |
| 243 | bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 244 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 245 | if (!info) |
| 246 | return false; |
| 247 | |
| 248 | BY_HANDLE_FILE_INFORMATION file_info; |
| 249 | if (GetFileInformationByHandle(file, &file_info) == 0) |
| 250 | return false; |
| 251 | |
| 252 | LARGE_INTEGER size; |
| 253 | size.HighPart = file_info.nFileSizeHigh; |
| 254 | size.LowPart = file_info.nFileSizeLow; |
| 255 | info->size = size.QuadPart; |
| 256 | info->is_directory = |
[email protected] | 23b59d2 | 2011-12-29 22:59:22 | [diff] [blame] | 257 | (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 258 | info->is_symbolic_link = false; // Windows doesn't have symbolic links. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 259 | info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); |
| 260 | info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime); |
| 261 | info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime); |
| 262 | return true; |
[email protected] | ee8d4c8 | 2009-08-28 21:58:28 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | 3b95e1d0 | 2013-10-10 23:02:39 | [diff] [blame] | 265 | PlatformFileError LockPlatformFile(PlatformFile file) { |
| 266 | BOOL result = LockFile(file, 0, 0, MAXDWORD, MAXDWORD); |
| 267 | if (!result) |
| 268 | return LastErrorToPlatformFileError(GetLastError()); |
| 269 | return PLATFORM_FILE_OK; |
| 270 | } |
| 271 | |
| 272 | PlatformFileError UnlockPlatformFile(PlatformFile file) { |
| 273 | BOOL result = UnlockFile(file, 0, 0, MAXDWORD, MAXDWORD); |
| 274 | if (!result) |
| 275 | return LastErrorToPlatformFileError(GetLastError()); |
| 276 | return PLATFORM_FILE_OK; |
| 277 | } |
| 278 | |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 279 | PlatformFileError LastErrorToPlatformFileError(DWORD last_error) { |
| 280 | switch (last_error) { |
| 281 | case ERROR_SHARING_VIOLATION: |
| 282 | return PLATFORM_FILE_ERROR_IN_USE; |
| 283 | case ERROR_FILE_EXISTS: |
| 284 | return PLATFORM_FILE_ERROR_EXISTS; |
| 285 | case ERROR_FILE_NOT_FOUND: |
| 286 | case ERROR_PATH_NOT_FOUND: |
| 287 | return PLATFORM_FILE_ERROR_NOT_FOUND; |
| 288 | case ERROR_ACCESS_DENIED: |
| 289 | return PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 290 | case ERROR_TOO_MANY_OPEN_FILES: |
| 291 | return PLATFORM_FILE_ERROR_TOO_MANY_OPENED; |
| 292 | case ERROR_OUTOFMEMORY: |
| 293 | case ERROR_NOT_ENOUGH_MEMORY: |
| 294 | return PLATFORM_FILE_ERROR_NO_MEMORY; |
| 295 | case ERROR_HANDLE_DISK_FULL: |
| 296 | case ERROR_DISK_FULL: |
| 297 | case ERROR_DISK_RESOURCES_EXHAUSTED: |
| 298 | return PLATFORM_FILE_ERROR_NO_SPACE; |
| 299 | case ERROR_USER_MAPPED_FILE: |
| 300 | return PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 301 | case ERROR_NOT_READY: |
| 302 | case ERROR_SECTOR_NOT_FOUND: |
| 303 | case ERROR_DEV_NOT_EXIST: |
| 304 | case ERROR_IO_DEVICE: |
| 305 | case ERROR_FILE_CORRUPT: |
| 306 | case ERROR_DISK_CORRUPT: |
| 307 | return PLATFORM_FILE_ERROR_IO; |
| 308 | default: |
[email protected] | b71b8d04 | 2013-05-15 09:49:52 | [diff] [blame] | 309 | UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 310 | last_error); |
| 311 | return PLATFORM_FILE_ERROR_FAILED; |
| 312 | } |
| 313 | } |
| 314 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 315 | } // namespace base |