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/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <libgen.h> | 10 #include <libgen.h> |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 } | 435 } |
436 | 436 |
437 bool CreateTemporaryFile(FilePath* path) { | 437 bool CreateTemporaryFile(FilePath* path) { |
438 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 438 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
439 FilePath directory; | 439 FilePath directory; |
440 if (!GetTempDir(&directory)) | 440 if (!GetTempDir(&directory)) |
441 return false; | 441 return false; |
442 int fd = CreateAndOpenFdForTemporaryFile(directory, path); | 442 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
443 if (fd < 0) | 443 if (fd < 0) |
444 return false; | 444 return false; |
445 ignore_result(HANDLE_EINTR(close(fd))); | 445 close(fd); |
446 return true; | 446 return true; |
447 } | 447 } |
448 | 448 |
449 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { | 449 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { |
450 FilePath directory; | 450 FilePath directory; |
451 if (!GetShmemTempDir(&directory, executable)) | 451 if (!GetShmemTempDir(&directory, executable)) |
452 return NULL; | 452 return NULL; |
453 | 453 |
454 return CreateAndOpenTemporaryFileInDir(directory, path); | 454 return CreateAndOpenTemporaryFileInDir(directory, path); |
455 } | 455 } |
456 | 456 |
457 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 457 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
458 int fd = CreateAndOpenFdForTemporaryFile(dir, path); | 458 int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
459 if (fd < 0) | 459 if (fd < 0) |
460 return NULL; | 460 return NULL; |
461 | 461 |
462 FILE* file = fdopen(fd, "a+"); | 462 FILE* file = fdopen(fd, "a+"); |
463 if (!file) | 463 if (!file) |
464 ignore_result(HANDLE_EINTR(close(fd))); | 464 close(fd); |
465 return file; | 465 return file; |
466 } | 466 } |
467 | 467 |
468 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 468 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
469 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 469 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
470 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); | 470 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
471 return ((fd >= 0) && !HANDLE_EINTR(close(fd))); | 471 return ((fd >= 0) && !IGNORE_EINTR(close(fd))); |
472 } | 472 } |
473 | 473 |
474 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, | 474 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
475 const FilePath::StringType& name_tmpl, | 475 const FilePath::StringType& name_tmpl, |
476 FilePath* new_dir) { | 476 FilePath* new_dir) { |
477 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). | 477 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). |
478 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) | 478 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
479 << "Directory name template must contain \"XXXXXX\"."; | 479 << "Directory name template must contain \"XXXXXX\"."; |
480 | 480 |
481 FilePath sub_dir = base_dir.Append(name_tmpl); | 481 FilePath sub_dir = base_dir.Append(name_tmpl); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 return result; | 637 return result; |
638 } | 638 } |
639 | 639 |
640 int ReadFile(const FilePath& filename, char* data, int size) { | 640 int ReadFile(const FilePath& filename, char* data, int size) { |
641 base::ThreadRestrictions::AssertIOAllowed(); | 641 base::ThreadRestrictions::AssertIOAllowed(); |
642 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); | 642 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); |
643 if (fd < 0) | 643 if (fd < 0) |
644 return -1; | 644 return -1; |
645 | 645 |
646 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); | 646 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); |
647 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 647 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
648 return ret; | 648 return ret; |
649 return bytes_read; | 649 return bytes_read; |
650 } | 650 } |
651 | 651 |
652 int WriteFile(const FilePath& filename, const char* data, int size) { | 652 int WriteFile(const FilePath& filename, const char* data, int size) { |
653 base::ThreadRestrictions::AssertIOAllowed(); | 653 base::ThreadRestrictions::AssertIOAllowed(); |
654 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); | 654 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); |
655 if (fd < 0) | 655 if (fd < 0) |
656 return -1; | 656 return -1; |
657 | 657 |
658 int bytes_written = WriteFileDescriptor(fd, data, size); | 658 int bytes_written = WriteFileDescriptor(fd, data, size); |
659 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 659 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
660 return ret; | 660 return ret; |
661 return bytes_written; | 661 return bytes_written; |
662 } | 662 } |
663 | 663 |
664 int WriteFileDescriptor(const int fd, const char* data, int size) { | 664 int WriteFileDescriptor(const int fd, const char* data, int size) { |
665 // Allow for partial writes. | 665 // Allow for partial writes. |
666 ssize_t bytes_written_total = 0; | 666 ssize_t bytes_written_total = 0; |
667 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; | 667 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
668 bytes_written_total += bytes_written_partial) { | 668 bytes_written_total += bytes_written_partial) { |
669 bytes_written_partial = | 669 bytes_written_partial = |
670 HANDLE_EINTR(write(fd, data + bytes_written_total, | 670 HANDLE_EINTR(write(fd, data + bytes_written_total, |
671 size - bytes_written_total)); | 671 size - bytes_written_total)); |
672 if (bytes_written_partial < 0) | 672 if (bytes_written_partial < 0) |
673 return -1; | 673 return -1; |
674 } | 674 } |
675 | 675 |
676 return bytes_written_total; | 676 return bytes_written_total; |
677 } | 677 } |
678 | 678 |
679 int AppendToFile(const FilePath& filename, const char* data, int size) { | 679 int AppendToFile(const FilePath& filename, const char* data, int size) { |
680 base::ThreadRestrictions::AssertIOAllowed(); | 680 base::ThreadRestrictions::AssertIOAllowed(); |
681 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); | 681 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); |
682 if (fd < 0) | 682 if (fd < 0) |
683 return -1; | 683 return -1; |
684 | 684 |
685 int bytes_written = WriteFileDescriptor(fd, data, size); | 685 int bytes_written = WriteFileDescriptor(fd, data, size); |
686 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 686 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
687 return ret; | 687 return ret; |
688 return bytes_written; | 688 return bytes_written; |
689 } | 689 } |
690 | 690 |
691 // Gets the current working directory for the process. | 691 // Gets the current working directory for the process. |
692 bool GetCurrentDirectory(FilePath* dir) { | 692 bool GetCurrentDirectory(FilePath* dir) { |
693 // getcwd can return ENOENT, which implies it checks against the disk. | 693 // getcwd can return ENOENT, which implies it checks against the disk. |
694 base::ThreadRestrictions::AssertIOAllowed(); | 694 base::ThreadRestrictions::AssertIOAllowed(); |
695 | 695 |
696 char system_buffer[PATH_MAX] = ""; | 696 char system_buffer[PATH_MAX] = ""; |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
928 #if !defined(OS_MACOSX) | 928 #if !defined(OS_MACOSX) |
929 // Mac has its own implementation, this is for all other Posix systems. | 929 // Mac has its own implementation, this is for all other Posix systems. |
930 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { | 930 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { |
931 ThreadRestrictions::AssertIOAllowed(); | 931 ThreadRestrictions::AssertIOAllowed(); |
932 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); | 932 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); |
933 if (infile < 0) | 933 if (infile < 0) |
934 return false; | 934 return false; |
935 | 935 |
936 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); | 936 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); |
937 if (outfile < 0) { | 937 if (outfile < 0) { |
938 ignore_result(HANDLE_EINTR(close(infile))); | 938 close(infile); |
939 return false; | 939 return false; |
940 } | 940 } |
941 | 941 |
942 const size_t kBufferSize = 32768; | 942 const size_t kBufferSize = 32768; |
943 std::vector<char> buffer(kBufferSize); | 943 std::vector<char> buffer(kBufferSize); |
944 bool result = true; | 944 bool result = true; |
945 | 945 |
946 while (result) { | 946 while (result) { |
947 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); | 947 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); |
948 if (bytes_read < 0) { | 948 if (bytes_read < 0) { |
(...skipping 10 matching lines...) Expand all Loading... |
959 &buffer[bytes_written_per_read], | 959 &buffer[bytes_written_per_read], |
960 bytes_read - bytes_written_per_read)); | 960 bytes_read - bytes_written_per_read)); |
961 if (bytes_written_partial < 0) { | 961 if (bytes_written_partial < 0) { |
962 result = false; | 962 result = false; |
963 break; | 963 break; |
964 } | 964 } |
965 bytes_written_per_read += bytes_written_partial; | 965 bytes_written_per_read += bytes_written_partial; |
966 } while (bytes_written_per_read < bytes_read); | 966 } while (bytes_written_per_read < bytes_read); |
967 } | 967 } |
968 | 968 |
969 if (HANDLE_EINTR(close(infile)) < 0) | 969 if (IGNORE_EINTR(close(infile)) < 0) |
970 result = false; | 970 result = false; |
971 if (HANDLE_EINTR(close(outfile)) < 0) | 971 if (IGNORE_EINTR(close(outfile)) < 0) |
972 result = false; | 972 result = false; |
973 | 973 |
974 return result; | 974 return result; |
975 } | 975 } |
976 #endif // !defined(OS_MACOSX) | 976 #endif // !defined(OS_MACOSX) |
977 | 977 |
978 } // namespace internal | 978 } // namespace internal |
979 } // namespace base | 979 } // namespace base |
OLD | NEW |