| 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_posix_hooks_internal.h" | |
| 14 #include "base/logging.h" | 13 #include "base/logging.h" |
| 15 #include "base/metrics/sparse_histogram.h" | 14 #include "base/metrics/sparse_histogram.h" |
| 16 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 19 | 18 |
| 20 #if defined(OS_ANDROID) | 19 #if defined(OS_ANDROID) |
| 21 #include "base/os_compat_android.h" | 20 #include "base/os_compat_android.h" |
| 22 #endif | 21 #endif |
| 23 | 22 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 Time::FromTimeT(last_accessed_sec) + | 150 Time::FromTimeT(last_accessed_sec) + |
| 152 TimeDelta::FromMicroseconds(last_accessed_nsec / | 151 TimeDelta::FromMicroseconds(last_accessed_nsec / |
| 153 Time::kNanosecondsPerMicrosecond); | 152 Time::kNanosecondsPerMicrosecond); |
| 154 | 153 |
| 155 creation_time = | 154 creation_time = |
| 156 Time::FromTimeT(creation_time_sec) + | 155 Time::FromTimeT(creation_time_sec) + |
| 157 TimeDelta::FromMicroseconds(creation_time_nsec / | 156 TimeDelta::FromMicroseconds(creation_time_nsec / |
| 158 Time::kNanosecondsPerMicrosecond); | 157 Time::kNanosecondsPerMicrosecond); |
| 159 } | 158 } |
| 160 | 159 |
| 161 // Default implementations of Protect/Unprotect hooks defined as weak symbols | |
| 162 // where possible. | |
| 163 void ProtectFileDescriptor(int fd) { | |
| 164 } | |
| 165 | |
| 166 void UnprotectFileDescriptor(int fd) { | |
| 167 } | |
| 168 | |
| 169 bool File::IsValid() const { | 160 bool File::IsValid() const { |
| 170 return file_.is_valid(); | 161 return file_.is_valid(); |
| 171 } | 162 } |
| 172 | 163 |
| 173 PlatformFile File::GetPlatformFile() const { | 164 PlatformFile File::GetPlatformFile() const { |
| 174 return file_.get(); | 165 return file_.get(); |
| 175 } | 166 } |
| 176 | 167 |
| 177 PlatformFile File::TakePlatformFile() { | 168 PlatformFile File::TakePlatformFile() { |
| 178 if (IsValid()) | |
| 179 UnprotectFileDescriptor(GetPlatformFile()); | |
| 180 return file_.release(); | 169 return file_.release(); |
| 181 } | 170 } |
| 182 | 171 |
| 183 void File::Close() { | 172 void File::Close() { |
| 184 if (!IsValid()) | 173 if (!IsValid()) |
| 185 return; | 174 return; |
| 186 | 175 |
| 187 ThreadRestrictions::AssertIOAllowed(); | 176 ThreadRestrictions::AssertIOAllowed(); |
| 188 UnprotectFileDescriptor(GetPlatformFile()); | |
| 189 file_.reset(); | 177 file_.reset(); |
| 190 } | 178 } |
| 191 | 179 |
| 192 int64 File::Seek(Whence whence, int64 offset) { | 180 int64 File::Seek(Whence whence, int64 offset) { |
| 193 ThreadRestrictions::AssertIOAllowed(); | 181 ThreadRestrictions::AssertIOAllowed(); |
| 194 DCHECK(IsValid()); | 182 DCHECK(IsValid()); |
| 195 | 183 |
| 196 #if defined(OS_ANDROID) | 184 #if defined(OS_ANDROID) |
| 197 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 185 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
| 198 return lseek64(file_.get(), static_cast<off64_t>(offset), | 186 return lseek64(file_.get(), static_cast<off64_t>(offset), |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 518 |
| 531 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 519 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
| 532 created_ = true; | 520 created_ = true; |
| 533 | 521 |
| 534 if (flags & FLAG_DELETE_ON_CLOSE) | 522 if (flags & FLAG_DELETE_ON_CLOSE) |
| 535 unlink(name.value().c_str()); | 523 unlink(name.value().c_str()); |
| 536 | 524 |
| 537 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 525 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 538 error_details_ = FILE_OK; | 526 error_details_ = FILE_OK; |
| 539 file_.reset(descriptor); | 527 file_.reset(descriptor); |
| 540 ProtectFileDescriptor(descriptor); | |
| 541 } | 528 } |
| 542 #endif // !defined(OS_NACL) | 529 #endif // !defined(OS_NACL) |
| 543 | 530 |
| 544 bool File::DoFlush() { | 531 bool File::DoFlush() { |
| 545 ThreadRestrictions::AssertIOAllowed(); | 532 ThreadRestrictions::AssertIOAllowed(); |
| 546 DCHECK(IsValid()); | 533 DCHECK(IsValid()); |
| 547 #if defined(OS_NACL) | 534 #if defined(OS_NACL) |
| 548 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. | 535 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 549 return true; | 536 return true; |
| 550 #elif defined(OS_LINUX) || defined(OS_ANDROID) | 537 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
| 551 return !HANDLE_EINTR(fdatasync(file_.get())); | 538 return !HANDLE_EINTR(fdatasync(file_.get())); |
| 552 #else | 539 #else |
| 553 return !HANDLE_EINTR(fsync(file_.get())); | 540 return !HANDLE_EINTR(fsync(file_.get())); |
| 554 #endif | 541 #endif |
| 555 } | 542 } |
| 556 | 543 |
| 557 void File::SetPlatformFile(PlatformFile file) { | 544 void File::SetPlatformFile(PlatformFile file) { |
| 558 CHECK(!file_.is_valid()); | 545 DCHECK(!file_.is_valid()); |
| 559 file_.reset(file); | 546 file_.reset(file); |
| 560 if (file_.is_valid()) | |
| 561 ProtectFileDescriptor(GetPlatformFile()); | |
| 562 } | 547 } |
| 563 | 548 |
| 564 } // namespace base | 549 } // namespace base |
| OLD | NEW |