OLD | NEW |
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 <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_tracing.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
15 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
18 | 19 |
19 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
20 #include "base/os_compat_android.h" | 21 #include "base/os_compat_android.h" |
21 #endif | 22 #endif |
22 | 23 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 167 } |
167 | 168 |
168 PlatformFile File::TakePlatformFile() { | 169 PlatformFile File::TakePlatformFile() { |
169 return file_.release(); | 170 return file_.release(); |
170 } | 171 } |
171 | 172 |
172 void File::Close() { | 173 void File::Close() { |
173 if (!IsValid()) | 174 if (!IsValid()) |
174 return; | 175 return; |
175 | 176 |
| 177 SCOPED_FILE_TRACE("Close"); |
176 ThreadRestrictions::AssertIOAllowed(); | 178 ThreadRestrictions::AssertIOAllowed(); |
177 file_.reset(); | 179 file_.reset(); |
178 } | 180 } |
179 | 181 |
180 int64 File::Seek(Whence whence, int64 offset) { | 182 int64 File::Seek(Whence whence, int64 offset) { |
181 ThreadRestrictions::AssertIOAllowed(); | 183 ThreadRestrictions::AssertIOAllowed(); |
182 DCHECK(IsValid()); | 184 DCHECK(IsValid()); |
183 | 185 |
| 186 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); |
| 187 |
184 #if defined(OS_ANDROID) | 188 #if defined(OS_ANDROID) |
185 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 189 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
186 return lseek64(file_.get(), static_cast<off64_t>(offset), | 190 return lseek64(file_.get(), static_cast<off64_t>(offset), |
187 static_cast<int>(whence)); | 191 static_cast<int>(whence)); |
188 #else | 192 #else |
189 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 193 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
190 return lseek(file_.get(), static_cast<off_t>(offset), | 194 return lseek(file_.get(), static_cast<off_t>(offset), |
191 static_cast<int>(whence)); | 195 static_cast<int>(whence)); |
192 #endif | 196 #endif |
193 } | 197 } |
194 | 198 |
195 int File::Read(int64 offset, char* data, int size) { | 199 int File::Read(int64 offset, char* data, int size) { |
196 ThreadRestrictions::AssertIOAllowed(); | 200 ThreadRestrictions::AssertIOAllowed(); |
197 DCHECK(IsValid()); | 201 DCHECK(IsValid()); |
198 if (size < 0) | 202 if (size < 0) |
199 return -1; | 203 return -1; |
200 | 204 |
| 205 SCOPED_FILE_TRACE_WITH_SIZE("Read", size); |
| 206 |
201 int bytes_read = 0; | 207 int bytes_read = 0; |
202 int rv; | 208 int rv; |
203 do { | 209 do { |
204 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, | 210 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
205 size - bytes_read, offset + bytes_read)); | 211 size - bytes_read, offset + bytes_read)); |
206 if (rv <= 0) | 212 if (rv <= 0) |
207 break; | 213 break; |
208 | 214 |
209 bytes_read += rv; | 215 bytes_read += rv; |
210 } while (bytes_read < size); | 216 } while (bytes_read < size); |
211 | 217 |
212 return bytes_read ? bytes_read : rv; | 218 return bytes_read ? bytes_read : rv; |
213 } | 219 } |
214 | 220 |
215 int File::ReadAtCurrentPos(char* data, int size) { | 221 int File::ReadAtCurrentPos(char* data, int size) { |
216 ThreadRestrictions::AssertIOAllowed(); | 222 ThreadRestrictions::AssertIOAllowed(); |
217 DCHECK(IsValid()); | 223 DCHECK(IsValid()); |
218 if (size < 0) | 224 if (size < 0) |
219 return -1; | 225 return -1; |
220 | 226 |
| 227 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size); |
| 228 |
221 int bytes_read = 0; | 229 int bytes_read = 0; |
222 int rv; | 230 int rv; |
223 do { | 231 do { |
224 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); | 232 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); |
225 if (rv <= 0) | 233 if (rv <= 0) |
226 break; | 234 break; |
227 | 235 |
228 bytes_read += rv; | 236 bytes_read += rv; |
229 } while (bytes_read < size); | 237 } while (bytes_read < size); |
230 | 238 |
231 return bytes_read ? bytes_read : rv; | 239 return bytes_read ? bytes_read : rv; |
232 } | 240 } |
233 | 241 |
234 int File::ReadNoBestEffort(int64 offset, char* data, int size) { | 242 int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
235 ThreadRestrictions::AssertIOAllowed(); | 243 ThreadRestrictions::AssertIOAllowed(); |
236 DCHECK(IsValid()); | 244 DCHECK(IsValid()); |
237 | 245 SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size); |
238 return HANDLE_EINTR(pread(file_.get(), data, size, offset)); | 246 return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
239 } | 247 } |
240 | 248 |
241 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { | 249 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
242 ThreadRestrictions::AssertIOAllowed(); | 250 ThreadRestrictions::AssertIOAllowed(); |
243 DCHECK(IsValid()); | 251 DCHECK(IsValid()); |
244 if (size < 0) | 252 if (size < 0) |
245 return -1; | 253 return -1; |
246 | 254 |
| 255 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPosNoBestEffort", size); |
247 return HANDLE_EINTR(read(file_.get(), data, size)); | 256 return HANDLE_EINTR(read(file_.get(), data, size)); |
248 } | 257 } |
249 | 258 |
250 int File::Write(int64 offset, const char* data, int size) { | 259 int File::Write(int64 offset, const char* data, int size) { |
251 ThreadRestrictions::AssertIOAllowed(); | 260 ThreadRestrictions::AssertIOAllowed(); |
252 | 261 |
253 if (IsOpenAppend(file_.get())) | 262 if (IsOpenAppend(file_.get())) |
254 return WriteAtCurrentPos(data, size); | 263 return WriteAtCurrentPos(data, size); |
255 | 264 |
256 DCHECK(IsValid()); | 265 DCHECK(IsValid()); |
257 if (size < 0) | 266 if (size < 0) |
258 return -1; | 267 return -1; |
259 | 268 |
| 269 SCOPED_FILE_TRACE_WITH_SIZE("Write", size); |
| 270 |
260 int bytes_written = 0; | 271 int bytes_written = 0; |
261 int rv; | 272 int rv; |
262 do { | 273 do { |
263 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, | 274 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
264 size - bytes_written, offset + bytes_written)); | 275 size - bytes_written, offset + bytes_written)); |
265 if (rv <= 0) | 276 if (rv <= 0) |
266 break; | 277 break; |
267 | 278 |
268 bytes_written += rv; | 279 bytes_written += rv; |
269 } while (bytes_written < size); | 280 } while (bytes_written < size); |
270 | 281 |
271 return bytes_written ? bytes_written : rv; | 282 return bytes_written ? bytes_written : rv; |
272 } | 283 } |
273 | 284 |
274 int File::WriteAtCurrentPos(const char* data, int size) { | 285 int File::WriteAtCurrentPos(const char* data, int size) { |
275 ThreadRestrictions::AssertIOAllowed(); | 286 ThreadRestrictions::AssertIOAllowed(); |
276 DCHECK(IsValid()); | 287 DCHECK(IsValid()); |
277 if (size < 0) | 288 if (size < 0) |
278 return -1; | 289 return -1; |
279 | 290 |
| 291 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size); |
| 292 |
280 int bytes_written = 0; | 293 int bytes_written = 0; |
281 int rv; | 294 int rv; |
282 do { | 295 do { |
283 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, | 296 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
284 size - bytes_written)); | 297 size - bytes_written)); |
285 if (rv <= 0) | 298 if (rv <= 0) |
286 break; | 299 break; |
287 | 300 |
288 bytes_written += rv; | 301 bytes_written += rv; |
289 } while (bytes_written < size); | 302 } while (bytes_written < size); |
290 | 303 |
291 return bytes_written ? bytes_written : rv; | 304 return bytes_written ? bytes_written : rv; |
292 } | 305 } |
293 | 306 |
294 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { | 307 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
295 ThreadRestrictions::AssertIOAllowed(); | 308 ThreadRestrictions::AssertIOAllowed(); |
296 DCHECK(IsValid()); | 309 DCHECK(IsValid()); |
297 if (size < 0) | 310 if (size < 0) |
298 return -1; | 311 return -1; |
299 | 312 |
| 313 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size); |
300 return HANDLE_EINTR(write(file_.get(), data, size)); | 314 return HANDLE_EINTR(write(file_.get(), data, size)); |
301 } | 315 } |
302 | 316 |
303 int64 File::GetLength() { | 317 int64 File::GetLength() { |
304 DCHECK(IsValid()); | 318 DCHECK(IsValid()); |
305 | 319 |
| 320 SCOPED_FILE_TRACE("GetLength"); |
| 321 |
306 stat_wrapper_t file_info; | 322 stat_wrapper_t file_info; |
307 if (CallFstat(file_.get(), &file_info)) | 323 if (CallFstat(file_.get(), &file_info)) |
308 return false; | 324 return false; |
309 | 325 |
310 return file_info.st_size; | 326 return file_info.st_size; |
311 } | 327 } |
312 | 328 |
313 bool File::SetLength(int64 length) { | 329 bool File::SetLength(int64 length) { |
314 ThreadRestrictions::AssertIOAllowed(); | 330 ThreadRestrictions::AssertIOAllowed(); |
315 DCHECK(IsValid()); | 331 DCHECK(IsValid()); |
| 332 |
| 333 SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length); |
316 return !CallFtruncate(file_.get(), length); | 334 return !CallFtruncate(file_.get(), length); |
317 } | 335 } |
318 | 336 |
| 337 bool File::Flush() { |
| 338 ThreadRestrictions::AssertIOAllowed(); |
| 339 DCHECK(IsValid()); |
| 340 |
| 341 #if defined(OS_NACL) |
| 342 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 343 return true; |
| 344 #else |
| 345 SCOPED_FILE_TRACE("Flush"); |
| 346 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 347 return !HANDLE_EINTR(fdatasync(file_.get())); |
| 348 #else |
| 349 return !HANDLE_EINTR(fsync(file_.get())); |
| 350 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 351 #endif // defined(OS_NACL) |
| 352 } |
| 353 |
| 354 void File::SetPlatformFile(PlatformFile file) { |
| 355 DCHECK(!file_.is_valid()); |
| 356 file_.reset(file); |
| 357 } |
| 358 |
319 bool File::SetTimes(Time last_access_time, Time last_modified_time) { | 359 bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
320 ThreadRestrictions::AssertIOAllowed(); | 360 ThreadRestrictions::AssertIOAllowed(); |
321 DCHECK(IsValid()); | 361 DCHECK(IsValid()); |
322 | 362 |
| 363 SCOPED_FILE_TRACE("SetTimes"); |
| 364 |
323 timeval times[2]; | 365 timeval times[2]; |
324 times[0] = last_access_time.ToTimeVal(); | 366 times[0] = last_access_time.ToTimeVal(); |
325 times[1] = last_modified_time.ToTimeVal(); | 367 times[1] = last_modified_time.ToTimeVal(); |
326 | 368 |
327 return !CallFutimes(file_.get(), times); | 369 return !CallFutimes(file_.get(), times); |
328 } | 370 } |
329 | 371 |
330 bool File::GetInfo(Info* info) { | 372 bool File::GetInfo(Info* info) { |
331 DCHECK(IsValid()); | 373 DCHECK(IsValid()); |
332 | 374 |
| 375 SCOPED_FILE_TRACE("GetInfo"); |
| 376 |
333 stat_wrapper_t file_info; | 377 stat_wrapper_t file_info; |
334 if (CallFstat(file_.get(), &file_info)) | 378 if (CallFstat(file_.get(), &file_info)) |
335 return false; | 379 return false; |
336 | 380 |
337 info->FromStat(file_info); | 381 info->FromStat(file_info); |
338 return true; | 382 return true; |
339 } | 383 } |
340 | 384 |
341 File::Error File::Lock() { | 385 File::Error File::Lock() { |
| 386 SCOPED_FILE_TRACE("Lock"); |
342 return CallFctnlFlock(file_.get(), true); | 387 return CallFctnlFlock(file_.get(), true); |
343 } | 388 } |
344 | 389 |
345 File::Error File::Unlock() { | 390 File::Error File::Unlock() { |
| 391 SCOPED_FILE_TRACE("Unlock"); |
346 return CallFctnlFlock(file_.get(), false); | 392 return CallFctnlFlock(file_.get(), false); |
347 } | 393 } |
348 | 394 |
349 File File::Duplicate() { | 395 File File::Duplicate() { |
350 if (!IsValid()) | 396 if (!IsValid()) |
351 return File(); | 397 return File(); |
352 | 398 |
| 399 SCOPED_FILE_TRACE("Duplicate"); |
| 400 |
353 PlatformFile other_fd = dup(GetPlatformFile()); | 401 PlatformFile other_fd = dup(GetPlatformFile()); |
354 if (other_fd == -1) | 402 if (other_fd == -1) |
355 return File(OSErrorToFileError(errno)); | 403 return File(OSErrorToFileError(errno)); |
356 | 404 |
357 File other(other_fd); | 405 File other(other_fd); |
358 if (async()) | 406 if (async()) |
359 other.async_ = true; | 407 other.async_ = true; |
360 return other.Pass(); | 408 return other.Pass(); |
361 } | 409 } |
362 | 410 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; | 483 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; |
436 } | 484 } |
437 | 485 |
438 void File::MemoryCheckingScopedFD::UpdateChecksum() { | 486 void File::MemoryCheckingScopedFD::UpdateChecksum() { |
439 ComputeMemoryChecksum(&file_memory_checksum_); | 487 ComputeMemoryChecksum(&file_memory_checksum_); |
440 } | 488 } |
441 | 489 |
442 // NaCl doesn't implement system calls to open files directly. | 490 // NaCl doesn't implement system calls to open files directly. |
443 #if !defined(OS_NACL) | 491 #if !defined(OS_NACL) |
444 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 492 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
445 void File::DoInitialize(const FilePath& name, uint32 flags) { | 493 void File::DoInitialize(uint32 flags) { |
446 ThreadRestrictions::AssertIOAllowed(); | 494 ThreadRestrictions::AssertIOAllowed(); |
447 DCHECK(!IsValid()); | 495 DCHECK(!IsValid()); |
448 | 496 |
449 int open_flags = 0; | 497 int open_flags = 0; |
450 if (flags & FLAG_CREATE) | 498 if (flags & FLAG_CREATE) |
451 open_flags = O_CREAT | O_EXCL; | 499 open_flags = O_CREAT | O_EXCL; |
452 | 500 |
453 created_ = false; | 501 created_ = false; |
454 | 502 |
455 if (flags & FLAG_CREATE_ALWAYS) { | 503 if (flags & FLAG_CREATE_ALWAYS) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 else if (flags & FLAG_APPEND) | 538 else if (flags & FLAG_APPEND) |
491 open_flags |= O_APPEND | O_WRONLY; | 539 open_flags |= O_APPEND | O_WRONLY; |
492 | 540 |
493 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); | 541 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
494 | 542 |
495 int mode = S_IRUSR | S_IWUSR; | 543 int mode = S_IRUSR | S_IWUSR; |
496 #if defined(OS_CHROMEOS) | 544 #if defined(OS_CHROMEOS) |
497 mode |= S_IRGRP | S_IROTH; | 545 mode |= S_IRGRP | S_IROTH; |
498 #endif | 546 #endif |
499 | 547 |
500 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); | 548 int descriptor = |
| 549 HANDLE_EINTR(open(unsafe_path_.value().c_str(), open_flags, mode)); |
501 | 550 |
502 if (flags & FLAG_OPEN_ALWAYS) { | 551 if (flags & FLAG_OPEN_ALWAYS) { |
503 if (descriptor < 0) { | 552 if (descriptor < 0) { |
504 open_flags |= O_CREAT; | 553 open_flags |= O_CREAT; |
505 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) | 554 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
506 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW | 555 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
507 | 556 |
508 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); | 557 descriptor = |
| 558 HANDLE_EINTR(open(unsafe_path_.value().c_str(), open_flags, mode)); |
509 if (descriptor >= 0) | 559 if (descriptor >= 0) |
510 created_ = true; | 560 created_ = true; |
511 } | 561 } |
512 } | 562 } |
513 | 563 |
514 if (descriptor < 0) { | 564 if (descriptor < 0) { |
515 error_details_ = File::OSErrorToFileError(errno); | 565 error_details_ = File::OSErrorToFileError(errno); |
516 return; | 566 return; |
517 } | 567 } |
518 | 568 |
519 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 569 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
520 created_ = true; | 570 created_ = true; |
521 | 571 |
522 if (flags & FLAG_DELETE_ON_CLOSE) | 572 if (flags & FLAG_DELETE_ON_CLOSE) |
523 unlink(name.value().c_str()); | 573 unlink(unsafe_path_.value().c_str()); |
524 | 574 |
525 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 575 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
526 error_details_ = FILE_OK; | 576 error_details_ = FILE_OK; |
527 file_.reset(descriptor); | 577 file_.reset(descriptor); |
528 } | 578 } |
529 #endif // !defined(OS_NACL) | 579 #endif // !defined(OS_NACL) |
530 | 580 |
531 bool File::DoFlush() { | |
532 ThreadRestrictions::AssertIOAllowed(); | |
533 DCHECK(IsValid()); | |
534 #if defined(OS_NACL) | |
535 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. | |
536 return true; | |
537 #elif defined(OS_LINUX) || defined(OS_ANDROID) | |
538 return !HANDLE_EINTR(fdatasync(file_.get())); | |
539 #else | |
540 return !HANDLE_EINTR(fsync(file_.get())); | |
541 #endif | |
542 } | |
543 | |
544 void File::SetPlatformFile(PlatformFile file) { | |
545 DCHECK(!file_.is_valid()); | |
546 file_.reset(file); | |
547 } | |
548 | |
549 } // namespace base | 581 } // namespace base |
OLD | NEW |