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