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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, | 130 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, |
131 const FilePath& filename2); | 131 const FilePath& filename2); |
132 | 132 |
133 // Read the file at |path| into |contents|, returning true on success. | 133 // Read the file at |path| into |contents|, returning true on success. |
134 // This function fails if the |path| contains path traversal components ('..'). | 134 // This function fails if the |path| contains path traversal components ('..'). |
135 // |contents| may be NULL, in which case this function is useful for its | 135 // |contents| may be NULL, in which case this function is useful for its |
136 // side effect of priming the disk cache. | 136 // side effect of priming the disk cache. |
137 // Useful for unit tests. | 137 // Useful for unit tests. |
138 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); | 138 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); |
139 | 139 |
140 } // namespace base | 140 #if defined(OS_POSIX) |
141 | 141 |
142 // ----------------------------------------------------------------------------- | |
143 | |
144 namespace file_util { | |
145 | |
146 #if defined(OS_POSIX) | |
147 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result | 142 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
148 // in |buffer|. This function is protected against EINTR and partial reads. | 143 // in |buffer|. This function is protected against EINTR and partial reads. |
149 // Returns true iff |bytes| bytes have been successfully read from |fd|. | 144 // Returns true iff |bytes| bytes have been successfully read from |fd|. |
150 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); | 145 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
151 | 146 |
152 // Creates a symbolic link at |symlink| pointing to |target|. Returns | 147 // Creates a symbolic link at |symlink| pointing to |target|. Returns |
153 // false on failure. | 148 // false on failure. |
154 BASE_EXPORT bool CreateSymbolicLink(const base::FilePath& target, | 149 BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, |
155 const base::FilePath& symlink); | 150 const FilePath& symlink); |
156 | 151 |
157 // Reads the given |symlink| and returns where it points to in |target|. | 152 // Reads the given |symlink| and returns where it points to in |target|. |
158 // Returns false upon failure. | 153 // Returns false upon failure. |
159 BASE_EXPORT bool ReadSymbolicLink(const base::FilePath& symlink, | 154 BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); |
160 base::FilePath* target); | |
161 | 155 |
162 // Bits ans masks of the file permission. | 156 // Bits ans masks of the file permission. |
163 enum FilePermissionBits { | 157 enum FilePermissionBits { |
164 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, | 158 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, |
165 FILE_PERMISSION_USER_MASK = S_IRWXU, | 159 FILE_PERMISSION_USER_MASK = S_IRWXU, |
166 FILE_PERMISSION_GROUP_MASK = S_IRWXG, | 160 FILE_PERMISSION_GROUP_MASK = S_IRWXG, |
167 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, | 161 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, |
168 | 162 |
169 FILE_PERMISSION_READ_BY_USER = S_IRUSR, | 163 FILE_PERMISSION_READ_BY_USER = S_IRUSR, |
170 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, | 164 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, |
171 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, | 165 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, |
172 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, | 166 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, |
173 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, | 167 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, |
174 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, | 168 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, |
175 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, | 169 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, |
176 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, | 170 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, |
177 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, | 171 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, |
178 }; | 172 }; |
179 | 173 |
180 // Reads the permission of the given |path|, storing the file permission | 174 // Reads the permission of the given |path|, storing the file permission |
181 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of | 175 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of |
182 // a file which the symlink points to. | 176 // a file which the symlink points to. |
183 BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path, | 177 BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode); |
184 int* mode); | |
185 // Sets the permission of the given |path|. If |path| is symbolic link, sets | 178 // Sets the permission of the given |path|. If |path| is symbolic link, sets |
186 // the permission of a file which the symlink points to. | 179 // the permission of a file which the symlink points to. |
187 BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path, | 180 BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode); |
188 int mode); | 181 |
189 #endif // defined(OS_POSIX) | 182 #endif // OS_POSIX |
| 183 |
| 184 } // namespace base |
| 185 |
| 186 // ----------------------------------------------------------------------------- |
| 187 |
| 188 namespace file_util { |
190 | 189 |
191 // Return true if the given directory is empty | 190 // Return true if the given directory is empty |
192 BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path); | 191 BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path); |
193 | 192 |
194 // Get the temporary directory provided by the system. | 193 // Get the temporary directory provided by the system. |
195 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of | 194 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of |
196 // the functions below. | 195 // the functions below. |
197 BASE_EXPORT bool GetTempDir(base::FilePath* path); | 196 BASE_EXPORT bool GetTempDir(base::FilePath* path); |
198 // Get a temporary directory for shared memory files. | 197 // Get a temporary directory for shared memory files. |
199 // Only useful on POSIX; redirects to GetTempDir() on Windows. | 198 // Only useful on POSIX; redirects to GetTempDir() on Windows. |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 // This function simulates Move(), but unlike Move() it works across volumes. | 451 // This function simulates Move(), but unlike Move() it works across volumes. |
453 // This function is not transactional. | 452 // This function is not transactional. |
454 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 453 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
455 const FilePath& to_path); | 454 const FilePath& to_path); |
456 #endif // defined(OS_WIN) | 455 #endif // defined(OS_WIN) |
457 | 456 |
458 } // namespace internal | 457 } // namespace internal |
459 } // namespace base | 458 } // namespace base |
460 | 459 |
461 #endif // BASE_FILE_UTIL_H_ | 460 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |