| 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 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 using base::VerifySpecificPathControlledByUser; | 531 using base::VerifySpecificPathControlledByUser; |
| 532 | 532 |
| 533 bool CreateTemporaryFile(FilePath* path) { | 533 bool CreateTemporaryFile(FilePath* path) { |
| 534 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 534 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
| 535 FilePath directory; | 535 FilePath directory; |
| 536 if (!GetTempDir(&directory)) | 536 if (!GetTempDir(&directory)) |
| 537 return false; | 537 return false; |
| 538 int fd = CreateAndOpenFdForTemporaryFile(directory, path); | 538 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
| 539 if (fd < 0) | 539 if (fd < 0) |
| 540 return false; | 540 return false; |
| 541 ignore_result(HANDLE_EINTR(close(fd))); | 541 close(fd); |
| 542 return true; | 542 return true; |
| 543 } | 543 } |
| 544 | 544 |
| 545 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { | 545 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { |
| 546 FilePath directory; | 546 FilePath directory; |
| 547 if (!GetShmemTempDir(executable, &directory)) | 547 if (!GetShmemTempDir(executable, &directory)) |
| 548 return NULL; | 548 return NULL; |
| 549 | 549 |
| 550 return CreateAndOpenTemporaryFileInDir(directory, path); | 550 return CreateAndOpenTemporaryFileInDir(directory, path); |
| 551 } | 551 } |
| 552 | 552 |
| 553 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 553 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 554 int fd = CreateAndOpenFdForTemporaryFile(dir, path); | 554 int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
| 555 if (fd < 0) | 555 if (fd < 0) |
| 556 return NULL; | 556 return NULL; |
| 557 | 557 |
| 558 FILE* file = fdopen(fd, "a+"); | 558 FILE* file = fdopen(fd, "a+"); |
| 559 if (!file) | 559 if (!file) |
| 560 ignore_result(HANDLE_EINTR(close(fd))); | 560 close(fd); |
| 561 return file; | 561 return file; |
| 562 } | 562 } |
| 563 | 563 |
| 564 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 564 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| 565 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 565 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
| 566 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); | 566 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
| 567 return ((fd >= 0) && !HANDLE_EINTR(close(fd))); | 567 return ((fd >= 0) && !IGNORE_EINTR(close(fd))); |
| 568 } | 568 } |
| 569 | 569 |
| 570 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, | 570 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
| 571 const FilePath::StringType& name_tmpl, | 571 const FilePath::StringType& name_tmpl, |
| 572 FilePath* new_dir) { | 572 FilePath* new_dir) { |
| 573 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). | 573 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). |
| 574 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) | 574 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
| 575 << "Directory name template must contain \"XXXXXX\"."; | 575 << "Directory name template must contain \"XXXXXX\"."; |
| 576 | 576 |
| 577 FilePath sub_dir = base_dir.Append(name_tmpl); | 577 FilePath sub_dir = base_dir.Append(name_tmpl); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 return result; | 733 return result; |
| 734 } | 734 } |
| 735 | 735 |
| 736 int ReadFile(const FilePath& filename, char* data, int size) { | 736 int ReadFile(const FilePath& filename, char* data, int size) { |
| 737 base::ThreadRestrictions::AssertIOAllowed(); | 737 base::ThreadRestrictions::AssertIOAllowed(); |
| 738 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); | 738 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); |
| 739 if (fd < 0) | 739 if (fd < 0) |
| 740 return -1; | 740 return -1; |
| 741 | 741 |
| 742 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); | 742 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); |
| 743 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 743 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
| 744 return ret; | 744 return ret; |
| 745 return bytes_read; | 745 return bytes_read; |
| 746 } | 746 } |
| 747 | 747 |
| 748 int WriteFile(const FilePath& filename, const char* data, int size) { | 748 int WriteFile(const FilePath& filename, const char* data, int size) { |
| 749 base::ThreadRestrictions::AssertIOAllowed(); | 749 base::ThreadRestrictions::AssertIOAllowed(); |
| 750 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); | 750 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); |
| 751 if (fd < 0) | 751 if (fd < 0) |
| 752 return -1; | 752 return -1; |
| 753 | 753 |
| 754 int bytes_written = WriteFileDescriptor(fd, data, size); | 754 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 755 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 755 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
| 756 return ret; | 756 return ret; |
| 757 return bytes_written; | 757 return bytes_written; |
| 758 } | 758 } |
| 759 | 759 |
| 760 int WriteFileDescriptor(const int fd, const char* data, int size) { | 760 int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 761 // Allow for partial writes. | 761 // Allow for partial writes. |
| 762 ssize_t bytes_written_total = 0; | 762 ssize_t bytes_written_total = 0; |
| 763 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; | 763 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 764 bytes_written_total += bytes_written_partial) { | 764 bytes_written_total += bytes_written_partial) { |
| 765 bytes_written_partial = | 765 bytes_written_partial = |
| 766 HANDLE_EINTR(write(fd, data + bytes_written_total, | 766 HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 767 size - bytes_written_total)); | 767 size - bytes_written_total)); |
| 768 if (bytes_written_partial < 0) | 768 if (bytes_written_partial < 0) |
| 769 return -1; | 769 return -1; |
| 770 } | 770 } |
| 771 | 771 |
| 772 return bytes_written_total; | 772 return bytes_written_total; |
| 773 } | 773 } |
| 774 | 774 |
| 775 int AppendToFile(const FilePath& filename, const char* data, int size) { | 775 int AppendToFile(const FilePath& filename, const char* data, int size) { |
| 776 base::ThreadRestrictions::AssertIOAllowed(); | 776 base::ThreadRestrictions::AssertIOAllowed(); |
| 777 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); | 777 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); |
| 778 if (fd < 0) | 778 if (fd < 0) |
| 779 return -1; | 779 return -1; |
| 780 | 780 |
| 781 int bytes_written = WriteFileDescriptor(fd, data, size); | 781 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 782 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 782 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
| 783 return ret; | 783 return ret; |
| 784 return bytes_written; | 784 return bytes_written; |
| 785 } | 785 } |
| 786 | 786 |
| 787 // Gets the current working directory for the process. | 787 // Gets the current working directory for the process. |
| 788 bool GetCurrentDirectory(FilePath* dir) { | 788 bool GetCurrentDirectory(FilePath* dir) { |
| 789 // getcwd can return ENOENT, which implies it checks against the disk. | 789 // getcwd can return ENOENT, which implies it checks against the disk. |
| 790 base::ThreadRestrictions::AssertIOAllowed(); | 790 base::ThreadRestrictions::AssertIOAllowed(); |
| 791 | 791 |
| 792 char system_buffer[PATH_MAX] = ""; | 792 char system_buffer[PATH_MAX] = ""; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 929 #if !defined(OS_MACOSX) | 929 #if !defined(OS_MACOSX) |
| 930 // Mac has its own implementation, this is for all other Posix systems. | 930 // Mac has its own implementation, this is for all other Posix systems. |
| 931 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { | 931 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 932 ThreadRestrictions::AssertIOAllowed(); | 932 ThreadRestrictions::AssertIOAllowed(); |
| 933 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); | 933 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); |
| 934 if (infile < 0) | 934 if (infile < 0) |
| 935 return false; | 935 return false; |
| 936 | 936 |
| 937 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); | 937 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); |
| 938 if (outfile < 0) { | 938 if (outfile < 0) { |
| 939 ignore_result(HANDLE_EINTR(close(infile))); | 939 close(infile); |
| 940 return false; | 940 return false; |
| 941 } | 941 } |
| 942 | 942 |
| 943 const size_t kBufferSize = 32768; | 943 const size_t kBufferSize = 32768; |
| 944 std::vector<char> buffer(kBufferSize); | 944 std::vector<char> buffer(kBufferSize); |
| 945 bool result = true; | 945 bool result = true; |
| 946 | 946 |
| 947 while (result) { | 947 while (result) { |
| 948 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); | 948 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); |
| 949 if (bytes_read < 0) { | 949 if (bytes_read < 0) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 960 &buffer[bytes_written_per_read], | 960 &buffer[bytes_written_per_read], |
| 961 bytes_read - bytes_written_per_read)); | 961 bytes_read - bytes_written_per_read)); |
| 962 if (bytes_written_partial < 0) { | 962 if (bytes_written_partial < 0) { |
| 963 result = false; | 963 result = false; |
| 964 break; | 964 break; |
| 965 } | 965 } |
| 966 bytes_written_per_read += bytes_written_partial; | 966 bytes_written_per_read += bytes_written_partial; |
| 967 } while (bytes_written_per_read < bytes_read); | 967 } while (bytes_written_per_read < bytes_read); |
| 968 } | 968 } |
| 969 | 969 |
| 970 if (HANDLE_EINTR(close(infile)) < 0) | 970 if (IGNORE_EINTR(close(infile)) < 0) |
| 971 result = false; | 971 result = false; |
| 972 if (HANDLE_EINTR(close(outfile)) < 0) | 972 if (IGNORE_EINTR(close(outfile)) < 0) |
| 973 result = false; | 973 result = false; |
| 974 | 974 |
| 975 return result; | 975 return result; |
| 976 } | 976 } |
| 977 #endif // !defined(OS_MACOSX) | 977 #endif // !defined(OS_MACOSX) |
| 978 | 978 |
| 979 } // namespace internal | 979 } // namespace internal |
| 980 } // namespace base | 980 } // namespace base |
| OLD | NEW |