• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <io.h>
8 #include <stdint.h>
9 
10 #include <tuple>
11 
12 #include "base/check_op.h"
13 #include "base/files/file_util.h"
14 #include "base/immediate_crash.h"
15 #include "base/metrics/histogram_functions.h"
16 #include "base/notreached.h"
17 #include "base/strings/string_util.h"
18 #include "base/threading/scoped_blocking_call.h"
19 
20 #include <windows.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()171 int64_t File::GetLength() {
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       UmaHistogramSparse("PlatformFile.UnknownErrors.Windows",
363                          static_cast<int>(last_error));
364       // This function should only be called for errors.
365       DCHECK_NE(static_cast<DWORD>(ERROR_SUCCESS), last_error);
366       return FILE_ERROR_FAILED;
367   }
368 }
369 
DoInitialize(const FilePath & path,uint32_t flags)370 void File::DoInitialize(const FilePath& path, uint32_t flags) {
371   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
372   DCHECK(!IsValid());
373 
374   DWORD disposition = 0;
375 
376   if (flags & FLAG_OPEN)
377     disposition = OPEN_EXISTING;
378 
379   if (flags & FLAG_CREATE) {
380     DCHECK(!disposition);
381     disposition = CREATE_NEW;
382   }
383 
384   if (flags & FLAG_OPEN_ALWAYS) {
385     DCHECK(!disposition);
386     disposition = OPEN_ALWAYS;
387   }
388 
389   if (flags & FLAG_CREATE_ALWAYS) {
390     DCHECK(!disposition);
391     DCHECK(flags & FLAG_WRITE);
392     disposition = CREATE_ALWAYS;
393   }
394 
395   if (flags & FLAG_OPEN_TRUNCATED) {
396     DCHECK(!disposition);
397     DCHECK(flags & FLAG_WRITE);
398     disposition = TRUNCATE_EXISTING;
399   }
400 
401   if (!disposition) {
402     ::SetLastError(ERROR_INVALID_PARAMETER);
403     error_details_ = FILE_ERROR_FAILED;
404     NOTREACHED();
405     return;
406   }
407 
408   DWORD access = 0;
409   if (flags & FLAG_WRITE)
410     access = GENERIC_WRITE;
411   if (flags & FLAG_APPEND) {
412     DCHECK(!access);
413     access = FILE_APPEND_DATA;
414   }
415   if (flags & FLAG_READ)
416     access |= GENERIC_READ;
417   if (flags & FLAG_WRITE_ATTRIBUTES)
418     access |= FILE_WRITE_ATTRIBUTES;
419   if (flags & FLAG_WIN_EXECUTE) {
420     // Specifying both FLAG_WIN_EXECUTE and FLAG_WIN_NO_EXECUTE would
421     // constitute a security risk, so deny the access here.
422     CHECK_EQ(flags & FLAG_WIN_NO_EXECUTE, 0U);
423     access |= GENERIC_EXECUTE;
424   }
425   if (flags & FLAG_CAN_DELETE_ON_CLOSE)
426     access |= DELETE;
427 
428   DWORD sharing = (flags & FLAG_WIN_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ;
429   if (!(flags & FLAG_WIN_EXCLUSIVE_WRITE))
430     sharing |= FILE_SHARE_WRITE;
431   if (flags & FLAG_WIN_SHARE_DELETE)
432     sharing |= FILE_SHARE_DELETE;
433 
434   DWORD create_flags = 0;
435   if (flags & FLAG_ASYNC)
436     create_flags |= FILE_FLAG_OVERLAPPED;
437   if (flags & FLAG_WIN_TEMPORARY)
438     create_flags |= FILE_ATTRIBUTE_TEMPORARY;
439   if (flags & FLAG_WIN_HIDDEN)
440     create_flags |= FILE_ATTRIBUTE_HIDDEN;
441   if (flags & FLAG_DELETE_ON_CLOSE)
442     create_flags |= FILE_FLAG_DELETE_ON_CLOSE;
443   if (flags & FLAG_WIN_BACKUP_SEMANTICS)
444     create_flags |= FILE_FLAG_BACKUP_SEMANTICS;
445   if (flags & FLAG_WIN_SEQUENTIAL_SCAN)
446     create_flags |= FILE_FLAG_SEQUENTIAL_SCAN;
447 
448   file_.Set(CreateFile(path.value().c_str(), access, sharing, NULL, disposition,
449                        create_flags, NULL));
450 
451   if (file_.is_valid()) {
452     error_details_ = FILE_OK;
453     async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
454 
455     if (flags & (FLAG_OPEN_ALWAYS))
456       created_ = (ERROR_ALREADY_EXISTS != GetLastError());
457     else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
458       created_ = true;
459     if (flags & FLAG_WIN_NO_EXECUTE) {
460       // These two DCHECKs make sure that no callers are trying to remove
461       // execute permission from a file that might need to be mapped executable
462       // later. If they hit in code then the file should not have
463       // FLAG_WIN_NO_EXECUTE flag, but this will mean that the file cannot be
464       // passed to renderers.
465       DCHECK(!base::FilePath::CompareEqualIgnoreCase(FILE_PATH_LITERAL(".exe"),
466                                                      path.Extension()));
467       DCHECK(!base::FilePath::CompareEqualIgnoreCase(FILE_PATH_LITERAL(".dll"),
468                                                      path.Extension()));
469 
470       // It is possible that the ACE could not be added if the file was created
471       // in a path for which the caller does not have WRITE_DAC access. In this
472       // case, ignore the error since if this is occurring then it's likely the
473       // file cannot be opened for write and more serious I/O failures are
474       // occurring or about to occur.
475       std::ignore = PreventExecuteMapping(path);
476     }
477   } else {
478     error_details_ = GetLastFileError();
479   }
480 }
481 
Flush()482 bool File::Flush() {
483   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
484   DCHECK(IsValid());
485   SCOPED_FILE_TRACE("Flush");
486 
487   // On Windows 8 and above, FlushFileBuffers is guaranteed to flush the storage
488   // device's internal buffers (if they exist) before returning.
489   // https://blogs.msdn.microsoft.com/oldnewthing/20170510-00/?p=95505
490   return ::FlushFileBuffers(file_.get()) != FALSE;
491 }
492 
SetPlatformFile(PlatformFile file)493 void File::SetPlatformFile(PlatformFile file) {
494   file_.Set(file);
495 }
496 
497 // static
GetLastFileError()498 File::Error File::GetLastFileError() {
499   return File::OSErrorToFileError(GetLastError());
500 }
501 
502 }  // namespace base
503