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@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 169 | // NaCl doesn't implement system calls to open files directly. |
| 170 | #if !defined(OS_NACL) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 171 | // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 172 | void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 173 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 174 | DCHECK(!IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 175 | |
| 176 | int open_flags = 0; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 177 | if (flags & FLAG_CREATE) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 178 | open_flags = O_CREAT | O_EXCL; |
| 179 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 180 | created_ = false; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 181 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 182 | if (flags & FLAG_CREATE_ALWAYS) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 183 | DCHECK(!open_flags); |
mdempsky@chromium.org | 6acba39 | 2014-06-11 03:37:56 +0900 | [diff] [blame] | 184 | DCHECK(flags & FLAG_WRITE); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 185 | open_flags = O_CREAT | O_TRUNC; |
| 186 | } |
| 187 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 188 | if (flags & FLAG_OPEN_TRUNCATED) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 189 | DCHECK(!open_flags); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 190 | DCHECK(flags & FLAG_WRITE); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 191 | open_flags = O_TRUNC; |
| 192 | } |
| 193 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 194 | if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 195 | NOTREACHED(); |
| 196 | errno = EOPNOTSUPP; |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 197 | error_details_ = FILE_ERROR_FAILED; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 198 | return; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 199 | } |
| 200 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 201 | if (flags & FLAG_WRITE && flags & FLAG_READ) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 202 | open_flags |= O_RDWR; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 203 | } else if (flags & FLAG_WRITE) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 204 | open_flags |= O_WRONLY; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 205 | } else if (!(flags & FLAG_READ) && |
| 206 | !(flags & FLAG_WRITE_ATTRIBUTES) && |
| 207 | !(flags & FLAG_APPEND) && |
| 208 | !(flags & FLAG_OPEN_ALWAYS)) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 209 | NOTREACHED(); |
| 210 | } |
| 211 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 212 | if (flags & FLAG_TERMINAL_DEVICE) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 213 | open_flags |= O_NOCTTY | O_NDELAY; |
| 214 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 215 | if (flags & FLAG_APPEND && flags & FLAG_READ) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 216 | open_flags |= O_APPEND | O_RDWR; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 217 | else if (flags & FLAG_APPEND) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 218 | open_flags |= O_APPEND | O_WRONLY; |
| 219 | |
| 220 | COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
| 221 | |
| 222 | int mode = S_IRUSR | S_IWUSR; |
| 223 | #if defined(OS_CHROMEOS) |
| 224 | mode |= S_IRGRP | S_IROTH; |
| 225 | #endif |
| 226 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 227 | int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 228 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 229 | if (flags & FLAG_OPEN_ALWAYS) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 230 | if (descriptor < 0) { |
| 231 | open_flags |= O_CREAT; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 232 | if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 233 | open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 234 | |
| 235 | descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
| 236 | if (descriptor >= 0) |
| 237 | created_ = true; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 241 | if (descriptor < 0) { |
| 242 | error_details_ = File::OSErrorToFileError(errno); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 247 | created_ = true; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 248 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 249 | if (flags & FLAG_DELETE_ON_CLOSE) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 250 | unlink(name.value().c_str()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 251 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 252 | async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 253 | error_details_ = FILE_OK; |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 254 | file_.reset(descriptor); |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 255 | ProtectFileDescriptor(descriptor); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 256 | } |
| 257 | #endif // !defined(OS_NACL) |
| 258 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 259 | bool File::IsValid() const { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 260 | return file_.is_valid(); |
| 261 | } |
| 262 | |
| 263 | PlatformFile File::GetPlatformFile() const { |
| 264 | return file_.get(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 265 | } |
| 266 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 267 | PlatformFile File::TakePlatformFile() { |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 268 | if (IsValid()) |
| 269 | UnprotectFileDescriptor(GetPlatformFile()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 270 | return file_.release(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void File::Close() { |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 274 | if (!IsValid()) |
| 275 | return; |
| 276 | |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 277 | ThreadRestrictions::AssertIOAllowed(); |
pasko | 63cb679 | 2014-11-18 22:31:36 +0900 | [diff] [blame] | 278 | UnprotectFileDescriptor(GetPlatformFile()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 279 | file_.reset(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | int64 File::Seek(Whence whence, int64 offset) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 283 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 284 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 285 | |
rvargas@chromium.org | ba1a954 | 2014-06-21 08:30:18 +0900 | [diff] [blame] | 286 | #if defined(OS_ANDROID) |
| 287 | COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
| 288 | return lseek64(file_.get(), static_cast<off64_t>(offset), |
| 289 | static_cast<int>(whence)); |
| 290 | #else |
| 291 | COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 292 | return lseek(file_.get(), static_cast<off_t>(offset), |
| 293 | static_cast<int>(whence)); |
rvargas@chromium.org | ba1a954 | 2014-06-21 08:30:18 +0900 | [diff] [blame] | 294 | #endif |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 295 | } |
| 296 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 297 | int File::Read(int64 offset, char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 298 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 299 | DCHECK(IsValid()); |
| 300 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 301 | return -1; |
| 302 | |
| 303 | int bytes_read = 0; |
| 304 | int rv; |
| 305 | do { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 306 | rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 307 | size - bytes_read, offset + bytes_read)); |
| 308 | if (rv <= 0) |
| 309 | break; |
| 310 | |
| 311 | bytes_read += rv; |
| 312 | } while (bytes_read < size); |
| 313 | |
| 314 | return bytes_read ? bytes_read : rv; |
| 315 | } |
| 316 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 317 | int File::ReadAtCurrentPos(char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 318 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 319 | DCHECK(IsValid()); |
| 320 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 321 | return -1; |
| 322 | |
| 323 | int bytes_read = 0; |
| 324 | int rv; |
| 325 | do { |
hashimoto@chromium.org | 94f5b7d | 2014-04-01 07:18:09 +0900 | [diff] [blame] | 326 | 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] | 327 | if (rv <= 0) |
| 328 | break; |
| 329 | |
| 330 | bytes_read += rv; |
| 331 | } while (bytes_read < size); |
| 332 | |
| 333 | return bytes_read ? bytes_read : rv; |
| 334 | } |
| 335 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 336 | int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 337 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 338 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 339 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 340 | return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 341 | } |
| 342 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 343 | int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 344 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 345 | DCHECK(IsValid()); |
| 346 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 347 | return -1; |
| 348 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 349 | return HANDLE_EINTR(read(file_.get(), data, size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 350 | } |
| 351 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 352 | int File::Write(int64 offset, const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 353 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 354 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 355 | if (IsOpenAppend(file_.get())) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 356 | return WriteAtCurrentPos(data, size); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 357 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 358 | DCHECK(IsValid()); |
| 359 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 360 | return -1; |
| 361 | |
| 362 | int bytes_written = 0; |
| 363 | int rv; |
| 364 | do { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 365 | rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 366 | size - bytes_written, offset + bytes_written)); |
| 367 | if (rv <= 0) |
| 368 | break; |
| 369 | |
| 370 | bytes_written += rv; |
| 371 | } while (bytes_written < size); |
| 372 | |
| 373 | return bytes_written ? bytes_written : rv; |
| 374 | } |
| 375 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 376 | int File::WriteAtCurrentPos(const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 377 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 378 | DCHECK(IsValid()); |
| 379 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 380 | return -1; |
| 381 | |
| 382 | int bytes_written = 0; |
| 383 | int rv; |
| 384 | do { |
hashimoto@chromium.org | 94f5b7d | 2014-04-01 07:18:09 +0900 | [diff] [blame] | 385 | rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
| 386 | size - bytes_written)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 387 | if (rv <= 0) |
| 388 | break; |
| 389 | |
| 390 | bytes_written += rv; |
| 391 | } while (bytes_written < size); |
| 392 | |
| 393 | return bytes_written ? bytes_written : rv; |
| 394 | } |
| 395 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 396 | int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 397 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 398 | DCHECK(IsValid()); |
| 399 | if (size < 0) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 400 | return -1; |
| 401 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 402 | return HANDLE_EINTR(write(file_.get(), data, size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 403 | } |
| 404 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 405 | int64 File::GetLength() { |
| 406 | DCHECK(IsValid()); |
| 407 | |
| 408 | stat_wrapper_t file_info; |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 409 | if (CallFstat(file_.get(), &file_info)) |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 410 | return false; |
| 411 | |
| 412 | return file_info.st_size; |
| 413 | } |
| 414 | |
| 415 | bool File::SetLength(int64 length) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 416 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 417 | DCHECK(IsValid()); |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 418 | return !CallFtruncate(file_.get(), length); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 419 | } |
| 420 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 421 | bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
dbeam | e2a95e6 | 2015-04-24 06:42:34 +0900 | [diff] [blame^] | 422 | ThreadRestrictions::AssertIOAllowed(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 423 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 424 | |
| 425 | timeval times[2]; |
| 426 | times[0] = last_access_time.ToTimeVal(); |
| 427 | times[1] = last_modified_time.ToTimeVal(); |
| 428 | |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 429 | return !CallFutimes(file_.get(), times); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 430 | } |
| 431 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 432 | bool File::GetInfo(Info* info) { |
| 433 | DCHECK(IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 434 | |
| 435 | stat_wrapper_t file_info; |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 436 | if (CallFstat(file_.get(), &file_info)) |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 437 | return false; |
| 438 | |
rvargas@chromium.org | 527ea31 | 2014-04-05 11:39:18 +0900 | [diff] [blame] | 439 | info->FromStat(file_info); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 443 | File::Error File::Lock() { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 444 | return CallFctnlFlock(file_.get(), true); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 445 | } |
| 446 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 447 | File::Error File::Unlock() { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 448 | return CallFctnlFlock(file_.get(), false); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 449 | } |
| 450 | |
grt | 98ea39f | 2015-03-20 02:54:40 +0900 | [diff] [blame] | 451 | File File::Duplicate() { |
| 452 | if (!IsValid()) |
| 453 | return File(); |
| 454 | |
| 455 | PlatformFile other_fd = dup(GetPlatformFile()); |
| 456 | if (other_fd == -1) |
| 457 | return File(OSErrorToFileError(errno)); |
| 458 | |
| 459 | File other(other_fd); |
| 460 | if (async()) |
| 461 | other.async_ = true; |
| 462 | return other.Pass(); |
| 463 | } |
| 464 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 465 | // Static. |
| 466 | File::Error File::OSErrorToFileError(int saved_errno) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 467 | switch (saved_errno) { |
| 468 | case EACCES: |
| 469 | case EISDIR: |
| 470 | case EROFS: |
| 471 | case EPERM: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 472 | return FILE_ERROR_ACCESS_DENIED; |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 473 | case EBUSY: |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 474 | #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. |
| 475 | case ETXTBSY: |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 476 | #endif |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 477 | return FILE_ERROR_IN_USE; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 478 | case EEXIST: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 479 | return FILE_ERROR_EXISTS; |
cmumford | 2eb6692 | 2015-03-14 04:35:02 +0900 | [diff] [blame] | 480 | case EIO: |
| 481 | return FILE_ERROR_IO; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 482 | case ENOENT: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 483 | return FILE_ERROR_NOT_FOUND; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 484 | case EMFILE: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 485 | return FILE_ERROR_TOO_MANY_OPENED; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 486 | case ENOMEM: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 487 | return FILE_ERROR_NO_MEMORY; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 488 | case ENOSPC: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 489 | return FILE_ERROR_NO_SPACE; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 490 | case ENOTDIR: |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 491 | return FILE_ERROR_NOT_A_DIRECTORY; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 492 | default: |
| 493 | #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 494 | UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
| 495 | saved_errno); |
| 496 | #endif |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 497 | return FILE_ERROR_FAILED; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 501 | File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() { |
| 502 | UpdateChecksum(); |
| 503 | } |
| 504 | |
| 505 | File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd) : file_(fd) { |
| 506 | UpdateChecksum(); |
| 507 | } |
| 508 | |
| 509 | File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {} |
| 510 | |
| 511 | // static |
| 512 | void File::MemoryCheckingScopedFD::ComputeMemoryChecksum( |
| 513 | unsigned int* out_checksum) const { |
| 514 | // Use a single iteration of a linear congruentional generator (lcg) to |
| 515 | // provide a cheap checksum unlikely to be accidentally matched by a random |
| 516 | // memory corruption. |
| 517 | |
| 518 | // By choosing constants that satisfy the Hull-Duebell Theorem on lcg cycle |
| 519 | // length, we insure that each distinct fd value maps to a distinct checksum, |
| 520 | // which maximises the utility of our checksum. |
| 521 | |
| 522 | // This code uses "unsigned int" throughout for its defined modular semantics, |
| 523 | // which implicitly gives us a divisor that is a power of two. |
| 524 | |
| 525 | const unsigned int kMultiplier = 13035 * 4 + 1; |
| 526 | COMPILE_ASSERT(((kMultiplier - 1) & 3) == 0, pred_must_be_multiple_of_four); |
| 527 | const unsigned int kIncrement = 1595649551; |
| 528 | COMPILE_ASSERT(kIncrement & 1, must_be_coprime_to_powers_of_two); |
| 529 | |
| 530 | *out_checksum = |
| 531 | static_cast<unsigned int>(file_.get()) * kMultiplier + kIncrement; |
| 532 | } |
| 533 | |
| 534 | void File::MemoryCheckingScopedFD::Check() const { |
| 535 | unsigned int computed_checksum; |
| 536 | ComputeMemoryChecksum(&computed_checksum); |
| 537 | CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; |
| 538 | } |
| 539 | |
| 540 | void File::MemoryCheckingScopedFD::UpdateChecksum() { |
| 541 | ComputeMemoryChecksum(&file_memory_checksum_); |
| 542 | } |
| 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 |