| 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 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 | 644 |
| 645 FILE* OpenFile(const FilePath& filename, const char* mode) { | 645 FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 646 ThreadRestrictions::AssertIOAllowed(); | 646 ThreadRestrictions::AssertIOAllowed(); |
| 647 FILE* result = NULL; | 647 FILE* result = NULL; |
| 648 do { | 648 do { |
| 649 result = fopen(filename.value().c_str(), mode); | 649 result = fopen(filename.value().c_str(), mode); |
| 650 } while (!result && errno == EINTR); | 650 } while (!result && errno == EINTR); |
| 651 return result; | 651 return result; |
| 652 } | 652 } |
| 653 | 653 |
| 654 int ReadFile(const FilePath& filename, char* data, int size) { | 654 int ReadFile(const FilePath& filename, char* data, int max_size) { |
| 655 ThreadRestrictions::AssertIOAllowed(); | 655 ThreadRestrictions::AssertIOAllowed(); |
| 656 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); | 656 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); |
| 657 if (fd < 0) | 657 if (fd < 0) |
| 658 return -1; | 658 return -1; |
| 659 | 659 |
| 660 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); | 660 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size)); |
| 661 if (int ret = IGNORE_EINTR(close(fd)) < 0) | 661 if (IGNORE_EINTR(close(fd)) < 0) |
| 662 return ret; | 662 return -1; |
| 663 return bytes_read; | 663 return bytes_read; |
| 664 } | 664 } |
| 665 | 665 |
| 666 int WriteFile(const FilePath& filename, const char* data, int size) { | 666 int WriteFile(const FilePath& filename, const char* data, int size) { |
| 667 ThreadRestrictions::AssertIOAllowed(); | 667 ThreadRestrictions::AssertIOAllowed(); |
| 668 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); | 668 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); |
| 669 if (fd < 0) | 669 if (fd < 0) |
| 670 return -1; | 670 return -1; |
| 671 | 671 |
| 672 int bytes_written = WriteFileDescriptor(fd, data, size); | 672 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 673 if (int ret = IGNORE_EINTR(close(fd)) < 0) | 673 if (IGNORE_EINTR(close(fd)) < 0) |
| 674 return ret; | 674 return -1; |
| 675 return bytes_written; | 675 return bytes_written; |
| 676 } | 676 } |
| 677 | 677 |
| 678 int WriteFileDescriptor(const int fd, const char* data, int size) { | 678 int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 679 // Allow for partial writes. | 679 // Allow for partial writes. |
| 680 ssize_t bytes_written_total = 0; | 680 ssize_t bytes_written_total = 0; |
| 681 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; | 681 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 682 bytes_written_total += bytes_written_partial) { | 682 bytes_written_total += bytes_written_partial) { |
| 683 bytes_written_partial = | 683 bytes_written_partial = |
| 684 HANDLE_EINTR(write(fd, data + bytes_written_total, | 684 HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 685 size - bytes_written_total)); | 685 size - bytes_written_total)); |
| 686 if (bytes_written_partial < 0) | 686 if (bytes_written_partial < 0) |
| 687 return -1; | 687 return -1; |
| 688 } | 688 } |
| 689 | 689 |
| 690 return bytes_written_total; | 690 return bytes_written_total; |
| 691 } | 691 } |
| 692 | 692 |
| 693 int AppendToFile(const FilePath& filename, const char* data, int size) { | 693 int AppendToFile(const FilePath& filename, const char* data, int size) { |
| 694 ThreadRestrictions::AssertIOAllowed(); | 694 ThreadRestrictions::AssertIOAllowed(); |
| 695 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); | 695 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); |
| 696 if (fd < 0) | 696 if (fd < 0) |
| 697 return -1; | 697 return -1; |
| 698 | 698 |
| 699 int bytes_written = WriteFileDescriptor(fd, data, size); | 699 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 700 if (int ret = IGNORE_EINTR(close(fd)) < 0) | 700 if (IGNORE_EINTR(close(fd)) < 0) |
| 701 return ret; | 701 return -1; |
| 702 return bytes_written; | 702 return bytes_written; |
| 703 } | 703 } |
| 704 | 704 |
| 705 // Gets the current working directory for the process. | 705 // Gets the current working directory for the process. |
| 706 bool GetCurrentDirectory(FilePath* dir) { | 706 bool GetCurrentDirectory(FilePath* dir) { |
| 707 // getcwd can return ENOENT, which implies it checks against the disk. | 707 // getcwd can return ENOENT, which implies it checks against the disk. |
| 708 ThreadRestrictions::AssertIOAllowed(); | 708 ThreadRestrictions::AssertIOAllowed(); |
| 709 | 709 |
| 710 char system_buffer[PATH_MAX] = ""; | 710 char system_buffer[PATH_MAX] = ""; |
| 711 if (!getcwd(system_buffer, sizeof(system_buffer))) { | 711 if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 result = false; | 890 result = false; |
| 891 if (IGNORE_EINTR(close(outfile)) < 0) | 891 if (IGNORE_EINTR(close(outfile)) < 0) |
| 892 result = false; | 892 result = false; |
| 893 | 893 |
| 894 return result; | 894 return result; |
| 895 } | 895 } |
| 896 #endif // !defined(OS_MACOSX) | 896 #endif // !defined(OS_MACOSX) |
| 897 | 897 |
| 898 } // namespace internal | 898 } // namespace internal |
| 899 } // namespace base | 899 } // namespace base |
| OLD | NEW |