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 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 results->last_accessed = Time::FromTimeT(file_info.st_atime); | 682 results->last_accessed = Time::FromTimeT(file_info.st_atime); |
683 results->creation_time = Time::FromTimeT(file_info.st_ctime); | 683 results->creation_time = Time::FromTimeT(file_info.st_ctime); |
684 #else | 684 #else |
685 results->last_modified = Time::FromTimeSpec(file_info.st_mtim); | 685 results->last_modified = Time::FromTimeSpec(file_info.st_mtim); |
686 results->last_accessed = Time::FromTimeSpec(file_info.st_atim); | 686 results->last_accessed = Time::FromTimeSpec(file_info.st_atim); |
687 results->creation_time = Time::FromTimeSpec(file_info.st_ctim); | 687 results->creation_time = Time::FromTimeSpec(file_info.st_ctim); |
688 #endif | 688 #endif |
689 return true; | 689 return true; |
690 } | 690 } |
691 | 691 |
| 692 FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 693 ThreadRestrictions::AssertIOAllowed(); |
| 694 FILE* result = NULL; |
| 695 do { |
| 696 result = fopen(filename.value().c_str(), mode); |
| 697 } while (!result && errno == EINTR); |
| 698 return result; |
| 699 } |
| 700 |
| 701 int ReadFile(const FilePath& filename, char* data, int size) { |
| 702 ThreadRestrictions::AssertIOAllowed(); |
| 703 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); |
| 704 if (fd < 0) |
| 705 return -1; |
| 706 |
| 707 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); |
| 708 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
| 709 return ret; |
| 710 return bytes_read; |
| 711 } |
| 712 |
692 } // namespace base | 713 } // namespace base |
693 | 714 |
694 // ----------------------------------------------------------------------------- | 715 // ----------------------------------------------------------------------------- |
695 | 716 |
696 namespace file_util { | 717 namespace file_util { |
697 | 718 |
698 using base::stat_wrapper_t; | 719 using base::stat_wrapper_t; |
699 using base::CallStat; | 720 using base::CallStat; |
700 using base::CallLstat; | 721 using base::CallLstat; |
701 using base::CreateAndOpenFdForTemporaryFile; | 722 using base::CreateAndOpenFdForTemporaryFile; |
(...skipping 14 matching lines...) Expand all Loading... |
716 path.InsertBeforeExtensionASCII( | 737 path.InsertBeforeExtensionASCII( |
717 base::StringPrintf(" (%d)", uniquifier)); | 738 base::StringPrintf(" (%d)", uniquifier)); |
718 if (mkdir(test_path.value().c_str(), 0777) == 0) | 739 if (mkdir(test_path.value().c_str(), 0777) == 0) |
719 return test_path; | 740 return test_path; |
720 else if (errno != EEXIST) | 741 else if (errno != EEXIST) |
721 break; | 742 break; |
722 } | 743 } |
723 return base::FilePath(); | 744 return base::FilePath(); |
724 } | 745 } |
725 | 746 |
726 bool GetInode(const FilePath& path, ino_t* inode) { | |
727 base::ThreadRestrictions::AssertIOAllowed(); // For call to stat(). | |
728 struct stat buffer; | |
729 int result = stat(path.value().c_str(), &buffer); | |
730 if (result < 0) | |
731 return false; | |
732 | |
733 *inode = buffer.st_ino; | |
734 return true; | |
735 } | |
736 | |
737 FILE* OpenFile(const std::string& filename, const char* mode) { | 747 FILE* OpenFile(const std::string& filename, const char* mode) { |
738 return OpenFile(FilePath(filename), mode); | 748 return OpenFile(FilePath(filename), mode); |
739 } | 749 } |
740 | 750 |
741 FILE* OpenFile(const FilePath& filename, const char* mode) { | |
742 base::ThreadRestrictions::AssertIOAllowed(); | |
743 FILE* result = NULL; | |
744 do { | |
745 result = fopen(filename.value().c_str(), mode); | |
746 } while (!result && errno == EINTR); | |
747 return result; | |
748 } | |
749 | |
750 int ReadFile(const FilePath& filename, char* data, int size) { | |
751 base::ThreadRestrictions::AssertIOAllowed(); | |
752 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); | |
753 if (fd < 0) | |
754 return -1; | |
755 | |
756 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); | |
757 if (int ret = IGNORE_EINTR(close(fd)) < 0) | |
758 return ret; | |
759 return bytes_read; | |
760 } | |
761 | |
762 int WriteFile(const FilePath& filename, const char* data, int size) { | 751 int WriteFile(const FilePath& filename, const char* data, int size) { |
763 base::ThreadRestrictions::AssertIOAllowed(); | 752 base::ThreadRestrictions::AssertIOAllowed(); |
764 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); | 753 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); |
765 if (fd < 0) | 754 if (fd < 0) |
766 return -1; | 755 return -1; |
767 | 756 |
768 int bytes_written = WriteFileDescriptor(fd, data, size); | 757 int bytes_written = WriteFileDescriptor(fd, data, size); |
769 if (int ret = IGNORE_EINTR(close(fd)) < 0) | 758 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
770 return ret; | 759 return ret; |
771 return bytes_written; | 760 return bytes_written; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 result = false; | 958 result = false; |
970 if (IGNORE_EINTR(close(outfile)) < 0) | 959 if (IGNORE_EINTR(close(outfile)) < 0) |
971 result = false; | 960 result = false; |
972 | 961 |
973 return result; | 962 return result; |
974 } | 963 } |
975 #endif // !defined(OS_MACOSX) | 964 #endif // !defined(OS_MACOSX) |
976 | 965 |
977 } // namespace internal | 966 } // namespace internal |
978 } // namespace base | 967 } // namespace base |
OLD | NEW |