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

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

Powered by Google App Engine
This is Rietveld 408576698