1 // Copyright (c) 2011 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.
5 #include "base/platform_file.h"
12 #include "base/eintr_wrapper.h"
13 #include "base/file_path.h"
14 #include "base/logging.h"
15 #include "base/utf_string_conversions.h"
17 #if defined(OS_ANDROID)
18 #include "base/os_compat_android.h"
23 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \
24 (defined(OS_MACOSX) && \
25 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
26 typedef struct stat stat_wrapper_t;
27 static int CallFstat(int fd, stat_wrapper_t *sb) {
31 typedef struct stat64 stat_wrapper_t;
32 static int CallFstat(int fd, stat_wrapper_t *sb) {
33 return fstat64(fd, sb);
37 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
38 PlatformFile CreatePlatformFile(const FilePath& name, int flags,
39 bool* created, PlatformFileError* error_code) {
41 if (flags & PLATFORM_FILE_CREATE)
42 open_flags = O_CREAT | O_EXCL;
47 if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
49 open_flags = O_CREAT | O_TRUNC;
52 if (flags & PLATFORM_FILE_OPEN_TRUNCATED) {
54 DCHECK(flags & PLATFORM_FILE_WRITE);
58 if (!open_flags && !(flags & PLATFORM_FILE_OPEN) &&
59 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
63 *error_code = PLATFORM_FILE_ERROR_FAILED;
64 return kInvalidPlatformFileValue;
67 if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
69 } else if (flags & PLATFORM_FILE_WRITE) {
70 open_flags |= O_WRONLY;
71 } else if (!(flags & PLATFORM_FILE_READ) &&
72 !(flags & PLATFORM_FILE_WRITE_ATTRIBUTES) &&
73 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
77 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
80 HANDLE_EINTR(open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR));
82 if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
83 if (descriptor <= 0) {
84 open_flags |= O_CREAT;
85 if (flags & PLATFORM_FILE_EXCLUSIVE_READ ||
86 flags & PLATFORM_FILE_EXCLUSIVE_WRITE) {
87 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
89 descriptor = HANDLE_EINTR(
90 open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR));
91 if (created && descriptor > 0)
96 if (created && (descriptor > 0) &&
97 (flags & (PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_CREATE)))
100 if ((descriptor > 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) {
101 unlink(name.value().c_str());
106 *error_code = PLATFORM_FILE_OK;
113 *error_code = PLATFORM_FILE_ERROR_ACCESS_DENIED;
116 *error_code = PLATFORM_FILE_ERROR_IN_USE;
119 *error_code = PLATFORM_FILE_ERROR_EXISTS;
122 *error_code = PLATFORM_FILE_ERROR_NOT_FOUND;
125 *error_code = PLATFORM_FILE_ERROR_TOO_MANY_OPENED;
128 *error_code = PLATFORM_FILE_ERROR_NO_MEMORY;
131 *error_code = PLATFORM_FILE_ERROR_NO_SPACE;
134 *error_code = PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
137 *error_code = PLATFORM_FILE_ERROR_FAILED;
145 bool ClosePlatformFile(PlatformFile file) {
146 return !HANDLE_EINTR(close(file));
149 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) {
153 return HANDLE_EINTR(pread(file, data, size, offset));
156 int WritePlatformFile(PlatformFile file, int64 offset,
157 const char* data, int size) {
161 return HANDLE_EINTR(pwrite(file, data, size, offset));
164 bool TruncatePlatformFile(PlatformFile file, int64 length) {
165 return ((file >= 0) && !HANDLE_EINTR(ftruncate(file, length)));
168 bool FlushPlatformFile(PlatformFile file) {
169 return !HANDLE_EINTR(fsync(file));
172 bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time,
173 const base::Time& last_modified_time) {
178 times[0] = last_access_time.ToTimeVal();
179 times[1] = last_modified_time.ToTimeVal();
180 return !futimes(file, times);
183 bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) {
187 stat_wrapper_t file_info;
188 if (CallFstat(file, &file_info))
191 info->is_directory = S_ISDIR(file_info.st_mode);
192 info->is_symbolic_link = S_ISLNK(file_info.st_mode);
193 info->size = file_info.st_size;
194 info->last_modified = base::Time::FromTimeT(file_info.st_mtime);
195 info->last_accessed = base::Time::FromTimeT(file_info.st_atime);
196 info->creation_time = base::Time::FromTimeT(file_info.st_ctime);