Invoke the iOS hook from TestSuite so each run_all_unittests.cc file does not
[chromium-blink-merge.git] / base / platform_file_posix.cc
blob646c82e8dfd2ec74e6a600a4874f191f360776e9
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.
5 #include "base/platform_file.h"
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/utf_string_conversions.h"
18 #if defined(OS_ANDROID)
19 #include "base/os_compat_android.h"
20 #endif
22 namespace base {
24 // Make sure our Whence mappings match the system headers.
25 COMPILE_ASSERT(PLATFORM_FILE_FROM_BEGIN == SEEK_SET &&
26 PLATFORM_FILE_FROM_CURRENT == SEEK_CUR &&
27 PLATFORM_FILE_FROM_END == SEEK_END, whence_matches_system);
29 #if defined(OS_BSD) || defined(OS_MACOSX)
30 typedef struct stat stat_wrapper_t;
31 static int CallFstat(int fd, stat_wrapper_t *sb) {
32 base::ThreadRestrictions::AssertIOAllowed();
33 return fstat(fd, sb);
35 #else
36 typedef struct stat64 stat_wrapper_t;
37 static int CallFstat(int fd, stat_wrapper_t *sb) {
38 base::ThreadRestrictions::AssertIOAllowed();
39 return fstat64(fd, sb);
41 #endif
43 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
44 PlatformFile CreatePlatformFileUnsafe(const FilePath& name,
45 int flags,
46 bool* created,
47 PlatformFileError* error) {
48 base::ThreadRestrictions::AssertIOAllowed();
50 int open_flags = 0;
51 if (flags & PLATFORM_FILE_CREATE)
52 open_flags = O_CREAT | O_EXCL;
54 if (created)
55 *created = false;
57 if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
58 DCHECK(!open_flags);
59 open_flags = O_CREAT | O_TRUNC;
62 if (flags & PLATFORM_FILE_OPEN_TRUNCATED) {
63 DCHECK(!open_flags);
64 DCHECK(flags & PLATFORM_FILE_WRITE);
65 open_flags = O_TRUNC;
68 if (!open_flags && !(flags & PLATFORM_FILE_OPEN) &&
69 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
70 NOTREACHED();
71 errno = EOPNOTSUPP;
72 if (error)
73 *error = PLATFORM_FILE_ERROR_FAILED;
74 return kInvalidPlatformFileValue;
77 if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
78 open_flags |= O_RDWR;
79 } else if (flags & PLATFORM_FILE_WRITE) {
80 open_flags |= O_WRONLY;
81 } else if (!(flags & PLATFORM_FILE_READ) &&
82 !(flags & PLATFORM_FILE_WRITE_ATTRIBUTES) &&
83 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
84 NOTREACHED();
87 if (flags & PLATFORM_FILE_TERMINAL_DEVICE)
88 open_flags |= O_NOCTTY | O_NDELAY;
90 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
92 int mode = S_IRUSR | S_IWUSR;
93 #if defined(OS_CHROMEOS)
94 mode |= S_IRGRP | S_IROTH;
95 #endif
97 int descriptor =
98 HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
100 if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
101 if (descriptor < 0) {
102 open_flags |= O_CREAT;
103 if (flags & PLATFORM_FILE_EXCLUSIVE_READ ||
104 flags & PLATFORM_FILE_EXCLUSIVE_WRITE) {
105 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
107 descriptor = HANDLE_EINTR(
108 open(name.value().c_str(), open_flags, mode));
109 if (created && descriptor >= 0)
110 *created = true;
114 if (created && (descriptor >= 0) &&
115 (flags & (PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_CREATE)))
116 *created = true;
118 if ((descriptor >= 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) {
119 unlink(name.value().c_str());
122 if (error) {
123 if (descriptor >= 0)
124 *error = PLATFORM_FILE_OK;
125 else {
126 switch (errno) {
127 case EACCES:
128 case EISDIR:
129 case EROFS:
130 case EPERM:
131 *error = PLATFORM_FILE_ERROR_ACCESS_DENIED;
132 break;
133 case ETXTBSY:
134 *error = PLATFORM_FILE_ERROR_IN_USE;
135 break;
136 case EEXIST:
137 *error = PLATFORM_FILE_ERROR_EXISTS;
138 break;
139 case ENOENT:
140 *error = PLATFORM_FILE_ERROR_NOT_FOUND;
141 break;
142 case EMFILE:
143 *error = PLATFORM_FILE_ERROR_TOO_MANY_OPENED;
144 break;
145 case ENOMEM:
146 *error = PLATFORM_FILE_ERROR_NO_MEMORY;
147 break;
148 case ENOSPC:
149 *error = PLATFORM_FILE_ERROR_NO_SPACE;
150 break;
151 case ENOTDIR:
152 *error = PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
153 break;
154 default:
155 *error = PLATFORM_FILE_ERROR_FAILED;
160 return descriptor;
163 FILE* FdopenPlatformFile(PlatformFile file, const char* mode) {
164 return fdopen(file, mode);
167 bool ClosePlatformFile(PlatformFile file) {
168 base::ThreadRestrictions::AssertIOAllowed();
169 return !HANDLE_EINTR(close(file));
172 int64 SeekPlatformFile(PlatformFile file,
173 PlatformFileWhence whence,
174 int64 offset) {
175 base::ThreadRestrictions::AssertIOAllowed();
176 if (file < 0 || offset < 0)
177 return -1;
179 return lseek(file, static_cast<off_t>(offset), static_cast<int>(whence));
182 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) {
183 base::ThreadRestrictions::AssertIOAllowed();
184 if (file < 0 || size < 0)
185 return -1;
187 int bytes_read = 0;
188 int rv;
189 do {
190 rv = HANDLE_EINTR(pread(file, data + bytes_read,
191 size - bytes_read, offset + bytes_read));
192 if (rv <= 0)
193 break;
195 bytes_read += rv;
196 } while (bytes_read < size);
198 return bytes_read ? bytes_read : rv;
201 int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) {
202 base::ThreadRestrictions::AssertIOAllowed();
203 if (file < 0 || size < 0)
204 return -1;
206 int bytes_read = 0;
207 int rv;
208 do {
209 rv = HANDLE_EINTR(read(file, data, size));
210 if (rv <= 0)
211 break;
213 bytes_read += rv;
214 } while (bytes_read < size);
216 return bytes_read ? bytes_read : rv;
219 int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
220 char* data, int size) {
221 base::ThreadRestrictions::AssertIOAllowed();
222 if (file < 0)
223 return -1;
225 return HANDLE_EINTR(pread(file, data, size, offset));
228 int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
229 char* data, int size) {
230 base::ThreadRestrictions::AssertIOAllowed();
231 if (file < 0 || size < 0)
232 return -1;
234 return HANDLE_EINTR(read(file, data, size));
237 int WritePlatformFile(PlatformFile file, int64 offset,
238 const char* data, int size) {
239 base::ThreadRestrictions::AssertIOAllowed();
240 if (file < 0 || size < 0)
241 return -1;
243 int bytes_written = 0;
244 int rv;
245 do {
246 rv = HANDLE_EINTR(pwrite(file, data + bytes_written,
247 size - bytes_written, offset + bytes_written));
248 if (rv <= 0)
249 break;
251 bytes_written += rv;
252 } while (bytes_written < size);
254 return bytes_written ? bytes_written : rv;
257 int WritePlatformFileAtCurrentPos(PlatformFile file,
258 const char* data, int size) {
259 base::ThreadRestrictions::AssertIOAllowed();
260 if (file < 0 || size < 0)
261 return -1;
263 int bytes_written = 0;
264 int rv;
265 do {
266 rv = HANDLE_EINTR(write(file, data, size));
267 if (rv <= 0)
268 break;
270 bytes_written += rv;
271 } while (bytes_written < size);
273 return bytes_written ? bytes_written : rv;
276 int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
277 const char* data, int size) {
278 base::ThreadRestrictions::AssertIOAllowed();
279 if (file < 0 || size < 0)
280 return -1;
282 return HANDLE_EINTR(write(file, data, size));
285 bool TruncatePlatformFile(PlatformFile file, int64 length) {
286 base::ThreadRestrictions::AssertIOAllowed();
287 return ((file >= 0) && !HANDLE_EINTR(ftruncate(file, length)));
290 bool FlushPlatformFile(PlatformFile file) {
291 base::ThreadRestrictions::AssertIOAllowed();
292 return !HANDLE_EINTR(fsync(file));
295 bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time,
296 const base::Time& last_modified_time) {
297 base::ThreadRestrictions::AssertIOAllowed();
298 if (file < 0)
299 return false;
301 timeval times[2];
302 times[0] = last_access_time.ToTimeVal();
303 times[1] = last_modified_time.ToTimeVal();
304 return !futimes(file, times);
307 bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) {
308 if (!info)
309 return false;
311 stat_wrapper_t file_info;
312 if (CallFstat(file, &file_info))
313 return false;
315 info->is_directory = S_ISDIR(file_info.st_mode);
316 info->is_symbolic_link = S_ISLNK(file_info.st_mode);
317 info->size = file_info.st_size;
318 info->last_modified = base::Time::FromTimeT(file_info.st_mtime);
319 info->last_accessed = base::Time::FromTimeT(file_info.st_atime);
320 info->creation_time = base::Time::FromTimeT(file_info.st_ctime);
321 return true;
324 } // namespace base