1 // Copyright 2012 The Chromium Authors
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/files/file.h"
6
7 #include <windows.h>
8
9 #include <io.h>
10 #include <stdint.h>
11
12 #include <tuple>
13
14 #include "base/check_op.h"
15 #include "base/files/file_util.h"
16 #include "base/immediate_crash.h"
17 #include "base/metrics/histogram_functions.h"
18 #include "base/notreached.h"
19 #include "base/strings/string_util.h"
20 #include "base/threading/scoped_blocking_call.h"
21
22 namespace base {
23
24 // Make sure our Whence mappings match the system headers.
25 static_assert(File::FROM_BEGIN == FILE_BEGIN &&
26 File::FROM_CURRENT == FILE_CURRENT &&
27 File::FROM_END == FILE_END,
28 "whence mapping must match the system headers");
29
IsValid() const30 bool File::IsValid() const {
31 return file_.is_valid();
32 }
33
GetPlatformFile() const34 PlatformFile File::GetPlatformFile() const {
35 return file_.get();
36 }
37
TakePlatformFile()38 PlatformFile File::TakePlatformFile() {
39 return file_.release();
40 }
41
Close()42 void File::Close() {
43 if (!file_.is_valid())
44 return;
45
46 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
47 SCOPED_FILE_TRACE("Close");
48 file_.Close();
49 }
50
Seek(Whence whence,int64_t offset)51 int64_t File::Seek(Whence whence, int64_t offset) {
52 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
53 DCHECK(IsValid());
54
55 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset);
56
57 LARGE_INTEGER distance, res;
58 distance.QuadPart = offset;
59 DWORD move_method = static_cast<DWORD>(whence);
60 if (!SetFilePointerEx(file_.get(), distance, &res, move_method))
61 return -1;
62 return res.QuadPart;
63 }
64
Read(int64_t offset,char * data,int size)65 int File::Read(int64_t offset, char* data, int size) {
66 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
67 DCHECK(IsValid());
68 DCHECK(!async_);
69 if (size < 0 || offset < 0)
70 return -1;
71
72 SCOPED_FILE_TRACE_WITH_SIZE("Read", size);
73
74 ULARGE_INTEGER offset_li;
75 offset_li.QuadPart = static_cast<uint64_t>(offset);
76
77 OVERLAPPED overlapped = {};
78 overlapped.Offset = offset_li.LowPart;
79 overlapped.OffsetHigh = offset_li.HighPart;
80
81 DWORD bytes_read;
82 if (::ReadFile(file_.get(), data, static_cast<DWORD>(size), &bytes_read,
83 &overlapped)) {
84 // TODO(crbug.com/1333521): Change to return some type with a uint64_t size
85 // and eliminate this cast.
86 return checked_cast<int>(bytes_read);
87 }
88 if (ERROR_HANDLE_EOF == GetLastError())
89 return 0;
90
91 return -1;
92 }
93
ReadAtCurrentPos(char * data,int size)94 int File::ReadAtCurrentPos(char* data, int size) {
95 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
96 DCHECK(IsValid());
97 DCHECK(!async_);
98 if (size < 0)
99 return -1;
100
101 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size);
102
103 DWORD bytes_read;
104 if (::ReadFile(file_.get(), data, static_cast<DWORD>(size), &bytes_read,
105 NULL)) {
106 // TODO(crbug.com/1333521): Change to return some type with a uint64_t size
107 // and eliminate this cast.
108 return checked_cast<int>(bytes_read);
109 }
110 if (ERROR_HANDLE_EOF == GetLastError())
111 return 0;
112
113 return -1;
114 }
115
ReadNoBestEffort(int64_t offset,char * data,int size)116 int File::ReadNoBestEffort(int64_t offset, char* data, int size) {
117 // TODO(dbeam): trace this separately?
118 return Read(offset, data, size);
119 }
120
ReadAtCurrentPosNoBestEffort(char * data,int size)121 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
122 // TODO(dbeam): trace this separately?
123 return ReadAtCurrentPos(data, size);
124 }
125
Write(int64_t offset,const char * data,int size)126 int File::Write(int64_t offset, const char* data, int size) {
127 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
128 DCHECK(IsValid());
129 DCHECK(!async_);
130 if (size < 0 || offset < 0)
131 return -1;
132
133 SCOPED_FILE_TRACE_WITH_SIZE("Write", size);
134
135 ULARGE_INTEGER offset_li;
136 offset_li.QuadPart = static_cast<uint64_t>(offset);
137
138 OVERLAPPED overlapped = {};
139 overlapped.Offset = offset_li.LowPart;
140 overlapped.OffsetHigh = offset_li.HighPart;
141
142 DWORD bytes_written;
143 if (::WriteFile(file_.get(), data, static_cast<DWORD>(size), &bytes_written,
144 &overlapped))
145 return static_cast<int>(bytes_written);
146
147 return -1;
148 }
149
WriteAtCurrentPos(const char * data,int size)150 int File::WriteAtCurrentPos(const char* data, int size) {
151 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
152 DCHECK(IsValid());
153 DCHECK(!async_);
154 if (size < 0)
155 return -1;
156
157 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size);
158
159 DWORD bytes_written;
160 if (::WriteFile(file_.get(), data, static_cast<DWORD>(size), &bytes_written,
161 NULL))
162 return static_cast<int>(bytes_written);
163
164 return -1;
165 }
166
WriteAtCurrentPosNoBestEffort(const char * data,int size)167 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
168 return WriteAtCurrentPos(data, size);
169 }
170
GetLength() const171 int64_t File::GetLength() const {
172 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
173 DCHECK(IsValid());
174
175 SCOPED_FILE_TRACE("GetLength");
176
177 LARGE_INTEGER size;
178 if (!::GetFileSizeEx(file_.get(), &size))
179 return -1;
180
181 return static_cast<int64_t>(size.QuadPart);
182 }
183
SetLength(int64_t length)184 bool File::SetLength(int64_t length) {
185 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
186 DCHECK(IsValid());
187
188 SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length);
189
190 // Get the current file pointer.
191 LARGE_INTEGER file_pointer;
192 LARGE_INTEGER zero;
193 zero.QuadPart = 0;
194 if (!::SetFilePointerEx(file_.get(), zero, &file_pointer, FILE_CURRENT))
195 return false;
196
197 LARGE_INTEGER length_li;
198 length_li.QuadPart = length;
199 // If length > file size, SetFilePointerEx() should extend the file
200 // with zeroes on all Windows standard file systems (NTFS, FATxx).
201 if (!::SetFilePointerEx(file_.get(), length_li, NULL, FILE_BEGIN))
202 return false;
203
204 // Set the new file length and move the file pointer to its old position.
205 // This is consistent with ftruncate()'s behavior, even when the file
206 // pointer points to a location beyond the end of the file.
207 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not
208 // promised by the interface (nor was promised by PlatformFile). See if this
209 // implementation detail can be removed.
210 return ((::SetEndOfFile(file_.get()) != FALSE) &&
211 (::SetFilePointerEx(file_.get(), file_pointer, NULL, FILE_BEGIN) !=
212 FALSE));
213 }
214
SetTimes(Time last_access_time,Time last_modified_time)215 bool File::SetTimes(Time last_access_time, Time last_modified_time) {
216 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
217 DCHECK(IsValid());
218
219 SCOPED_FILE_TRACE("SetTimes");
220
221 FILETIME last_access_filetime = last_access_time.ToFileTime();
222 FILETIME last_modified_filetime = last_modified_time.ToFileTime();
223 return (::SetFileTime(file_.get(), NULL, &last_access_filetime,
224 &last_modified_filetime) != FALSE);
225 }
226
GetInfo(Info * info)227 bool File::GetInfo(Info* info) {
228 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
229 DCHECK(IsValid());
230
231 SCOPED_FILE_TRACE("GetInfo");
232
233 BY_HANDLE_FILE_INFORMATION file_info;
234 if (!GetFileInformationByHandle(file_.get(), &file_info))
235 return false;
236
237 ULARGE_INTEGER size;
238 size.HighPart = file_info.nFileSizeHigh;
239 size.LowPart = file_info.nFileSizeLow;
240 // TODO(crbug.com/1333521): Change Info::size to uint64_t and eliminate this
241 // cast.
242 info->size = checked_cast<int64_t>(size.QuadPart);
243 info->is_directory =
244 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
245 info->is_symbolic_link = false; // Windows doesn't have symbolic links.
246 info->last_modified = Time::FromFileTime(file_info.ftLastWriteTime);
247 info->last_accessed = Time::FromFileTime(file_info.ftLastAccessTime);
248 info->creation_time = Time::FromFileTime(file_info.ftCreationTime);
249 return true;
250 }
251
252 namespace {
253
LockFileFlagsForMode(File::LockMode mode)254 DWORD LockFileFlagsForMode(File::LockMode mode) {
255 DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
256 switch (mode) {
257 case File::LockMode::kShared:
258 return flags;
259 case File::LockMode::kExclusive:
260 return flags | LOCKFILE_EXCLUSIVE_LOCK;
261 }
262 NOTREACHED();
263 }
264
265 } // namespace
266
Lock(File::LockMode mode)267 File::Error File::Lock(File::LockMode mode) {
268 DCHECK(IsValid());
269
270 SCOPED_FILE_TRACE("Lock");
271
272 OVERLAPPED overlapped = {};
273 BOOL result =
274 LockFileEx(file_.get(), LockFileFlagsForMode(mode), /*dwReserved=*/0,
275 /*nNumberOfBytesToLockLow=*/MAXDWORD,
276 /*nNumberOfBytesToLockHigh=*/MAXDWORD, &overlapped);
277 if (!result)
278 return GetLastFileError();
279 return FILE_OK;
280 }
281
Unlock()282 File::Error File::Unlock() {
283 DCHECK(IsValid());
284
285 SCOPED_FILE_TRACE("Unlock");
286
287 OVERLAPPED overlapped = {};
288 BOOL result =
289 UnlockFileEx(file_.get(), /*dwReserved=*/0,
290 /*nNumberOfBytesToLockLow=*/MAXDWORD,
291 /*nNumberOfBytesToLockHigh=*/MAXDWORD, &overlapped);
292 if (!result)
293 return GetLastFileError();
294 return FILE_OK;
295 }
296
Duplicate() const297 File File::Duplicate() const {
298 if (!IsValid())
299 return File();
300
301 SCOPED_FILE_TRACE("Duplicate");
302
303 HANDLE other_handle = nullptr;
304
305 if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle
306 GetPlatformFile(),
307 GetCurrentProcess(), // hTargetProcessHandle
308 &other_handle,
309 0, // dwDesiredAccess ignored due to SAME_ACCESS
310 FALSE, // !bInheritHandle
311 DUPLICATE_SAME_ACCESS)) {
312 return File(GetLastFileError());
313 }
314
315 return File(ScopedPlatformFile(other_handle), async());
316 }
317
DeleteOnClose(bool delete_on_close)318 bool File::DeleteOnClose(bool delete_on_close) {
319 FILE_DISPOSITION_INFO disposition = {delete_on_close};
320 return ::SetFileInformationByHandle(GetPlatformFile(), FileDispositionInfo,
321 &disposition, sizeof(disposition)) != 0;
322 }
323
324 // Static.
OSErrorToFileError(DWORD last_error)325 File::Error File::OSErrorToFileError(DWORD last_error) {
326 switch (last_error) {
327 case ERROR_SHARING_VIOLATION:
328 case ERROR_UNABLE_TO_REMOVE_REPLACED: // ReplaceFile failure cases.
329 case ERROR_UNABLE_TO_MOVE_REPLACEMENT:
330 case ERROR_UNABLE_TO_MOVE_REPLACEMENT_2:
331 return FILE_ERROR_IN_USE;
332 case ERROR_ALREADY_EXISTS:
333 case ERROR_FILE_EXISTS:
334 return FILE_ERROR_EXISTS;
335 case ERROR_FILE_NOT_FOUND:
336 case ERROR_PATH_NOT_FOUND:
337 return FILE_ERROR_NOT_FOUND;
338 case ERROR_ACCESS_DENIED:
339 case ERROR_LOCK_VIOLATION:
340 return FILE_ERROR_ACCESS_DENIED;
341 case ERROR_TOO_MANY_OPEN_FILES:
342 return FILE_ERROR_TOO_MANY_OPENED;
343 case ERROR_OUTOFMEMORY:
344 case ERROR_NOT_ENOUGH_MEMORY:
345 return FILE_ERROR_NO_MEMORY;
346 case ERROR_HANDLE_DISK_FULL:
347 case ERROR_DISK_FULL:
348 case ERROR_DISK_RESOURCES_EXHAUSTED:
349 return FILE_ERROR_NO_SPACE;
350 case ERROR_USER_MAPPED_FILE:
351 return FILE_ERROR_INVALID_OPERATION;
352 case ERROR_NOT_READY: // The device is not ready.
353 case ERROR_SECTOR_NOT_FOUND: // The drive cannot find the sector requested.
354 case ERROR_GEN_FAILURE: // A device ... is not functioning.
355 case ERROR_DEV_NOT_EXIST: // Net resource or device is no longer available.
356 case ERROR_IO_DEVICE:
357 case ERROR_DISK_OPERATION_FAILED:
358 case ERROR_FILE_CORRUPT: // File or directory is corrupted and unreadable.
359 case ERROR_DISK_CORRUPT: // The disk structure is corrupted and unreadable.
360 return FILE_ERROR_IO;
361 default:
362 // This function should only be called for errors.
363 DCHECK_NE(static_cast<DWORD>(ERROR_SUCCESS), last_error);
364 return FILE_ERROR_FAILED;
365 }
366 }
367
DoInitialize(const FilePath & path,uint32_t flags)368 void File::DoInitialize(const FilePath& path, uint32_t flags) {
369 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
370 DCHECK(!IsValid());
371
372 DWORD disposition = 0;
373
374 if (flags & FLAG_OPEN)
375 disposition = OPEN_EXISTING;
376
377 if (flags & FLAG_CREATE) {
378 DCHECK(!disposition);
379 disposition = CREATE_NEW;
380 }
381
382 if (flags & FLAG_OPEN_ALWAYS) {
383 DCHECK(!disposition);
384 disposition = OPEN_ALWAYS;
385 }
386
387 if (flags & FLAG_CREATE_ALWAYS) {
388 DCHECK(!disposition);
389 DCHECK(flags & FLAG_WRITE);
390 disposition = CREATE_ALWAYS;
391 }
392
393 if (flags & FLAG_OPEN_TRUNCATED) {
394 DCHECK(!disposition);
395 DCHECK(flags & FLAG_WRITE);
396 disposition = TRUNCATE_EXISTING;
397 }
398
399 if (!disposition) {
400 ::SetLastError(ERROR_INVALID_PARAMETER);
401 error_details_ = FILE_ERROR_FAILED;
402 NOTREACHED();
403 return;
404 }
405
406 DWORD access = 0;
407 if (flags & FLAG_WRITE)
408 access = GENERIC_WRITE;
409 if (flags & FLAG_APPEND) {
410 DCHECK(!access);
411 access = FILE_APPEND_DATA;
412 }
413 if (flags & FLAG_READ)
414 access |= GENERIC_READ;
415 if (flags & FLAG_WRITE_ATTRIBUTES)
416 access |= FILE_WRITE_ATTRIBUTES;
417 if (flags & FLAG_WIN_EXECUTE) {
418 // Specifying both FLAG_WIN_EXECUTE and FLAG_WIN_NO_EXECUTE would
419 // constitute a security risk, so deny the access here.
420 CHECK_EQ(flags & FLAG_WIN_NO_EXECUTE, 0U);
421 access |= GENERIC_EXECUTE;
422 }
423 if (flags & FLAG_CAN_DELETE_ON_CLOSE)
424 access |= DELETE;
425
426 DWORD sharing = (flags & FLAG_WIN_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ;
427 if (!(flags & FLAG_WIN_EXCLUSIVE_WRITE))
428 sharing |= FILE_SHARE_WRITE;
429 if (flags & FLAG_WIN_SHARE_DELETE)
430 sharing |= FILE_SHARE_DELETE;
431
432 DWORD create_flags = 0;
433 if (flags & FLAG_ASYNC)
434 create_flags |= FILE_FLAG_OVERLAPPED;
435 if (flags & FLAG_WIN_TEMPORARY)
436 create_flags |= FILE_ATTRIBUTE_TEMPORARY;
437 if (flags & FLAG_WIN_HIDDEN)
438 create_flags |= FILE_ATTRIBUTE_HIDDEN;
439 if (flags & FLAG_DELETE_ON_CLOSE)
440 create_flags |= FILE_FLAG_DELETE_ON_CLOSE;
441 if (flags & FLAG_WIN_BACKUP_SEMANTICS)
442 create_flags |= FILE_FLAG_BACKUP_SEMANTICS;
443 if (flags & FLAG_WIN_SEQUENTIAL_SCAN)
444 create_flags |= FILE_FLAG_SEQUENTIAL_SCAN;
445
446 file_.Set(CreateFile(path.value().c_str(), access, sharing, NULL, disposition,
447 create_flags, NULL));
448
449 if (file_.is_valid()) {
450 error_details_ = FILE_OK;
451 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
452
453 if (flags & (FLAG_OPEN_ALWAYS))
454 created_ = (ERROR_ALREADY_EXISTS != GetLastError());
455 else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
456 created_ = true;
457 if (flags & FLAG_WIN_NO_EXECUTE) {
458 // These two DCHECKs make sure that no callers are trying to remove
459 // execute permission from a file that might need to be mapped executable
460 // later. If they hit in code then the file should not have
461 // FLAG_WIN_NO_EXECUTE flag, but this will mean that the file cannot be
462 // passed to renderers.
463 DCHECK(!base::FilePath::CompareEqualIgnoreCase(FILE_PATH_LITERAL(".exe"),
464 path.Extension()));
465 DCHECK(!base::FilePath::CompareEqualIgnoreCase(FILE_PATH_LITERAL(".dll"),
466 path.Extension()));
467
468 // It is possible that the ACE could not be added if the file was created
469 // in a path for which the caller does not have WRITE_DAC access. In this
470 // case, ignore the error since if this is occurring then it's likely the
471 // file cannot be opened for write and more serious I/O failures are
472 // occurring or about to occur.
473 std::ignore = PreventExecuteMapping(path);
474 }
475 } else {
476 error_details_ = GetLastFileError();
477 }
478 }
479
Flush()480 bool File::Flush() {
481 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
482 DCHECK(IsValid());
483 SCOPED_FILE_TRACE("Flush");
484
485 // On Windows 8 and above, FlushFileBuffers is guaranteed to flush the storage
486 // device's internal buffers (if they exist) before returning.
487 // https://blogs.msdn.microsoft.com/oldnewthing/20170510-00/?p=95505
488 return ::FlushFileBuffers(file_.get()) != FALSE;
489 }
490
SetPlatformFile(PlatformFile file)491 void File::SetPlatformFile(PlatformFile file) {
492 file_.Set(file);
493 }
494
495 // static
GetLastFileError()496 File::Error File::GetLastFileError() {
497 return File::OSErrorToFileError(GetLastError());
498 }
499
500 } // namespace base
501