Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: base/files/file_win.cc

Issue 183333004: Implement File::WriteAtCurrentPos for Windows (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/files/file_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file.h" 5 #include "base/files/file.h"
6 6
7 #include <io.h> 7 #include <io.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 return -1; 132 return -1;
133 133
134 LARGE_INTEGER offset_li; 134 LARGE_INTEGER offset_li;
135 offset_li.QuadPart = offset; 135 offset_li.QuadPart = offset;
136 136
137 OVERLAPPED overlapped = {0}; 137 OVERLAPPED overlapped = {0};
138 overlapped.Offset = offset_li.LowPart; 138 overlapped.Offset = offset_li.LowPart;
139 overlapped.OffsetHigh = offset_li.HighPart; 139 overlapped.OffsetHigh = offset_li.HighPart;
140 140
141 DWORD bytes_read; 141 DWORD bytes_read;
142 if (::ReadFile(file_, data, size, &bytes_read, &overlapped) != 0) 142 if (::ReadFile(file_, data, size, &bytes_read, &overlapped))
cpu_(ooo_6.6-7.5) 2014/03/06 20:43:41 I am confused about the style unification here, be
rvargas (doing something else) 2014/03/06 21:55:34 I generally don't want to compare against anything
143 return bytes_read; 143 return bytes_read;
144 if (ERROR_HANDLE_EOF == GetLastError()) 144 if (ERROR_HANDLE_EOF == GetLastError())
145 return 0; 145 return 0;
146 146
147 return -1; 147 return -1;
148 } 148 }
cpu_(ooo_6.6-7.5) 2014/03/06 20:43:41 Just a suggestion: it would look pretty if there
rvargas (doing something else) 2014/03/06 21:55:34 That method would look a little suspicious to me a
149 149
150 int File::ReadAtCurrentPos(char* data, int size) { 150 int File::ReadAtCurrentPos(char* data, int size) {
151 base::ThreadRestrictions::AssertIOAllowed(); 151 base::ThreadRestrictions::AssertIOAllowed();
152 DCHECK(IsValid()); 152 DCHECK(IsValid());
153 DCHECK(!async_); 153 DCHECK(!async_);
154 if (size < 0) 154 if (size < 0)
155 return -1; 155 return -1;
156 156
157 DWORD bytes_read; 157 DWORD bytes_read;
158 if (::ReadFile(file_, data, size, &bytes_read, NULL) != 0) 158 if (::ReadFile(file_, data, size, &bytes_read, NULL))
159 return bytes_read; 159 return bytes_read;
160 if (ERROR_HANDLE_EOF == GetLastError()) 160 if (ERROR_HANDLE_EOF == GetLastError())
161 return 0; 161 return 0;
162 162
163 return -1; 163 return -1;
164 } 164 }
165 165
166 int File::ReadNoBestEffort(int64 offset, char* data, int size) { 166 int File::ReadNoBestEffort(int64 offset, char* data, int size) {
167 return Read(offset, data, size); 167 return Read(offset, data, size);
168 } 168 }
169 169
170 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { 170 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
171 return ReadAtCurrentPos(data, size); 171 return ReadAtCurrentPos(data, size);
172 } 172 }
173 173
174 int File::Write(int64 offset, const char* data, int size) { 174 int File::Write(int64 offset, const char* data, int size) {
175 base::ThreadRestrictions::AssertIOAllowed(); 175 base::ThreadRestrictions::AssertIOAllowed();
176 DCHECK(IsValid()); 176 DCHECK(IsValid());
177 DCHECK(!async_); 177 DCHECK(!async_);
178 178
179 LARGE_INTEGER offset_li; 179 LARGE_INTEGER offset_li;
180 offset_li.QuadPart = offset; 180 offset_li.QuadPart = offset;
181 181
182 OVERLAPPED overlapped = {0}; 182 OVERLAPPED overlapped = {0};
183 overlapped.Offset = offset_li.LowPart; 183 overlapped.Offset = offset_li.LowPart;
184 overlapped.OffsetHigh = offset_li.HighPart; 184 overlapped.OffsetHigh = offset_li.HighPart;
185 185
186 DWORD bytes_written; 186 DWORD bytes_written;
187 if (::WriteFile(file_, data, size, &bytes_written, &overlapped) != 0) 187 if (::WriteFile(file_, data, size, &bytes_written, &overlapped))
188 return bytes_written; 188 return bytes_written;
189 189
190 return -1; 190 return -1;
191 } 191 }
192 192
193 int File::WriteAtCurrentPos(const char* data, int size) { 193 int File::WriteAtCurrentPos(const char* data, int size) {
Nico 2014/03/06 23:57:59 fwiw, i'd have named "write" "writeatoffset" and "
194 NOTREACHED(); 194 base::ThreadRestrictions::AssertIOAllowed();
195 DCHECK(IsValid());
196 DCHECK(!async_);
197 if (size < 0)
198 return -1;
199
200 DWORD bytes_written;
201 if (::WriteFile(file_, data, size, &bytes_written, NULL))
202 return bytes_written;
203
195 return -1; 204 return -1;
196 } 205 }
197 206
198 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { 207 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
199 return WriteAtCurrentPos(data, size); 208 return WriteAtCurrentPos(data, size);
200 } 209 }
201 210
202 int64 File::GetLength() { 211 int64 File::GetLength() {
203 base::ThreadRestrictions::AssertIOAllowed(); 212 base::ThreadRestrictions::AssertIOAllowed();
204 DCHECK(IsValid()); 213 DCHECK(IsValid());
205 LARGE_INTEGER size; 214 LARGE_INTEGER size;
206 if (!::GetFileSizeEx(file_.Get(), &size)) 215 if (!::GetFileSizeEx(file_.Get(), &size))
207 return -1; 216 return -1;
208 217
209 return static_cast<int64>(size.QuadPart); 218 return static_cast<int64>(size.QuadPart);
210 } 219 }
211 220
212 bool File::SetLength(int64 length) { 221 bool File::SetLength(int64 length) {
213 base::ThreadRestrictions::AssertIOAllowed(); 222 base::ThreadRestrictions::AssertIOAllowed();
214 DCHECK(IsValid()); 223 DCHECK(IsValid());
215 224
216 // Get the current file pointer. 225 // Get the current file pointer.
217 LARGE_INTEGER file_pointer; 226 LARGE_INTEGER file_pointer;
218 LARGE_INTEGER zero; 227 LARGE_INTEGER zero;
219 zero.QuadPart = 0; 228 zero.QuadPart = 0;
220 if (::SetFilePointerEx(file_, zero, &file_pointer, FILE_CURRENT) == 0) 229 if (!::SetFilePointerEx(file_, zero, &file_pointer, FILE_CURRENT))
221 return false; 230 return false;
222 231
223 LARGE_INTEGER length_li; 232 LARGE_INTEGER length_li;
224 length_li.QuadPart = length; 233 length_li.QuadPart = length;
225 // If length > file size, SetFilePointerEx() should extend the file 234 // If length > file size, SetFilePointerEx() should extend the file
226 // with zeroes on all Windows standard file systems (NTFS, FATxx). 235 // with zeroes on all Windows standard file systems (NTFS, FATxx).
227 if (!::SetFilePointerEx(file_, length_li, NULL, FILE_BEGIN)) 236 if (!::SetFilePointerEx(file_, length_li, NULL, FILE_BEGIN))
228 return false; 237 return false;
229 238
230 // Set the new file length and move the file pointer to its old position. 239 // Set the new file length and move the file pointer to its old position.
231 // This is consistent with ftruncate()'s behavior, even when the file 240 // This is consistent with ftruncate()'s behavior, even when the file
232 // pointer points to a location beyond the end of the file. 241 // pointer points to a location beyond the end of the file.
233 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not 242 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not
234 // promised by the interface (nor was promised by PlatformFile). See if this 243 // promised by the interface (nor was promised by PlatformFile). See if this
235 // implementation detail can be removed. 244 // implementation detail can be removed.
236 return ((::SetEndOfFile(file_) != 0) && 245 return ((::SetEndOfFile(file_) != FALSE) &&
237 (::SetFilePointerEx(file_, file_pointer, NULL, FILE_BEGIN) != 0)); 246 (::SetFilePointerEx(file_, file_pointer, NULL, FILE_BEGIN) != FALSE));
238 } 247 }
239 248
240 bool File::Flush() { 249 bool File::Flush() {
241 base::ThreadRestrictions::AssertIOAllowed(); 250 base::ThreadRestrictions::AssertIOAllowed();
242 DCHECK(IsValid()); 251 DCHECK(IsValid());
243 return ::FlushFileBuffers(file_) != FALSE; 252 return ::FlushFileBuffers(file_) != FALSE;
244 } 253 }
245 254
246 bool File::SetTimes(Time last_access_time, Time last_modified_time) { 255 bool File::SetTimes(Time last_access_time, Time last_modified_time) {
247 base::ThreadRestrictions::AssertIOAllowed(); 256 base::ThreadRestrictions::AssertIOAllowed();
248 DCHECK(IsValid()); 257 DCHECK(IsValid());
249 258
250 FILETIME last_access_filetime = last_access_time.ToFileTime(); 259 FILETIME last_access_filetime = last_access_time.ToFileTime();
251 FILETIME last_modified_filetime = last_modified_time.ToFileTime(); 260 FILETIME last_modified_filetime = last_modified_time.ToFileTime();
252 return (::SetFileTime(file_, NULL, &last_access_filetime, 261 return (::SetFileTime(file_, NULL, &last_access_filetime,
253 &last_modified_filetime) != 0); 262 &last_modified_filetime) != FALSE);
254 } 263 }
255 264
256 bool File::GetInfo(Info* info) { 265 bool File::GetInfo(Info* info) {
257 base::ThreadRestrictions::AssertIOAllowed(); 266 base::ThreadRestrictions::AssertIOAllowed();
258 DCHECK(IsValid()); 267 DCHECK(IsValid());
259 268
260 BY_HANDLE_FILE_INFORMATION file_info; 269 BY_HANDLE_FILE_INFORMATION file_info;
261 if (GetFileInformationByHandle(file_, &file_info) == 0) 270 if (!GetFileInformationByHandle(file_, &file_info))
262 return false; 271 return false;
263 272
264 LARGE_INTEGER size; 273 LARGE_INTEGER size;
265 size.HighPart = file_info.nFileSizeHigh; 274 size.HighPart = file_info.nFileSizeHigh;
266 size.LowPart = file_info.nFileSizeLow; 275 size.LowPart = file_info.nFileSizeLow;
267 info->size = size.QuadPart; 276 info->size = size.QuadPart;
268 info->is_directory = 277 info->is_directory =
269 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; 278 (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
270 info->is_symbolic_link = false; // Windows doesn't have symbolic links. 279 info->is_symbolic_link = false; // Windows doesn't have symbolic links.
271 info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); 280 info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 last_error); 334 last_error);
326 return FILE_ERROR_FAILED; 335 return FILE_ERROR_FAILED;
327 } 336 }
328 } 337 }
329 338
330 void File::SetPlatformFile(PlatformFile file) { 339 void File::SetPlatformFile(PlatformFile file) {
331 file_.Set(file); 340 file_.Set(file);
332 } 341 }
333 342
334 } // namespace base 343 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698