[email protected] | 9ad009a | 2013-11-28 01:31:31 | [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 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 5 | #include "base/files/file.h" |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 6 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 9 | #include <sys/stat.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "base/files/file_path.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/metrics/sparse_histogram.h" |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 15 | // TODO(rvargas): remove this (needed for kInvalidPlatformFileValue). |
| 16 | #include "base/platform_file.h" |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 17 | #include "base/posix/eintr_wrapper.h" |
| 18 | #include "base/strings/utf_string_conversions.h" |
| 19 | #include "base/threading/thread_restrictions.h" |
| 20 | |
| 21 | #if defined(OS_ANDROID) |
| 22 | #include "base/os_compat_android.h" |
| 23 | #endif |
| 24 | |
| 25 | namespace base { |
| 26 | |
| 27 | // Make sure our Whence mappings match the system headers. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 28 | COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && |
| 29 | File::FROM_CURRENT == SEEK_CUR && |
| 30 | File::FROM_END == SEEK_END, whence_matches_system); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 35 | static int CallFstat(int fd, stat_wrapper_t *sb) { |
| 36 | base::ThreadRestrictions::AssertIOAllowed(); |
| 37 | return fstat(fd, sb); |
| 38 | } |
| 39 | #else |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 40 | static int CallFstat(int fd, stat_wrapper_t *sb) { |
| 41 | base::ThreadRestrictions::AssertIOAllowed(); |
| 42 | return fstat64(fd, sb); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | // NaCl doesn't provide the following system calls, so either simulate them or |
| 47 | // wrap them in order to minimize the number of #ifdef's in this file. |
| 48 | #if !defined(OS_NACL) |
| 49 | static bool IsOpenAppend(PlatformFile file) { |
| 50 | return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
| 51 | } |
| 52 | |
| 53 | static int CallFtruncate(PlatformFile file, int64 length) { |
| 54 | return HANDLE_EINTR(ftruncate(file, length)); |
| 55 | } |
| 56 | |
| 57 | static int CallFsync(PlatformFile file) { |
| 58 | return HANDLE_EINTR(fsync(file)); |
| 59 | } |
| 60 | |
| 61 | static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 62 | #ifdef __USE_XOPEN2K8 |
| 63 | // futimens should be available, but futimes might not be |
| 64 | // http://pubs.opengroup.org/onlinepubs/9699919799/ |
| 65 | |
| 66 | timespec ts_times[2]; |
| 67 | ts_times[0].tv_sec = times[0].tv_sec; |
| 68 | ts_times[0].tv_nsec = times[0].tv_usec * 1000; |
| 69 | ts_times[1].tv_sec = times[1].tv_sec; |
| 70 | ts_times[1].tv_nsec = times[1].tv_usec * 1000; |
| 71 | |
| 72 | return futimens(file, ts_times); |
| 73 | #else |
| 74 | return futimes(file, times); |
| 75 | #endif |
| 76 | } |
| 77 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 78 | static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 79 | struct flock lock; |
| 80 | lock.l_type = F_WRLCK; |
| 81 | lock.l_whence = SEEK_SET; |
| 82 | lock.l_start = 0; |
| 83 | lock.l_len = 0; // Lock entire file. |
| 84 | if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 85 | return File::OSErrorToFileError(errno); |
| 86 | return File::FILE_OK; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 87 | } |
| 88 | #else // defined(OS_NACL) |
| 89 | |
| 90 | static bool IsOpenAppend(PlatformFile file) { |
| 91 | // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX |
| 92 | // standard and always appends if the file is opened with O_APPEND, just |
| 93 | // return false here. |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | static int CallFtruncate(PlatformFile file, int64 length) { |
| 98 | NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int CallFsync(PlatformFile file) { |
| 103 | NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
| 108 | NOTIMPLEMENTED(); // NaCl doesn't implement futimes. |
| 109 | return 0; |
| 110 | } |
| 111 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 112 | static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 113 | NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 114 | return File::FILE_ERROR_INVALID_OPERATION; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 115 | } |
| 116 | #endif // defined(OS_NACL) |
| 117 | |
| 118 | } // namespace |
| 119 | |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 120 | void File::Info::FromStat(const stat_wrapper_t& stat_info) { |
| 121 | is_directory = S_ISDIR(stat_info.st_mode); |
| 122 | is_symbolic_link = S_ISLNK(stat_info.st_mode); |
| 123 | size = stat_info.st_size; |
| 124 | |
| 125 | #if defined(OS_LINUX) |
| 126 | time_t last_modified_sec = stat_info.st_mtim.tv_sec; |
| 127 | int64 last_modified_nsec = stat_info.st_mtim.tv_nsec; |
| 128 | time_t last_accessed_sec = stat_info.st_atim.tv_sec; |
| 129 | int64 last_accessed_nsec = stat_info.st_atim.tv_nsec; |
| 130 | time_t creation_time_sec = stat_info.st_ctim.tv_sec; |
| 131 | int64 creation_time_nsec = stat_info.st_ctim.tv_nsec; |
| 132 | #elif defined(OS_ANDROID) |
| 133 | time_t last_modified_sec = stat_info.st_mtime; |
| 134 | int64 last_modified_nsec = stat_info.st_mtime_nsec; |
| 135 | time_t last_accessed_sec = stat_info.st_atime; |
| 136 | int64 last_accessed_nsec = stat_info.st_atime_nsec; |
| 137 | time_t creation_time_sec = stat_info.st_ctime; |
| 138 | int64 creation_time_nsec = stat_info.st_ctime_nsec; |
| 139 | #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) |
| 140 | time_t last_modified_sec = stat_info.st_mtimespec.tv_sec; |
| 141 | int64 last_modified_nsec = stat_info.st_mtimespec.tv_nsec; |
| 142 | time_t last_accessed_sec = stat_info.st_atimespec.tv_sec; |
| 143 | int64 last_accessed_nsec = stat_info.st_atimespec.tv_nsec; |
| 144 | time_t creation_time_sec = stat_info.st_ctimespec.tv_sec; |
| 145 | int64 creation_time_nsec = stat_info.st_ctimespec.tv_nsec; |
| 146 | #else |
| 147 | time_t last_modified_sec = stat_info.st_mtime; |
| 148 | int64 last_modified_nsec = 0; |
| 149 | time_t last_accessed_sec = stat_info.st_atime; |
| 150 | int64 last_accessed_nsec = 0; |
| 151 | time_t creation_time_sec = stat_info.st_ctime; |
| 152 | int64 creation_time_nsec = 0; |
| 153 | #endif |
| 154 | |
| 155 | last_modified = |
| 156 | Time::FromTimeT(last_modified_sec) + |
| 157 | TimeDelta::FromMicroseconds(last_modified_nsec / |
| 158 | Time::kNanosecondsPerMicrosecond); |
| 159 | |
| 160 | last_accessed = |
| 161 | Time::FromTimeT(last_accessed_sec) + |
| 162 | TimeDelta::FromMicroseconds(last_accessed_nsec / |
| 163 | Time::kNanosecondsPerMicrosecond); |
| 164 | |
| 165 | creation_time = |
| 166 | Time::FromTimeT(creation_time_sec) + |
| 167 | TimeDelta::FromMicroseconds(creation_time_nsec / |
| 168 | Time::kNanosecondsPerMicrosecond); |
| 169 | } |
| 170 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 171 | // NaCl doesn't implement system calls to open files directly. |
| 172 | #if !defined(OS_NACL) |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 173 | // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 174 | void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 175 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 176 | DCHECK(!IsValid()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 177 | |
| 178 | int open_flags = 0; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 179 | if (flags & FLAG_CREATE) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 180 | open_flags = O_CREAT | O_EXCL; |
| 181 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 182 | created_ = false; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 183 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 184 | if (flags & FLAG_CREATE_ALWAYS) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 185 | DCHECK(!open_flags); |
| 186 | open_flags = O_CREAT | O_TRUNC; |
| 187 | } |
| 188 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 189 | if (flags & FLAG_OPEN_TRUNCATED) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 190 | DCHECK(!open_flags); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 191 | DCHECK(flags & FLAG_WRITE); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 192 | open_flags = O_TRUNC; |
| 193 | } |
| 194 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 195 | if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 196 | NOTREACHED(); |
| 197 | errno = EOPNOTSUPP; |
[email protected] | ee8808c | 2014-01-08 22:30:21 | [diff] [blame] | 198 | error_details_ = FILE_ERROR_FAILED; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 199 | return; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 202 | if (flags & FLAG_WRITE && flags & FLAG_READ) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 203 | open_flags |= O_RDWR; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 204 | } else if (flags & FLAG_WRITE) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 205 | open_flags |= O_WRONLY; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 206 | } else if (!(flags & FLAG_READ) && |
| 207 | !(flags & FLAG_WRITE_ATTRIBUTES) && |
| 208 | !(flags & FLAG_APPEND) && |
| 209 | !(flags & FLAG_OPEN_ALWAYS)) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 210 | NOTREACHED(); |
| 211 | } |
| 212 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 213 | if (flags & FLAG_TERMINAL_DEVICE) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 214 | open_flags |= O_NOCTTY | O_NDELAY; |
| 215 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 216 | if (flags & FLAG_APPEND && flags & FLAG_READ) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 217 | open_flags |= O_APPEND | O_RDWR; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 218 | else if (flags & FLAG_APPEND) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 219 | open_flags |= O_APPEND | O_WRONLY; |
| 220 | |
| 221 | COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
| 222 | |
| 223 | int mode = S_IRUSR | S_IWUSR; |
| 224 | #if defined(OS_CHROMEOS) |
| 225 | mode |= S_IRGRP | S_IROTH; |
| 226 | #endif |
| 227 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 228 | int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 229 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 230 | if (flags & FLAG_OPEN_ALWAYS) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 231 | if (descriptor < 0) { |
| 232 | open_flags |= O_CREAT; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 233 | if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 234 | open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 235 | |
| 236 | descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
| 237 | if (descriptor >= 0) |
| 238 | created_ = true; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 242 | if (descriptor < 0) { |
| 243 | error_details_ = File::OSErrorToFileError(errno); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 248 | created_ = true; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 249 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 250 | if (flags & FLAG_DELETE_ON_CLOSE) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 251 | unlink(name.value().c_str()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 252 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 253 | async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 254 | error_details_ = FILE_OK; |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 255 | file_.reset(descriptor); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 256 | } |
| 257 | #endif // !defined(OS_NACL) |
| 258 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 259 | bool File::IsValid() const { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 260 | return file_.is_valid(); |
| 261 | } |
| 262 | |
| 263 | PlatformFile File::GetPlatformFile() const { |
| 264 | return file_.get(); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 267 | PlatformFile File::TakePlatformFile() { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 268 | return file_.release(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void File::Close() { |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 272 | if (!IsValid()) |
| 273 | return; |
| 274 | |
[email protected] | 48abb28 | 2014-02-21 17:25:53 | [diff] [blame] | 275 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 276 | file_.reset(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | int64 File::Seek(Whence whence, int64 offset) { |
| 280 | base::ThreadRestrictions::AssertIOAllowed(); |
| 281 | DCHECK(IsValid()); |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 282 | if (offset < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 283 | return -1; |
| 284 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 285 | return lseek(file_.get(), static_cast<off_t>(offset), |
| 286 | static_cast<int>(whence)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 289 | int File::Read(int64 offset, char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 290 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 291 | DCHECK(IsValid()); |
| 292 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 293 | return -1; |
| 294 | |
| 295 | int bytes_read = 0; |
| 296 | int rv; |
| 297 | do { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 298 | rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 299 | size - bytes_read, offset + bytes_read)); |
| 300 | if (rv <= 0) |
| 301 | break; |
| 302 | |
| 303 | bytes_read += rv; |
| 304 | } while (bytes_read < size); |
| 305 | |
| 306 | return bytes_read ? bytes_read : rv; |
| 307 | } |
| 308 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 309 | int File::ReadAtCurrentPos(char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 310 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 311 | DCHECK(IsValid()); |
| 312 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 313 | return -1; |
| 314 | |
| 315 | int bytes_read = 0; |
| 316 | int rv; |
| 317 | do { |
[email protected] | 09ebff2 | 2014-03-31 22:18:09 | [diff] [blame] | 318 | rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 319 | if (rv <= 0) |
| 320 | break; |
| 321 | |
| 322 | bytes_read += rv; |
| 323 | } while (bytes_read < size); |
| 324 | |
| 325 | return bytes_read ? bytes_read : rv; |
| 326 | } |
| 327 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 328 | int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 329 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 330 | DCHECK(IsValid()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 331 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 332 | return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 335 | int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 336 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 337 | DCHECK(IsValid()); |
| 338 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 339 | return -1; |
| 340 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 341 | return HANDLE_EINTR(read(file_.get(), data, size)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 344 | int File::Write(int64 offset, const char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 345 | base::ThreadRestrictions::AssertIOAllowed(); |
| 346 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 347 | if (IsOpenAppend(file_.get())) |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 348 | return WriteAtCurrentPos(data, size); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 349 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 350 | DCHECK(IsValid()); |
| 351 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 352 | return -1; |
| 353 | |
| 354 | int bytes_written = 0; |
| 355 | int rv; |
| 356 | do { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 357 | rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 358 | size - bytes_written, offset + bytes_written)); |
| 359 | if (rv <= 0) |
| 360 | break; |
| 361 | |
| 362 | bytes_written += rv; |
| 363 | } while (bytes_written < size); |
| 364 | |
| 365 | return bytes_written ? bytes_written : rv; |
| 366 | } |
| 367 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 368 | int File::WriteAtCurrentPos(const char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 369 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 370 | DCHECK(IsValid()); |
| 371 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 372 | return -1; |
| 373 | |
| 374 | int bytes_written = 0; |
| 375 | int rv; |
| 376 | do { |
[email protected] | 09ebff2 | 2014-03-31 22:18:09 | [diff] [blame] | 377 | rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
| 378 | size - bytes_written)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 379 | if (rv <= 0) |
| 380 | break; |
| 381 | |
| 382 | bytes_written += rv; |
| 383 | } while (bytes_written < size); |
| 384 | |
| 385 | return bytes_written ? bytes_written : rv; |
| 386 | } |
| 387 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 388 | int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 389 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 390 | DCHECK(IsValid()); |
| 391 | if (size < 0) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 392 | return -1; |
| 393 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 394 | return HANDLE_EINTR(write(file_.get(), data, size)); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 397 | int64 File::GetLength() { |
| 398 | DCHECK(IsValid()); |
| 399 | |
| 400 | stat_wrapper_t file_info; |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 401 | if (CallFstat(file_.get(), &file_info)) |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 402 | return false; |
| 403 | |
| 404 | return file_info.st_size; |
| 405 | } |
| 406 | |
| 407 | bool File::SetLength(int64 length) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 408 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 409 | DCHECK(IsValid()); |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 410 | return !CallFtruncate(file_.get(), length); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 411 | } |
| 412 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 413 | bool File::Flush() { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 414 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 415 | DCHECK(IsValid()); |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 416 | return !CallFsync(file_.get()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 419 | bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 420 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 421 | DCHECK(IsValid()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 422 | |
| 423 | timeval times[2]; |
| 424 | times[0] = last_access_time.ToTimeVal(); |
| 425 | times[1] = last_modified_time.ToTimeVal(); |
| 426 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 427 | return !CallFutimes(file_.get(), times); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 430 | bool File::GetInfo(Info* info) { |
| 431 | DCHECK(IsValid()); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 432 | |
| 433 | stat_wrapper_t file_info; |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 434 | if (CallFstat(file_.get(), &file_info)) |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 435 | return false; |
| 436 | |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 437 | info->FromStat(file_info); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 438 | return true; |
| 439 | } |
| 440 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 441 | File::Error File::Lock() { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 442 | return CallFctnlFlock(file_.get(), true); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 445 | File::Error File::Unlock() { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 446 | return CallFctnlFlock(file_.get(), false); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 447 | } |
| 448 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 449 | // Static. |
| 450 | File::Error File::OSErrorToFileError(int saved_errno) { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 451 | switch (saved_errno) { |
| 452 | case EACCES: |
| 453 | case EISDIR: |
| 454 | case EROFS: |
| 455 | case EPERM: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 456 | return FILE_ERROR_ACCESS_DENIED; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 457 | #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. |
| 458 | case ETXTBSY: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 459 | return FILE_ERROR_IN_USE; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 460 | #endif |
| 461 | case EEXIST: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 462 | return FILE_ERROR_EXISTS; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 463 | case ENOENT: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 464 | return FILE_ERROR_NOT_FOUND; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 465 | case EMFILE: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 466 | return FILE_ERROR_TOO_MANY_OPENED; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 467 | case ENOMEM: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 468 | return FILE_ERROR_NO_MEMORY; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 469 | case ENOSPC: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 470 | return FILE_ERROR_NO_SPACE; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 471 | case ENOTDIR: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 472 | return FILE_ERROR_NOT_A_DIRECTORY; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 473 | default: |
| 474 | #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 475 | UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
| 476 | saved_errno); |
| 477 | #endif |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 478 | return FILE_ERROR_FAILED; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 482 | void File::SetPlatformFile(PlatformFile file) { |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 483 | DCHECK(!file_.is_valid()); |
| 484 | file_.reset(file); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 487 | } // namespace base |