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 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 bytes_written_partial = | 732 bytes_written_partial = |
733 HANDLE_EINTR(write(fd, data + bytes_written_total, | 733 HANDLE_EINTR(write(fd, data + bytes_written_total, |
734 size - bytes_written_total)); | 734 size - bytes_written_total)); |
735 if (bytes_written_partial < 0) | 735 if (bytes_written_partial < 0) |
736 return -1; | 736 return -1; |
737 } | 737 } |
738 | 738 |
739 return bytes_written_total; | 739 return bytes_written_total; |
740 } | 740 } |
741 | 741 |
| 742 int AppendToFile(const FilePath& filename, const char* data, int size) { |
| 743 ThreadRestrictions::AssertIOAllowed(); |
| 744 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); |
| 745 if (fd < 0) |
| 746 return -1; |
| 747 |
| 748 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 749 if (int ret = IGNORE_EINTR(close(fd)) < 0) |
| 750 return ret; |
| 751 return bytes_written; |
| 752 } |
| 753 |
| 754 // Gets the current working directory for the process. |
| 755 bool GetCurrentDirectory(FilePath* dir) { |
| 756 // getcwd can return ENOENT, which implies it checks against the disk. |
| 757 ThreadRestrictions::AssertIOAllowed(); |
| 758 |
| 759 char system_buffer[PATH_MAX] = ""; |
| 760 if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 761 NOTREACHED(); |
| 762 return false; |
| 763 } |
| 764 *dir = FilePath(system_buffer); |
| 765 return true; |
| 766 } |
| 767 |
| 768 // Sets the current working directory for the process. |
| 769 bool SetCurrentDirectory(const FilePath& path) { |
| 770 ThreadRestrictions::AssertIOAllowed(); |
| 771 int ret = chdir(path.value().c_str()); |
| 772 return !ret; |
| 773 } |
| 774 |
742 } // namespace base | 775 } // namespace base |
743 | 776 |
744 // ----------------------------------------------------------------------------- | 777 // ----------------------------------------------------------------------------- |
745 | 778 |
746 namespace file_util { | 779 namespace file_util { |
747 | 780 |
748 using base::stat_wrapper_t; | 781 using base::stat_wrapper_t; |
749 using base::CallStat; | 782 using base::CallStat; |
750 using base::CallLstat; | 783 using base::CallLstat; |
751 using base::CreateAndOpenFdForTemporaryFile; | 784 using base::CreateAndOpenFdForTemporaryFile; |
(...skipping 18 matching lines...) Expand all Loading... |
770 else if (errno != EEXIST) | 803 else if (errno != EEXIST) |
771 break; | 804 break; |
772 } | 805 } |
773 return base::FilePath(); | 806 return base::FilePath(); |
774 } | 807 } |
775 | 808 |
776 FILE* OpenFile(const std::string& filename, const char* mode) { | 809 FILE* OpenFile(const std::string& filename, const char* mode) { |
777 return OpenFile(FilePath(filename), mode); | 810 return OpenFile(FilePath(filename), mode); |
778 } | 811 } |
779 | 812 |
780 int AppendToFile(const FilePath& filename, const char* data, int size) { | |
781 base::ThreadRestrictions::AssertIOAllowed(); | |
782 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); | |
783 if (fd < 0) | |
784 return -1; | |
785 | |
786 int bytes_written = base::WriteFileDescriptor(fd, data, size); | |
787 if (int ret = IGNORE_EINTR(close(fd)) < 0) | |
788 return ret; | |
789 return bytes_written; | |
790 } | |
791 | |
792 // Gets the current working directory for the process. | |
793 bool GetCurrentDirectory(FilePath* dir) { | |
794 // getcwd can return ENOENT, which implies it checks against the disk. | |
795 base::ThreadRestrictions::AssertIOAllowed(); | |
796 | |
797 char system_buffer[PATH_MAX] = ""; | |
798 if (!getcwd(system_buffer, sizeof(system_buffer))) { | |
799 NOTREACHED(); | |
800 return false; | |
801 } | |
802 *dir = FilePath(system_buffer); | |
803 return true; | |
804 } | |
805 | |
806 // Sets the current working directory for the process. | |
807 bool SetCurrentDirectory(const FilePath& path) { | |
808 base::ThreadRestrictions::AssertIOAllowed(); | |
809 int ret = chdir(path.value().c_str()); | |
810 return !ret; | |
811 } | |
812 | |
813 bool VerifyPathControlledByUser(const FilePath& base, | 813 bool VerifyPathControlledByUser(const FilePath& base, |
814 const FilePath& path, | 814 const FilePath& path, |
815 uid_t owner_uid, | 815 uid_t owner_uid, |
816 const std::set<gid_t>& group_gids) { | 816 const std::set<gid_t>& group_gids) { |
817 if (base != path && !base.IsParent(path)) { | 817 if (base != path && !base.IsParent(path)) { |
818 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" | 818 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" |
819 << base.value() << "\", path = \"" << path.value() << "\""; | 819 << base.value() << "\", path = \"" << path.value() << "\""; |
820 return false; | 820 return false; |
821 } | 821 } |
822 | 822 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 result = false; | 960 result = false; |
961 if (IGNORE_EINTR(close(outfile)) < 0) | 961 if (IGNORE_EINTR(close(outfile)) < 0) |
962 result = false; | 962 result = false; |
963 | 963 |
964 return result; | 964 return result; |
965 } | 965 } |
966 #endif // !defined(OS_MACOSX) | 966 #endif // !defined(OS_MACOSX) |
967 | 967 |
968 } // namespace internal | 968 } // namespace internal |
969 } // namespace base | 969 } // namespace base |
OLD | NEW |