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"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/threading/thread_restrictions.h"
15 PlatformFile
CreatePlatformFileUnsafe(const FilePath
& name
,
18 PlatformFileError
* error
) {
19 base::ThreadRestrictions::AssertIOAllowed();
21 DWORD disposition
= 0;
25 if (flags
& PLATFORM_FILE_OPEN
)
26 disposition
= OPEN_EXISTING
;
28 if (flags
& PLATFORM_FILE_CREATE
) {
30 disposition
= CREATE_NEW
;
33 if (flags
& PLATFORM_FILE_OPEN_ALWAYS
) {
35 disposition
= OPEN_ALWAYS
;
38 if (flags
& PLATFORM_FILE_CREATE_ALWAYS
) {
40 disposition
= CREATE_ALWAYS
;
43 if (flags
& PLATFORM_FILE_OPEN_TRUNCATED
) {
45 DCHECK(flags
& PLATFORM_FILE_WRITE
);
46 disposition
= TRUNCATE_EXISTING
;
55 if (flags
& PLATFORM_FILE_WRITE
)
56 access
= GENERIC_WRITE
;
57 if (flags
& PLATFORM_FILE_APPEND
) {
59 access
= FILE_APPEND_DATA
;
61 if (flags
& PLATFORM_FILE_READ
)
62 access
|= GENERIC_READ
;
63 if (flags
& PLATFORM_FILE_WRITE_ATTRIBUTES
)
64 access
|= FILE_WRITE_ATTRIBUTES
;
65 if (flags
& PLATFORM_FILE_EXECUTE
)
66 access
|= GENERIC_EXECUTE
;
68 DWORD sharing
= (flags
& PLATFORM_FILE_EXCLUSIVE_READ
) ? 0 : FILE_SHARE_READ
;
69 if (!(flags
& PLATFORM_FILE_EXCLUSIVE_WRITE
))
70 sharing
|= FILE_SHARE_WRITE
;
71 if (flags
& PLATFORM_FILE_SHARE_DELETE
)
72 sharing
|= FILE_SHARE_DELETE
;
74 DWORD create_flags
= 0;
75 if (flags
& PLATFORM_FILE_ASYNC
)
76 create_flags
|= FILE_FLAG_OVERLAPPED
;
77 if (flags
& PLATFORM_FILE_TEMPORARY
)
78 create_flags
|= FILE_ATTRIBUTE_TEMPORARY
;
79 if (flags
& PLATFORM_FILE_HIDDEN
)
80 create_flags
|= FILE_ATTRIBUTE_HIDDEN
;
81 if (flags
& PLATFORM_FILE_DELETE_ON_CLOSE
)
82 create_flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
83 if (flags
& PLATFORM_FILE_BACKUP_SEMANTICS
)
84 create_flags
|= FILE_FLAG_BACKUP_SEMANTICS
;
86 HANDLE file
= CreateFile(name
.value().c_str(), access
, sharing
, NULL
,
87 disposition
, create_flags
, NULL
);
89 if (created
&& (INVALID_HANDLE_VALUE
!= file
)) {
90 if (flags
& (PLATFORM_FILE_OPEN_ALWAYS
))
91 *created
= (ERROR_ALREADY_EXISTS
!= GetLastError());
92 else if (flags
& (PLATFORM_FILE_CREATE_ALWAYS
| PLATFORM_FILE_CREATE
))
97 if (file
!= kInvalidPlatformFileValue
)
98 *error
= PLATFORM_FILE_OK
;
100 *error
= LastErrorToPlatformFileError(GetLastError());
106 FILE* FdopenPlatformFile(PlatformFile file
, const char* mode
) {
107 if (file
== kInvalidPlatformFileValue
)
109 int fd
= _open_osfhandle(reinterpret_cast<intptr_t>(file
), 0);
112 return _fdopen(fd
, mode
);
115 bool ClosePlatformFile(PlatformFile file
) {
116 base::ThreadRestrictions::AssertIOAllowed();
117 return (CloseHandle(file
) != 0);
120 int64
SeekPlatformFile(PlatformFile file
,
121 PlatformFileWhence whence
,
123 base::ThreadRestrictions::AssertIOAllowed();
124 if (file
< 0 || offset
< 0)
127 LARGE_INTEGER distance
, res
;
128 distance
.QuadPart
= offset
;
129 DWORD move_method
= static_cast<DWORD
>(whence
);
130 if (!SetFilePointerEx(file
, distance
, &res
, move_method
))
135 int ReadPlatformFile(PlatformFile file
, int64 offset
, char* data
, int size
) {
136 base::ThreadRestrictions::AssertIOAllowed();
137 if (file
== kInvalidPlatformFileValue
)
140 LARGE_INTEGER offset_li
;
141 offset_li
.QuadPart
= offset
;
143 OVERLAPPED overlapped
= {0};
144 overlapped
.Offset
= offset_li
.LowPart
;
145 overlapped
.OffsetHigh
= offset_li
.HighPart
;
148 if (::ReadFile(file
, data
, size
, &bytes_read
, &overlapped
) != 0)
150 else if (ERROR_HANDLE_EOF
== GetLastError())
156 int ReadPlatformFileAtCurrentPos(PlatformFile file
, char* data
, int size
) {
157 return ReadPlatformFile(file
, 0, data
, size
);
160 int ReadPlatformFileNoBestEffort(PlatformFile file
, int64 offset
, char* data
,
162 return ReadPlatformFile(file
, offset
, data
, size
);
165 int ReadPlatformFileCurPosNoBestEffort(PlatformFile file
,
166 char* data
, int size
) {
167 return ReadPlatformFile(file
, 0, data
, size
);
170 int WritePlatformFile(PlatformFile file
, int64 offset
,
171 const char* data
, int size
) {
172 base::ThreadRestrictions::AssertIOAllowed();
173 if (file
== kInvalidPlatformFileValue
)
176 LARGE_INTEGER offset_li
;
177 offset_li
.QuadPart
= offset
;
179 OVERLAPPED overlapped
= {0};
180 overlapped
.Offset
= offset_li
.LowPart
;
181 overlapped
.OffsetHigh
= offset_li
.HighPart
;
184 if (::WriteFile(file
, data
, size
, &bytes_written
, &overlapped
) != 0)
185 return bytes_written
;
190 int WritePlatformFileAtCurrentPos(PlatformFile file
, const char* data
,
192 return WritePlatformFile(file
, 0, data
, size
);
195 int WritePlatformFileCurPosNoBestEffort(PlatformFile file
,
196 const char* data
, int size
) {
197 return WritePlatformFile(file
, 0, data
, size
);
200 bool TruncatePlatformFile(PlatformFile file
, int64 length
) {
201 base::ThreadRestrictions::AssertIOAllowed();
202 if (file
== kInvalidPlatformFileValue
)
205 // Get the current file pointer.
206 LARGE_INTEGER file_pointer
;
209 if (::SetFilePointerEx(file
, zero
, &file_pointer
, FILE_CURRENT
) == 0)
212 LARGE_INTEGER length_li
;
213 length_li
.QuadPart
= length
;
214 // If length > file size, SetFilePointerEx() should extend the file
215 // with zeroes on all Windows standard file systems (NTFS, FATxx).
216 if (!::SetFilePointerEx(file
, length_li
, NULL
, FILE_BEGIN
))
219 // Set the new file length and move the file pointer to its old position.
220 // This is consistent with ftruncate()'s behavior, even when the file
221 // pointer points to a location beyond the end of the file.
222 return ((::SetEndOfFile(file
) != 0) &&
223 (::SetFilePointerEx(file
, file_pointer
, NULL
, FILE_BEGIN
) != 0));
226 bool FlushPlatformFile(PlatformFile file
) {
227 base::ThreadRestrictions::AssertIOAllowed();
228 return ((file
!= kInvalidPlatformFileValue
) && ::FlushFileBuffers(file
));
231 bool TouchPlatformFile(PlatformFile file
, const base::Time
& last_access_time
,
232 const base::Time
& last_modified_time
) {
233 base::ThreadRestrictions::AssertIOAllowed();
234 if (file
== kInvalidPlatformFileValue
)
237 FILETIME last_access_filetime
= last_access_time
.ToFileTime();
238 FILETIME last_modified_filetime
= last_modified_time
.ToFileTime();
239 return (::SetFileTime(file
, NULL
, &last_access_filetime
,
240 &last_modified_filetime
) != 0);
243 bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
) {
244 base::ThreadRestrictions::AssertIOAllowed();
248 BY_HANDLE_FILE_INFORMATION file_info
;
249 if (GetFileInformationByHandle(file
, &file_info
) == 0)
253 size
.HighPart
= file_info
.nFileSizeHigh
;
254 size
.LowPart
= file_info
.nFileSizeLow
;
255 info
->size
= size
.QuadPart
;
257 (file_info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
258 info
->is_symbolic_link
= false; // Windows doesn't have symbolic links.
259 info
->last_modified
= base::Time::FromFileTime(file_info
.ftLastWriteTime
);
260 info
->last_accessed
= base::Time::FromFileTime(file_info
.ftLastAccessTime
);
261 info
->creation_time
= base::Time::FromFileTime(file_info
.ftCreationTime
);
265 PlatformFileError
LastErrorToPlatformFileError(DWORD last_error
) {
266 switch (last_error
) {
267 case ERROR_SHARING_VIOLATION
:
268 return PLATFORM_FILE_ERROR_IN_USE
;
269 case ERROR_FILE_EXISTS
:
270 return PLATFORM_FILE_ERROR_EXISTS
;
271 case ERROR_FILE_NOT_FOUND
:
272 case ERROR_PATH_NOT_FOUND
:
273 return PLATFORM_FILE_ERROR_NOT_FOUND
;
274 case ERROR_ACCESS_DENIED
:
275 return PLATFORM_FILE_ERROR_ACCESS_DENIED
;
276 case ERROR_TOO_MANY_OPEN_FILES
:
277 return PLATFORM_FILE_ERROR_TOO_MANY_OPENED
;
278 case ERROR_OUTOFMEMORY
:
279 case ERROR_NOT_ENOUGH_MEMORY
:
280 return PLATFORM_FILE_ERROR_NO_MEMORY
;
281 case ERROR_HANDLE_DISK_FULL
:
282 case ERROR_DISK_FULL
:
283 case ERROR_DISK_RESOURCES_EXHAUSTED
:
284 return PLATFORM_FILE_ERROR_NO_SPACE
;
285 case ERROR_USER_MAPPED_FILE
:
286 return PLATFORM_FILE_ERROR_INVALID_OPERATION
;
287 case ERROR_NOT_READY
:
288 case ERROR_SECTOR_NOT_FOUND
:
289 case ERROR_DEV_NOT_EXIST
:
290 case ERROR_IO_DEVICE
:
291 case ERROR_FILE_CORRUPT
:
292 case ERROR_DISK_CORRUPT
:
293 return PLATFORM_FILE_ERROR_IO
;
295 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows",
297 return PLATFORM_FILE_ERROR_FAILED
;