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"
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"
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();
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
);
43 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
44 PlatformFile
CreatePlatformFileUnsafe(const FilePath
& name
,
47 PlatformFileError
* error
) {
48 base::ThreadRestrictions::AssertIOAllowed();
51 if (flags
& PLATFORM_FILE_CREATE
)
52 open_flags
= O_CREAT
| O_EXCL
;
57 if (flags
& PLATFORM_FILE_CREATE_ALWAYS
) {
59 open_flags
= O_CREAT
| O_TRUNC
;
62 if (flags
& PLATFORM_FILE_OPEN_TRUNCATED
) {
64 DCHECK(flags
& PLATFORM_FILE_WRITE
);
68 if (!open_flags
&& !(flags
& PLATFORM_FILE_OPEN
) &&
69 !(flags
& PLATFORM_FILE_OPEN_ALWAYS
)) {
73 *error
= PLATFORM_FILE_ERROR_FAILED
;
74 return kInvalidPlatformFileValue
;
77 if (flags
& PLATFORM_FILE_WRITE
&& flags
& PLATFORM_FILE_READ
) {
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
)) {
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
;
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)
114 if (created
&& (descriptor
>= 0) &&
115 (flags
& (PLATFORM_FILE_CREATE_ALWAYS
| PLATFORM_FILE_CREATE
)))
118 if ((descriptor
>= 0) && (flags
& PLATFORM_FILE_DELETE_ON_CLOSE
)) {
119 unlink(name
.value().c_str());
124 *error
= PLATFORM_FILE_OK
;
131 *error
= PLATFORM_FILE_ERROR_ACCESS_DENIED
;
134 *error
= PLATFORM_FILE_ERROR_IN_USE
;
137 *error
= PLATFORM_FILE_ERROR_EXISTS
;
140 *error
= PLATFORM_FILE_ERROR_NOT_FOUND
;
143 *error
= PLATFORM_FILE_ERROR_TOO_MANY_OPENED
;
146 *error
= PLATFORM_FILE_ERROR_NO_MEMORY
;
149 *error
= PLATFORM_FILE_ERROR_NO_SPACE
;
152 *error
= PLATFORM_FILE_ERROR_NOT_A_DIRECTORY
;
155 *error
= PLATFORM_FILE_ERROR_FAILED
;
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
,
175 base::ThreadRestrictions::AssertIOAllowed();
176 if (file
< 0 || offset
< 0)
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)
190 rv
= HANDLE_EINTR(pread(file
, data
+ bytes_read
,
191 size
- bytes_read
, offset
+ bytes_read
));
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)
209 rv
= HANDLE_EINTR(read(file
, data
, size
));
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();
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)
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)
243 int bytes_written
= 0;
246 rv
= HANDLE_EINTR(pwrite(file
, data
+ bytes_written
,
247 size
- bytes_written
, offset
+ bytes_written
));
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)
263 int bytes_written
= 0;
266 rv
= HANDLE_EINTR(write(file
, data
, size
));
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)
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();
302 times
[0] = last_access_time
.ToTimeVal();
303 times
[1] = last_modified_time
.ToTimeVal();
305 #ifdef __USE_XOPEN2K8
306 // futimens should be available, but futimes might not be
307 // http://pubs.opengroup.org/onlinepubs/9699919799/
309 timespec ts_times
[2];
310 ts_times
[0].tv_sec
= times
[0].tv_sec
;
311 ts_times
[0].tv_nsec
= times
[0].tv_usec
* 1000;
312 ts_times
[1].tv_sec
= times
[1].tv_sec
;
313 ts_times
[1].tv_nsec
= times
[1].tv_usec
* 1000;
315 return !futimens(file
, ts_times
);
317 return !futimes(file
, times
);
321 bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
) {
325 stat_wrapper_t file_info
;
326 if (CallFstat(file
, &file_info
))
329 info
->is_directory
= S_ISDIR(file_info
.st_mode
);
330 info
->is_symbolic_link
= S_ISLNK(file_info
.st_mode
);
331 info
->size
= file_info
.st_size
;
332 info
->last_modified
= base::Time::FromTimeT(file_info
.st_mtime
);
333 info
->last_accessed
= base::Time::FromTimeT(file_info
.st_atime
);
334 info
->creation_time
= base::Time::FromTimeT(file_info
.st_ctime
);