| 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 |
| 319 bool File::SetTimes(Time last_access_time, Time last_modified_time) { | 337 bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
| 320 ThreadRestrictions::AssertIOAllowed(); | 338 ThreadRestrictions::AssertIOAllowed(); |
| 321 DCHECK(IsValid()); | 339 DCHECK(IsValid()); |
| 322 | 340 |
| 341 SCOPED_FILE_TRACE("SetTimes"); |
| 342 |
| 323 timeval times[2]; | 343 timeval times[2]; |
| 324 times[0] = last_access_time.ToTimeVal(); | 344 times[0] = last_access_time.ToTimeVal(); |
| 325 times[1] = last_modified_time.ToTimeVal(); | 345 times[1] = last_modified_time.ToTimeVal(); |
| 326 | 346 |
| 327 return !CallFutimes(file_.get(), times); | 347 return !CallFutimes(file_.get(), times); |
| 328 } | 348 } |
| 329 | 349 |
| 330 bool File::GetInfo(Info* info) { | 350 bool File::GetInfo(Info* info) { |
| 331 DCHECK(IsValid()); | 351 DCHECK(IsValid()); |
| 332 | 352 |
| 353 SCOPED_FILE_TRACE("GetInfo"); |
| 354 |
| 333 stat_wrapper_t file_info; | 355 stat_wrapper_t file_info; |
| 334 if (CallFstat(file_.get(), &file_info)) | 356 if (CallFstat(file_.get(), &file_info)) |
| 335 return false; | 357 return false; |
| 336 | 358 |
| 337 info->FromStat(file_info); | 359 info->FromStat(file_info); |
| 338 return true; | 360 return true; |
| 339 } | 361 } |
| 340 | 362 |
| 341 File::Error File::Lock() { | 363 File::Error File::Lock() { |
| 364 SCOPED_FILE_TRACE("Lock"); |
| 342 return CallFctnlFlock(file_.get(), true); | 365 return CallFctnlFlock(file_.get(), true); |
| 343 } | 366 } |
| 344 | 367 |
| 345 File::Error File::Unlock() { | 368 File::Error File::Unlock() { |
| 369 SCOPED_FILE_TRACE("Unlock"); |
| 346 return CallFctnlFlock(file_.get(), false); | 370 return CallFctnlFlock(file_.get(), false); |
| 347 } | 371 } |
| 348 | 372 |
| 349 File File::Duplicate() { | 373 File File::Duplicate() { |
| 350 if (!IsValid()) | 374 if (!IsValid()) |
| 351 return File(); | 375 return File(); |
| 352 | 376 |
| 377 SCOPED_FILE_TRACE("Duplicate"); |
| 378 |
| 353 PlatformFile other_fd = dup(GetPlatformFile()); | 379 PlatformFile other_fd = dup(GetPlatformFile()); |
| 354 if (other_fd == -1) | 380 if (other_fd == -1) |
| 355 return File(OSErrorToFileError(errno)); | 381 return File(OSErrorToFileError(errno)); |
| 356 | 382 |
| 357 File other(other_fd); | 383 File other(other_fd); |
| 358 if (async()) | 384 if (async()) |
| 359 other.async_ = true; | 385 other.async_ = true; |
| 360 return other.Pass(); | 386 return other.Pass(); |
| 361 } | 387 } |
| 362 | 388 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; | 461 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; |
| 436 } | 462 } |
| 437 | 463 |
| 438 void File::MemoryCheckingScopedFD::UpdateChecksum() { | 464 void File::MemoryCheckingScopedFD::UpdateChecksum() { |
| 439 ComputeMemoryChecksum(&file_memory_checksum_); | 465 ComputeMemoryChecksum(&file_memory_checksum_); |
| 440 } | 466 } |
| 441 | 467 |
| 442 // NaCl doesn't implement system calls to open files directly. | 468 // NaCl doesn't implement system calls to open files directly. |
| 443 #if !defined(OS_NACL) | 469 #if !defined(OS_NACL) |
| 444 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 470 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
| 445 void File::DoInitialize(const FilePath& name, uint32 flags) { | 471 void File::DoInitialize(uint32 flags) { |
| 446 ThreadRestrictions::AssertIOAllowed(); | 472 ThreadRestrictions::AssertIOAllowed(); |
| 447 DCHECK(!IsValid()); | 473 DCHECK(!IsValid()); |
| 448 | 474 |
| 449 int open_flags = 0; | 475 int open_flags = 0; |
| 450 if (flags & FLAG_CREATE) | 476 if (flags & FLAG_CREATE) |
| 451 open_flags = O_CREAT | O_EXCL; | 477 open_flags = O_CREAT | O_EXCL; |
| 452 | 478 |
| 453 created_ = false; | 479 created_ = false; |
| 454 | 480 |
| 455 if (flags & FLAG_CREATE_ALWAYS) { | 481 if (flags & FLAG_CREATE_ALWAYS) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 else if (flags & FLAG_APPEND) | 516 else if (flags & FLAG_APPEND) |
| 491 open_flags |= O_APPEND | O_WRONLY; | 517 open_flags |= O_APPEND | O_WRONLY; |
| 492 | 518 |
| 493 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); | 519 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
| 494 | 520 |
| 495 int mode = S_IRUSR | S_IWUSR; | 521 int mode = S_IRUSR | S_IWUSR; |
| 496 #if defined(OS_CHROMEOS) | 522 #if defined(OS_CHROMEOS) |
| 497 mode |= S_IRGRP | S_IROTH; | 523 mode |= S_IRGRP | S_IROTH; |
| 498 #endif | 524 #endif |
| 499 | 525 |
| 500 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); | 526 int descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode)); |
| 501 | 527 |
| 502 if (flags & FLAG_OPEN_ALWAYS) { | 528 if (flags & FLAG_OPEN_ALWAYS) { |
| 503 if (descriptor < 0) { | 529 if (descriptor < 0) { |
| 504 open_flags |= O_CREAT; | 530 open_flags |= O_CREAT; |
| 505 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) | 531 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
| 506 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW | 532 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
| 507 | 533 |
| 508 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); | 534 descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode)); |
| 509 if (descriptor >= 0) | 535 if (descriptor >= 0) |
| 510 created_ = true; | 536 created_ = true; |
| 511 } | 537 } |
| 512 } | 538 } |
| 513 | 539 |
| 514 if (descriptor < 0) { | 540 if (descriptor < 0) { |
| 515 error_details_ = File::OSErrorToFileError(errno); | 541 error_details_ = File::OSErrorToFileError(errno); |
| 516 return; | 542 return; |
| 517 } | 543 } |
| 518 | 544 |
| 519 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 545 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
| 520 created_ = true; | 546 created_ = true; |
| 521 | 547 |
| 522 if (flags & FLAG_DELETE_ON_CLOSE) | 548 if (flags & FLAG_DELETE_ON_CLOSE) |
| 523 unlink(name.value().c_str()); | 549 unlink(path_.value().c_str()); |
| 524 | 550 |
| 525 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 551 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 526 error_details_ = FILE_OK; | 552 error_details_ = FILE_OK; |
| 527 file_.reset(descriptor); | 553 file_.reset(descriptor); |
| 528 } | 554 } |
| 529 #endif // !defined(OS_NACL) | 555 #endif // !defined(OS_NACL) |
| 530 | 556 |
| 531 bool File::DoFlush() { | 557 bool File::DoFlush() { |
| 532 ThreadRestrictions::AssertIOAllowed(); | 558 ThreadRestrictions::AssertIOAllowed(); |
| 533 DCHECK(IsValid()); | 559 DCHECK(IsValid()); |
| 560 |
| 534 #if defined(OS_NACL) | 561 #if defined(OS_NACL) |
| 535 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. | 562 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 536 return true; | 563 return true; |
| 537 #elif defined(OS_LINUX) || defined(OS_ANDROID) | 564 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
| 538 return !HANDLE_EINTR(fdatasync(file_.get())); | 565 return !HANDLE_EINTR(fdatasync(file_.get())); |
| 539 #else | 566 #else |
| 540 return !HANDLE_EINTR(fsync(file_.get())); | 567 return !HANDLE_EINTR(fsync(file_.get())); |
| 541 #endif | 568 #endif |
| 542 } | 569 } |
| 543 | 570 |
| 544 void File::SetPlatformFile(PlatformFile file) { | 571 void File::SetPlatformFile(PlatformFile file) { |
| 545 DCHECK(!file_.is_valid()); | 572 DCHECK(!file_.is_valid()); |
| 546 file_.reset(file); | 573 file_.reset(file); |
| 547 } | 574 } |
| 548 | 575 |
| 549 } // namespace base | 576 } // namespace base |
| OLD | NEW |