blob: ed9a5e2a8e3ba1611acfce7c34827a1935a9a06c [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// 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
5#include "base/files/file.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <stdint.h>
10#include <sys/stat.h>
11#include <unistd.h>
12
13#include "base/logging.h"
Scott Graham66962112018-06-08 12:42:08 -070014#include "base/posix/eintr_wrapper.h"
15#include "base/strings/utf_string_conversions.h"
Scott Graham76a8dc72018-06-18 13:37:29 -070016#include "util/build_config.h"
Scott Graham66962112018-06-08 12:42:08 -070017
Scott Graham66962112018-06-08 12:42:08 -070018namespace base {
19
20// Make sure our Whence mappings match the system headers.
21static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR &&
22 File::FROM_END == SEEK_END,
23 "whence mapping must match the system headers");
24
25namespace {
26
27#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) || \
28 defined(OS_ANDROID) && __ANDROID_API__ < 21
Scott Graham98cd3ca2018-06-14 22:26:55 -070029int CallFstat(int fd, stat_wrapper_t* sb) {
Scott Graham66962112018-06-08 12:42:08 -070030 return fstat(fd, sb);
31}
32#else
Scott Graham98cd3ca2018-06-14 22:26:55 -070033int CallFstat(int fd, stat_wrapper_t* sb) {
Scott Graham66962112018-06-08 12:42:08 -070034 return fstat64(fd, sb);
35}
36#endif
37
38// NaCl doesn't provide the following system calls, so either simulate them or
39// wrap them in order to minimize the number of #ifdef's in this file.
40#if !defined(OS_NACL) && !defined(OS_AIX)
41bool IsOpenAppend(PlatformFile file) {
42 return (fcntl(file, F_GETFL) & O_APPEND) != 0;
43}
44
45int CallFtruncate(PlatformFile file, int64_t length) {
46 return HANDLE_EINTR(ftruncate(file, length));
47}
48
Scott Graham66962112018-06-08 12:42:08 -070049#if !defined(OS_FUCHSIA)
50File::Error CallFcntlFlock(PlatformFile file, bool do_lock) {
51 struct flock lock;
52 lock.l_type = do_lock ? F_WRLCK : F_UNLCK;
53 lock.l_whence = SEEK_SET;
54 lock.l_start = 0;
55 lock.l_len = 0; // Lock entire file.
56 if (HANDLE_EINTR(fcntl(file, F_SETLK, &lock)) == -1)
57 return File::GetLastFileError();
58 return File::FILE_OK;
59}
60#endif
61
62#else // defined(OS_NACL) && !defined(OS_AIX)
63
64bool IsOpenAppend(PlatformFile file) {
65 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX
66 // standard and always appends if the file is opened with O_APPEND, just
67 // return false here.
68 return false;
69}
70
71int CallFtruncate(PlatformFile file, int64_t length) {
72 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate.
73 return 0;
74}
75
Scott Graham66962112018-06-08 12:42:08 -070076File::Error CallFcntlFlock(PlatformFile file, bool do_lock) {
77 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct.
78 return File::FILE_ERROR_INVALID_OPERATION;
79}
80#endif // defined(OS_NACL)
81
82} // namespace
83
84void File::Info::FromStat(const stat_wrapper_t& stat_info) {
85 is_directory = S_ISDIR(stat_info.st_mode);
86 is_symbolic_link = S_ISLNK(stat_info.st_mode);
87 size = stat_info.st_size;
88
Peter Collingbourne4dcd2772019-02-05 22:24:34 -080089#if defined(OS_MACOSX)
Scott Graham66962112018-06-08 12:42:08 -070090 time_t last_modified_sec = stat_info.st_mtimespec.tv_sec;
91 int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec;
92 time_t last_accessed_sec = stat_info.st_atimespec.tv_sec;
93 int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec;
94 time_t creation_time_sec = stat_info.st_ctimespec.tv_sec;
95 int64_t creation_time_nsec = stat_info.st_ctimespec.tv_nsec;
John Barboza499868c2018-07-18 22:58:30 -040096#elif defined(OS_AIX)
97 time_t last_modified_sec = stat_info.st_mtime;
98 int64_t last_modified_nsec = 0;
99 time_t last_accessed_sec = stat_info.st_atime;
100 int64_t last_accessed_nsec = 0;
101 time_t creation_time_sec = stat_info.st_ctime;
102 int64_t creation_time_nsec = 0;
Peter Collingbourne4dcd2772019-02-05 22:24:34 -0800103#elif defined(OS_POSIX)
104 time_t last_modified_sec = stat_info.st_mtim.tv_sec;
105 int64_t last_modified_nsec = stat_info.st_mtim.tv_nsec;
106 time_t last_accessed_sec = stat_info.st_atim.tv_sec;
107 int64_t last_accessed_nsec = stat_info.st_atim.tv_nsec;
108 time_t creation_time_sec = stat_info.st_ctim.tv_sec;
109 int64_t creation_time_nsec = stat_info.st_ctim.tv_nsec;
Scott Graham66962112018-06-08 12:42:08 -0700110#else
Scott Graham622472c2018-06-14 16:53:22 -0700111#error
Scott Graham66962112018-06-08 12:42:08 -0700112#endif
113
Scott Grahamce047c92018-06-19 15:56:56 -0700114 constexpr uint64_t kNano = 1'000'000'000;
115 last_modified = last_modified_sec * kNano + last_modified_nsec;
116 last_accessed = last_accessed_sec * kNano + last_accessed_nsec;
117 creation_time = creation_time_sec * kNano + creation_time_nsec;
Scott Graham66962112018-06-08 12:42:08 -0700118}
119
120bool File::IsValid() const {
121 return file_.is_valid();
122}
123
124PlatformFile File::GetPlatformFile() const {
125 return file_.get();
126}
127
128PlatformFile File::TakePlatformFile() {
129 return file_.release();
130}
131
132void File::Close() {
133 if (!IsValid())
134 return;
135
Scott Graham66962112018-06-08 12:42:08 -0700136 file_.reset();
137}
138
139int64_t File::Seek(Whence whence, int64_t offset) {
Scott Graham66962112018-06-08 12:42:08 -0700140 DCHECK(IsValid());
141
Scott Graham66962112018-06-08 12:42:08 -0700142 static_assert(sizeof(int64_t) == sizeof(off_t), "off_t must be 64 bits");
143 return lseek(file_.get(), static_cast<off_t>(offset),
144 static_cast<int>(whence));
Scott Graham66962112018-06-08 12:42:08 -0700145}
146
147int File::Read(int64_t offset, char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700148 DCHECK(IsValid());
149 if (size < 0)
150 return -1;
151
Scott Graham66962112018-06-08 12:42:08 -0700152 int bytes_read = 0;
153 int rv;
154 do {
Scott Graham98cd3ca2018-06-14 22:26:55 -0700155 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, size - bytes_read,
156 offset + bytes_read));
Scott Graham66962112018-06-08 12:42:08 -0700157 if (rv <= 0)
158 break;
159
160 bytes_read += rv;
161 } while (bytes_read < size);
162
163 return bytes_read ? bytes_read : rv;
164}
165
166int File::ReadAtCurrentPos(char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700167 DCHECK(IsValid());
168 if (size < 0)
169 return -1;
170
Scott Graham66962112018-06-08 12:42:08 -0700171 int bytes_read = 0;
172 int rv;
173 do {
174 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read));
175 if (rv <= 0)
176 break;
177
178 bytes_read += rv;
179 } while (bytes_read < size);
180
181 return bytes_read ? bytes_read : rv;
182}
183
184int File::ReadNoBestEffort(int64_t offset, char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700185 DCHECK(IsValid());
Scott Graham66962112018-06-08 12:42:08 -0700186 return HANDLE_EINTR(pread(file_.get(), data, size, offset));
187}
188
189int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700190 DCHECK(IsValid());
191 if (size < 0)
192 return -1;
193
Scott Graham66962112018-06-08 12:42:08 -0700194 return HANDLE_EINTR(read(file_.get(), data, size));
195}
196
197int File::Write(int64_t offset, const char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700198 if (IsOpenAppend(file_.get()))
199 return WriteAtCurrentPos(data, size);
200
201 DCHECK(IsValid());
202 if (size < 0)
203 return -1;
204
Scott Graham66962112018-06-08 12:42:08 -0700205 int bytes_written = 0;
206 int rv;
207 do {
208 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written,
209 size - bytes_written, offset + bytes_written));
210 if (rv <= 0)
211 break;
212
213 bytes_written += rv;
214 } while (bytes_written < size);
215
216 return bytes_written ? bytes_written : rv;
217}
218
219int File::WriteAtCurrentPos(const char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700220 DCHECK(IsValid());
221 if (size < 0)
222 return -1;
223
Scott Graham66962112018-06-08 12:42:08 -0700224 int bytes_written = 0;
225 int rv;
226 do {
Scott Graham98cd3ca2018-06-14 22:26:55 -0700227 rv = HANDLE_EINTR(
228 write(file_.get(), data + bytes_written, size - bytes_written));
Scott Graham66962112018-06-08 12:42:08 -0700229 if (rv <= 0)
230 break;
231
232 bytes_written += rv;
233 } while (bytes_written < size);
234
235 return bytes_written ? bytes_written : rv;
236}
237
238int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
Scott Graham66962112018-06-08 12:42:08 -0700239 DCHECK(IsValid());
240 if (size < 0)
241 return -1;
242
Scott Graham66962112018-06-08 12:42:08 -0700243 return HANDLE_EINTR(write(file_.get(), data, size));
244}
245
246int64_t File::GetLength() {
247 DCHECK(IsValid());
248
Scott Graham66962112018-06-08 12:42:08 -0700249 stat_wrapper_t file_info;
250 if (CallFstat(file_.get(), &file_info))
251 return -1;
252
253 return file_info.st_size;
254}
255
256bool File::SetLength(int64_t length) {
Scott Graham66962112018-06-08 12:42:08 -0700257 DCHECK(IsValid());
258
Scott Graham66962112018-06-08 12:42:08 -0700259 return !CallFtruncate(file_.get(), length);
260}
261
Scott Graham66962112018-06-08 12:42:08 -0700262bool File::GetInfo(Info* info) {
263 DCHECK(IsValid());
264
Scott Graham66962112018-06-08 12:42:08 -0700265 stat_wrapper_t file_info;
266 if (CallFstat(file_.get(), &file_info))
267 return false;
268
269 info->FromStat(file_info);
270 return true;
271}
272
273#if !defined(OS_FUCHSIA)
274File::Error File::Lock() {
Scott Graham66962112018-06-08 12:42:08 -0700275 return CallFcntlFlock(file_.get(), true);
276}
277
278File::Error File::Unlock() {
Scott Graham66962112018-06-08 12:42:08 -0700279 return CallFcntlFlock(file_.get(), false);
280}
281#endif
282
283File File::Duplicate() const {
284 if (!IsValid())
285 return File();
286
Scott Graham66962112018-06-08 12:42:08 -0700287 PlatformFile other_fd = HANDLE_EINTR(dup(GetPlatformFile()));
288 if (other_fd == -1)
289 return File(File::GetLastFileError());
290
291 File other(other_fd);
292 if (async())
293 other.async_ = true;
294 return other;
295}
296
297// Static.
298File::Error File::OSErrorToFileError(int saved_errno) {
299 switch (saved_errno) {
300 case EACCES:
301 case EISDIR:
302 case EROFS:
303 case EPERM:
304 return FILE_ERROR_ACCESS_DENIED;
305 case EBUSY:
306#if !defined(OS_NACL) // ETXTBSY not defined by NaCl.
307 case ETXTBSY:
308#endif
309 return FILE_ERROR_IN_USE;
310 case EEXIST:
311 return FILE_ERROR_EXISTS;
312 case EIO:
313 return FILE_ERROR_IO;
314 case ENOENT:
315 return FILE_ERROR_NOT_FOUND;
316 case ENFILE: // fallthrough
317 case EMFILE:
318 return FILE_ERROR_TOO_MANY_OPENED;
319 case ENOMEM:
320 return FILE_ERROR_NO_MEMORY;
321 case ENOSPC:
322 return FILE_ERROR_NO_SPACE;
323 case ENOTDIR:
324 return FILE_ERROR_NOT_A_DIRECTORY;
325 default:
Scott Graham66962112018-06-08 12:42:08 -0700326 // This function should only be called for errors.
327 DCHECK_NE(0, saved_errno);
328 return FILE_ERROR_FAILED;
329 }
330}
331
332// NaCl doesn't implement system calls to open files directly.
333#if !defined(OS_NACL)
334// TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
335void File::DoInitialize(const FilePath& path, uint32_t flags) {
Scott Graham66962112018-06-08 12:42:08 -0700336 DCHECK(!IsValid());
337
338 int open_flags = 0;
339 if (flags & FLAG_CREATE)
340 open_flags = O_CREAT | O_EXCL;
341
342 created_ = false;
343
344 if (flags & FLAG_CREATE_ALWAYS) {
345 DCHECK(!open_flags);
346 DCHECK(flags & FLAG_WRITE);
347 open_flags = O_CREAT | O_TRUNC;
348 }
349
350 if (flags & FLAG_OPEN_TRUNCATED) {
351 DCHECK(!open_flags);
352 DCHECK(flags & FLAG_WRITE);
353 open_flags = O_TRUNC;
354 }
355
356 if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
357 NOTREACHED();
358 errno = EOPNOTSUPP;
359 error_details_ = FILE_ERROR_FAILED;
360 return;
361 }
362
363 if (flags & FLAG_WRITE && flags & FLAG_READ) {
364 open_flags |= O_RDWR;
365 } else if (flags & FLAG_WRITE) {
366 open_flags |= O_WRONLY;
Scott Graham98cd3ca2018-06-14 22:26:55 -0700367 } else if (!(flags & FLAG_READ) && !(flags & FLAG_WRITE_ATTRIBUTES) &&
368 !(flags & FLAG_APPEND) && !(flags & FLAG_OPEN_ALWAYS)) {
Scott Graham66962112018-06-08 12:42:08 -0700369 NOTREACHED();
370 }
371
372 if (flags & FLAG_TERMINAL_DEVICE)
373 open_flags |= O_NOCTTY | O_NDELAY;
374
375 if (flags & FLAG_APPEND && flags & FLAG_READ)
376 open_flags |= O_APPEND | O_RDWR;
377 else if (flags & FLAG_APPEND)
378 open_flags |= O_APPEND | O_WRONLY;
379
380 static_assert(O_RDONLY == 0, "O_RDONLY must equal zero");
381
382 int mode = S_IRUSR | S_IWUSR;
Scott Graham66962112018-06-08 12:42:08 -0700383 int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
384
385 if (flags & FLAG_OPEN_ALWAYS) {
386 if (descriptor < 0) {
387 open_flags |= O_CREAT;
388 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
Scott Graham98cd3ca2018-06-14 22:26:55 -0700389 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
Scott Graham66962112018-06-08 12:42:08 -0700390
391 descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
392 if (descriptor >= 0)
393 created_ = true;
394 }
395 }
396
397 if (descriptor < 0) {
398 error_details_ = File::GetLastFileError();
399 return;
400 }
401
402 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
403 created_ = true;
404
405 if (flags & FLAG_DELETE_ON_CLOSE)
406 unlink(path.value().c_str());
407
408 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
409 error_details_ = FILE_OK;
410 file_.reset(descriptor);
411}
412#endif // !defined(OS_NACL)
413
414bool File::Flush() {
Scott Graham66962112018-06-08 12:42:08 -0700415 DCHECK(IsValid());
Scott Graham66962112018-06-08 12:42:08 -0700416
Scott Graham622472c2018-06-14 16:53:22 -0700417#if defined(OS_LINUX)
Scott Graham66962112018-06-08 12:42:08 -0700418 return !HANDLE_EINTR(fdatasync(file_.get()));
419#else
420 return !HANDLE_EINTR(fsync(file_.get()));
421#endif
422}
423
424void File::SetPlatformFile(PlatformFile file) {
425 DCHECK(!file_.is_valid());
426 file_.reset(file);
427}
428
429// static
430File::Error File::GetLastFileError() {
431 return base::File::OSErrorToFileError(errno);
432}
433
434} // namespace base