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