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

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

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