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 WritePlatformFile(PlatformFile file
, int64 offset
,
138 const char* data
, int size
) {
139 base::ThreadRestrictions::AssertIOAllowed();
140 if (file
== kInvalidPlatformFileValue
)
143 LARGE_INTEGER offset_li
;
144 offset_li
.QuadPart
= offset
;
146 OVERLAPPED overlapped
= {0};
147 overlapped
.Offset
= offset_li
.LowPart
;
148 overlapped
.OffsetHigh
= offset_li
.HighPart
;
151 if (::WriteFile(file
, data
, size
, &bytes_written
, &overlapped
) != 0)
152 return bytes_written
;
157 bool TruncatePlatformFile(PlatformFile file
, int64 length
) {
158 base::ThreadRestrictions::AssertIOAllowed();
159 if (file
== kInvalidPlatformFileValue
)
162 // Get the current file pointer.
163 LARGE_INTEGER file_pointer
;
166 if (::SetFilePointerEx(file
, zero
, &file_pointer
, FILE_CURRENT
) == 0)
169 LARGE_INTEGER length_li
;
170 length_li
.QuadPart
= length
;
171 // If length > file size, SetFilePointerEx() should extend the file
172 // with zeroes on all Windows standard file systems (NTFS, FATxx).
173 if (!::SetFilePointerEx(file
, length_li
, NULL
, FILE_BEGIN
))
176 // Set the new file length and move the file pointer to its old position.
177 // This is consistent with ftruncate()'s behavior, even when the file
178 // pointer points to a location beyond the end of the file.
179 return ((::SetEndOfFile(file
) != 0) &&
180 (::SetFilePointerEx(file
, file_pointer
, NULL
, FILE_BEGIN
) != 0));
183 bool FlushPlatformFile(PlatformFile file
) {
184 base::ThreadRestrictions::AssertIOAllowed();
185 return ((file
!= kInvalidPlatformFileValue
) && ::FlushFileBuffers(file
));
188 bool TouchPlatformFile(PlatformFile file
, const base::Time
& last_access_time
,
189 const base::Time
& last_modified_time
) {
190 base::ThreadRestrictions::AssertIOAllowed();
191 if (file
== kInvalidPlatformFileValue
)
194 FILETIME last_access_filetime
= last_access_time
.ToFileTime();
195 FILETIME last_modified_filetime
= last_modified_time
.ToFileTime();
196 return (::SetFileTime(file
, NULL
, &last_access_filetime
,
197 &last_modified_filetime
) != 0);
200 bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
) {
201 base::ThreadRestrictions::AssertIOAllowed();
205 BY_HANDLE_FILE_INFORMATION file_info
;
206 if (GetFileInformationByHandle(file
, &file_info
) == 0)
210 size
.HighPart
= file_info
.nFileSizeHigh
;
211 size
.LowPart
= file_info
.nFileSizeLow
;
212 info
->size
= size
.QuadPart
;
214 file_info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
!= 0;
215 info
->is_symbolic_link
= false; // Windows doesn't have symbolic links.
216 info
->last_modified
= base::Time::FromFileTime(file_info
.ftLastWriteTime
);
217 info
->last_accessed
= base::Time::FromFileTime(file_info
.ftLastAccessTime
);
218 info
->creation_time
= base::Time::FromFileTime(file_info
.ftCreationTime
);
222 } // namespace disk_cache