1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "base/files/file.h"
#include "base/notimplemented.h"
// The only 32-bit platform that uses this file is Android. On Android APIs
// >= 21, this standard define is the right way to express that you want a
// 64-bit offset in struct stat, and the stat64 struct and functions aren't
// useful.
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
static_assert(sizeof(base::stat_wrapper_t::st_size) >= 8);
#include <atomic>
#include <optional>
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/content_uri_utils.h"
#include "base/os_compat_android.h"
#endif
namespace base {
// Make sure our Whence mappings match the system headers.
static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR &&
File::FROM_END == SEEK_END,
"whence mapping must match the system headers");
namespace {
#if BUILDFLAG(IS_APPLE)
// When enabled, `F_FULLFSYNC` is not used in `File::Flush`. Instead,
// `F_BARRIERFSYNC` or `flush()` is used (depending on the
// "MacEfficientFileFlushUseBarrier" param). See
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
BASE_FEATURE(kMacEfficientFileFlush,
"MacEfficientFileFlush",
base::FEATURE_ENABLED_BY_DEFAULT);
const FeatureParam<bool> kMacEfficientFileFlushUseBarrier{
&kMacEfficientFileFlush, "MacEfficientFileFlushUseBarrier", true};
enum class MacFileFlushMechanism {
kFlush,
kFullFsync,
kBarrierFsync,
};
std::atomic<MacFileFlushMechanism> g_mac_file_flush_mechanism{
MacFileFlushMechanism::kFullFsync};
#endif // BUILDFLAG(IS_APPLE)
// NaCl doesn't provide the following system calls, so either simulate them or
// wrap them in order to minimize the number of #ifdef's in this file.
#if !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)
bool IsOpenAppend(PlatformFile file) {
return (fcntl(file, F_GETFL) & O_APPEND) != 0;
}
int CallFtruncate(PlatformFile file, int64_t length) {
#if BUILDFLAG(IS_BSD) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_FUCHSIA)
static_assert(sizeof(off_t) >= sizeof(int64_t),
"off_t is not a 64-bit integer");
return HANDLE_EINTR(ftruncate(file, length));
#else
return HANDLE_EINTR(ftruncate64(file, length));
#endif
}
int CallFutimes(PlatformFile file, const struct timeval times[2]) {
#ifdef __USE_XOPEN2K8
// futimens should be available, but futimes might not be
// http://pubs.opengroup.org/onlinepubs/9699919799/
timespec ts_times[2];
ts_times[0].tv_sec = times[0].tv_sec;
ts_times[0].tv_nsec = times[0].tv_usec * 1000;
ts_times[1].tv_sec = times[1].tv_sec;
ts_times[1].tv_nsec = times[1].tv_usec * 1000;
return futimens(file, ts_times);
#else
#pragma clang diagnostic push // Can be removed once Cronet's min-sdk is >= 26.
#pragma clang diagnostic ignored "-Wunguarded-availability"
return futimes(file, times);
#pragma clang diagnostic pop
#endif
}
#if !BUILDFLAG(IS_FUCHSIA)
short FcntlFlockType(std::optional<File::LockMode> mode) {
if (!mode.has_value()) {
return F_UNLCK;
}
switch (mode.value()) {
case File::LockMode::kShared:
return F_RDLCK;
case File::LockMode::kExclusive:
return F_WRLCK;
}
NOTREACHED();
}
File::Error CallFcntlFlock(PlatformFile file,
std::optional<File::LockMode> mode) {
struct flock lock;
lock.l_type = FcntlFlockType(std::move(mode));
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0; // Lock entire file.
if (HANDLE_EINTR(fcntl(file, F_SETLK, &lock)) == -1) {
return File::GetLastFileError();
}
return File::FILE_OK;
}
#endif
#else // BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)
bool IsOpenAppend(PlatformFile file) {
// NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX
// standard and always appends if the file is opened with O_APPEND, just
// return false here.
return false;
}
int CallFtruncate(PlatformFile file, int64_t length) {
NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate.
return 0;
}
int CallFutimes(PlatformFile file, const struct timeval times[2]) {
NOTIMPLEMENTED(); // NaCl doesn't implement futimes.
return 0;
}
File::Error CallFcntlFlock(PlatformFile file,
std::optional<File::LockMode> mode) {
NOTIMPLEMENTED(); // NaCl doesn't implement flock struct.
return File::FILE_ERROR_INVALID_OPERATION;
}
#endif // BUILDFLAG(IS_NACL)
#if BUILDFLAG(IS_ANDROID)
bool GetContentUriInfo(const base::FilePath& path, File::Info* info) {
FileEnumerator::FileInfo file_info;
bool result = internal::ContentUriGetFileInfo(path, &file_info);
if (result) {
info->FromStat(file_info.stat());
}
return result;
}
#endif
} // namespace
void File::Info::FromStat(const stat_wrapper_t& stat_info) {
is_directory = S_ISDIR(stat_info.st_mode);
is_symbolic_link = S_ISLNK(stat_info.st_mode);
size = stat_info.st_size;
// Get last modification time, last access time, and creation time from
// |stat_info|.
// Note: st_ctime is actually last status change time when the inode was last
// updated, which happens on any metadata change. It is not the file's
// creation time. However, other than on Mac & iOS where the actual file
// creation time is included as st_birthtime, the rest of POSIX platforms have
// no portable way to get the creation time.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
time_t last_modified_sec = stat_info.st_mtim.tv_sec;
int64_t last_modified_nsec = stat_info.st_mtim.tv_nsec;
time_t last_accessed_sec = stat_info.st_atim.tv_sec;
int64_t last_accessed_nsec = stat_info.st_atim.tv_nsec;
time_t creation_time_sec = stat_info.st_ctim.tv_sec;
int64_t creation_time_nsec = stat_info.st_ctim.tv_nsec;
#elif BUILDFLAG(IS_ANDROID)
time_t last_modified_sec = stat_info.st_mtime;
int64_t last_modified_nsec = stat_info.st_mtime_nsec;
time_t last_accessed_sec = stat_info.st_atime;
int64_t last_accessed_nsec = stat_info.st_atime_nsec;
time_t creation_time_sec = stat_info.st_ctime;
int64_t creation_time_nsec = stat_info.st_ctime_nsec;
#elif BUILDFLAG(IS_APPLE)
time_t last_modified_sec = stat_info.st_mtimespec.tv_sec;
int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec;
time_t last_accessed_sec = stat_info.st_atimespec.tv_sec;
int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec;
time_t creation_time_sec = stat_info.st_birthtimespec.tv_sec;
int64_t creation_time_nsec = stat_info.st_birthtimespec.tv_nsec;
#elif BUILDFLAG(IS_BSD)
time_t last_modified_sec = stat_info.st_mtimespec.tv_sec;
int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec;
time_t last_accessed_sec = stat_info.st_atimespec.tv_sec;
int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec;
time_t creation_time_sec = stat_info.st_ctimespec.tv_sec;
int64_t creation_time_nsec = stat_info.st_ctimespec.tv_nsec;
#else
time_t last_modified_sec = stat_info.st_mtime;
int64_t last_modified_nsec = 0;
time_t last_accessed_sec = stat_info.st_atime;
int64_t last_accessed_nsec = 0;
time_t creation_time_sec = stat_info.st_ctime;
int64_t creation_time_nsec = 0;
#endif
last_modified =
Time::FromTimeT(last_modified_sec) +
Microseconds(last_modified_nsec / Time::kNanosecondsPerMicrosecond);
last_accessed =
Time::FromTimeT(last_accessed_sec) +
Microseconds(last_accessed_nsec / Time::kNanosecondsPerMicrosecond);
creation_time =
Time::FromTimeT(creation_time_sec) +
Microseconds(creation_time_nsec / Time::kNanosecondsPerMicrosecond);
}
bool File::IsValid() const {
return file_.is_valid();
}
PlatformFile File::GetPlatformFile() const {
return file_.get();
}
PlatformFile File::TakePlatformFile() {
return file_.release();
}
void File::Close() {
if (!IsValid()) {
return;
}
SCOPED_FILE_TRACE("Close");
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
#if BUILDFLAG(IS_ANDROID)
if (java_parcel_file_descriptor_) {
internal::ContentUriClose(java_parcel_file_descriptor_);
}
#endif
file_.reset();
}
int64_t File::Seek(Whence whence, int64_t offset) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset);
#if BUILDFLAG(IS_ANDROID)
static_assert(sizeof(int64_t) == sizeof(off64_t), "off64_t must be 64 bits");
return lseek64(file_.get(), static_cast<off64_t>(offset),
static_cast<int>(whence));
#else
static_assert(sizeof(int64_t) == sizeof(off_t), "off_t must be 64 bits");
return lseek(file_.get(), static_cast<off_t>(offset),
static_cast<int>(whence));
#endif
}
int File::Read(int64_t offset, char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0 || !IsValueInRangeForNumericType<off_t>(offset + size - 1)) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("Read", size);
int bytes_read = 0;
long rv;
do {
rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read,
static_cast<size_t>(size - bytes_read),
static_cast<off_t>(offset + bytes_read)));
if (rv <= 0) {
break;
}
bytes_read += rv;
} while (bytes_read < size);
return bytes_read ? bytes_read : checked_cast<int>(rv);
}
int File::ReadAtCurrentPos(char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size);
int bytes_read = 0;
long rv;
do {
rv = HANDLE_EINTR(read(file_.get(), data + bytes_read,
static_cast<size_t>(size - bytes_read)));
if (rv <= 0) {
break;
}
bytes_read += rv;
} while (bytes_read < size);
return bytes_read ? bytes_read : checked_cast<int>(rv);
}
int File::ReadNoBestEffort(int64_t offset, char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0 || !IsValueInRangeForNumericType<off_t>(offset)) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size);
return checked_cast<int>(
HANDLE_EINTR(pread(file_.get(), data, static_cast<size_t>(size),
static_cast<off_t>(offset))));
}
int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPosNoBestEffort", size);
return checked_cast<int>(
HANDLE_EINTR(read(file_.get(), data, static_cast<size_t>(size))));
}
int File::Write(int64_t offset, const char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
if (IsOpenAppend(file_.get())) {
return WriteAtCurrentPos(data, size);
}
DCHECK(IsValid());
if (size < 0) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("Write", size);
int bytes_written = 0;
long rv;
do {
#if BUILDFLAG(IS_ANDROID)
// In case __USE_FILE_OFFSET64 is not used, we need to call pwrite64()
// instead of pwrite().
static_assert(sizeof(int64_t) == sizeof(off64_t),
"off64_t must be 64 bits");
rv = HANDLE_EINTR(pwrite64(file_.get(), data + bytes_written,
static_cast<size_t>(size - bytes_written),
offset + bytes_written));
#else
rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written,
static_cast<size_t>(size - bytes_written),
offset + bytes_written));
#endif
if (rv <= 0) {
break;
}
bytes_written += rv;
} while (bytes_written < size);
return bytes_written ? bytes_written : checked_cast<int>(rv);
}
int File::WriteAtCurrentPos(const char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size);
int bytes_written = 0;
long rv;
do {
rv = HANDLE_EINTR(write(file_.get(), data + bytes_written,
static_cast<size_t>(size - bytes_written)));
if (rv <= 0) {
break;
}
bytes_written += rv;
} while (bytes_written < size);
return bytes_written ? bytes_written : checked_cast<int>(rv);
}
int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
if (size < 0) {
return -1;
}
SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size);
return checked_cast<int>(
HANDLE_EINTR(write(file_.get(), data, static_cast<size_t>(size))));
}
int64_t File::GetLength() const {
DCHECK(IsValid());
SCOPED_FILE_TRACE("GetLength");
Info info;
if (!GetInfo(&info)) {
return -1;
}
return info.size;
}
bool File::SetLength(int64_t length) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length);
return !CallFtruncate(file_.get(), length);
}
bool File::SetTimes(Time last_access_time, Time last_modified_time) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
SCOPED_FILE_TRACE("SetTimes");
timeval times[2];
times[0] = last_access_time.ToTimeVal();
times[1] = last_modified_time.ToTimeVal();
return !CallFutimes(file_.get(), times);
}
bool File::GetInfo(Info* info) const {
DCHECK(IsValid());
SCOPED_FILE_TRACE("GetInfo");
stat_wrapper_t file_info;
bool success = (Fstat(file_.get(), &file_info) == 0);
if (success) {
info->FromStat(file_info);
}
#if BUILDFLAG(IS_ANDROID)
if (path_.IsContentUri()) {
// Content-URIs may represent files on the local disk, or may be virtual
// files backed by a ContentProvider which may or may not use FUSE to back
// the FDs.
//
// For Document URIs, always use ContentUriGetFileInfo() since it will
// succeed by using the Java API DocumentFile, which can provide
// last-modified where FUSE cannot. FUSE always returns the current-time
// which is problematic because Blobs are registered with an
// expected-last-modified, and will fail if it changes by the time a client
// accesses it.
//
// For other Content-URIS, if fstat() succeeded with a non-zero size, then
// use the result, otherwise try via the Java APIs.
return (success && info->size > 0 && !internal::IsDocumentUri(path_)) ||
GetContentUriInfo(path_, info);
}
#endif
return success;
}
#if !BUILDFLAG(IS_FUCHSIA)
File::Error File::Lock(File::LockMode mode) {
SCOPED_FILE_TRACE("Lock");
return CallFcntlFlock(file_.get(), mode);
}
File::Error File::Unlock() {
SCOPED_FILE_TRACE("Unlock");
return CallFcntlFlock(file_.get(), std::optional<File::LockMode>());
}
#endif
File File::Duplicate() const {
if (!IsValid()) {
return File();
}
SCOPED_FILE_TRACE("Duplicate");
ScopedPlatformFile other_fd(HANDLE_EINTR(dup(GetPlatformFile())));
if (!other_fd.is_valid()) {
return File(File::GetLastFileError());
}
return File(std::move(other_fd), async());
}
#if BUILDFLAG(IS_APPLE)
void File::InitializeFeatures() {
if (FeatureList::IsEnabled(kMacEfficientFileFlush)) {
// "relaxed" because there is no dependency between these memory operations
// and other memory operations.
if (kMacEfficientFileFlushUseBarrier.Get()) {
g_mac_file_flush_mechanism.store(MacFileFlushMechanism::kBarrierFsync,
std::memory_order_relaxed);
} else {
g_mac_file_flush_mechanism.store(MacFileFlushMechanism::kFlush,
std::memory_order_relaxed);
}
}
}
#endif // BUILDFLAG(IS_APPLE)
// Static.
File::Error File::OSErrorToFileError(int saved_errno) {
switch (saved_errno) {
case EACCES:
case EISDIR:
case EROFS:
case EPERM:
return FILE_ERROR_ACCESS_DENIED;
case EBUSY:
#if !BUILDFLAG(IS_NACL) // ETXTBSY not defined by NaCl.
case ETXTBSY:
#endif
return FILE_ERROR_IN_USE;
case EEXIST:
return FILE_ERROR_EXISTS;
case EIO:
return FILE_ERROR_IO;
case ENOENT:
return FILE_ERROR_NOT_FOUND;
case ENFILE: // fallthrough
case EMFILE:
return FILE_ERROR_TOO_MANY_OPENED;
case ENOMEM:
return FILE_ERROR_NO_MEMORY;
case ENOSPC:
return FILE_ERROR_NO_SPACE;
case ENOTDIR:
return FILE_ERROR_NOT_A_DIRECTORY;
default:
// This function should only be called for errors.
DCHECK_NE(0, saved_errno);
return FILE_ERROR_FAILED;
}
}
// NaCl doesn't implement system calls to open files directly.
#if !BUILDFLAG(IS_NACL)
// TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
void File::DoInitialize(const FilePath& path, uint32_t flags) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(!IsValid());
int open_flags = 0;
if (flags & FLAG_CREATE) {
open_flags = O_CREAT | O_EXCL;
}
created_ = false;
if (flags & FLAG_CREATE_ALWAYS) {
DCHECK(!open_flags);
DCHECK(flags & FLAG_WRITE);
open_flags = O_CREAT | O_TRUNC;
}
if (flags & FLAG_OPEN_TRUNCATED) {
DCHECK(!open_flags);
DCHECK(flags & FLAG_WRITE);
open_flags = O_TRUNC;
}
if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
NOTREACHED();
}
if (flags & FLAG_WRITE && flags & FLAG_READ) {
open_flags |= O_RDWR;
} else if (flags & FLAG_WRITE) {
open_flags |= O_WRONLY;
} else if (!(flags & FLAG_READ) && !(flags & FLAG_WRITE_ATTRIBUTES) &&
!(flags & FLAG_APPEND) && !(flags & FLAG_OPEN_ALWAYS)) {
// Note: For FLAG_WRITE_ATTRIBUTES and no other read/write flags, we'll
// open the file in O_RDONLY mode (== 0, see static_assert below), so that
// we get a fd that can be used for SetTimes().
NOTREACHED();
}
if (flags & FLAG_TERMINAL_DEVICE) {
open_flags |= O_NOCTTY | O_NDELAY;
}
if (flags & FLAG_APPEND && flags & FLAG_READ) {
open_flags |= O_APPEND | O_RDWR;
} else if (flags & FLAG_APPEND) {
open_flags |= O_APPEND | O_WRONLY;
}
static_assert(O_RDONLY == 0, "O_RDONLY must equal zero");
mode_t mode = S_IRUSR | S_IWUSR;
#if BUILDFLAG(IS_CHROMEOS)
mode |= S_IRGRP | S_IROTH;
#endif
#if BUILDFLAG(IS_ANDROID)
if (path.IsContentUri()) {
java_parcel_file_descriptor_ = internal::OpenContentUri(path, flags);
int fd = internal::ContentUriGetFd(java_parcel_file_descriptor_);
if (fd < 0) {
error_details_ = FILE_ERROR_FAILED;
return;
}
// Save path for any call to GetInfo().
path_ = path;
created_ = (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE));
async_ = (flags & FLAG_ASYNC);
error_details_ = FILE_OK;
file_.reset(fd);
return;
}
#endif
int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
if (flags & FLAG_OPEN_ALWAYS) {
if (descriptor < 0) {
open_flags |= O_CREAT;
descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
if (descriptor >= 0) {
created_ = true;
}
}
}
if (descriptor < 0) {
error_details_ = File::GetLastFileError();
return;
}
if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) {
created_ = true;
}
if (flags & FLAG_DELETE_ON_CLOSE) {
unlink(path.value().c_str());
}
async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
error_details_ = FILE_OK;
file_.reset(descriptor);
}
#endif // !BUILDFLAG(IS_NACL)
bool File::Flush() {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
DCHECK(IsValid());
SCOPED_FILE_TRACE("Flush");
#if BUILDFLAG(IS_NACL)
NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
return true;
#elif BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
return !HANDLE_EINTR(fdatasync(file_.get()));
#elif BUILDFLAG(IS_APPLE)
// On macOS and iOS, fsync() is guaranteed to send the file's data to the
// underlying storage device, but may return before the device actually writes
// the data to the medium. When used by database systems, this may result in
// unexpected data loss. Depending on experiment state, this function may use
// F_BARRIERFSYNC or F_FULLFSYNC to provide stronger guarantees than fsync().
//
// See documentation:
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
//
// "relaxed" because there is no dependency between this memory operation and
// other memory operations.
switch (g_mac_file_flush_mechanism.load(std::memory_order_relaxed)) {
case MacFileFlushMechanism::kBarrierFsync: {
if (!HANDLE_EINTR(fcntl(file_.get(), F_BARRIERFSYNC))) {
return true;
}
// Fall back to `fsync()` in case of failure.
break;
}
case MacFileFlushMechanism::kFullFsync: {
if (!HANDLE_EINTR(fcntl(file_.get(), F_FULLFSYNC))) {
return true;
}
// Fall back to `fsync()` in case of failure.
break;
}
case MacFileFlushMechanism::kFlush: {
// Fall back to `fsync()`.
break;
}
}
// `fsync()` if `F_BARRIERFSYNC` or `F_FULLFSYNC` failed, or if the mechanism
// is `kFlush`. Some file systems do not support `F_FULLFSYNC` /
// `F_BARRIERFSYNC` but we cannot use the error code as a definitive indicator
// that it's the case, so we'll keep trying `F_FULLFSYNC` / `F_BARRIERFSYNC`
// for every call to this method when it's the case. See the CL description at
// https://crrev.com/c/1400159 for details.
return !HANDLE_EINTR(fsync(file_.get()));
#else
return !HANDLE_EINTR(fsync(file_.get()));
#endif
}
void File::SetPlatformFile(PlatformFile file) {
DCHECK(!file_.is_valid());
file_.reset(file);
}
// static
File::Error File::GetLastFileError() {
return base::File::OSErrorToFileError(errno);
}
int File::Stat(const FilePath& path, stat_wrapper_t* sb) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
#if BUILDFLAG(IS_ANDROID)
if (path.IsContentUri()) {
// Attempt to open the file and call GetInfo(), otherwise call Java code
// with the path which is required for dirs.
File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
Info info;
if ((file.IsValid() && file.GetInfo(&info)) ||
GetContentUriInfo(path, &info)) {
memset(sb, 0, sizeof(*sb));
sb->st_mode = info.is_directory ? S_IFDIR : S_IFREG;
sb->st_size = info.size;
sb->st_mtime = info.last_modified.ToTimeT();
// Time internally is stored as microseconds since windows epoch, so first
// get subsecond time, and then convert to nanos. Do not subtract
// Time::UnixEpoch() (which is a little bigger than 2^53), or convert to
// nanos (multiply by 10^3 which is just under 2^10) prior to doing
// modulo as these can cause overflow / clamping at [-2^63, 2^63) which
// will corrupt the result.
sb->st_mtime_nsec =
(info.last_modified.ToDeltaSinceWindowsEpoch().InMicroseconds() %
Time::kMicrosecondsPerSecond) *
Time::kNanosecondsPerMicrosecond;
return 0;
}
}
#endif
return stat(path.value().c_str(), sb);
}
int File::Fstat(int fd, stat_wrapper_t* sb) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
return fstat(fd, sb);
}
int File::Lstat(const FilePath& path, stat_wrapper_t* sb) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
return lstat(path.value().c_str(), sb);
}
} // namespace base
|