| 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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local |
| 6 // filesystem. | 6 // filesystem. |
| 7 | 7 |
| 8 #ifndef BASE_FILE_UTIL_H_ | 8 #ifndef BASE_FILE_UTIL_H_ |
| 9 #define BASE_FILE_UTIL_H_ | 9 #define BASE_FILE_UTIL_H_ |
| 10 | 10 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // Returns true if the contents of the two files given are equal, false | 129 // Returns true if the contents of the two files given are equal, false |
| 130 // otherwise. If either file can't be read, returns false. | 130 // otherwise. If either file can't be read, returns false. |
| 131 BASE_EXPORT bool ContentsEqual(const FilePath& filename1, | 131 BASE_EXPORT bool ContentsEqual(const FilePath& filename1, |
| 132 const FilePath& filename2); | 132 const FilePath& filename2); |
| 133 | 133 |
| 134 // Returns true if the contents of the two text files given are equal, false | 134 // Returns true if the contents of the two text files given are equal, false |
| 135 // otherwise. This routine treats "\r\n" and "\n" as equivalent. | 135 // otherwise. This routine treats "\r\n" and "\n" as equivalent. |
| 136 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, | 136 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, |
| 137 const FilePath& filename2); | 137 const FilePath& filename2); |
| 138 | 138 |
| 139 // Reads the file at |path| into |contents| and returns true on success. | 139 // Reads the file at |path| into |contents| and returns true on success and |
| 140 // |contents| may be NULL, in which case this function is useful for its | 140 // false on error. For security reasons, a |path| containing path traversal |
| 141 // side effect of priming the disk cache (could be used for unit tests). | 141 // components ('..') is treated as a read error and |contents| is set to empty. |
| 142 // The function returns false and the string pointed to by |contents| is | 142 // In case of I/O error, |contents| holds the data that could be read from the |
| 143 // cleared when |path| does not exist or if it contains path traversal | 143 // file before the error occurred. |
| 144 // components ('..'). | 144 // |contents| may be NULL, in which case this function is useful for its side |
| 145 // effect of priming the disk cache (could be used for unit tests). |
| 145 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); | 146 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); |
| 146 | 147 |
| 147 // Reads the file at |path| into |contents| and returns true on success. | 148 // Reads the file at |path| into |contents| and returns true on success and |
| 148 // |contents| may be NULL, in which case this function is useful for its | 149 // false on error. For security reasons, a |path| containing path traversal |
| 149 // side effect of priming the disk cache (could be used for unit tests). | 150 // components ('..') is treated as a read error and |contents| is set to empty. |
| 150 // The function returns false and the string pointed to by |contents| is | 151 // In case of I/O error, |contents| holds the data that could be read from the |
| 151 // cleared when |path| does not exist or if it contains path traversal | 152 // file before the error occurred. When the file size exceeds |max_size|, the |
| 152 // components ('..'). | 153 // function returns false with |contents| holding the file truncated to |
| 153 // When the file size exceeds |max_size|, the function returns false | 154 // |max_size|. |
| 154 // with |contents| holding the file truncated to |max_size|. | 155 // |contents| may be NULL, in which case this function is useful for its side |
| 156 // effect of priming the disk cache (could be used for unit tests). |
| 155 BASE_EXPORT bool ReadFileToString(const FilePath& path, | 157 BASE_EXPORT bool ReadFileToString(const FilePath& path, |
| 156 std::string* contents, | 158 std::string* contents, |
| 157 size_t max_size); | 159 size_t max_size); |
| 158 | 160 |
| 159 #if defined(OS_POSIX) | 161 #if defined(OS_POSIX) |
| 160 | 162 |
| 161 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result | 163 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
| 162 // in |buffer|. This function is protected against EINTR and partial reads. | 164 // in |buffer|. This function is protected against EINTR and partial reads. |
| 163 // Returns true iff |bytes| bytes have been successfully read from |fd|. | 165 // Returns true iff |bytes| bytes have been successfully read from |fd|. |
| 164 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); | 166 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
| 165 | 167 |
| 166 // Creates a symbolic link at |symlink| pointing to |target|. Returns | 168 // Creates a symbolic link at |symlink| pointing to |target|. Returns |
| 167 // false on failure. | 169 // false on failure. |
| 168 BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, | 170 BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, |
| 169 const FilePath& symlink); | 171 const FilePath& symlink); |
| 170 | 172 |
| 171 // Reads the given |symlink| and returns where it points to in |target|. | 173 // Reads the given |symlink| and returns where it points to in |target|. |
| 172 // Returns false upon failure. | 174 // Returns false upon failure. |
| 173 BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); | 175 BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); |
| 174 | 176 |
| 175 // Bits ans masks of the file permission. | 177 // Bits and masks of the file permission. |
| 176 enum FilePermissionBits { | 178 enum FilePermissionBits { |
| 177 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, | 179 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, |
| 178 FILE_PERMISSION_USER_MASK = S_IRWXU, | 180 FILE_PERMISSION_USER_MASK = S_IRWXU, |
| 179 FILE_PERMISSION_GROUP_MASK = S_IRWXG, | 181 FILE_PERMISSION_GROUP_MASK = S_IRWXG, |
| 180 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, | 182 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, |
| 181 | 183 |
| 182 FILE_PERMISSION_READ_BY_USER = S_IRUSR, | 184 FILE_PERMISSION_READ_BY_USER = S_IRUSR, |
| 183 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, | 185 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, |
| 184 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, | 186 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, |
| 185 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, | 187 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 // This function simulates Move(), but unlike Move() it works across volumes. | 445 // This function simulates Move(), but unlike Move() it works across volumes. |
| 444 // This function is not transactional. | 446 // This function is not transactional. |
| 445 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 447 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 446 const FilePath& to_path); | 448 const FilePath& to_path); |
| 447 #endif // defined(OS_WIN) | 449 #endif // defined(OS_WIN) |
| 448 | 450 |
| 449 } // namespace internal | 451 } // namespace internal |
| 450 } // namespace base | 452 } // namespace base |
| 451 | 453 |
| 452 #endif // BASE_FILE_UTIL_H_ | 454 #endif // BASE_FILE_UTIL_H_ |
| OLD | NEW |