Fix platform conditionalization of NaCl IRT file name
[chromium-blink-merge.git] / base / platform_file_win.cc
blobd3755aaa62b5a809c821c1a687206b6c234a8d4d
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"
11 namespace base {
13 PlatformFile CreatePlatformFile(const FilePath& name,
14 int flags,
15 bool* created,
16 PlatformFileError* error_code) {
17 base::ThreadRestrictions::AssertIOAllowed();
19 DWORD disposition = 0;
20 if (created)
21 *created = false;
23 if (flags & PLATFORM_FILE_OPEN)
24 disposition = OPEN_EXISTING;
26 if (flags & PLATFORM_FILE_CREATE) {
27 DCHECK(!disposition);
28 disposition = CREATE_NEW;
31 if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
32 DCHECK(!disposition);
33 disposition = OPEN_ALWAYS;
36 if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
37 DCHECK(!disposition);
38 disposition = CREATE_ALWAYS;
41 if (flags & PLATFORM_FILE_OPEN_TRUNCATED) {
42 DCHECK(!disposition);
43 DCHECK(flags & PLATFORM_FILE_WRITE);
44 disposition = TRUNCATE_EXISTING;
47 if (!disposition) {
48 NOTREACHED();
49 return NULL;
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))
81 *created = true;
84 if (error_code) {
85 if (file != kInvalidPlatformFileValue)
86 *error_code = PLATFORM_FILE_OK;
87 else {
88 DWORD last_error = GetLastError();
89 switch (last_error) {
90 case ERROR_SHARING_VIOLATION:
91 *error_code = PLATFORM_FILE_ERROR_IN_USE;
92 break;
93 case ERROR_FILE_EXISTS:
94 *error_code = PLATFORM_FILE_ERROR_EXISTS;
95 break;
96 case ERROR_FILE_NOT_FOUND:
97 *error_code = PLATFORM_FILE_ERROR_NOT_FOUND;
98 break;
99 case ERROR_ACCESS_DENIED:
100 *error_code = PLATFORM_FILE_ERROR_ACCESS_DENIED;
101 break;
102 default:
103 *error_code = PLATFORM_FILE_ERROR_FAILED;
108 return file;
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)
119 return -1;
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;
128 DWORD bytes_read;
129 if (::ReadFile(file, data, size, &bytes_read, &overlapped) != 0)
130 return bytes_read;
131 else if (ERROR_HANDLE_EOF == GetLastError())
132 return 0;
134 return -1;
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)
146 return -1;
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;
155 DWORD bytes_written;
156 if (::WriteFile(file, data, size, &bytes_written, &overlapped) != 0)
157 return bytes_written;
159 return -1;
162 bool TruncatePlatformFile(PlatformFile file, int64 length) {
163 base::ThreadRestrictions::AssertIOAllowed();
164 if (file == kInvalidPlatformFileValue)
165 return false;
167 // Get the current file pointer.
168 LARGE_INTEGER file_pointer;
169 LARGE_INTEGER zero;
170 zero.QuadPart = 0;
171 if (::SetFilePointerEx(file, zero, &file_pointer, FILE_CURRENT) == 0)
172 return false;
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))
179 return false;
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)
197 return false;
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();
207 if (!info)
208 return false;
210 BY_HANDLE_FILE_INFORMATION file_info;
211 if (GetFileInformationByHandle(file, &file_info) == 0)
212 return false;
214 LARGE_INTEGER size;
215 size.HighPart = file_info.nFileSizeHigh;
216 size.LowPart = file_info.nFileSizeLow;
217 info->size = size.QuadPart;
218 info->is_directory =
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);
224 return true;
227 } // namespace base