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 <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_tracing.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/metrics/sparse_histogram.h" | 14 #include "base/metrics/sparse_histogram.h" |
16 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
17 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
18 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
19 | 18 |
20 #if defined(OS_ANDROID) | 19 #if defined(OS_ANDROID) |
21 #include "base/os_compat_android.h" | 20 #include "base/os_compat_android.h" |
22 #endif | 21 #endif |
23 | 22 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 } | 166 } |
168 | 167 |
169 PlatformFile File::TakePlatformFile() { | 168 PlatformFile File::TakePlatformFile() { |
170 return file_.release(); | 169 return file_.release(); |
171 } | 170 } |
172 | 171 |
173 void File::Close() { | 172 void File::Close() { |
174 if (!IsValid()) | 173 if (!IsValid()) |
175 return; | 174 return; |
176 | 175 |
177 SCOPED_FILE_TRACE("Close"); | |
178 ThreadRestrictions::AssertIOAllowed(); | 176 ThreadRestrictions::AssertIOAllowed(); |
179 file_.reset(); | 177 file_.reset(); |
180 } | 178 } |
181 | 179 |
182 int64 File::Seek(Whence whence, int64 offset) { | 180 int64 File::Seek(Whence whence, int64 offset) { |
183 ThreadRestrictions::AssertIOAllowed(); | 181 ThreadRestrictions::AssertIOAllowed(); |
184 DCHECK(IsValid()); | 182 DCHECK(IsValid()); |
185 | 183 |
186 SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); | |
187 | |
188 #if defined(OS_ANDROID) | 184 #if defined(OS_ANDROID) |
189 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); | 185 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); |
190 return lseek64(file_.get(), static_cast<off64_t>(offset), | 186 return lseek64(file_.get(), static_cast<off64_t>(offset), |
191 static_cast<int>(whence)); | 187 static_cast<int>(whence)); |
192 #else | 188 #else |
193 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 189 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
194 return lseek(file_.get(), static_cast<off_t>(offset), | 190 return lseek(file_.get(), static_cast<off_t>(offset), |
195 static_cast<int>(whence)); | 191 static_cast<int>(whence)); |
196 #endif | 192 #endif |
197 } | 193 } |
198 | 194 |
199 int File::Read(int64 offset, char* data, int size) { | 195 int File::Read(int64 offset, char* data, int size) { |
200 ThreadRestrictions::AssertIOAllowed(); | 196 ThreadRestrictions::AssertIOAllowed(); |
201 DCHECK(IsValid()); | 197 DCHECK(IsValid()); |
202 if (size < 0) | 198 if (size < 0) |
203 return -1; | 199 return -1; |
204 | 200 |
205 SCOPED_FILE_TRACE_WITH_SIZE("Read", size); | |
206 | |
207 int bytes_read = 0; | 201 int bytes_read = 0; |
208 int rv; | 202 int rv; |
209 do { | 203 do { |
210 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, | 204 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, |
211 size - bytes_read, offset + bytes_read)); | 205 size - bytes_read, offset + bytes_read)); |
212 if (rv <= 0) | 206 if (rv <= 0) |
213 break; | 207 break; |
214 | 208 |
215 bytes_read += rv; | 209 bytes_read += rv; |
216 } while (bytes_read < size); | 210 } while (bytes_read < size); |
217 | 211 |
218 return bytes_read ? bytes_read : rv; | 212 return bytes_read ? bytes_read : rv; |
219 } | 213 } |
220 | 214 |
221 int File::ReadAtCurrentPos(char* data, int size) { | 215 int File::ReadAtCurrentPos(char* data, int size) { |
222 ThreadRestrictions::AssertIOAllowed(); | 216 ThreadRestrictions::AssertIOAllowed(); |
223 DCHECK(IsValid()); | 217 DCHECK(IsValid()); |
224 if (size < 0) | 218 if (size < 0) |
225 return -1; | 219 return -1; |
226 | 220 |
227 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size); | |
228 | |
229 int bytes_read = 0; | 221 int bytes_read = 0; |
230 int rv; | 222 int rv; |
231 do { | 223 do { |
232 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); | 224 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); |
233 if (rv <= 0) | 225 if (rv <= 0) |
234 break; | 226 break; |
235 | 227 |
236 bytes_read += rv; | 228 bytes_read += rv; |
237 } while (bytes_read < size); | 229 } while (bytes_read < size); |
238 | 230 |
239 return bytes_read ? bytes_read : rv; | 231 return bytes_read ? bytes_read : rv; |
240 } | 232 } |
241 | 233 |
242 int File::ReadNoBestEffort(int64 offset, char* data, int size) { | 234 int File::ReadNoBestEffort(int64 offset, char* data, int size) { |
243 ThreadRestrictions::AssertIOAllowed(); | 235 ThreadRestrictions::AssertIOAllowed(); |
244 DCHECK(IsValid()); | 236 DCHECK(IsValid()); |
245 SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size); | 237 |
246 return HANDLE_EINTR(pread(file_.get(), data, size, offset)); | 238 return HANDLE_EINTR(pread(file_.get(), data, size, offset)); |
247 } | 239 } |
248 | 240 |
249 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { | 241 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
250 ThreadRestrictions::AssertIOAllowed(); | 242 ThreadRestrictions::AssertIOAllowed(); |
251 DCHECK(IsValid()); | 243 DCHECK(IsValid()); |
252 if (size < 0) | 244 if (size < 0) |
253 return -1; | 245 return -1; |
254 | 246 |
255 SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPosNoBestEffort", size); | |
256 return HANDLE_EINTR(read(file_.get(), data, size)); | 247 return HANDLE_EINTR(read(file_.get(), data, size)); |
257 } | 248 } |
258 | 249 |
259 int File::Write(int64 offset, const char* data, int size) { | 250 int File::Write(int64 offset, const char* data, int size) { |
260 ThreadRestrictions::AssertIOAllowed(); | 251 ThreadRestrictions::AssertIOAllowed(); |
261 | 252 |
262 if (IsOpenAppend(file_.get())) | 253 if (IsOpenAppend(file_.get())) |
263 return WriteAtCurrentPos(data, size); | 254 return WriteAtCurrentPos(data, size); |
264 | 255 |
265 DCHECK(IsValid()); | 256 DCHECK(IsValid()); |
266 if (size < 0) | 257 if (size < 0) |
267 return -1; | 258 return -1; |
268 | 259 |
269 SCOPED_FILE_TRACE_WITH_SIZE("Write", size); | |
270 | |
271 int bytes_written = 0; | 260 int bytes_written = 0; |
272 int rv; | 261 int rv; |
273 do { | 262 do { |
274 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, | 263 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, |
275 size - bytes_written, offset + bytes_written)); | 264 size - bytes_written, offset + bytes_written)); |
276 if (rv <= 0) | 265 if (rv <= 0) |
277 break; | 266 break; |
278 | 267 |
279 bytes_written += rv; | 268 bytes_written += rv; |
280 } while (bytes_written < size); | 269 } while (bytes_written < size); |
281 | 270 |
282 return bytes_written ? bytes_written : rv; | 271 return bytes_written ? bytes_written : rv; |
283 } | 272 } |
284 | 273 |
285 int File::WriteAtCurrentPos(const char* data, int size) { | 274 int File::WriteAtCurrentPos(const char* data, int size) { |
286 ThreadRestrictions::AssertIOAllowed(); | 275 ThreadRestrictions::AssertIOAllowed(); |
287 DCHECK(IsValid()); | 276 DCHECK(IsValid()); |
288 if (size < 0) | 277 if (size < 0) |
289 return -1; | 278 return -1; |
290 | 279 |
291 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size); | |
292 | |
293 int bytes_written = 0; | 280 int bytes_written = 0; |
294 int rv; | 281 int rv; |
295 do { | 282 do { |
296 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, | 283 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, |
297 size - bytes_written)); | 284 size - bytes_written)); |
298 if (rv <= 0) | 285 if (rv <= 0) |
299 break; | 286 break; |
300 | 287 |
301 bytes_written += rv; | 288 bytes_written += rv; |
302 } while (bytes_written < size); | 289 } while (bytes_written < size); |
303 | 290 |
304 return bytes_written ? bytes_written : rv; | 291 return bytes_written ? bytes_written : rv; |
305 } | 292 } |
306 | 293 |
307 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { | 294 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
308 ThreadRestrictions::AssertIOAllowed(); | 295 ThreadRestrictions::AssertIOAllowed(); |
309 DCHECK(IsValid()); | 296 DCHECK(IsValid()); |
310 if (size < 0) | 297 if (size < 0) |
311 return -1; | 298 return -1; |
312 | 299 |
313 SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size); | |
314 return HANDLE_EINTR(write(file_.get(), data, size)); | 300 return HANDLE_EINTR(write(file_.get(), data, size)); |
315 } | 301 } |
316 | 302 |
317 int64 File::GetLength() { | 303 int64 File::GetLength() { |
318 DCHECK(IsValid()); | 304 DCHECK(IsValid()); |
319 | 305 |
320 SCOPED_FILE_TRACE("GetLength"); | |
321 | |
322 stat_wrapper_t file_info; | 306 stat_wrapper_t file_info; |
323 if (CallFstat(file_.get(), &file_info)) | 307 if (CallFstat(file_.get(), &file_info)) |
324 return false; | 308 return false; |
325 | 309 |
326 return file_info.st_size; | 310 return file_info.st_size; |
327 } | 311 } |
328 | 312 |
329 bool File::SetLength(int64 length) { | 313 bool File::SetLength(int64 length) { |
330 ThreadRestrictions::AssertIOAllowed(); | 314 ThreadRestrictions::AssertIOAllowed(); |
331 DCHECK(IsValid()); | 315 DCHECK(IsValid()); |
332 | |
333 SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length); | |
334 return !CallFtruncate(file_.get(), length); | 316 return !CallFtruncate(file_.get(), length); |
335 } | 317 } |
336 | 318 |
337 bool File::SetTimes(Time last_access_time, Time last_modified_time) { | 319 bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
338 ThreadRestrictions::AssertIOAllowed(); | 320 ThreadRestrictions::AssertIOAllowed(); |
339 DCHECK(IsValid()); | 321 DCHECK(IsValid()); |
340 | 322 |
341 SCOPED_FILE_TRACE("SetTimes"); | |
342 | |
343 timeval times[2]; | 323 timeval times[2]; |
344 times[0] = last_access_time.ToTimeVal(); | 324 times[0] = last_access_time.ToTimeVal(); |
345 times[1] = last_modified_time.ToTimeVal(); | 325 times[1] = last_modified_time.ToTimeVal(); |
346 | 326 |
347 return !CallFutimes(file_.get(), times); | 327 return !CallFutimes(file_.get(), times); |
348 } | 328 } |
349 | 329 |
350 bool File::GetInfo(Info* info) { | 330 bool File::GetInfo(Info* info) { |
351 DCHECK(IsValid()); | 331 DCHECK(IsValid()); |
352 | 332 |
353 SCOPED_FILE_TRACE("GetInfo"); | |
354 | |
355 stat_wrapper_t file_info; | 333 stat_wrapper_t file_info; |
356 if (CallFstat(file_.get(), &file_info)) | 334 if (CallFstat(file_.get(), &file_info)) |
357 return false; | 335 return false; |
358 | 336 |
359 info->FromStat(file_info); | 337 info->FromStat(file_info); |
360 return true; | 338 return true; |
361 } | 339 } |
362 | 340 |
363 File::Error File::Lock() { | 341 File::Error File::Lock() { |
364 SCOPED_FILE_TRACE("Lock"); | |
365 return CallFctnlFlock(file_.get(), true); | 342 return CallFctnlFlock(file_.get(), true); |
366 } | 343 } |
367 | 344 |
368 File::Error File::Unlock() { | 345 File::Error File::Unlock() { |
369 SCOPED_FILE_TRACE("Unlock"); | |
370 return CallFctnlFlock(file_.get(), false); | 346 return CallFctnlFlock(file_.get(), false); |
371 } | 347 } |
372 | 348 |
373 File File::Duplicate() { | 349 File File::Duplicate() { |
374 if (!IsValid()) | 350 if (!IsValid()) |
375 return File(); | 351 return File(); |
376 | 352 |
377 SCOPED_FILE_TRACE("Duplicate"); | |
378 | |
379 PlatformFile other_fd = dup(GetPlatformFile()); | 353 PlatformFile other_fd = dup(GetPlatformFile()); |
380 if (other_fd == -1) | 354 if (other_fd == -1) |
381 return File(OSErrorToFileError(errno)); | 355 return File(OSErrorToFileError(errno)); |
382 | 356 |
383 File other(other_fd); | 357 File other(other_fd); |
384 if (async()) | 358 if (async()) |
385 other.async_ = true; | 359 other.async_ = true; |
386 return other.Pass(); | 360 return other.Pass(); |
387 } | 361 } |
388 | 362 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; | 435 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; |
462 } | 436 } |
463 | 437 |
464 void File::MemoryCheckingScopedFD::UpdateChecksum() { | 438 void File::MemoryCheckingScopedFD::UpdateChecksum() { |
465 ComputeMemoryChecksum(&file_memory_checksum_); | 439 ComputeMemoryChecksum(&file_memory_checksum_); |
466 } | 440 } |
467 | 441 |
468 // NaCl doesn't implement system calls to open files directly. | 442 // NaCl doesn't implement system calls to open files directly. |
469 #if !defined(OS_NACL) | 443 #if !defined(OS_NACL) |
470 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 444 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
471 void File::DoInitialize(uint32 flags) { | 445 void File::DoInitialize(const FilePath& name, uint32 flags) { |
472 ThreadRestrictions::AssertIOAllowed(); | 446 ThreadRestrictions::AssertIOAllowed(); |
473 DCHECK(!IsValid()); | 447 DCHECK(!IsValid()); |
474 | 448 |
475 int open_flags = 0; | 449 int open_flags = 0; |
476 if (flags & FLAG_CREATE) | 450 if (flags & FLAG_CREATE) |
477 open_flags = O_CREAT | O_EXCL; | 451 open_flags = O_CREAT | O_EXCL; |
478 | 452 |
479 created_ = false; | 453 created_ = false; |
480 | 454 |
481 if (flags & FLAG_CREATE_ALWAYS) { | 455 if (flags & FLAG_CREATE_ALWAYS) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 else if (flags & FLAG_APPEND) | 490 else if (flags & FLAG_APPEND) |
517 open_flags |= O_APPEND | O_WRONLY; | 491 open_flags |= O_APPEND | O_WRONLY; |
518 | 492 |
519 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); | 493 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); |
520 | 494 |
521 int mode = S_IRUSR | S_IWUSR; | 495 int mode = S_IRUSR | S_IWUSR; |
522 #if defined(OS_CHROMEOS) | 496 #if defined(OS_CHROMEOS) |
523 mode |= S_IRGRP | S_IROTH; | 497 mode |= S_IRGRP | S_IROTH; |
524 #endif | 498 #endif |
525 | 499 |
526 int descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode)); | 500 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
527 | 501 |
528 if (flags & FLAG_OPEN_ALWAYS) { | 502 if (flags & FLAG_OPEN_ALWAYS) { |
529 if (descriptor < 0) { | 503 if (descriptor < 0) { |
530 open_flags |= O_CREAT; | 504 open_flags |= O_CREAT; |
531 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) | 505 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) |
532 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW | 506 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
533 | 507 |
534 descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode)); | 508 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); |
535 if (descriptor >= 0) | 509 if (descriptor >= 0) |
536 created_ = true; | 510 created_ = true; |
537 } | 511 } |
538 } | 512 } |
539 | 513 |
540 if (descriptor < 0) { | 514 if (descriptor < 0) { |
541 error_details_ = File::OSErrorToFileError(errno); | 515 error_details_ = File::OSErrorToFileError(errno); |
542 return; | 516 return; |
543 } | 517 } |
544 | 518 |
545 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | 519 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
546 created_ = true; | 520 created_ = true; |
547 | 521 |
548 if (flags & FLAG_DELETE_ON_CLOSE) | 522 if (flags & FLAG_DELETE_ON_CLOSE) |
549 unlink(path_.value().c_str()); | 523 unlink(name.value().c_str()); |
550 | 524 |
551 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | 525 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
552 error_details_ = FILE_OK; | 526 error_details_ = FILE_OK; |
553 file_.reset(descriptor); | 527 file_.reset(descriptor); |
554 } | 528 } |
555 #endif // !defined(OS_NACL) | 529 #endif // !defined(OS_NACL) |
556 | 530 |
557 bool File::DoFlush() { | 531 bool File::DoFlush() { |
558 ThreadRestrictions::AssertIOAllowed(); | 532 ThreadRestrictions::AssertIOAllowed(); |
559 DCHECK(IsValid()); | 533 DCHECK(IsValid()); |
560 | |
561 #if defined(OS_NACL) | 534 #if defined(OS_NACL) |
562 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. | 535 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
563 return true; | 536 return true; |
564 #elif defined(OS_LINUX) || defined(OS_ANDROID) | 537 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
565 return !HANDLE_EINTR(fdatasync(file_.get())); | 538 return !HANDLE_EINTR(fdatasync(file_.get())); |
566 #else | 539 #else |
567 return !HANDLE_EINTR(fsync(file_.get())); | 540 return !HANDLE_EINTR(fsync(file_.get())); |
568 #endif | 541 #endif |
569 } | 542 } |
570 | 543 |
571 void File::SetPlatformFile(PlatformFile file) { | 544 void File::SetPlatformFile(PlatformFile file) { |
572 DCHECK(!file_.is_valid()); | 545 DCHECK(!file_.is_valid()); |
573 file_.reset(file); | 546 file_.reset(file); |
574 } | 547 } |
575 | 548 |
576 } // namespace base | 549 } // namespace base |
OLD | NEW |