OLD | NEW |
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" |
11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 void File::CreateBaseFileUnsafe(const FilePath& name, uint32 flags) { | 16 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
17 base::ThreadRestrictions::AssertIOAllowed(); | 17 base::ThreadRestrictions::AssertIOAllowed(); |
18 DCHECK(!IsValid()); | 18 DCHECK(!IsValid()); |
19 | 19 |
20 DWORD disposition = 0; | 20 DWORD disposition = 0; |
21 | 21 |
22 if (flags & FLAG_OPEN) | 22 if (flags & FLAG_OPEN) |
23 disposition = OPEN_EXISTING; | 23 disposition = OPEN_EXISTING; |
24 | 24 |
25 if (flags & FLAG_CREATE) { | 25 if (flags & FLAG_CREATE) { |
26 DCHECK(!disposition); | 26 DCHECK(!disposition); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 190 |
191 int File::WriteAtCurrentPos(const char* data, int size) { | 191 int File::WriteAtCurrentPos(const char* data, int size) { |
192 NOTREACHED(); | 192 NOTREACHED(); |
193 return -1; | 193 return -1; |
194 } | 194 } |
195 | 195 |
196 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { | 196 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
197 return WriteAtCurrentPos(data, size); | 197 return WriteAtCurrentPos(data, size); |
198 } | 198 } |
199 | 199 |
200 bool File::Truncate(int64 length) { | 200 int64 File::GetLength() { |
| 201 base::ThreadRestrictions::AssertIOAllowed(); |
| 202 DCHECK(IsValid()); |
| 203 LARGE_INTEGER size; |
| 204 if (!::GetFileSizeEx(file_.Get(), &size)) |
| 205 return -1; |
| 206 |
| 207 return static_cast<int64>(size.QuadPart); |
| 208 } |
| 209 |
| 210 bool File::SetLength(int64 length) { |
201 base::ThreadRestrictions::AssertIOAllowed(); | 211 base::ThreadRestrictions::AssertIOAllowed(); |
202 DCHECK(IsValid()); | 212 DCHECK(IsValid()); |
203 | 213 |
204 // Get the current file pointer. | 214 // Get the current file pointer. |
205 LARGE_INTEGER file_pointer; | 215 LARGE_INTEGER file_pointer; |
206 LARGE_INTEGER zero; | 216 LARGE_INTEGER zero; |
207 zero.QuadPart = 0; | 217 zero.QuadPart = 0; |
208 if (::SetFilePointerEx(file_, zero, &file_pointer, FILE_CURRENT) == 0) | 218 if (::SetFilePointerEx(file_, zero, &file_pointer, FILE_CURRENT) == 0) |
209 return false; | 219 return false; |
210 | 220 |
211 LARGE_INTEGER length_li; | 221 LARGE_INTEGER length_li; |
212 length_li.QuadPart = length; | 222 length_li.QuadPart = length; |
213 // If length > file size, SetFilePointerEx() should extend the file | 223 // If length > file size, SetFilePointerEx() should extend the file |
214 // with zeroes on all Windows standard file systems (NTFS, FATxx). | 224 // with zeroes on all Windows standard file systems (NTFS, FATxx). |
215 if (!::SetFilePointerEx(file_, length_li, NULL, FILE_BEGIN)) | 225 if (!::SetFilePointerEx(file_, length_li, NULL, FILE_BEGIN)) |
216 return false; | 226 return false; |
217 | 227 |
218 // Set the new file length and move the file pointer to its old position. | 228 // Set the new file length and move the file pointer to its old position. |
219 // This is consistent with ftruncate()'s behavior, even when the file | 229 // This is consistent with ftruncate()'s behavior, even when the file |
220 // pointer points to a location beyond the end of the file. | 230 // pointer points to a location beyond the end of the file. |
| 231 // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not |
| 232 // promised by the interface (nor was promised by PlatformFile). See if this |
| 233 // implementation detail can be removed. |
221 return ((::SetEndOfFile(file_) != 0) && | 234 return ((::SetEndOfFile(file_) != 0) && |
222 (::SetFilePointerEx(file_, file_pointer, NULL, FILE_BEGIN) != 0)); | 235 (::SetFilePointerEx(file_, file_pointer, NULL, FILE_BEGIN) != 0)); |
223 } | 236 } |
224 | 237 |
225 bool File::Flush() { | 238 bool File::Flush() { |
226 base::ThreadRestrictions::AssertIOAllowed(); | 239 base::ThreadRestrictions::AssertIOAllowed(); |
227 DCHECK(IsValid()); | 240 DCHECK(IsValid()); |
228 return ::FlushFileBuffers(file_) != FALSE; | 241 return ::FlushFileBuffers(file_) != FALSE; |
229 } | 242 } |
230 | 243 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 last_error); | 323 last_error); |
311 return FILE_ERROR_FAILED; | 324 return FILE_ERROR_FAILED; |
312 } | 325 } |
313 } | 326 } |
314 | 327 |
315 void File::SetPlatformFile(PlatformFile file) { | 328 void File::SetPlatformFile(PlatformFile file) { |
316 file_.Set(file); | 329 file_.Set(file); |
317 } | 330 } |
318 | 331 |
319 } // namespace base | 332 } // namespace base |
OLD | NEW |