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> |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 456 } |
457 | 457 |
458 File::Error File::Lock() { | 458 File::Error File::Lock() { |
459 return CallFctnlFlock(file_.get(), true); | 459 return CallFctnlFlock(file_.get(), true); |
460 } | 460 } |
461 | 461 |
462 File::Error File::Unlock() { | 462 File::Error File::Unlock() { |
463 return CallFctnlFlock(file_.get(), false); | 463 return CallFctnlFlock(file_.get(), false); |
464 } | 464 } |
465 | 465 |
| 466 File File::Duplicate() { |
| 467 if (!IsValid()) |
| 468 return File(); |
| 469 |
| 470 PlatformFile other_fd = dup(GetPlatformFile()); |
| 471 if (other_fd == -1) |
| 472 return File(OSErrorToFileError(errno)); |
| 473 |
| 474 File other(other_fd); |
| 475 if (async()) |
| 476 other.async_ = true; |
| 477 return other.Pass(); |
| 478 } |
| 479 |
466 // Static. | 480 // Static. |
467 File::Error File::OSErrorToFileError(int saved_errno) { | 481 File::Error File::OSErrorToFileError(int saved_errno) { |
468 switch (saved_errno) { | 482 switch (saved_errno) { |
469 case EACCES: | 483 case EACCES: |
470 case EISDIR: | 484 case EISDIR: |
471 case EROFS: | 485 case EROFS: |
472 case EPERM: | 486 case EPERM: |
473 return FILE_ERROR_ACCESS_DENIED; | 487 return FILE_ERROR_ACCESS_DENIED; |
474 case EBUSY: | 488 case EBUSY: |
475 #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. | 489 #if !defined(OS_NACL) // ETXTBSY not defined by NaCl. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 } | 557 } |
544 | 558 |
545 void File::SetPlatformFile(PlatformFile file) { | 559 void File::SetPlatformFile(PlatformFile file) { |
546 CHECK(!file_.is_valid()); | 560 CHECK(!file_.is_valid()); |
547 file_.reset(file); | 561 file_.reset(file); |
548 if (file_.is_valid()) | 562 if (file_.is_valid()) |
549 ProtectFileDescriptor(GetPlatformFile()); | 563 ProtectFileDescriptor(GetPlatformFile()); |
550 } | 564 } |
551 | 565 |
552 } // namespace base | 566 } // namespace base |
OLD | NEW |