rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 5 | #include "base/files/file.h" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 6 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 7 | #include <errno.h> |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 8 | #include <fcntl.h> |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 9 | #include <sys/stat.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "base/files/file_path.h" |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 13 | #include "base/files/file_posix_hooks_internal.h" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/metrics/sparse_histogram.h" |
| 16 | #include "base/posix/eintr_wrapper.h" |
| 17 | #include "base/strings/utf_string_conversions.h" |
| 18 | #include "base/threading/thread_restrictions.h" |
| 19 | |
| 20 | #if defined(OS_ANDROID) |
| 21 | #include "base/os_compat_android.h" |
| 22 | #endif |
| 23 | |
| 24 | namespace base { |
| 25 | |
| 26 | // Make sure our Whence mappings match the system headers. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 27 | COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && |
| 28 | File::FROM_CURRENT == SEEK_CUR && |
| 29 | File::FROM_END == SEEK_END, whence_matches_system); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 34 | static int CallFstat(int fd, stat_wrapper_t *sb) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 35 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 36 | return fstat(fd, sb); |
| 37 | } |
| 38 | #else |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 39 | static int CallFstat(int fd, stat_wrapper_t *sb) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 40 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 41 | return fstat64(fd, sb); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | // NaCl doesn't provide the following system calls, so either simulate them or |
| 46 | // wrap them in order to minimize the number of #ifdef's in this file. |
| 47 | #if !defined(OS_NACL) |
| 48 | static bool IsOpenAppend(PlatformFile file) { |
| 49 | return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
| 50 | } |
| 51 | |
| 52 | static int CallFtruncate(PlatformFile file, int64 length) { |
| 53 | return HANDLE_EINTR(ftruncate(file, length)); |
| 54 | } |
| 55 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 56 | static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 57 | #ifdef __USE_XOPEN2K8 |
| 58 | // futimens should be available, but futimes might not be |
| 59 | // http://pubs.opengroup.org/onlinepubs/9699919799/ |
| 60 | |
| 61 | timespec ts_times[2]; |
| 62 | ts_times[0].tv_sec = times[0].tv_sec; |
| 63 | ts_times[0].tv_nsec = times[0].tv_usec * 1000; |
| 64 | ts_times[1].tv_sec = times[1].tv_sec; |
| 65 | ts_times[1].tv_nsec = times[1].tv_usec * 1000; |
| 66 | |
| 67 | return futimens(file, ts_times); |
| 68 | #else |
| 69 | return futimes(file, times); |
| 70 | #endif |
| 71 | } |
| 72 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 73 | static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 74 | struct flock lock; |
| 75 | lock.l_type = F_WRLCK; |
| 76 | lock.l_whence = SEEK_SET; |
| 77 | lock.l_start = 0; |
| 78 | lock.l_len = 0; // Lock entire file. |
| 79 | if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 80 | return File::OSErrorToFileError(errno); |
| 81 | return File::FILE_OK; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 82 | } |
| 83 | #else // defined(OS_NACL) |
| 84 | |
| 85 | static bool IsOpenAppend(PlatformFile file) { |
| 86 | // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX |
| 87 | // standard and always appends if the file is opened with O_APPEND, just |
| 88 | // return false here. |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | static int CallFtruncate(PlatformFile file, int64 length) { |
| 93 | NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
| 94 | return 0; |
| 95 | } |
| 96 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 97 | static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 98 | NOTIMPLEMENTED(); // NaCl doesn't implement futimes. |
| 99 | return 0; |
| 100 | } |
| 101 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 102 | static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 103 | NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 104 | return File::FILE_ERROR_INVALID_OPERATION; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 105 | } |
| 106 | #endif // defined(OS_NACL) |
| 107 | |
| 108 | } // namespace |
| 109 | |
rvargas@chromium.org | 527ea31 | 2014-04-05 11:39:18 +0900 | [diff] [blame] | 110 | void File::Info::FromStat(const stat_wrapper_t& stat_info) { |
| 111 | is_directory = S_ISDIR(stat_info.st_mode); |
| 112 | is_symbolic_link = S_ISLNK(stat_info.st_mode); |
| 113 | size = stat_info.st_size; |
| 114 | |
| 115 | #if defined(OS_LINUX) |
| 116 | time_t last_modified_sec = stat_info.st_mtim.tv_sec; |
| 117 | int64 last_modified_nsec = stat_info.st_mtim.tv_nsec; |
| 118 | time_t last_accessed_sec = stat_info.st_atim.tv_sec; |
| 119 | int64 last_accessed_nsec = stat_info.st_atim.tv_nsec; |
| 120 | time_t creation_time_sec = stat_info.st_ctim.tv_sec; |
| 121 | int64 creation_time_nsec = stat_info.st_ctim.tv_nsec; |
| 122 | #elif defined(OS_ANDROID) |
| 123 | time_t last_modified_sec = stat_info.st_mtime; |
| 124 | int64 last_modified_nsec = stat_info.st_mtime_nsec; |
| 125 | time_t last_accessed_sec = stat_info.st_atime; |
| 126 | int64 last_accessed_nsec = stat_info.st_atime_nsec; |
| 127 | time_t creation_time_sec = stat_info.st_ctime; |
| 128 | int64 creation_time_nsec = stat_info.st_ctime_nsec; |
| 129 | #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) |
| 130 | time_t last_modified_sec = stat_info.st_mtimespec.tv_sec; |
| 131 | int64 last_modified_nsec = stat_info.st_mtimespec.tv_nsec; |
| 132 | time_t last_accessed_sec = stat_info.st_atimespec.tv_sec; |
| 133 | int64 last_accessed_nsec = stat_info.st_atimespec.tv_nsec; |
| 134 | time_t creation_time_sec = stat_info.st_ctimespec.tv_sec; |
| 135 | int64 creation_time_nsec = stat_info.st_ctimespec.tv_nsec; |
| 136 | #else |
| 137 | time_t last_modified_sec = stat_info.st_mtime; |
| 138 | int64 last_modified_nsec = 0; |
| 139 | time_t last_accessed_sec = stat_info.st_atime; |
| 140 | int64 last_accessed_nsec = 0; |
| 141 | time_t creation_time_sec = stat_info.st_ctime; |
| 142 | int64 creation_time_nsec = 0; |
| 143 | #endif |
| 144 | |
| 145 | last_modified = |
| 146 | Time::FromTimeT(last_modified_sec) + |
| 147 | TimeDelta::FromMicroseconds(last_modified_nsec / |
| 148 | Time::kNanosecondsPerMicrosecond); |
| 149 | |
| 150 | last_accessed = |
| 151 | Time::FromTimeT(last_accessed_sec) + |
| 152 | TimeDelta::FromMicroseconds(last_accessed_nsec / |
| 153 | Time::kNanosecondsPerMicrosecond); |
| 154 | |
| 155 | creation_time = |
| 156 | Time::FromTimeT(creation_time_sec) + |
| 157 | TimeDelta::FromMicroseconds(creation_time_nsec / |
| 158 | Time::kNanosecondsPerMicrosecond); |
| 159 | } |
| 160 | |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 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 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 169 | bool File::IsValid() const { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 170 | return file_.is_valid(); |
| 171 | } |
| 172 | |
| 173 | PlatformFile File::GetPlatformFile() const { |
| 174 | return file_.get(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 175 | } |
| 176 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 177 | PlatformFile File::TakePlatformFile() { |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 178 | if (IsValid()) |
| 179 | UnprotectFileDescriptor(GetPlatformFile()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 180 | return file_.release(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void File::Close() { |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 184 | if (!IsValid()) |
| 185 | return; |
| 186 | |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 187 | ThreadRestrictions::AssertIOAllowed(); |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 188 | UnprotectFileDescriptor(GetPlatformFile()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 189 | file_.reset(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int64 File::Seek(Whence whence, int64 offset) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 193 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 194 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 195 | |
rvargas@chromium.org | ba1a954 | 2014-06-21 08:30:18 +0900 | [diff] [blame] | 196 | #if defined(OS_ANDROID) |
| 197 | COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
| 198 | return lseek64(file_.get(), static_cast<off64_t>(offset), |
| 199 | static_cast<int>(whence)); |
| 200 | #else |
| 201 | COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 202 | return lseek(file_.get(), static_cast<off_t>(offset), |
| 203 | static_cast<int>(whence)); |
rvargas@chromium.org | ba1a954 | 2014-06-21 08:30:18 +0900 | [diff] [blame] | 204 | #endif |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 205 | } |
| 206 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 207 | int File::Read(int64 offset, char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 208 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 209 | DCHECK(IsValid()); |
| 210 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 211 | return -1; |
| 212 | |
| 213 | int bytes_read = 0; |
| 214 | int rv; |
| 215 | do { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 216 | rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 217 | size - bytes_read, offset + bytes_read)); |
| 218 | if (rv <= 0) |
| 219 | break; |
| 220 | |
| 221 | bytes_read += rv; |
| 222 | } while (bytes_read < size); |
| 223 | |
| 224 | return bytes_read ? bytes_read : rv; |
| 225 | } |
| 226 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 227 | int File::ReadAtCurrentPos(char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 228 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 229 | DCHECK(IsValid()); |
| 230 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 231 | return -1; |
| 232 | |
| 233 | int bytes_read = 0; |
| 234 | int rv; |
| 235 | do { |
hashimoto@chromium.org | 94f5b7d | 2014-04-01 07:18:09 +0900 | [diff] [blame] | 236 | rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 237 | if (rv <= 0) |
| 238 | break; |
| 239 | |
| 240 | bytes_read += rv; |
| 241 | } while (bytes_read < size); |
| 242 | |
| 243 | return bytes_read ? bytes_read : rv; |
| 244 | } |
| 245 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 246 | int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 247 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 248 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 249 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 250 | return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 251 | } |
| 252 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 253 | int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 254 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 255 | DCHECK(IsValid()); |
| 256 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 257 | return -1; |
| 258 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 259 | return HANDLE_EINTR(read(file_.get(), data, size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 260 | } |
| 261 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 262 | int File::Write(int64 offset, const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 263 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 264 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 265 | if (IsOpenAppend(file_.get())) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 266 | return WriteAtCurrentPos(data, size); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 267 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 268 | DCHECK(IsValid()); |
| 269 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 270 | return -1; |
| 271 | |
| 272 | int bytes_written = 0; |
| 273 | int rv; |
| 274 | do { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 275 | rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 276 | size - bytes_written, offset + bytes_written)); |
| 277 | if (rv <= 0) |
| 278 | break; |
| 279 | |
| 280 | bytes_written += rv; |
| 281 | } while (bytes_written < size); |
| 282 | |
| 283 | return bytes_written ? bytes_written : rv; |
| 284 | } |
| 285 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 286 | int File::WriteAtCurrentPos(const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 287 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 288 | DCHECK(IsValid()); |
| 289 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 290 | return -1; |
| 291 | |
| 292 | int bytes_written = 0; |
| 293 | int rv; |
| 294 | do { |
hashimoto@chromium.org | 94f5b7d | 2014-04-01 07:18:09 +0900 | [diff] [blame] | 295 | rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
| 296 | size - bytes_written)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 297 | if (rv <= 0) |
| 298 | break; |
| 299 | |
| 300 | bytes_written += rv; |
| 301 | } while (bytes_written < size); |
| 302 | |
| 303 | return bytes_written ? bytes_written : rv; |
| 304 | } |
| 305 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 306 | int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 307 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 308 | DCHECK(IsValid()); |
| 309 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 310 | return -1; |
| 311 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 312 | return HANDLE_EINTR(write(file_.get(), data, size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 313 | } |
| 314 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 315 | int64 File::GetLength() { |
| 316 | DCHECK(IsValid()); |
| 317 | |
| 318 | stat_wrapper_t file_info; |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 319 | if (CallFstat(file_.get(), &file_info)) |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 320 | return false; |
| 321 | |
| 322 | return file_info.st_size; |
| 323 | } |
| 324 | |
| 325 | bool File::SetLength(int64 length) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 326 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 327 | DCHECK(IsValid()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 328 | return !CallFtruncate(file_.get(), length); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 329 | } |
| 330 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 331 | bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 332 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 333 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 334 | |
| 335 | timeval times[2]; |
| 336 | times[0] = last_access_time.ToTimeVal(); |
| 337 | times[1] = last_modified_time.ToTimeVal(); |
| 338 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 339 | return !CallFutimes(file_.get(), times); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 340 | } |
| 341 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 342 | bool File::GetInfo(Info* info) { |
| 343 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 344 | |
| 345 | stat_wrapper_t file_info; |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 346 | if (CallFstat(file_.get(), &file_info)) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 347 | return false; |
| 348 | |
rvargas@chromium.org | 527ea31 | 2014-04-05 11:39:18 +0900 | [diff] [blame] | 349 | info->FromStat(file_info); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 353 | File::Error File::Lock() { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 354 | return CallFctnlFlock(file_.get(), true); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 355 | } |
| 356 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 357 | File::Error File::Unlock() { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 358 | return CallFctnlFlock(file_.get(), false); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 359 | } |
| 360 | |
grt | 98ea39f | 2015-03-20 02:54:40 +0900 | [diff] [blame] | 361 | File File::Duplicate() { |
| 362 | if (!IsValid()) |
| 363 | return File(); |
| 364 | |
| 365 | PlatformFile other_fd = dup(GetPlatformFile()); |
| 366 | if (other_fd == -1) |
| 367 | return File(OSErrorToFileError(errno)); |
| 368 | |
| 369 | File other(other_fd); |
| 370 | if (async()) |
| 371 | other.async_ = true; |
| 372 | return other.Pass(); |
| 373 | } |
| 374 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 375 | // Static. |
| 376 | File::Error File::OSErrorToFileError(int saved_errno) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 377 | switch (saved_errno) { |
| 378 | case EACCES: |
| 379 | case EISDIR: |
| 380 | case EROFS: |
| 381 | case EPERM: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 382 | return FILE_ERROR_ACCESS_DENIED; |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 383 | case EBUSY: |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 384 | #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. |
| 385 | case ETXTBSY: |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 386 | #endif |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 387 | return FILE_ERROR_IN_USE; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 388 | case EEXIST: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 389 | return FILE_ERROR_EXISTS; |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 390 | case EIO: |
| 391 | return FILE_ERROR_IO; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 392 | case ENOENT: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 393 | return FILE_ERROR_NOT_FOUND; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 394 | case EMFILE: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 395 | return FILE_ERROR_TOO_MANY_OPENED; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 396 | case ENOMEM: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 397 | return FILE_ERROR_NO_MEMORY; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 398 | case ENOSPC: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 399 | return FILE_ERROR_NO_SPACE; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 400 | case ENOTDIR: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 401 | return FILE_ERROR_NOT_A_DIRECTORY; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 402 | default: |
| 403 | #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 404 | UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
| 405 | saved_errno); |
| 406 | #endif |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 407 | return FILE_ERROR_FAILED; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 411 | File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() { |
| 412 | UpdateChecksum(); |
| 413 | } |
| 414 | |
| 415 | File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd) : file_(fd) { |
| 416 | UpdateChecksum(); |
| 417 | } |
| 418 | |
| 419 | File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {} |
| 420 | |
| 421 | // static |
| 422 | void File::MemoryCheckingScopedFD::ComputeMemoryChecksum( |
| 423 | unsigned int* out_checksum) const { |
| 424 | // Use a single iteration of a linear congruentional generator (lcg) to |
| 425 | // provide a cheap checksum unlikely to be accidentally matched by a random |
| 426 | // memory corruption. |
| 427 | |
| 428 | // By choosing constants that satisfy the Hull-Duebell Theorem on lcg cycle |
| 429 | // length, we insure that each distinct fd value maps to a distinct checksum, |
| 430 | // which maximises the utility of our checksum. |
| 431 | |
| 432 | // This code uses "unsigned int" throughout for its defined modular semantics, |
| 433 | // which implicitly gives us a divisor that is a power of two. |
| 434 | |
| 435 | const unsigned int kMultiplier = 13035 * 4 + 1; |
| 436 | COMPILE_ASSERT(((kMultiplier - 1) & 3) == 0, pred_must_be_multiple_of_four); |
| 437 | const unsigned int kIncrement = 1595649551; |
| 438 | COMPILE_ASSERT(kIncrement & 1, must_be_coprime_to_powers_of_two); |
| 439 | |
| 440 | *out_checksum = |
| 441 | static_cast<unsigned int>(file_.get()) * kMultiplier + kIncrement; |
| 442 | } |
| 443 | |
| 444 | void File::MemoryCheckingScopedFD::Check() const { |
| 445 | unsigned int computed_checksum; |
| 446 | ComputeMemoryChecksum(&computed_checksum); |
| 447 | CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; |
| 448 | } |
| 449 | |
| 450 | void File::MemoryCheckingScopedFD::UpdateChecksum() { |
| 451 | ComputeMemoryChecksum(&file_memory_checksum_); |
| 452 | } |
| 453 | |
dbeam | 740120e | 2015-04-24 10:27:53 +0900 | [diff] [blame] | 454 | // NaCl doesn't implement system calls to open files directly. |
| 455 | #if !defined(OS_NACL) |
| 456 | // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
| 457 | void File::DoInitialize(const FilePath& name, uint32 flags) { |
| 458 | ThreadRestrictions::AssertIOAllowed(); |
| 459 | DCHECK(!IsValid()); |
| 460 | |
| 461 | int open_flags = 0; |
| 462 | if (flags & FLAG_CREATE) |
| 463 | open_flags = O_CREAT | O_EXCL; |
| 464 | |
| 465 | created_ = false; |
| 466 | |
| 467 | if (flags & FLAG_CREATE_ALWAYS) { |
| 468 | DCHECK(!open_flags); |
| 469 | DCHECK(flags & FLAG_WRITE); |
| 470 | open_flags = O_CREAT | O_TRUNC; |
| 471 | } |
| 472 | |
| 473 | if (flags & FLAG_OPEN_TRUNCATED) { |
| 474 | DCHECK(!open_flags); |
| 475 | DCHECK(flags & FLAG_WRITE); |
| 476 | open_flags = O_TRUNC; |
| 477 | } |
| 478 | |
| 479 | if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) { |
| 480 | NOTREACHED(); |
| 481 | errno = EOPNOTSUPP; |
| 482 | error_details_ = FILE_ERROR_FAILED; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | if (flags & FLAG_WRITE && flags & FLAG_READ) { |
| 487 | open_flags |= O_RDWR; |
| 488 | } else if (flags & FLAG_WRITE) { |
| 489 | open_flags |= O_WRONLY; |
| 490 | } else if (!(flags & FLAG_READ) && |
| 491 | !(flags & FLAG_WRITE_ATTRIBUTES) && |
| 492 | !(flags & FLAG_APPEND) && |
| 493 | !(flags & FLAG_OPEN_ALWAYS)) { |
| 494 | NOTREACHED(); |
| 495 | } |
| 496 | |
| 497 | if (flags & FLAG_TERMINAL_DEVICE) |
| 498 | open_flags |= O_NOCTTY | O_NDELAY; |
| 499 | |
| 500 | if (flags & FLAG_APPEND && flags & FLAG_READ) |
| 501 | open_flags |= O_APPEND | O_RDWR; |
| 502 | else if (flags & FLAG_APPEND) |
| 503 | open_flags |= O_APPEND | O_WRONLY; |
| 504 | |
| 505 | COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
| 506 | |
| 507 | int mode = S_IRUSR | S_IWUSR; |
| 508 | #if defined(OS_CHROMEOS) |
| 509 | mode |= S_IRGRP | S_IROTH; |
| 510 | #endif |
| 511 | |
| 512 | int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
| 513 | |
| 514 | if (flags & FLAG_OPEN_ALWAYS) { |
| 515 | if (descriptor < 0) { |
| 516 | open_flags |= O_CREAT; |
| 517 | if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
| 518 | open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
| 519 | |
| 520 | descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
| 521 | if (descriptor >= 0) |
| 522 | created_ = true; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (descriptor < 0) { |
| 527 | error_details_ = File::OSErrorToFileError(errno); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
| 532 | created_ = true; |
| 533 | |
| 534 | if (flags & FLAG_DELETE_ON_CLOSE) |
| 535 | unlink(name.value().c_str()); |
| 536 | |
| 537 | async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 538 | error_details_ = FILE_OK; |
| 539 | file_.reset(descriptor); |
| 540 | ProtectFileDescriptor(descriptor); |
| 541 | } |
| 542 | #endif // !defined(OS_NACL) |
| 543 | |
tnagel | 7eaf477 | 2015-04-03 19:11:46 +0900 | [diff] [blame] | 544 | bool File::DoFlush() { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame] | 545 | ThreadRestrictions::AssertIOAllowed(); |
tnagel | 7eaf477 | 2015-04-03 19:11:46 +0900 | [diff] [blame] | 546 | DCHECK(IsValid()); |
| 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 | #else |
| 553 | return !HANDLE_EINTR(fsync(file_.get())); |
| 554 | #endif |
| 555 | } |
| 556 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 557 | void File::SetPlatformFile(PlatformFile file) { |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 558 | CHECK(!file_.is_valid()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 559 | file_.reset(file); |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 560 | if (file_.is_valid()) |
| 561 | ProtectFileDescriptor(GetPlatformFile()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 562 | } |
| 563 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 564 | } // namespace base |