| 6 |
| 7 | #include <utility> |
| 8 |
| 9 | #include "base/files/file_path.h" |
| 10 | #include "base/files/file_tracing.h" |
| 11 | #include "base/metrics/histogram.h" |
| 12 | #include "base/numerics/safe_conversions.h" |
| 13 | #include "base/timer/elapsed_timer.h" |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 17 | #include <errno.h> |
| 18 | #endif |
| 19 |
| 20 | namespace base { |
| 21 |
| 22 | File::Info::Info() = default; |
| 23 |
| 24 | File::Info::~Info() = default; |
| 25 |
| 26 | File::File() = default; |
| 27 |
| 28 | #if !defined(OS_NACL) |
| 29 | File::File(const FilePath& path, uint32_t flags) : error_details_(FILE_OK) { |
| 30 | Initialize(path, flags); |
| 31 | } |
| 32 | #endif |
| 33 |
| 34 | File::File(ScopedPlatformFile platform_file) |
| 35 | : File(std::move(platform_file), false) {} |
| 36 |
| 37 | File::File(PlatformFile platform_file) : File(platform_file, false) {} |
| 38 |
| 39 | File::File(ScopedPlatformFile platform_file, bool async) |
| 40 | : file_(std::move(platform_file)), error_details_(FILE_OK), async_(async) { |
| 41 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 42 | DCHECK_GE(file_.get(), -1); |
| 43 | #endif |
| 44 | } |
| 45 |
| 46 | File::File(PlatformFile platform_file, bool async) |
| 47 | : file_(platform_file), |
| 48 | error_details_(FILE_OK), |
| 49 | async_(async) { |
| 50 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 51 | DCHECK_GE(platform_file, -1); |
| 52 | #endif |
| 53 | } |
| 54 |
| 55 | File::File(Error error_details) : error_details_(error_details) {} |
| 56 |
| 57 | File::File(File&& other) |
| 58 | : file_(other.TakePlatformFile()), |
| 59 | tracing_path_(other.tracing_path_), |
| 60 | error_details_(other.error_details()), |
| 61 | created_(other.created()), |
| 62 | async_(other.async_) {} |
| 63 |
| 64 | File::~File() { |
| 65 | // Go through the AssertIOAllowed logic. |
| 66 | Close(); |
| 67 | } |
| 68 |
| 69 | File& File::operator=(File&& other) { |
| 70 | Close(); |
| 71 | SetPlatformFile(other.TakePlatformFile()); |
| 72 | tracing_path_ = other.tracing_path_; |
| 73 | error_details_ = other.error_details(); |
| 74 | created_ = other.created(); |
| 75 | async_ = other.async_; |
| 76 | return *this; |
| 77 | } |
| 78 |
| 79 | #if !defined(OS_NACL) |
| 80 | void File::Initialize(const FilePath& path, uint32_t flags) { |
| 81 | if (path.ReferencesParent()) { |
| 82 | #if defined(OS_WIN) |
| 83 | ::SetLastError(ERROR_ACCESS_DENIED); |
| 84 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 85 | errno = EACCES; |
| 86 | #else |
| 87 | #error Unsupported platform |
| 88 | #endif |
| 89 | error_details_ = FILE_ERROR_ACCESS_DENIED; |
| 90 | return; |
| 91 | } |
| 92 | if (FileTracing::IsCategoryEnabled()) |
| 93 | tracing_path_ = path; |
| 94 | SCOPED_FILE_TRACE("Initialize"); |
| 95 | DoInitialize(path, flags); |
| 96 | } |
| 97 | #endif |
| 98 |
| 99 | bool File::ReadAndCheck(int64_t offset, span<uint8_t> data) { |
| 100 | int size = checked_cast<int>(data.size()); |
| 101 | return Read(offset, reinterpret_cast<char*>(data.data()), size) == size; |
| 102 | } |
| 103 |
| 104 | bool File::ReadAtCurrentPosAndCheck(span<uint8_t> data) { |
| 105 | int size = checked_cast<int>(data.size()); |
| 106 | return ReadAtCurrentPos(reinterpret_cast<char*>(data.data()), size) == size; |
| 107 | } |
| 108 |
| 109 | bool File::WriteAndCheck(int64_t offset, span<const uint8_t> data) { |
| 110 | int size = checked_cast<int>(data.size()); |
| 111 | return Write(offset, reinterpret_cast<const char*>(data.data()), size) == |
| 112 | size; |
| 113 | } |
| 114 |
| 115 | bool File::WriteAtCurrentPosAndCheck(span<const uint8_t> data) { |
| 116 | int size = checked_cast<int>(data.size()); |
| 117 | return WriteAtCurrentPos(reinterpret_cast<const char*>(data.data()), size) == |
| 118 | size; |
| 119 | } |
| 120 |
| 121 | // static |
| 122 | std::string File::ErrorToString(Error error) { |
| 123 | switch (error) { |
| 124 | case FILE_OK: |
| 125 | return "FILE_OK"; |
| 126 | case FILE_ERROR_FAILED: |
| 127 | return "FILE_ERROR_FAILED"; |
| 128 | case FILE_ERROR_IN_USE: |
| 129 | return "FILE_ERROR_IN_USE"; |
| 130 | case FILE_ERROR_EXISTS: |
| 131 | return "FILE_ERROR_EXISTS"; |
| 132 | case FILE_ERROR_NOT_FOUND: |
| 133 | return "FILE_ERROR_NOT_FOUND"; |
| 134 | case FILE_ERROR_ACCESS_DENIED: |
| 135 | return "FILE_ERROR_ACCESS_DENIED"; |
| 136 | case FILE_ERROR_TOO_MANY_OPENED: |
| 137 | return "FILE_ERROR_TOO_MANY_OPENED"; |
| 138 | case FILE_ERROR_NO_MEMORY: |
| 139 | return "FILE_ERROR_NO_MEMORY"; |
| 140 | case FILE_ERROR_NO_SPACE: |
| 141 | return "FILE_ERROR_NO_SPACE"; |
| 142 | case FILE_ERROR_NOT_A_DIRECTORY: |
| 143 | return "FILE_ERROR_NOT_A_DIRECTORY"; |
| 144 | case FILE_ERROR_INVALID_OPERATION: |
| 145 | return "FILE_ERROR_INVALID_OPERATION"; |
| 146 | case FILE_ERROR_SECURITY: |
| 147 | return "FILE_ERROR_SECURITY"; |
| 148 | case FILE_ERROR_ABORT: |
| 149 | return "FILE_ERROR_ABORT"; |
| 150 | case FILE_ERROR_NOT_A_FILE: |
| 151 | return "FILE_ERROR_NOT_A_FILE"; |
| 152 | case FILE_ERROR_NOT_EMPTY: |
| 153 | return "FILE_ERROR_NOT_EMPTY"; |
| 154 | case FILE_ERROR_INVALID_URL: |
| 155 | return "FILE_ERROR_INVALID_URL"; |
| 156 | case FILE_ERROR_IO: |
| 157 | return "FILE_ERROR_IO"; |
| 158 | case FILE_ERROR_MAX: |
| 159 | break; |
| 160 | } |
| 161 |
| 162 | NOTREACHED(); |
| 163 | return ""; |
| 164 | } |
| 165 |
| 166 | } // namespace base |
| 167 |