| 6 |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stdint.h> |
| 10 | #include <unistd.h> |
| 11 |
| 12 | #include "base/logging.h" |
| 13 | #include "base/metrics/histogram_functions.h" |
| 14 | #include "base/posix/eintr_wrapper.h" |
| 15 | #include "base/strings/utf_string_conversions.h" |
| 16 | #include "base/threading/scoped_blocking_call.h" |
| 17 | #include "build/build_config.h" |
| 18 |
| 19 | #if defined(OS_ANDROID) |
| 20 | #include "base/os_compat_android.h" |
| 21 | #endif |
| 22 |
| 23 | namespace base { |
| 24 |
| 25 | // Make sure our Whence mappings match the system headers. |
| 26 | static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR && |
| 27 | File::FROM_END == SEEK_END, |
| 28 | "whence mapping must match the system headers"); |
| 29 |
| 30 | namespace { |
| 31 |
| 32 | // NaCl doesn't provide the following system calls, so either simulate them or |
| 33 | // wrap them in order to minimize the number of #ifdef's in this file. |
| 34 | #if !defined(OS_NACL) && !defined(OS_AIX) |
| 35 | bool IsOpenAppend(PlatformFile file) { |
| 36 | return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
| 37 | } |
| 38 |
| 39 | int CallFtruncate(PlatformFile file, int64_t length) { |
| 40 | return HANDLE_EINTR(ftruncate(file, length)); |
| 41 | } |
| 42 |
| 43 | int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 44 | #ifdef __USE_XOPEN2K8 |
| 45 | // futimens should be available, but futimes might not be |
| 46 | // http://pubs.opengroup.org/onlinepubs/9699919799/ |
| 47 |
| 48 | timespec ts_times[2]; |
| 49 | ts_times[0].tv_sec = times[0].tv_sec; |
| 50 | ts_times[0].tv_nsec = times[0].tv_usec * 1000; |
| 51 | ts_times[1].tv_sec = times[1].tv_sec; |
| 52 | ts_times[1].tv_nsec = times[1].tv_usec * 1000; |
| 53 |
| 54 | return futimens(file, ts_times); |
| 55 | #else |
| 56 | return futimes(file, times); |
| 57 | #endif |
| 58 | } |
| 59 |
| 60 | #if !defined(OS_FUCHSIA) |
| 61 | short FcntlFlockType(base::Optional<File::LockMode> mode) { |
| 62 | if (!mode.has_value()) |
| 63 | return F_UNLCK; |
| 64 | switch (mode.value()) { |
| 65 | case File::LockMode::kShared: |
| 66 | return F_RDLCK; |
| 67 | case File::LockMode::kExclusive: |
| 68 | return F_WRLCK; |
| 69 | } |
| 70 | NOTREACHED(); |
| 71 | } |
| 72 |
| 73 | File::Error CallFcntlFlock(PlatformFile file, |
| 74 | base::Optional<File::LockMode> mode) { |
| 75 | struct flock lock; |
| 76 | lock.l_type = FcntlFlockType(std::move(mode)); |
| 77 | lock.l_whence = SEEK_SET; |
| 78 | lock.l_start = 0; |
| 79 | lock.l_len = 0; // Lock entire file. |
| 80 | if (HANDLE_EINTR(fcntl(file, F_SETLK, &lock)) == -1) |
| 81 | return File::GetLastFileError(); |
| 82 | return File::FILE_OK; |
| 83 | } |
| 84 | #endif |
| 85 |
| 86 | #else // defined(OS_NACL) && !defined(OS_AIX) |
| 87 |
| 88 | bool IsOpenAppend(PlatformFile file) { |
| 89 | // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX |
| 90 | // standard and always appends if the file is opened with O_APPEND, just |
| 91 | // return false here. |
| 92 | return false; |
| 93 | } |
| 94 |
| 95 | int CallFtruncate(PlatformFile file, int64_t length) { |
| 96 | NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
| 97 | return 0; |
| 98 | } |
| 99 |
| 100 | int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 101 | NOTIMPLEMENTED(); // NaCl doesn't implement futimes. |
| 102 | return 0; |
| 103 | } |
| 104 |
| 105 | File::Error CallFcntlFlock(PlatformFile file, |
| 106 | base::Optional<File::LockMode> mode) { |
| 107 | NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
| 108 | return File::FILE_ERROR_INVALID_OPERATION; |
| 109 | } |
| 110 | #endif // defined(OS_NACL) |
| 111 |
| 112 | } // namespace |
| 113 |
| 114 | void File::Info::FromStat(const stat_wrapper_t& stat_info) { |
| 115 | is_directory = S_ISDIR(stat_info.st_mode); |
| 116 | is_symbolic_link = S_ISLNK(stat_info.st_mode); |
| 117 | size = stat_info.st_size; |
| 118 |
| 119 | // Get last modification time, last access time, and creation time from |
| 120 | // |stat_info|. |
| 121 | // Note: st_ctime is actually last status change time when the inode was last |
| 122 | // updated, which happens on any metadata change. It is not the file's |
| 123 | // creation time. However, other than on Mac & iOS where the actual file |
| 124 | // creation time is included as st_birthtime, the rest of POSIX platforms have |
| 125 | // no portable way to get the creation time. |
| 126 | #if defined(OS_LINUX) || defined(OS_FUCHSIA) |
| 127 | time_t last_modified_sec = stat_info.st_mtim.tv_sec; |
| 128 | int64_t last_modified_nsec = stat_info.st_mtim.tv_nsec; |
| 129 | time_t last_accessed_sec = stat_info.st_atim.tv_sec; |
| 130 | int64_t last_accessed_nsec = stat_info.st_atim.tv_nsec; |
| 131 | time_t creation_time_sec = stat_info.st_ctim.tv_sec; |
| 132 | int64_t creation_time_nsec = stat_info.st_ctim.tv_nsec; |
| 133 | #elif defined(OS_ANDROID) |
| 134 | time_t last_modified_sec = stat_info.st_mtime; |
| 135 | int64_t last_modified_nsec = stat_info.st_mtime_nsec; |
| 136 | time_t last_accessed_sec = stat_info.st_atime; |
| 137 | int64_t last_accessed_nsec = stat_info.st_atime_nsec; |
| 138 | time_t creation_time_sec = stat_info.st_ctime; |
| 139 | int64_t creation_time_nsec = stat_info.st_ctime_nsec; |
| 140 | #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 141 | time_t last_modified_sec = stat_info.st_mtimespec.tv_sec; |
| 142 | int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec; |
| 143 | time_t last_accessed_sec = stat_info.st_atimespec.tv_sec; |
| 144 | int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec; |
| 145 | time_t creation_time_sec = stat_info.st_birthtimespec.tv_sec; |
| 146 | int64_t creation_time_nsec = stat_info.st_birthtimespec.tv_nsec; |
| 147 | #elif defined(OS_BSD) |
| 148 | time_t last_modified_sec = stat_info.st_mtimespec.tv_sec; |
| 149 | int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec; |
| 150 | time_t last_accessed_sec = stat_info.st_atimespec.tv_sec; |
| 151 | int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec; |
| 152 | time_t creation_time_sec = stat_info.st_ctimespec.tv_sec; |
| 153 | int64_t creation_time_nsec = stat_info.st_ctimespec.tv_nsec; |
| 154 | #else |
| 155 | time_t last_modified_sec = stat_info.st_mtime; |
| 156 | int64_t last_modified_nsec = 0; |
| 157 | time_t last_accessed_sec = stat_info.st_atime; |
| 158 | int64_t last_accessed_nsec = 0; |
| 159 | time_t creation_time_sec = stat_info.st_ctime; |
| 160 | int64_t creation_time_nsec = 0; |
| 161 | #endif |
| 162 |
| 163 | last_modified = |
| 164 | Time::FromTimeT(last_modified_sec) + |
| 165 | TimeDelta::FromMicroseconds(last_modified_nsec / |
| 166 | Time::kNanosecondsPerMicrosecond); |
| 167 |
| 168 | last_accessed = |
| 169 | Time::FromTimeT(last_accessed_sec) + |
| 170 | TimeDelta::FromMicroseconds(last_accessed_nsec / |
| 171 | Time::kNanosecondsPerMicrosecond); |
| 172 |
| 173 | creation_time = |
| 174 | Time::FromTimeT(creation_time_sec) + |
| 175 | TimeDelta::FromMicroseconds(creation_time_nsec / |
| 176 | Time::kNanosecondsPerMicrosecond); |
| 177 | } |
| 178 |
| 179 | bool File::IsValid() const { |
| 180 | return file_.is_valid(); |
| 181 | } |
| 182 |
| 183 | PlatformFile File::GetPlatformFile() const { |
| 184 | return file_.get(); |
| 185 | } |
| 186 |
| 187 | PlatformFile File::TakePlatformFile() { |
| 188 | return file_.release(); |
| 189 | } |
| 190 |
| 191 | void File::Close() { |
| 192 | if (!IsValid()) |
| 193 | return; |
| 194 |
| 195 | SCOPED_FILE_TRACE("Close"); |
| 196 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 197 | file_.reset(); |
| 198 | } |
| 199 |
| 200 | int64_t File::Seek(Whence whence, int64_t offset) { |
| 201 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 202 | DCHECK(IsValid()); |
| 203 |
| 204 | SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); |
| 205 |
| 206 | #if defined(OS_ANDROID) |
| 207 | static_assert(sizeof(int64_t) == sizeof(off64_t), "off64_t must be 64 bits"); |
| 208 | return lseek64(file_.get(), static_cast<off64_t>(offset), |
| 209 | static_cast<int>(whence)); |
| 210 | #else |
| 211 | static_assert(sizeof(int64_t) == sizeof(off_t), "off_t must be 64 bits"); |
| 212 | return lseek(file_.get(), static_cast<off_t>(offset), |
| 213 | static_cast<int>(whence)); |
| 214 | #endif |
| 215 | } |
| 216 |
| 217 | int File::Read(int64_t offset, char* data, int size) { |
| 218 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 219 | DCHECK(IsValid()); |
| 220 | if (size < 0) |
| 221 | return -1; |
| 222 |
| 223 | SCOPED_FILE_TRACE_WITH_SIZE("Read", size); |
| 224 |
| 225 | int bytes_read = 0; |
| 226 | int rv; |
| 227 | do { |
| 228 | rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
| 229 | size - bytes_read, offset + bytes_read)); |
| 230 | if (rv <= 0) |
| 231 | break; |
| 232 |
| 233 | bytes_read += rv; |
| 234 | } while (bytes_read < size); |
| 235 |
| 236 | return bytes_read ? bytes_read : rv; |
| 237 | } |
| 238 |
| 239 | int File::ReadAtCurrentPos(char* data, int size) { |
| 240 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 241 | DCHECK(IsValid()); |
| 242 | if (size < 0) |
| 243 | return -1; |
| 244 |
| 245 | SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size); |
| 246 |
| 247 | int bytes_read = 0; |
| 248 | int rv; |
| 249 | do { |
| 250 | rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); |
| 251 | if (rv <= 0) |
| 252 | break; |
| 253 |
| 254 | bytes_read += rv; |
| 255 | } while (bytes_read < size); |
| 256 |
| 257 | return bytes_read ? bytes_read : rv; |
| 258 | } |
| 259 |
| 260 | int File::ReadNoBestEffort(int64_t offset, char* data, int size) { |
| 261 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 262 | DCHECK(IsValid()); |
| 263 | SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size); |
| 264 | return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
| 265 | } |
| 266 |
| 267 | int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
| 268 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 269 | DCHECK(IsValid()); |
| 270 | if (size < 0) |
| 271 | return -1; |
| 272 |
| 273 | SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPosNoBestEffort", size); |
| 274 | return HANDLE_EINTR(read(file_.get(), data, size)); |
| 275 | } |
| 276 |
| 277 | int File::Write(int64_t offset, const char* data, int size) { |
| 278 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 279 |
| 280 | if (IsOpenAppend(file_.get())) |
| 281 | return WriteAtCurrentPos(data, size); |
| 282 |
| 283 | DCHECK(IsValid()); |
| 284 | if (size < 0) |
| 285 | return -1; |
| 286 |
| 287 | SCOPED_FILE_TRACE_WITH_SIZE("Write", size); |
| 288 |
| 289 | int bytes_written = 0; |
| 290 | int rv; |
| 291 | do { |
| 292 | #if defined(OS_ANDROID) |
| 293 | // In case __USE_FILE_OFFSET64 is not used, we need to call pwrite64() |
| 294 | // instead of pwrite(). |
| 295 | static_assert(sizeof(int64_t) == sizeof(off64_t), |
| 296 | "off64_t must be 64 bits"); |
| 297 | rv = HANDLE_EINTR(pwrite64(file_.get(), data + bytes_written, |
| 298 | size - bytes_written, offset + bytes_written)); |
| 299 | #else |
| 300 | rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
| 301 | size - bytes_written, offset + bytes_written)); |
| 302 | #endif |
| 303 | if (rv <= 0) |
| 304 | break; |
| 305 |
| 306 | bytes_written += rv; |
| 307 | } while (bytes_written < size); |
| 308 |
| 309 | return bytes_written ? bytes_written : rv; |
| 310 | } |
| 311 |
| 312 | int File::WriteAtCurrentPos(const char* data, int size) { |
| 313 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 314 | DCHECK(IsValid()); |
| 315 | if (size < 0) |
| 316 | return -1; |
| 317 |
| 318 | SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size); |
| 319 |
| 320 | int bytes_written = 0; |
| 321 | int rv; |
| 322 | do { |
| 323 | rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
| 324 | size - bytes_written)); |
| 325 | if (rv <= 0) |
| 326 | break; |
| 327 |
| 328 | bytes_written += rv; |
| 329 | } while (bytes_written < size); |
| 330 |
| 331 | return bytes_written ? bytes_written : rv; |
| 332 | } |
| 333 |
| 334 | int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
| 335 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 336 | DCHECK(IsValid()); |
| 337 | if (size < 0) |
| 338 | return -1; |
| 339 |
| 340 | SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size); |
| 341 | return HANDLE_EINTR(write(file_.get(), data, size)); |
| 342 | } |
| 343 |
| 344 | int64_t File::GetLength() { |
| 345 | DCHECK(IsValid()); |
| 346 |
| 347 | SCOPED_FILE_TRACE("GetLength"); |
| 348 |
| 349 | stat_wrapper_t file_info; |
| 350 | if (Fstat(file_.get(), &file_info)) |
| 351 | return -1; |
| 352 |
| 353 | return file_info.st_size; |
| 354 | } |
| 355 |
| 356 | bool File::SetLength(int64_t length) { |
| 357 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 358 | DCHECK(IsValid()); |
| 359 |
| 360 | SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length); |
| 361 | return !CallFtruncate(file_.get(), length); |
| 362 | } |
| 363 |
| 364 | bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
| 365 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 366 | DCHECK(IsValid()); |
| 367 |
| 368 | SCOPED_FILE_TRACE("SetTimes"); |
| 369 |
| 370 | timeval times[2]; |
| 371 | times[0] = last_access_time.ToTimeVal(); |
| 372 | times[1] = last_modified_time.ToTimeVal(); |
| 373 |
| 374 | return !CallFutimes(file_.get(), times); |
| 375 | } |
| 376 |
| 377 | bool File::GetInfo(Info* info) { |
| 378 | DCHECK(IsValid()); |
| 379 |
| 380 | SCOPED_FILE_TRACE("GetInfo"); |
| 381 |
| 382 | stat_wrapper_t file_info; |
| 383 | if (Fstat(file_.get(), &file_info)) |
| 384 | return false; |
| 385 |
| 386 | info->FromStat(file_info); |
| 387 | return true; |
| 388 | } |
| 389 |
| 390 | #if !defined(OS_FUCHSIA) |
| 391 | File::Error File::Lock(File::LockMode mode) { |
| 392 | SCOPED_FILE_TRACE("Lock"); |
| 393 | return CallFcntlFlock(file_.get(), mode); |
| 394 | } |
| 395 |
| 396 | File::Error File::Unlock() { |
| 397 | SCOPED_FILE_TRACE("Unlock"); |
| 398 | return CallFcntlFlock(file_.get(), base::Optional<File::LockMode>()); |
| 399 | } |
| 400 | #endif |
| 401 |
| 402 | File File::Duplicate() const { |
| 403 | if (!IsValid()) |
| 404 | return File(); |
| 405 |
| 406 | SCOPED_FILE_TRACE("Duplicate"); |
| 407 |
| 408 | ScopedPlatformFile other_fd(HANDLE_EINTR(dup(GetPlatformFile()))); |
| 409 | if (!other_fd.is_valid()) |
| 410 | return File(File::GetLastFileError()); |
| 411 |
| 412 | return File(std::move(other_fd), async()); |
| 413 | } |
| 414 |
| 415 | // Static. |
| 416 | File::Error File::OSErrorToFileError(int saved_errno) { |
| 417 | switch (saved_errno) { |
| 418 | case EACCES: |
| 419 | case EISDIR: |
| 420 | case EROFS: |
| 421 | case EPERM: |
| 422 | return FILE_ERROR_ACCESS_DENIED; |
| 423 | case EBUSY: |
| 424 | #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. |
| 425 | case ETXTBSY: |
| 426 | #endif |
| 427 | return FILE_ERROR_IN_USE; |
| 428 | case EEXIST: |
| 429 | return FILE_ERROR_EXISTS; |
| 430 | case EIO: |
| 431 | return FILE_ERROR_IO; |
| 432 | case ENOENT: |
| 433 | return FILE_ERROR_NOT_FOUND; |
| 434 | case ENFILE: // fallthrough |
| 435 | case EMFILE: |
| 436 | return FILE_ERROR_TOO_MANY_OPENED; |
| 437 | case ENOMEM: |
| 438 | return FILE_ERROR_NO_MEMORY; |
| 439 | case ENOSPC: |
| 440 | return FILE_ERROR_NO_SPACE; |
| 441 | case ENOTDIR: |
| 442 | return FILE_ERROR_NOT_A_DIRECTORY; |
| 443 | default: |
| 444 | #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 445 | UmaHistogramSparse("PlatformFile.UnknownErrors.Posix", saved_errno); |
| 446 | #endif |
| 447 | // This function should only be called for errors. |
| 448 | DCHECK_NE(0, saved_errno); |
| 449 | return FILE_ERROR_FAILED; |
| 450 | } |
| 451 | } |
| 452 |
| 453 | // NaCl doesn't implement system calls to open files directly. |
| 454 | #if !defined(OS_NACL) |
| 455 | // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
| 456 | void File::DoInitialize(const FilePath& path, uint32_t flags) { |
| 457 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 458 | DCHECK(!IsValid()); |
| 459 |
| 460 | int open_flags = 0; |
| 461 | if (flags & FLAG_CREATE) |
| 462 | open_flags = O_CREAT | O_EXCL; |
| 463 |
| 464 | created_ = false; |
| 465 |
| 466 | if (flags & FLAG_CREATE_ALWAYS) { |
| 467 | DCHECK(!open_flags); |
| 468 | DCHECK(flags & FLAG_WRITE); |
| 469 | open_flags = O_CREAT | O_TRUNC; |
| 470 | } |
| 471 |
| 472 | if (flags & FLAG_OPEN_TRUNCATED) { |
| 473 | DCHECK(!open_flags); |
| 474 | DCHECK(flags & FLAG_WRITE); |
| 475 | open_flags = O_TRUNC; |
| 476 | } |
| 477 |
| 478 | if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) { |
| 479 | NOTREACHED(); |
| 480 | errno = EOPNOTSUPP; |
| 481 | error_details_ = FILE_ERROR_FAILED; |
| 482 | return; |
| 483 | } |
| 484 |
| 485 | if (flags & FLAG_WRITE && flags & FLAG_READ) { |
| 486 | open_flags |= O_RDWR; |
| 487 | } else if (flags & FLAG_WRITE) { |
| 488 | open_flags |= O_WRONLY; |
| 489 | } else if (!(flags & FLAG_READ) && |
| 490 | !(flags & FLAG_WRITE_ATTRIBUTES) && |
| 491 | !(flags & FLAG_APPEND) && |
| 492 | !(flags & FLAG_OPEN_ALWAYS)) { |
| 493 | NOTREACHED(); |
| 494 | } |
| 495 |
| 496 | if (flags & FLAG_TERMINAL_DEVICE) |
| 497 | open_flags |= O_NOCTTY | O_NDELAY; |
| 498 |
| 499 | if (flags & FLAG_APPEND && flags & FLAG_READ) |
| 500 | open_flags |= O_APPEND | O_RDWR; |
| 501 | else if (flags & FLAG_APPEND) |
| 502 | open_flags |= O_APPEND | O_WRONLY; |
| 503 |
| 504 | static_assert(O_RDONLY == 0, "O_RDONLY must equal zero"); |
| 505 |
| 506 | int mode = S_IRUSR | S_IWUSR; |
| 507 | #if defined(OS_CHROMEOS) |
| 508 | mode |= S_IRGRP | S_IROTH; |
| 509 | #endif |
| 510 |
| 511 | int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode)); |
| 512 |
| 513 | if (flags & FLAG_OPEN_ALWAYS) { |
| 514 | if (descriptor < 0) { |
| 515 | open_flags |= O_CREAT; |
| 516 | if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
| 517 | open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
| 518 |
| 519 | descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode)); |
| 520 | if (descriptor >= 0) |
| 521 | created_ = true; |
| 522 | } |
| 523 | } |
| 524 |
| 525 | if (descriptor < 0) { |
| 526 | error_details_ = File::GetLastFileError(); |
| 527 | return; |
| 528 | } |
| 529 |
| 530 | if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
| 531 | created_ = true; |
| 532 |
| 533 | if (flags & FLAG_DELETE_ON_CLOSE) |
| 534 | unlink(path.value().c_str()); |
| 535 |
| 536 | async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 537 | error_details_ = FILE_OK; |
| 538 | file_.reset(descriptor); |
| 539 | } |
| 540 | #endif // !defined(OS_NACL) |
| 541 |
| 542 | bool File::Flush() { |
| 543 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 544 | DCHECK(IsValid()); |
| 545 | SCOPED_FILE_TRACE("Flush"); |
| 546 |
| 547 | #if defined(OS_NACL) |
| 548 | NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 549 | return true; |
| 550 | #elif defined(OS_LINUX) || defined(OS_ANDROID) |
| 551 | return !HANDLE_EINTR(fdatasync(file_.get())); |
| 552 | #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 553 | // On macOS and iOS, fsync() is guaranteed to send the file's data to the |
| 554 | // underlying storage device, but may return before the device actually writes |
| 555 | // the data to the medium. When used by database systems, this may result in |
| 556 | // unexpected data loss. |
| 557 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html |
| 558 | if (!HANDLE_EINTR(fcntl(file_.get(), F_FULLFSYNC))) |
| 559 | return true; |
| 560 |
| 561 | // Some filesystms do not support fcntl(F_FULLFSYNC). We handle these cases by |
| 562 | // falling back to fsync(). Unfortunately, lack of F_FULLFSYNC support results |
| 563 | // in various error codes, so we cannot use the error code as a definitive |
| 564 | // indicator that F_FULLFSYNC was not supported. So, if fcntl() errors out for |
| 565 | // any reason, we may end up making an unnecessary system call. |
| 566 | // |
| 567 | // See the CL description at https://crrev.com/c/1400159 for details. |
| 568 | return !HANDLE_EINTR(fsync(file_.get())); |
| 569 | #else |
| 570 | return !HANDLE_EINTR(fsync(file_.get())); |
| 571 | #endif |
| 572 | } |
| 573 |
| 574 | void File::SetPlatformFile(PlatformFile file) { |
| 575 | DCHECK(!file_.is_valid()); |
| 576 | file_.reset(file); |
| 577 | } |
| 578 |
| 579 | // static |
| 580 | File::Error File::GetLastFileError() { |
| 581 | return base::File::OSErrorToFileError(errno); |
| 582 | } |
| 583 |
| 584 | #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) || \ |
| 585 | defined(OS_FUCHSIA) || (defined(OS_ANDROID) && __ANDROID_API__ < 21) |
| 586 | int File::Stat(const char* path, stat_wrapper_t* sb) { |
| 587 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 588 | return stat(path, sb); |
| 589 | } |
| 590 | int File::Fstat(int fd, stat_wrapper_t* sb) { |
| 591 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 592 | return fstat(fd, sb); |
| 593 | } |
| 594 | int File::Lstat(const char* path, stat_wrapper_t* sb) { |
| 595 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 596 | return lstat(path, sb); |
| 597 | } |
| 598 | #else |
| 599 | int File::Stat(const char* path, stat_wrapper_t* sb) { |
| 600 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 601 | return stat64(path, sb); |
| 602 | } |
| 603 | int File::Fstat(int fd, stat_wrapper_t* sb) { |
| 604 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 605 | return fstat64(fd, sb); |
| 606 | } |
| 607 | int File::Lstat(const char* path, stat_wrapper_t* sb) { |
| 608 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 609 | return lstat64(path, sb); |
| 610 | } |
| 611 | #endif |
| 612 |
| 613 | } // namespace base |
| 614 |