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"
7 #include "base/file_path.h"
8 #include "base/logging.h"
9 #include "base/threading/thread_restrictions.h"
13 PlatformFile
CreatePlatformFile(const FilePath
& name
,
16 PlatformFileError
* error_code
) {
17 base::ThreadRestrictions::AssertIOAllowed();
19 DWORD disposition
= 0;
23 if (flags
& PLATFORM_FILE_OPEN
)
24 disposition
= OPEN_EXISTING
;
26 if (flags
& PLATFORM_FILE_CREATE
) {
28 disposition
= CREATE_NEW
;
31 if (flags
& PLATFORM_FILE_OPEN_ALWAYS
) {
33 disposition
= OPEN_ALWAYS
;
36 if (flags
& PLATFORM_FILE_CREATE_ALWAYS
) {
38 disposition
= CREATE_ALWAYS
;
41 if (flags
& PLATFORM_FILE_OPEN_TRUNCATED
) {
43 DCHECK(flags
& PLATFORM_FILE_WRITE
);
44 disposition
= TRUNCATE_EXISTING
;
52 DWORD access
= (flags
& PLATFORM_FILE_READ
) ? GENERIC_READ
: 0;
53 if (flags
& PLATFORM_FILE_WRITE
)
54 access
|= GENERIC_WRITE
;
55 if (flags
& PLATFORM_FILE_WRITE_ATTRIBUTES
)
56 access
|= FILE_WRITE_ATTRIBUTES
;
58 DWORD sharing
= (flags
& PLATFORM_FILE_EXCLUSIVE_READ
) ? 0 : FILE_SHARE_READ
;
59 if (!(flags
& PLATFORM_FILE_EXCLUSIVE_WRITE
))
60 sharing
|= FILE_SHARE_WRITE
;
61 if (flags
& PLATFORM_FILE_SHARE_DELETE
)
62 sharing
|= FILE_SHARE_DELETE
;
64 DWORD create_flags
= 0;
65 if (flags
& PLATFORM_FILE_ASYNC
)
66 create_flags
|= FILE_FLAG_OVERLAPPED
;
67 if (flags
& PLATFORM_FILE_TEMPORARY
)
68 create_flags
|= FILE_ATTRIBUTE_TEMPORARY
;
69 if (flags
& PLATFORM_FILE_HIDDEN
)
70 create_flags
|= FILE_ATTRIBUTE_HIDDEN
;
71 if (flags
& PLATFORM_FILE_DELETE_ON_CLOSE
)
72 create_flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
74 HANDLE file
= CreateFile(name
.value().c_str(), access
, sharing
, NULL
,
75 disposition
, create_flags
, NULL
);
77 if (created
&& (INVALID_HANDLE_VALUE
!= file
)) {
78 if (flags
& (PLATFORM_FILE_OPEN_ALWAYS
))
79 *created
= (ERROR_ALREADY_EXISTS
!= GetLastError());
80 else if (flags
& (PLATFORM_FILE_CREATE_ALWAYS
| PLATFORM_FILE_CREATE
))
85 if (file
!= kInvalidPlatformFileValue
)
86 *error_code
= PLATFORM_FILE_OK
;
88 DWORD last_error
= GetLastError();
90 case ERROR_SHARING_VIOLATION
:
91 *error_code
= PLATFORM_FILE_ERROR_IN_USE
;
93 case ERROR_FILE_EXISTS
:
94 *error_code
= PLATFORM_FILE_ERROR_EXISTS
;
96 case ERROR_FILE_NOT_FOUND
:
97 *error_code
= PLATFORM_FILE_ERROR_NOT_FOUND
;
99 case ERROR_ACCESS_DENIED
:
100 *error_code
= PLATFORM_FILE_ERROR_ACCESS_DENIED
;
103 *error_code
= PLATFORM_FILE_ERROR_FAILED
;
111 bool ClosePlatformFile(PlatformFile file
) {
112 base::ThreadRestrictions::AssertIOAllowed();
113 return (CloseHandle(file
) != 0);
116 int ReadPlatformFile(PlatformFile file
, int64 offset
, char* data
, int size
) {
117 base::ThreadRestrictions::AssertIOAllowed();
118 if (file
== kInvalidPlatformFileValue
)
121 LARGE_INTEGER offset_li
;
122 offset_li
.QuadPart
= offset
;
124 OVERLAPPED overlapped
= {0};
125 overlapped
.Offset
= offset_li
.LowPart
;
126 overlapped
.OffsetHigh
= offset_li
.HighPart
;
129 if (::ReadFile(file
, data
, size
, &bytes_read
, &overlapped
) != 0)
131 else if (ERROR_HANDLE_EOF
== GetLastError())
137 int ReadPlatformFileNoBestEffort(PlatformFile file
, int64 offset
,
138 char* data
, int size
) {
139 return ReadPlatformFile(file
, offset
, data
, size
);
142 int WritePlatformFile(PlatformFile file
, int64 offset
,
143 const char* data
, int size
) {
144 base::ThreadRestrictions::AssertIOAllowed();
145 if (file
== kInvalidPlatformFileValue
)
148 LARGE_INTEGER offset_li
;
149 offset_li
.QuadPart
= offset
;
151 OVERLAPPED overlapped
= {0};
152 overlapped
.Offset
= offset_li
.LowPart
;
153 overlapped
.OffsetHigh
= offset_li
.HighPart
;
156 if (::WriteFile(file
, data
, size
, &bytes_written
, &overlapped
) != 0)
157 return bytes_written
;
162 bool TruncatePlatformFile(PlatformFile file
, int64 length
) {
163 base::ThreadRestrictions::AssertIOAllowed();
164 if (file
== kInvalidPlatformFileValue
)
167 // Get the current file pointer.
168 LARGE_INTEGER file_pointer
;
171 if (::SetFilePointerEx(file
, zero
, &file_pointer
, FILE_CURRENT
) == 0)
174 LARGE_INTEGER length_li
;
175 length_li
.QuadPart
= length
;
176 // If length > file size, SetFilePointerEx() should extend the file
177 // with zeroes on all Windows standard file systems (NTFS, FATxx).
178 if (!::SetFilePointerEx(file
, length_li
, NULL
, FILE_BEGIN
))
181 // Set the new file length and move the file pointer to its old position.
182 // This is consistent with ftruncate()'s behavior, even when the file
183 // pointer points to a location beyond the end of the file.
184 return ((::SetEndOfFile(file
) != 0) &&
185 (::SetFilePointerEx(file
, file_pointer
, NULL
, FILE_BEGIN
) != 0));
188 bool FlushPlatformFile(PlatformFile file
) {
189 base::ThreadRestrictions::AssertIOAllowed();
190 return ((file
!= kInvalidPlatformFileValue
) && ::FlushFileBuffers(file
));
193 bool TouchPlatformFile(PlatformFile file
, const base::Time
& last_access_time
,
194 const base::Time
& last_modified_time
) {
195 base::ThreadRestrictions::AssertIOAllowed();
196 if (file
== kInvalidPlatformFileValue
)
199 FILETIME last_access_filetime
= last_access_time
.ToFileTime();
200 FILETIME last_modified_filetime
= last_modified_time
.ToFileTime();
201 return (::SetFileTime(file
, NULL
, &last_access_filetime
,
202 &last_modified_filetime
) != 0);
205 bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
) {
206 base::ThreadRestrictions::AssertIOAllowed();
210 BY_HANDLE_FILE_INFORMATION file_info
;
211 if (GetFileInformationByHandle(file
, &file_info
) == 0)
215 size
.HighPart
= file_info
.nFileSizeHigh
;
216 size
.LowPart
= file_info
.nFileSizeLow
;
217 info
->size
= size
.QuadPart
;
219 file_info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
!= 0;
220 info
->is_symbolic_link
= false; // Windows doesn't have symbolic links.
221 info
->last_modified
= base::Time::FromFileTime(file_info
.ftLastWriteTime
);
222 info
->last_accessed
= base::Time::FromFileTime(file_info
.ftLastAccessTime
);
223 info
->creation_time
= base::Time::FromFileTime(file_info
.ftCreationTime
);