Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: base/files/file_win.cc

Issue 1072133006: Add granular file tracing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@do-initialize
Patch Set: comment Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_tracing.cc ('k') | base/trace_event/trace_event.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
10 #include "base/logging.h" 9 #include "base/logging.h"
11 #include "base/metrics/sparse_histogram.h" 10 #include "base/metrics/sparse_histogram.h"
12 #include "base/threading/thread_restrictions.h" 11 #include "base/threading/thread_restrictions.h"
13 12
14 namespace base { 13 namespace base {
15 14
16 // Make sure our Whence mappings match the system headers. 15 // Make sure our Whence mappings match the system headers.
17 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && 16 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN &&
18 File::FROM_CURRENT == FILE_CURRENT && 17 File::FROM_CURRENT == FILE_CURRENT &&
19 File::FROM_END == FILE_END, whence_matches_system); 18 File::FROM_END == FILE_END, whence_matches_system);
20 19
21 bool File::IsValid() const { 20 bool File::IsValid() const {
22 return file_.IsValid(); 21 return file_.IsValid();
23 } 22 }
24 23
25 PlatformFile File::GetPlatformFile() const { 24 PlatformFile File::GetPlatformFile() const {
26 return file_.Get(); 25 return file_.Get();
27 } 26 }
28 27
29 PlatformFile File::TakePlatformFile() { 28 PlatformFile File::TakePlatformFile() {
30 return file_.Take(); 29 return file_.Take();
31 } 30 }
32 31
33 void File::Close() { 32 void File::Close() {
34 if (file_.IsValid()) { 33 if (!file_.IsValid())
35 ThreadRestrictions::AssertIOAllowed(); 34 return;
36 file_.Close(); 35
37 } 36 ThreadRestrictions::AssertIOAllowed();
37 SCOPED_FILE_TRACE("Close");
38 file_.Close();
38 } 39 }
39 40
40 int64 File::Seek(Whence whence, int64 offset) { 41 int64 File::Seek(Whence whence, int64 offset) {
41 ThreadRestrictions::AssertIOAllowed(); 42 ThreadRestrictions::AssertIOAllowed();
42 DCHECK(IsValid()); 43 DCHECK(IsValid());
43 44
45 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset);
46
44 LARGE_INTEGER distance, res; 47 LARGE_INTEGER distance, res;
45 distance.QuadPart = offset; 48 distance.QuadPart = offset;
46 DWORD move_method = static_cast<DWORD>(whence); 49 DWORD move_method = static_cast<DWORD>(whence);
47 if (!SetFilePointerEx(file_.Get(), distance, &res, move_method)) 50 if (!SetFilePointerEx(file_.Get(), distance, &res, move_method))
48 return -1; 51 return -1;
49 return res.QuadPart; 52 return res.QuadPart;
50 } 53 }
51 54
52 int File::Read(int64 offset, char* data, int size) { 55 int File::Read(int64 offset, char* data, int size) {
53 ThreadRestrictions::AssertIOAllowed(); 56 ThreadRestrictions::AssertIOAllowed();
54 DCHECK(IsValid()); 57 DCHECK(IsValid());
55 DCHECK(!async_); 58 DCHECK(!async_);
56 if (size < 0) 59 if (size < 0)
57 return -1; 60 return -1;
58 61
62 SCOPED_FILE_TRACE_WITH_SIZE("Read", size);
63
59 LARGE_INTEGER offset_li; 64 LARGE_INTEGER offset_li;
60 offset_li.QuadPart = offset; 65 offset_li.QuadPart = offset;
61 66
62 OVERLAPPED overlapped = {0}; 67 OVERLAPPED overlapped = {0};
63 overlapped.Offset = offset_li.LowPart; 68 overlapped.Offset = offset_li.LowPart;
64 overlapped.OffsetHigh = offset_li.HighPart; 69 overlapped.OffsetHigh = offset_li.HighPart;
65 70
66 DWORD bytes_read; 71 DWORD bytes_read;
67 if (::ReadFile(file_.Get(), data, size, &bytes_read, &overlapped)) 72 if (::ReadFile(file_.Get(), data, size, &bytes_read, &overlapped))
68 return bytes_read; 73 return bytes_read;
69 if (ERROR_HANDLE_EOF == GetLastError()) 74 if (ERROR_HANDLE_EOF == GetLastError())
70 return 0; 75 return 0;
71 76
72 return -1; 77 return -1;
73 } 78 }
74 79
75 int File::ReadAtCurrentPos(char* data, int size) { 80 int File::ReadAtCurrentPos(char* data, int size) {
76 ThreadRestrictions::AssertIOAllowed(); 81 ThreadRestrictions::AssertIOAllowed();
77 DCHECK(IsValid()); 82 DCHECK(IsValid());
78 DCHECK(!async_); 83 DCHECK(!async_);
79 if (size < 0) 84 if (size < 0)
80 return -1; 85 return -1;
81 86
87 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size);
88
82 DWORD bytes_read; 89 DWORD bytes_read;
83 if (::ReadFile(file_.Get(), data, size, &bytes_read, NULL)) 90 if (::ReadFile(file_.Get(), data, size, &bytes_read, NULL))
84 return bytes_read; 91 return bytes_read;
85 if (ERROR_HANDLE_EOF == GetLastError()) 92 if (ERROR_HANDLE_EOF == GetLastError())
86 return 0; 93 return 0;
87 94
88 return -1; 95 return -1;
89 } 96 }
90 97
91 int File::ReadNoBestEffort(int64 offset, char* data, int size) { 98 int File::ReadNoBestEffort(int64 offset, char* data, int size) {
99 // TODO(dbeam): trace this separately?
92 return Read(offset, data, size); 100 return Read(offset, data, size);
93 } 101 }
94 102
95 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { 103 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
104 // TODO(dbeam): trace this separately?
96 return ReadAtCurrentPos(data, size); 105 return ReadAtCurrentPos(data, size);
97 } 106 }
98 107
99 int File::Write(int64 offset, const char* data, int size) { 108 int File::Write(int64 offset, const char* data, int size) {
100 ThreadRestrictions::AssertIOAllowed(); 109 ThreadRestrictions::AssertIOAllowed();
101 DCHECK(IsValid()); 110 DCHECK(IsValid());
102 DCHECK(!async_); 111 DCHECK(!async_);
103 112
113 SCOPED_FILE_TRACE_WITH_SIZE("Write", size);
114
104 LARGE_INTEGER offset_li; 115 LARGE_INTEGER offset_li;
105 offset_li.QuadPart = offset; 116 offset_li.QuadPart = offset;
106 117
107 OVERLAPPED overlapped = {0}; 118 OVERLAPPED overlapped = {0};
108 overlapped.Offset = offset_li.LowPart; 119 overlapped.Offset = offset_li.LowPart;
109 overlapped.OffsetHigh = offset_li.HighPart; 120 overlapped.OffsetHigh = offset_li.HighPart;
110 121
111 DWORD bytes_written; 122 DWORD bytes_written;
112 if (::WriteFile(file_.Get(), data, size, &bytes_written, &overlapped)) 123 if (::WriteFile(file_.Get(), data, size, &bytes_written, &overlapped))
113 return bytes_written; 124 return bytes_written;
114 125
115 return -1; 126 return -1;
116 } 127 }
117 128
118 int File::WriteAtCurrentPos(const char* data, int size) { 129 int File::WriteAtCurrentPos(const char* data, int size) {
119 ThreadRestrictions::AssertIOAllowed(); 130 ThreadRestrictions::AssertIOAllowed();
120 DCHECK(IsValid()); 131 DCHECK(IsValid());
121 DCHECK(!async_); 132 DCHECK(!async_);
122 if (size < 0) 133 if (size < 0)
123 return -1; 134 return -1;
124 135
136 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size);
137
125 DWORD bytes_written; 138 DWORD bytes_written;
126 if (::WriteFile(file_.Get(), data, size, &bytes_written, NULL)) 139 if (::WriteFile(file_.Get(), data, size, &bytes_written, NULL))
127 return bytes_written; 140 return bytes_written;
128 141
129 return -1; 142 return -1;
130 } 143 }
131 144
132 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { 145 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
133 return WriteAtCurrentPos(data, size); 146 return WriteAtCurrentPos(data, size);
134 } 147 }
135 148
136 int64 File::GetLength() { 149 int64 File::GetLength() {
137 ThreadRestrictions::AssertIOAllowed(); 150 ThreadRestrictions::AssertIOAllowed();
138 DCHECK(IsValid()); 151 DCHECK(IsValid());
152
153 SCOPED_FILE_TRACE("GetLength");
154
139 LARGE_INTEGER size; 155 LARGE_INTEGER size;
140 if (!::GetFileSizeEx(file_.Get(), &size)) 156 if (!::GetFileSizeEx(file_.Get(), &size))
141 return -1; 157 return -1;
142 158
143 return static_cast<int64>(size.QuadPart); 159 return static_cast<int64>(size.QuadPart);
144 } 160 }
145 161
146 bool File::SetLength(int64 length) { 162 bool File::SetLength(int64 length) {
147 ThreadRestrictions::AssertIOAllowed(); 163 ThreadRestrictions::AssertIOAllowed();
148 DCHECK(IsValid()); 164 DCHECK(IsValid());
149 165
166 SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length);
167
150 // Get the current file pointer. 168 // Get the current file pointer.
151 LARGE_INTEGER file_pointer; 169 LARGE_INTEGER file_pointer;
152 LARGE_INTEGER zero; 170 LARGE_INTEGER zero;
153 zero.QuadPart = 0; 171 zero.QuadPart = 0;
154 if (!::SetFilePointerEx(file_.Get(), zero, &file_pointer, FILE_CURRENT)) 172 if (!::SetFilePointerEx(file_.Get(), zero, &file_pointer, FILE_CURRENT))
155 return false; 173 return false;
156 174
157 LARGE_INTEGER length_li; 175 LARGE_INTEGER length_li;
158 length_li.QuadPart = length; 176 length_li.QuadPart = length;
159 // If length > file size, SetFilePointerEx() should extend the file 177 // If length > file size, SetFilePointerEx() should extend the file
160 // with zeroes on all Windows standard file systems (NTFS, FATxx). 178 // with zeroes on all Windows standard file systems (NTFS, FATxx).
161 if (!::SetFilePointerEx(file_.Get(), length_li, NULL, FILE_BEGIN)) 179 if (!::SetFilePointerEx(file_.Get(), length_li, NULL, FILE_BEGIN))
162 return false; 180 return false;
163 181
164 // Set the new file length and move the file pointer to its old position. 182 // Set the new file length and move the file pointer to its old position.
165 // This is consistent with ftruncate()'s behavior, even when the file 183 // This is consistent with ftruncate()'s behavior, even when the file
166 // pointer points to a location beyond the end of the file. 184 // pointer points to a location beyond the end of the file.
167 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not 185 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not
168 // promised by the interface (nor was promised by PlatformFile). See if this 186 // promised by the interface (nor was promised by PlatformFile). See if this
169 // implementation detail can be removed. 187 // implementation detail can be removed.
170 return ((::SetEndOfFile(file_.Get()) != FALSE) && 188 return ((::SetEndOfFile(file_.Get()) != FALSE) &&
171 (::SetFilePointerEx(file_.Get(), file_pointer, NULL, FILE_BEGIN) != 189 (::SetFilePointerEx(file_.Get(), file_pointer, NULL, FILE_BEGIN) !=
172 FALSE)); 190 FALSE));
173 } 191 }
174 192
175 bool File::SetTimes(Time last_access_time, Time last_modified_time) { 193 bool File::SetTimes(Time last_access_time, Time last_modified_time) {
176 ThreadRestrictions::AssertIOAllowed(); 194 ThreadRestrictions::AssertIOAllowed();
177 DCHECK(IsValid()); 195 DCHECK(IsValid());
178 196
197 SCOPED_FILE_TRACE("SetTimes");
198
179 FILETIME last_access_filetime = last_access_time.ToFileTime(); 199 FILETIME last_access_filetime = last_access_time.ToFileTime();
180 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); 200 FILETIME last_modified_filetime = last_modified_time.ToFileTime();
181 return (::SetFileTime(file_.Get(), NULL, &last_access_filetime, 201 return (::SetFileTime(file_.Get(), NULL, &last_access_filetime,
182 &last_modified_filetime) != FALSE); 202 &last_modified_filetime) != FALSE);
183 } 203 }
184 204
185 bool File::GetInfo(Info* info) { 205 bool File::GetInfo(Info* info) {
186 ThreadRestrictions::AssertIOAllowed(); 206 ThreadRestrictions::AssertIOAllowed();
187 DCHECK(IsValid()); 207 DCHECK(IsValid());
188 208
209 SCOPED_FILE_TRACE("GetInfo");
210
189 BY_HANDLE_FILE_INFORMATION file_info; 211 BY_HANDLE_FILE_INFORMATION file_info;
190 if (!GetFileInformationByHandle(file_.Get(), &file_info)) 212 if (!GetFileInformationByHandle(file_.Get(), &file_info))
191 return false; 213 return false;
192 214
193 LARGE_INTEGER size; 215 LARGE_INTEGER size;
194 size.HighPart = file_info.nFileSizeHigh; 216 size.HighPart = file_info.nFileSizeHigh;
195 size.LowPart = file_info.nFileSizeLow; 217 size.LowPart = file_info.nFileSizeLow;
196 info->size = size.QuadPart; 218 info->size = size.QuadPart;
197 info->is_directory = 219 info->is_directory =
198 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; 220 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
199 info->is_symbolic_link = false; // Windows doesn't have symbolic links. 221 info->is_symbolic_link = false; // Windows doesn't have symbolic links.
200 info->last_modified = Time::FromFileTime(file_info.ftLastWriteTime); 222 info->last_modified = Time::FromFileTime(file_info.ftLastWriteTime);
201 info->last_accessed = Time::FromFileTime(file_info.ftLastAccessTime); 223 info->last_accessed = Time::FromFileTime(file_info.ftLastAccessTime);
202 info->creation_time = Time::FromFileTime(file_info.ftCreationTime); 224 info->creation_time = Time::FromFileTime(file_info.ftCreationTime);
203 return true; 225 return true;
204 } 226 }
205 227
206 File::Error File::Lock() { 228 File::Error File::Lock() {
207 DCHECK(IsValid()); 229 DCHECK(IsValid());
230
231 SCOPED_FILE_TRACE("Lock");
232
208 BOOL result = LockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); 233 BOOL result = LockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD);
209 if (!result) 234 if (!result)
210 return OSErrorToFileError(GetLastError()); 235 return OSErrorToFileError(GetLastError());
211 return FILE_OK; 236 return FILE_OK;
212 } 237 }
213 238
214 File::Error File::Unlock() { 239 File::Error File::Unlock() {
215 DCHECK(IsValid()); 240 DCHECK(IsValid());
241
242 SCOPED_FILE_TRACE("Unlock");
243
216 BOOL result = UnlockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD); 244 BOOL result = UnlockFile(file_.Get(), 0, 0, MAXDWORD, MAXDWORD);
217 if (!result) 245 if (!result)
218 return OSErrorToFileError(GetLastError()); 246 return OSErrorToFileError(GetLastError());
219 return FILE_OK; 247 return FILE_OK;
220 } 248 }
221 249
222 File File::Duplicate() { 250 File File::Duplicate() {
223 if (!IsValid()) 251 if (!IsValid())
224 return File(); 252 return File();
225 253
254 SCOPED_FILE_TRACE("Duplicate");
255
226 HANDLE other_handle = nullptr; 256 HANDLE other_handle = nullptr;
227 257
228 if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle 258 if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle
229 GetPlatformFile(), 259 GetPlatformFile(),
230 GetCurrentProcess(), // hTargetProcessHandle 260 GetCurrentProcess(), // hTargetProcessHandle
231 &other_handle, 261 &other_handle,
232 0, // dwDesiredAccess ignored due to SAME_ACCESS 262 0, // dwDesiredAccess ignored due to SAME_ACCESS
233 FALSE, // !bInheritHandle 263 FALSE, // !bInheritHandle
234 DUPLICATE_SAME_ACCESS)) { 264 DUPLICATE_SAME_ACCESS)) {
235 return File(OSErrorToFileError(GetLastError())); 265 return File(OSErrorToFileError(GetLastError()));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 case ERROR_FILE_CORRUPT: 301 case ERROR_FILE_CORRUPT:
272 case ERROR_DISK_CORRUPT: 302 case ERROR_DISK_CORRUPT:
273 return FILE_ERROR_IO; 303 return FILE_ERROR_IO;
274 default: 304 default:
275 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", 305 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows",
276 last_error); 306 last_error);
277 return FILE_ERROR_FAILED; 307 return FILE_ERROR_FAILED;
278 } 308 }
279 } 309 }
280 310
281 void File::DoInitialize(const FilePath& name, uint32 flags) { 311 void File::DoInitialize(uint32 flags) {
282 ThreadRestrictions::AssertIOAllowed(); 312 ThreadRestrictions::AssertIOAllowed();
283 DCHECK(!IsValid()); 313 DCHECK(!IsValid());
284 314
285 DWORD disposition = 0; 315 DWORD disposition = 0;
286 316
287 if (flags & FLAG_OPEN) 317 if (flags & FLAG_OPEN)
288 disposition = OPEN_EXISTING; 318 disposition = OPEN_EXISTING;
289 319
290 if (flags & FLAG_CREATE) { 320 if (flags & FLAG_CREATE) {
291 DCHECK(!disposition); 321 DCHECK(!disposition);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 create_flags |= FILE_FLAG_OVERLAPPED; 369 create_flags |= FILE_FLAG_OVERLAPPED;
340 if (flags & FLAG_TEMPORARY) 370 if (flags & FLAG_TEMPORARY)
341 create_flags |= FILE_ATTRIBUTE_TEMPORARY; 371 create_flags |= FILE_ATTRIBUTE_TEMPORARY;
342 if (flags & FLAG_HIDDEN) 372 if (flags & FLAG_HIDDEN)
343 create_flags |= FILE_ATTRIBUTE_HIDDEN; 373 create_flags |= FILE_ATTRIBUTE_HIDDEN;
344 if (flags & FLAG_DELETE_ON_CLOSE) 374 if (flags & FLAG_DELETE_ON_CLOSE)
345 create_flags |= FILE_FLAG_DELETE_ON_CLOSE; 375 create_flags |= FILE_FLAG_DELETE_ON_CLOSE;
346 if (flags & FLAG_BACKUP_SEMANTICS) 376 if (flags & FLAG_BACKUP_SEMANTICS)
347 create_flags |= FILE_FLAG_BACKUP_SEMANTICS; 377 create_flags |= FILE_FLAG_BACKUP_SEMANTICS;
348 378
349 file_.Set(CreateFile(name.value().c_str(), access, sharing, NULL, 379 file_.Set(CreateFile(path_.value().c_str(), access, sharing, NULL,
350 disposition, create_flags, NULL)); 380 disposition, create_flags, NULL));
351 381
352 if (file_.IsValid()) { 382 if (file_.IsValid()) {
353 error_details_ = FILE_OK; 383 error_details_ = FILE_OK;
354 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); 384 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
355 385
356 if (flags & (FLAG_OPEN_ALWAYS)) 386 if (flags & (FLAG_OPEN_ALWAYS))
357 created_ = (ERROR_ALREADY_EXISTS != GetLastError()); 387 created_ = (ERROR_ALREADY_EXISTS != GetLastError());
358 else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) 388 else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
359 created_ = true; 389 created_ = true;
360 } else { 390 } else {
361 error_details_ = OSErrorToFileError(GetLastError()); 391 error_details_ = OSErrorToFileError(GetLastError());
362 } 392 }
363 } 393 }
364 394
365 bool File::DoFlush() { 395 bool File::DoFlush() {
366 ThreadRestrictions::AssertIOAllowed(); 396 ThreadRestrictions::AssertIOAllowed();
367 DCHECK(IsValid()); 397 DCHECK(IsValid());
368 return ::FlushFileBuffers(file_.Get()) != FALSE; 398 return ::FlushFileBuffers(file_.Get()) != FALSE;
369 } 399 }
370 400
371 void File::SetPlatformFile(PlatformFile file) { 401 void File::SetPlatformFile(PlatformFile file) {
372 file_.Set(file); 402 file_.Set(file);
373 } 403 }
374 404
375 } // namespace base 405 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_tracing.cc ('k') | base/trace_event/trace_event.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698