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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } | 42 } |
43 | 43 |
44 namespace file_util { | 44 namespace file_util { |
45 | 45 |
46 extern bool g_bug108724_debug; | 46 extern bool g_bug108724_debug; |
47 | 47 |
48 //----------------------------------------------------------------------------- | 48 //----------------------------------------------------------------------------- |
49 // Functions that operate purely on a path string w/o touching the filesystem: | 49 // Functions that operate purely on a path string w/o touching the filesystem: |
50 | 50 |
51 // Returns true if the given path ends with a path separator character. | 51 // Returns true if the given path ends with a path separator character. |
52 BASE_EXPORT bool EndsWithSeparator(const FilePath& path); | 52 BASE_EXPORT bool EndsWithSeparator(const base::FilePath& path); |
53 | 53 |
54 // Makes sure that |path| ends with a separator IFF path is a directory that | 54 // Makes sure that |path| ends with a separator IFF path is a directory that |
55 // exists. Returns true if |path| is an existing directory, false otherwise. | 55 // exists. Returns true if |path| is an existing directory, false otherwise. |
56 BASE_EXPORT bool EnsureEndsWithSeparator(FilePath* path); | 56 BASE_EXPORT bool EnsureEndsWithSeparator(base::FilePath* path); |
57 | 57 |
58 // Convert provided relative path into an absolute path. Returns false on | 58 // Convert provided relative path into an absolute path. Returns false on |
59 // error. On POSIX, this function fails if the path does not exist. | 59 // error. On POSIX, this function fails if the path does not exist. |
60 BASE_EXPORT bool AbsolutePath(FilePath* path); | 60 BASE_EXPORT bool AbsolutePath(base::FilePath* path); |
61 | 61 |
62 // Returns true if |parent| contains |child|. Both paths are converted to | 62 // Returns true if |parent| contains |child|. Both paths are converted to |
63 // absolute paths before doing the comparison. | 63 // absolute paths before doing the comparison. |
64 BASE_EXPORT bool ContainsPath(const FilePath& parent, const FilePath& child); | 64 BASE_EXPORT bool ContainsPath(const base::FilePath& parent, |
| 65 const base::FilePath& child); |
65 | 66 |
66 //----------------------------------------------------------------------------- | 67 //----------------------------------------------------------------------------- |
67 // Functions that involve filesystem access or modification: | 68 // Functions that involve filesystem access or modification: |
68 | 69 |
69 // Returns the number of files matching the current path that were | 70 // Returns the number of files matching the current path that were |
70 // created on or after the given |file_time|. Doesn't count ".." or ".". | 71 // created on or after the given |file_time|. Doesn't count ".." or ".". |
71 // | 72 // |
72 // Note for POSIX environments: a file created before |file_time| | 73 // Note for POSIX environments: a file created before |file_time| |
73 // can be mis-detected as a newer file due to low precision of | 74 // can be mis-detected as a newer file due to low precision of |
74 // timestmap of file creation time. If you need to avoid such | 75 // timestmap of file creation time. If you need to avoid such |
75 // mis-detection perfectly, you should wait one second before | 76 // mis-detection perfectly, you should wait one second before |
76 // obtaining |file_time|. | 77 // obtaining |file_time|. |
77 BASE_EXPORT int CountFilesCreatedAfter(const FilePath& path, | 78 BASE_EXPORT int CountFilesCreatedAfter(const base::FilePath& path, |
78 const base::Time& file_time); | 79 const base::Time& file_time); |
79 | 80 |
80 // Returns the total number of bytes used by all the files under |root_path|. | 81 // Returns the total number of bytes used by all the files under |root_path|. |
81 // If the path does not exist the function returns 0. | 82 // If the path does not exist the function returns 0. |
82 // | 83 // |
83 // This function is implemented using the FileEnumerator class so it is not | 84 // This function is implemented using the FileEnumerator class so it is not |
84 // particularly speedy in any platform. | 85 // particularly speedy in any platform. |
85 BASE_EXPORT int64 ComputeDirectorySize(const FilePath& root_path); | 86 BASE_EXPORT int64 ComputeDirectorySize(const base::FilePath& root_path); |
86 | 87 |
87 // Returns the total number of bytes used by all files matching the provided | 88 // Returns the total number of bytes used by all files matching the provided |
88 // |pattern|, on this |directory| (without recursion). If the path does not | 89 // |pattern|, on this |directory| (without recursion). If the path does not |
89 // exist the function returns 0. | 90 // exist the function returns 0. |
90 // | 91 // |
91 // This function is implemented using the FileEnumerator class so it is not | 92 // This function is implemented using the FileEnumerator class so it is not |
92 // particularly speedy in any platform. | 93 // particularly speedy in any platform. |
93 BASE_EXPORT int64 ComputeFilesSize(const FilePath& directory, | 94 BASE_EXPORT int64 ComputeFilesSize(const base::FilePath& directory, |
94 const FilePath::StringType& pattern); | 95 const base::FilePath::StringType& pattern); |
95 | 96 |
96 // Deletes the given path, whether it's a file or a directory. | 97 // Deletes the given path, whether it's a file or a directory. |
97 // If it's a directory, it's perfectly happy to delete all of the | 98 // If it's a directory, it's perfectly happy to delete all of the |
98 // directory's contents. Passing true to recursive deletes | 99 // directory's contents. Passing true to recursive deletes |
99 // subdirectories and their contents as well. | 100 // subdirectories and their contents as well. |
100 // Returns true if successful, false otherwise. | 101 // Returns true if successful, false otherwise. |
101 // | 102 // |
102 // In posix environment and if |path| is a symbolic link, this deletes only | 103 // In posix environment and if |path| is a symbolic link, this deletes only |
103 // the symlink. (even if the symlink points to a non-existent file) | 104 // the symlink. (even if the symlink points to a non-existent file) |
104 // | 105 // |
105 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 106 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
106 // TO "rm -rf", SO USE WITH CAUTION. | 107 // TO "rm -rf", SO USE WITH CAUTION. |
107 BASE_EXPORT bool Delete(const FilePath& path, bool recursive); | 108 BASE_EXPORT bool Delete(const base::FilePath& path, bool recursive); |
108 | 109 |
109 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
110 // Schedules to delete the given path, whether it's a file or a directory, until | 111 // Schedules to delete the given path, whether it's a file or a directory, until |
111 // the operating system is restarted. | 112 // the operating system is restarted. |
112 // Note: | 113 // Note: |
113 // 1) The file/directory to be deleted should exist in a temp folder. | 114 // 1) The file/directory to be deleted should exist in a temp folder. |
114 // 2) The directory to be deleted must be empty. | 115 // 2) The directory to be deleted must be empty. |
115 BASE_EXPORT bool DeleteAfterReboot(const FilePath& path); | 116 BASE_EXPORT bool DeleteAfterReboot(const base::FilePath& path); |
116 #endif | 117 #endif |
117 | 118 |
118 // Moves the given path, whether it's a file or a directory. | 119 // Moves the given path, whether it's a file or a directory. |
119 // If a simple rename is not possible, such as in the case where the paths are | 120 // If a simple rename is not possible, such as in the case where the paths are |
120 // on different volumes, this will attempt to copy and delete. Returns | 121 // on different volumes, this will attempt to copy and delete. Returns |
121 // true for success. | 122 // true for success. |
122 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); | 123 BASE_EXPORT bool Move(const base::FilePath& from_path, |
| 124 const base::FilePath& to_path); |
123 | 125 |
124 // Renames file |from_path| to |to_path|. Both paths must be on the same | 126 // Renames file |from_path| to |to_path|. Both paths must be on the same |
125 // volume, or the function will fail. Destination file will be created | 127 // volume, or the function will fail. Destination file will be created |
126 // if it doesn't exist. Prefer this function over Move when dealing with | 128 // if it doesn't exist. Prefer this function over Move when dealing with |
127 // temporary files. On Windows it preserves attributes of the target file. | 129 // temporary files. On Windows it preserves attributes of the target file. |
128 // Returns true on success. | 130 // Returns true on success. |
129 BASE_EXPORT bool ReplaceFile(const FilePath& from_path, | 131 BASE_EXPORT bool ReplaceFile(const base::FilePath& from_path, |
130 const FilePath& to_path); | 132 const base::FilePath& to_path); |
131 | 133 |
132 // Copies a single file. Use CopyDirectory to copy directories. | 134 // Copies a single file. Use CopyDirectory to copy directories. |
133 BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path); | 135 BASE_EXPORT bool CopyFile(const base::FilePath& from_path, |
| 136 const base::FilePath& to_path); |
134 | 137 |
135 // Copies the given path, and optionally all subdirectories and their contents | 138 // Copies the given path, and optionally all subdirectories and their contents |
136 // as well. | 139 // as well. |
137 // If there are files existing under to_path, always overwrite. | 140 // If there are files existing under to_path, always overwrite. |
138 // Returns true if successful, false otherwise. | 141 // Returns true if successful, false otherwise. |
139 // Don't use wildcards on the names, it may stop working without notice. | 142 // Don't use wildcards on the names, it may stop working without notice. |
140 // | 143 // |
141 // If you only need to copy a file use CopyFile, it's faster. | 144 // If you only need to copy a file use CopyFile, it's faster. |
142 BASE_EXPORT bool CopyDirectory(const FilePath& from_path, | 145 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path, |
143 const FilePath& to_path, | 146 const base::FilePath& to_path, |
144 bool recursive); | 147 bool recursive); |
145 | 148 |
146 // Returns true if the given path exists on the local filesystem, | 149 // Returns true if the given path exists on the local filesystem, |
147 // false otherwise. | 150 // false otherwise. |
148 BASE_EXPORT bool PathExists(const FilePath& path); | 151 BASE_EXPORT bool PathExists(const base::FilePath& path); |
149 | 152 |
150 // Returns true if the given path is writable by the user, false otherwise. | 153 // Returns true if the given path is writable by the user, false otherwise. |
151 BASE_EXPORT bool PathIsWritable(const FilePath& path); | 154 BASE_EXPORT bool PathIsWritable(const base::FilePath& path); |
152 | 155 |
153 // Returns true if the given path exists and is a directory, false otherwise. | 156 // Returns true if the given path exists and is a directory, false otherwise. |
154 BASE_EXPORT bool DirectoryExists(const FilePath& path); | 157 BASE_EXPORT bool DirectoryExists(const base::FilePath& path); |
155 | 158 |
156 // Returns true if the contents of the two files given are equal, false | 159 // Returns true if the contents of the two files given are equal, false |
157 // otherwise. If either file can't be read, returns false. | 160 // otherwise. If either file can't be read, returns false. |
158 BASE_EXPORT bool ContentsEqual(const FilePath& filename1, | 161 BASE_EXPORT bool ContentsEqual(const base::FilePath& filename1, |
159 const FilePath& filename2); | 162 const base::FilePath& filename2); |
160 | 163 |
161 // Returns true if the contents of the two text files given are equal, false | 164 // Returns true if the contents of the two text files given are equal, false |
162 // otherwise. This routine treats "\r\n" and "\n" as equivalent. | 165 // otherwise. This routine treats "\r\n" and "\n" as equivalent. |
163 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, | 166 BASE_EXPORT bool TextContentsEqual(const base::FilePath& filename1, |
164 const FilePath& filename2); | 167 const base::FilePath& filename2); |
165 | 168 |
166 // Read the file at |path| into |contents|, returning true on success. | 169 // Read the file at |path| into |contents|, returning true on success. |
167 // This function fails if the |path| contains path traversal components ('..'). | 170 // This function fails if the |path| contains path traversal components ('..'). |
168 // |contents| may be NULL, in which case this function is useful for its | 171 // |contents| may be NULL, in which case this function is useful for its |
169 // side effect of priming the disk cache. | 172 // side effect of priming the disk cache. |
170 // Useful for unit tests. | 173 // Useful for unit tests. |
171 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); | 174 BASE_EXPORT bool ReadFileToString(const base::FilePath& path, |
| 175 std::string* contents); |
172 | 176 |
173 #if defined(OS_POSIX) | 177 #if defined(OS_POSIX) |
174 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result | 178 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
175 // in |buffer|. This function is protected against EINTR and partial reads. | 179 // in |buffer|. This function is protected against EINTR and partial reads. |
176 // Returns true iff |bytes| bytes have been successfuly read from |fd|. | 180 // Returns true iff |bytes| bytes have been successfuly read from |fd|. |
177 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); | 181 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
178 | 182 |
179 // Creates a symbolic link at |symlink| pointing to |target|. Returns | 183 // Creates a symbolic link at |symlink| pointing to |target|. Returns |
180 // false on failure. | 184 // false on failure. |
181 BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, | 185 BASE_EXPORT bool CreateSymbolicLink(const base::FilePath& target, |
182 const FilePath& symlink); | 186 const base::FilePath& symlink); |
183 | 187 |
184 // Reads the given |symlink| and returns where it points to in |target|. | 188 // Reads the given |symlink| and returns where it points to in |target|. |
185 // Returns false upon failure. | 189 // Returns false upon failure. |
186 BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); | 190 BASE_EXPORT bool ReadSymbolicLink(const base::FilePath& symlink, |
| 191 base::FilePath* target); |
187 | 192 |
188 // Bits ans masks of the file permission. | 193 // Bits ans masks of the file permission. |
189 enum FilePermissionBits { | 194 enum FilePermissionBits { |
190 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, | 195 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, |
191 FILE_PERMISSION_USER_MASK = S_IRWXU, | 196 FILE_PERMISSION_USER_MASK = S_IRWXU, |
192 FILE_PERMISSION_GROUP_MASK = S_IRWXG, | 197 FILE_PERMISSION_GROUP_MASK = S_IRWXG, |
193 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, | 198 FILE_PERMISSION_OTHERS_MASK = S_IRWXO, |
194 | 199 |
195 FILE_PERMISSION_READ_BY_USER = S_IRUSR, | 200 FILE_PERMISSION_READ_BY_USER = S_IRUSR, |
196 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, | 201 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, |
197 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, | 202 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, |
198 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, | 203 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, |
199 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, | 204 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, |
200 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, | 205 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, |
201 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, | 206 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, |
202 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, | 207 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, |
203 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, | 208 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, |
204 }; | 209 }; |
205 | 210 |
206 // Reads the permission of the given |path|, storing the file permission | 211 // Reads the permission of the given |path|, storing the file permission |
207 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of | 212 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of |
208 // a file which the symlink points to. | 213 // a file which the symlink points to. |
209 BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, | 214 BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path, |
210 int* mode); | 215 int* mode); |
211 // Sets the permission of the given |path|. If |path| is symbolic link, sets | 216 // Sets the permission of the given |path|. If |path| is symbolic link, sets |
212 // the permission of a file which the symlink points to. | 217 // the permission of a file which the symlink points to. |
213 BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, | 218 BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path, |
214 int mode); | 219 int mode); |
215 #endif // defined(OS_POSIX) | 220 #endif // defined(OS_POSIX) |
216 | 221 |
217 #if defined(OS_WIN) | 222 #if defined(OS_WIN) |
218 // Copy from_path to to_path recursively and then delete from_path recursively. | 223 // Copy from_path to to_path recursively and then delete from_path recursively. |
219 // Returns true if all operations succeed. | 224 // Returns true if all operations succeed. |
220 // This function simulates Move(), but unlike Move() it works across volumes. | 225 // This function simulates Move(), but unlike Move() it works across volumes. |
221 // This fuction is not transactional. | 226 // This fuction is not transactional. |
222 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 227 BASE_EXPORT bool CopyAndDeleteDirectory(const base::FilePath& from_path, |
223 const FilePath& to_path); | 228 const base::FilePath& to_path); |
224 #endif // defined(OS_WIN) | 229 #endif // defined(OS_WIN) |
225 | 230 |
226 // Return true if the given directory is empty | 231 // Return true if the given directory is empty |
227 BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path); | 232 BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path); |
228 | 233 |
229 // Get the temporary directory provided by the system. | 234 // Get the temporary directory provided by the system. |
230 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of | 235 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of |
231 // the functions below. | 236 // the functions below. |
232 BASE_EXPORT bool GetTempDir(FilePath* path); | 237 BASE_EXPORT bool GetTempDir(base::FilePath* path); |
233 // Get a temporary directory for shared memory files. | 238 // Get a temporary directory for shared memory files. |
234 // Only useful on POSIX; redirects to GetTempDir() on Windows. | 239 // Only useful on POSIX; redirects to GetTempDir() on Windows. |
235 BASE_EXPORT bool GetShmemTempDir(FilePath* path, bool executable); | 240 BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable); |
236 | 241 |
237 // Get the home directory. This is more complicated than just getenv("HOME") | 242 // Get the home directory. This is more complicated than just getenv("HOME") |
238 // as it knows to fall back on getpwent() etc. | 243 // as it knows to fall back on getpwent() etc. |
239 BASE_EXPORT FilePath GetHomeDir(); | 244 BASE_EXPORT base::FilePath GetHomeDir(); |
240 | 245 |
241 // Creates a temporary file. The full path is placed in |path|, and the | 246 // Creates a temporary file. The full path is placed in |path|, and the |
242 // function returns true if was successful in creating the file. The file will | 247 // function returns true if was successful in creating the file. The file will |
243 // be empty and all handles closed after this function returns. | 248 // be empty and all handles closed after this function returns. |
244 BASE_EXPORT bool CreateTemporaryFile(FilePath* path); | 249 BASE_EXPORT bool CreateTemporaryFile(base::FilePath* path); |
245 | 250 |
246 // Same as CreateTemporaryFile but the file is created in |dir|. | 251 // Same as CreateTemporaryFile but the file is created in |dir|. |
247 BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir, | 252 BASE_EXPORT bool CreateTemporaryFileInDir(const base::FilePath& dir, |
248 FilePath* temp_file); | 253 base::FilePath* temp_file); |
249 | 254 |
250 // Create and open a temporary file. File is opened for read/write. | 255 // Create and open a temporary file. File is opened for read/write. |
251 // The full path is placed in |path|. | 256 // The full path is placed in |path|. |
252 // Returns a handle to the opened file or NULL if an error occured. | 257 // Returns a handle to the opened file or NULL if an error occured. |
253 BASE_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path); | 258 BASE_EXPORT FILE* CreateAndOpenTemporaryFile(base::FilePath* path); |
254 // Like above but for shmem files. Only useful for POSIX. | 259 // Like above but for shmem files. Only useful for POSIX. |
255 // The executable flag says the file needs to support using | 260 // The executable flag says the file needs to support using |
256 // mprotect with PROT_EXEC after mapping. | 261 // mprotect with PROT_EXEC after mapping. |
257 BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, | 262 BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(base::FilePath* path, |
258 bool executable); | 263 bool executable); |
259 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. | 264 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. |
260 BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, | 265 BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const base::FilePath& dir, |
261 FilePath* path); | 266 base::FilePath* path); |
262 | 267 |
263 // Create a new directory. If prefix is provided, the new directory name is in | 268 // Create a new directory. If prefix is provided, the new directory name is in |
264 // the format of prefixyyyy. | 269 // the format of prefixyyyy. |
265 // NOTE: prefix is ignored in the POSIX implementation. | 270 // NOTE: prefix is ignored in the POSIX implementation. |
266 // If success, return true and output the full path of the directory created. | 271 // If success, return true and output the full path of the directory created. |
267 BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 272 BASE_EXPORT bool CreateNewTempDirectory( |
268 FilePath* new_temp_path); | 273 const base::FilePath::StringType& prefix, |
| 274 base::FilePath* new_temp_path); |
269 | 275 |
270 // Create a directory within another directory. | 276 // Create a directory within another directory. |
271 // Extra characters will be appended to |prefix| to ensure that the | 277 // Extra characters will be appended to |prefix| to ensure that the |
272 // new directory does not have the same name as an existing directory. | 278 // new directory does not have the same name as an existing directory. |
273 BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir, | 279 BASE_EXPORT bool CreateTemporaryDirInDir( |
274 const FilePath::StringType& prefix, | 280 const base::FilePath& base_dir, |
275 FilePath* new_dir); | 281 const base::FilePath::StringType& prefix, |
| 282 base::FilePath* new_dir); |
276 | 283 |
277 // Creates a directory, as well as creating any parent directories, if they | 284 // Creates a directory, as well as creating any parent directories, if they |
278 // don't exist. Returns 'true' on successful creation, or if the directory | 285 // don't exist. Returns 'true' on successful creation, or if the directory |
279 // already exists. The directory is only readable by the current user. | 286 // already exists. The directory is only readable by the current user. |
280 BASE_EXPORT bool CreateDirectory(const FilePath& full_path); | 287 BASE_EXPORT bool CreateDirectory(const base::FilePath& full_path); |
281 | 288 |
282 // Returns the file size. Returns true on success. | 289 // Returns the file size. Returns true on success. |
283 BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size); | 290 BASE_EXPORT bool GetFileSize(const base::FilePath& file_path, int64* file_size); |
284 | 291 |
285 // Returns true if the given path's base name is ".". | 292 // Returns true if the given path's base name is ".". |
286 BASE_EXPORT bool IsDot(const FilePath& path); | 293 BASE_EXPORT bool IsDot(const base::FilePath& path); |
287 | 294 |
288 // Returns true if the given path's base name is "..". | 295 // Returns true if the given path's base name is "..". |
289 BASE_EXPORT bool IsDotDot(const FilePath& path); | 296 BASE_EXPORT bool IsDotDot(const base::FilePath& path); |
290 | 297 |
291 // Sets |real_path| to |path| with symbolic links and junctions expanded. | 298 // Sets |real_path| to |path| with symbolic links and junctions expanded. |
292 // On windows, make sure the path starts with a lettered drive. | 299 // On windows, make sure the path starts with a lettered drive. |
293 // |path| must reference a file. Function will fail if |path| points to | 300 // |path| must reference a file. Function will fail if |path| points to |
294 // a directory or to a nonexistent path. On windows, this function will | 301 // a directory or to a nonexistent path. On windows, this function will |
295 // fail if |path| is a junction or symlink that points to an empty file, | 302 // fail if |path| is a junction or symlink that points to an empty file, |
296 // or if |real_path| would be longer than MAX_PATH characters. | 303 // or if |real_path| would be longer than MAX_PATH characters. |
297 BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path); | 304 BASE_EXPORT bool NormalizeFilePath(const base::FilePath& path, |
| 305 base::FilePath* real_path); |
298 | 306 |
299 #if defined(OS_WIN) | 307 #if defined(OS_WIN) |
300 | 308 |
301 // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."), | 309 // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."), |
302 // return in |drive_letter_path| the equivalent path that starts with | 310 // return in |drive_letter_path| the equivalent path that starts with |
303 // a drive letter ("C:\..."). Return false if no such path exists. | 311 // a drive letter ("C:\..."). Return false if no such path exists. |
304 BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path, | 312 BASE_EXPORT bool DevicePathToDriveLetterPath(const base::FilePath& device_path, |
305 FilePath* drive_letter_path); | 313 base::FilePath* drive_letter_path); |
306 | 314 |
307 // Given an existing file in |path|, set |real_path| to the path | 315 // Given an existing file in |path|, set |real_path| to the path |
308 // in native NT format, of the form "\Device\HarddiskVolumeXX\..". | 316 // in native NT format, of the form "\Device\HarddiskVolumeXX\..". |
309 // Returns false if the path can not be found. Empty files cannot | 317 // Returns false if the path can not be found. Empty files cannot |
310 // be resolved with this function. | 318 // be resolved with this function. |
311 BASE_EXPORT bool NormalizeToNativeFilePath(const FilePath& path, | 319 BASE_EXPORT bool NormalizeToNativeFilePath(const base::FilePath& path, |
312 FilePath* nt_path); | 320 base::FilePath* nt_path); |
313 #endif | 321 #endif |
314 | 322 |
315 // This function will return if the given file is a symlink or not. | 323 // This function will return if the given file is a symlink or not. |
316 BASE_EXPORT bool IsLink(const FilePath& file_path); | 324 BASE_EXPORT bool IsLink(const base::FilePath& file_path); |
317 | 325 |
318 // Returns information about the given file path. | 326 // Returns information about the given file path. |
319 BASE_EXPORT bool GetFileInfo(const FilePath& file_path, | 327 BASE_EXPORT bool GetFileInfo(const base::FilePath& file_path, |
320 base::PlatformFileInfo* info); | 328 base::PlatformFileInfo* info); |
321 | 329 |
322 // Sets the time of the last access and the time of the last modification. | 330 // Sets the time of the last access and the time of the last modification. |
323 BASE_EXPORT bool TouchFile(const FilePath& path, | 331 BASE_EXPORT bool TouchFile(const base::FilePath& path, |
324 const base::Time& last_accessed, | 332 const base::Time& last_accessed, |
325 const base::Time& last_modified); | 333 const base::Time& last_modified); |
326 | 334 |
327 // Set the time of the last modification. Useful for unit tests. | 335 // Set the time of the last modification. Useful for unit tests. |
328 BASE_EXPORT bool SetLastModifiedTime(const FilePath& path, | 336 BASE_EXPORT bool SetLastModifiedTime(const base::FilePath& path, |
329 const base::Time& last_modified); | 337 const base::Time& last_modified); |
330 | 338 |
331 #if defined(OS_POSIX) | 339 #if defined(OS_POSIX) |
332 // Store inode number of |path| in |inode|. Return true on success. | 340 // Store inode number of |path| in |inode|. Return true on success. |
333 BASE_EXPORT bool GetInode(const FilePath& path, ino_t* inode); | 341 BASE_EXPORT bool GetInode(const base::FilePath& path, ino_t* inode); |
334 #endif | 342 #endif |
335 | 343 |
336 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. | 344 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
337 BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode); | 345 BASE_EXPORT FILE* OpenFile(const base::FilePath& filename, const char* mode); |
338 | 346 |
339 // Closes file opened by OpenFile. Returns true on success. | 347 // Closes file opened by OpenFile. Returns true on success. |
340 BASE_EXPORT bool CloseFile(FILE* file); | 348 BASE_EXPORT bool CloseFile(FILE* file); |
341 | 349 |
342 // Truncates an open file to end at the location of the current file pointer. | 350 // Truncates an open file to end at the location of the current file pointer. |
343 // This is a cross-platform analog to Windows' SetEndOfFile() function. | 351 // This is a cross-platform analog to Windows' SetEndOfFile() function. |
344 BASE_EXPORT bool TruncateFile(FILE* file); | 352 BASE_EXPORT bool TruncateFile(FILE* file); |
345 | 353 |
346 // Reads the given number of bytes from the file into the buffer. Returns | 354 // Reads the given number of bytes from the file into the buffer. Returns |
347 // the number of read bytes, or -1 on error. | 355 // the number of read bytes, or -1 on error. |
348 BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int size); | 356 BASE_EXPORT int ReadFile(const base::FilePath& filename, char* data, int size); |
349 | 357 |
350 // Writes the given buffer into the file, overwriting any data that was | 358 // Writes the given buffer into the file, overwriting any data that was |
351 // previously there. Returns the number of bytes written, or -1 on error. | 359 // previously there. Returns the number of bytes written, or -1 on error. |
352 BASE_EXPORT int WriteFile(const FilePath& filename, const char* data, int size); | 360 BASE_EXPORT int WriteFile(const base::FilePath& filename, const char* data, |
| 361 int size); |
353 #if defined(OS_POSIX) | 362 #if defined(OS_POSIX) |
354 // Append the data to |fd|. Does not close |fd| when done. | 363 // Append the data to |fd|. Does not close |fd| when done. |
355 BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size); | 364 BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size); |
356 #endif | 365 #endif |
357 // Append the given buffer into the file. Returns the number of bytes written, | 366 // Append the given buffer into the file. Returns the number of bytes written, |
358 // or -1 on error. | 367 // or -1 on error. |
359 BASE_EXPORT int AppendToFile(const FilePath& filename, | 368 BASE_EXPORT int AppendToFile(const base::FilePath& filename, |
360 const char* data, int size); | 369 const char* data, int size); |
361 | 370 |
362 // Gets the current working directory for the process. | 371 // Gets the current working directory for the process. |
363 BASE_EXPORT bool GetCurrentDirectory(FilePath* path); | 372 BASE_EXPORT bool GetCurrentDirectory(base::FilePath* path); |
364 | 373 |
365 // Sets the current working directory for the process. | 374 // Sets the current working directory for the process. |
366 BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); | 375 BASE_EXPORT bool SetCurrentDirectory(const base::FilePath& path); |
367 | 376 |
368 // Attempts to find a number that can be appended to the |path| to make it | 377 // Attempts to find a number that can be appended to the |path| to make it |
369 // unique. If |path| does not exist, 0 is returned. If it fails to find such | 378 // unique. If |path| does not exist, 0 is returned. If it fails to find such |
370 // a number, -1 is returned. If |suffix| is not empty, also checks the | 379 // a number, -1 is returned. If |suffix| is not empty, also checks the |
371 // existence of it with the given suffix. | 380 // existence of it with the given suffix. |
372 BASE_EXPORT int GetUniquePathNumber(const FilePath& path, | 381 BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path, |
373 const FilePath::StringType& suffix); | 382 const base::FilePath::StringType& suffix); |
374 | 383 |
375 #if defined(OS_POSIX) | 384 #if defined(OS_POSIX) |
376 // Test that |path| can only be changed by a given user and members of | 385 // Test that |path| can only be changed by a given user and members of |
377 // a given set of groups. | 386 // a given set of groups. |
378 // Specifically, test that all parts of |path| under (and including) |base|: | 387 // Specifically, test that all parts of |path| under (and including) |base|: |
379 // * Exist. | 388 // * Exist. |
380 // * Are owned by a specific user. | 389 // * Are owned by a specific user. |
381 // * Are not writable by all users. | 390 // * Are not writable by all users. |
382 // * Are owned by a memeber of a given set of groups, or are not writable by | 391 // * Are owned by a memeber of a given set of groups, or are not writable by |
383 // their group. | 392 // their group. |
384 // * Are not symbolic links. | 393 // * Are not symbolic links. |
385 // This is useful for checking that a config file is administrator-controlled. | 394 // This is useful for checking that a config file is administrator-controlled. |
386 // |base| must contain |path|. | 395 // |base| must contain |path|. |
387 BASE_EXPORT bool VerifyPathControlledByUser(const FilePath& base, | 396 BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base, |
388 const FilePath& path, | 397 const base::FilePath& path, |
389 uid_t owner_uid, | 398 uid_t owner_uid, |
390 const std::set<gid_t>& group_gids); | 399 const std::set<gid_t>& group_gids); |
391 #endif // defined(OS_POSIX) | 400 #endif // defined(OS_POSIX) |
392 | 401 |
393 #if defined(OS_MACOSX) && !defined(OS_IOS) | 402 #if defined(OS_MACOSX) && !defined(OS_IOS) |
394 // Is |path| writable only by a user with administrator privileges? | 403 // Is |path| writable only by a user with administrator privileges? |
395 // This function uses Mac OS conventions. The super user is assumed to have | 404 // This function uses Mac OS conventions. The super user is assumed to have |
396 // uid 0, and the administrator group is assumed to be named "admin". | 405 // uid 0, and the administrator group is assumed to be named "admin". |
397 // Testing that |path|, and every parent directory including the root of | 406 // Testing that |path|, and every parent directory including the root of |
398 // the filesystem, are owned by the superuser, controlled by the group | 407 // the filesystem, are owned by the superuser, controlled by the group |
399 // "admin", are not writable by all users, and contain no symbolic links. | 408 // "admin", are not writable by all users, and contain no symbolic links. |
400 // Will return false if |path| does not exist. | 409 // Will return false if |path| does not exist. |
401 BASE_EXPORT bool VerifyPathControlledByAdmin(const FilePath& path); | 410 BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path); |
402 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 411 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
403 | 412 |
404 // A class to handle auto-closing of FILE*'s. | 413 // A class to handle auto-closing of FILE*'s. |
405 class ScopedFILEClose { | 414 class ScopedFILEClose { |
406 public: | 415 public: |
407 inline void operator()(FILE* x) const { | 416 inline void operator()(FILE* x) const { |
408 if (x) { | 417 if (x) { |
409 fclose(x); | 418 fclose(x); |
410 } | 419 } |
411 } | 420 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 // | 475 // |
467 // |pattern| is an optional pattern for which files to match. This | 476 // |pattern| is an optional pattern for which files to match. This |
468 // works like shell globbing. For example, "*.txt" or "Foo???.doc". | 477 // works like shell globbing. For example, "*.txt" or "Foo???.doc". |
469 // However, be careful in specifying patterns that aren't cross platform | 478 // However, be careful in specifying patterns that aren't cross platform |
470 // since the underlying code uses OS-specific matching routines. In general, | 479 // since the underlying code uses OS-specific matching routines. In general, |
471 // Windows matching is less featureful than others, so test there first. | 480 // Windows matching is less featureful than others, so test there first. |
472 // If unspecified, this will match all files. | 481 // If unspecified, this will match all files. |
473 // NOTE: the pattern only matches the contents of root_path, not files in | 482 // NOTE: the pattern only matches the contents of root_path, not files in |
474 // recursive subdirectories. | 483 // recursive subdirectories. |
475 // TODO(erikkay): Fix the pattern matching to work at all levels. | 484 // TODO(erikkay): Fix the pattern matching to work at all levels. |
476 FileEnumerator(const FilePath& root_path, | 485 FileEnumerator(const base::FilePath& root_path, |
477 bool recursive, | 486 bool recursive, |
478 int file_type); | 487 int file_type); |
479 FileEnumerator(const FilePath& root_path, | 488 FileEnumerator(const base::FilePath& root_path, |
480 bool recursive, | 489 bool recursive, |
481 int file_type, | 490 int file_type, |
482 const FilePath::StringType& pattern); | 491 const base::FilePath::StringType& pattern); |
483 ~FileEnumerator(); | 492 ~FileEnumerator(); |
484 | 493 |
485 // Returns an empty string if there are no more results. | 494 // Returns an empty string if there are no more results. |
486 FilePath Next(); | 495 base::FilePath Next(); |
487 | 496 |
488 // Write the file info into |info|. | 497 // Write the file info into |info|. |
489 void GetFindInfo(FindInfo* info); | 498 void GetFindInfo(FindInfo* info); |
490 | 499 |
491 // Looks inside a FindInfo and determines if it's a directory. | 500 // Looks inside a FindInfo and determines if it's a directory. |
492 static bool IsDirectory(const FindInfo& info); | 501 static bool IsDirectory(const FindInfo& info); |
493 | 502 |
494 static FilePath GetFilename(const FindInfo& find_info); | 503 static base::FilePath GetFilename(const FindInfo& find_info); |
495 static int64 GetFilesize(const FindInfo& find_info); | 504 static int64 GetFilesize(const FindInfo& find_info); |
496 static base::Time GetLastModifiedTime(const FindInfo& find_info); | 505 static base::Time GetLastModifiedTime(const FindInfo& find_info); |
497 | 506 |
498 private: | 507 private: |
499 // Returns true if the given path should be skipped in enumeration. | 508 // Returns true if the given path should be skipped in enumeration. |
500 bool ShouldSkip(const FilePath& path); | 509 bool ShouldSkip(const base::FilePath& path); |
501 | 510 |
502 | 511 |
503 #if defined(OS_WIN) | 512 #if defined(OS_WIN) |
504 // True when find_data_ is valid. | 513 // True when find_data_ is valid. |
505 bool has_find_data_; | 514 bool has_find_data_; |
506 WIN32_FIND_DATA find_data_; | 515 WIN32_FIND_DATA find_data_; |
507 HANDLE find_handle_; | 516 HANDLE find_handle_; |
508 #elif defined(OS_POSIX) | 517 #elif defined(OS_POSIX) |
509 struct DirectoryEntryInfo { | 518 struct DirectoryEntryInfo { |
510 FilePath filename; | 519 base::FilePath filename; |
511 struct stat stat; | 520 struct stat stat; |
512 }; | 521 }; |
513 | 522 |
514 // Read the filenames in source into the vector of DirectoryEntryInfo's | 523 // Read the filenames in source into the vector of DirectoryEntryInfo's |
515 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, | 524 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
516 const FilePath& source, bool show_links); | 525 const base::FilePath& source, bool show_links); |
517 | 526 |
518 // The files in the current directory | 527 // The files in the current directory |
519 std::vector<DirectoryEntryInfo> directory_entries_; | 528 std::vector<DirectoryEntryInfo> directory_entries_; |
520 | 529 |
521 // The next entry to use from the directory_entries_ vector | 530 // The next entry to use from the directory_entries_ vector |
522 size_t current_directory_entry_; | 531 size_t current_directory_entry_; |
523 #endif | 532 #endif |
524 | 533 |
525 FilePath root_path_; | 534 base::FilePath root_path_; |
526 bool recursive_; | 535 bool recursive_; |
527 int file_type_; | 536 int file_type_; |
528 FilePath::StringType pattern_; // Empty when we want to find everything. | 537 base::FilePath::StringType pattern_; // Empty when we want to find |
| 538 // everything. |
529 | 539 |
530 // A stack that keeps track of which subdirectories we still need to | 540 // A stack that keeps track of which subdirectories we still need to |
531 // enumerate in the breadth-first search. | 541 // enumerate in the breadth-first search. |
532 std::stack<FilePath> pending_paths_; | 542 std::stack<base::FilePath> pending_paths_; |
533 | 543 |
534 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | 544 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
535 }; | 545 }; |
536 | 546 |
537 class BASE_EXPORT MemoryMappedFile { | 547 class BASE_EXPORT MemoryMappedFile { |
538 public: | 548 public: |
539 // The default constructor sets all members to invalid/null values. | 549 // The default constructor sets all members to invalid/null values. |
540 MemoryMappedFile(); | 550 MemoryMappedFile(); |
541 ~MemoryMappedFile(); | 551 ~MemoryMappedFile(); |
542 | 552 |
543 // Opens an existing file and maps it into memory. Access is restricted to | 553 // Opens an existing file and maps it into memory. Access is restricted to |
544 // read only. If this object already points to a valid memory mapped file | 554 // read only. If this object already points to a valid memory mapped file |
545 // then this method will fail and return false. If it cannot open the file, | 555 // then this method will fail and return false. If it cannot open the file, |
546 // the file does not exist, or the memory mapping fails, it will return false. | 556 // the file does not exist, or the memory mapping fails, it will return false. |
547 // Later we may want to allow the user to specify access. | 557 // Later we may want to allow the user to specify access. |
548 bool Initialize(const FilePath& file_name); | 558 bool Initialize(const base::FilePath& file_name); |
549 // As above, but works with an already-opened file. MemoryMappedFile will take | 559 // As above, but works with an already-opened file. MemoryMappedFile will take |
550 // ownership of |file| and close it when done. | 560 // ownership of |file| and close it when done. |
551 bool Initialize(base::PlatformFile file); | 561 bool Initialize(base::PlatformFile file); |
552 | 562 |
553 #if defined(OS_WIN) | 563 #if defined(OS_WIN) |
554 // Opens an existing file and maps it as an image section. Please refer to | 564 // Opens an existing file and maps it as an image section. Please refer to |
555 // the Initialize function above for additional information. | 565 // the Initialize function above for additional information. |
556 bool InitializeAsImageSection(const FilePath& file_name); | 566 bool InitializeAsImageSection(const base::FilePath& file_name); |
557 #endif // OS_WIN | 567 #endif // OS_WIN |
558 | 568 |
559 const uint8* data() const { return data_; } | 569 const uint8* data() const { return data_; } |
560 size_t length() const { return length_; } | 570 size_t length() const { return length_; } |
561 | 571 |
562 // Is file_ a valid file handle that points to an open, memory mapped file? | 572 // Is file_ a valid file handle that points to an open, memory mapped file? |
563 bool IsValid() const; | 573 bool IsValid() const; |
564 | 574 |
565 private: | 575 private: |
566 // Open the given file and pass it to MapFileToMemoryInternal(). | 576 // Open the given file and pass it to MapFileToMemoryInternal(). |
567 bool MapFileToMemory(const FilePath& file_name); | 577 bool MapFileToMemory(const base::FilePath& file_name); |
568 | 578 |
569 // Map the file to memory, set data_ to that memory address. Return true on | 579 // Map the file to memory, set data_ to that memory address. Return true on |
570 // success, false on any kind of failure. This is a helper for Initialize(). | 580 // success, false on any kind of failure. This is a helper for Initialize(). |
571 bool MapFileToMemoryInternal(); | 581 bool MapFileToMemoryInternal(); |
572 | 582 |
573 // Closes all open handles. Later we may want to make this public. | 583 // Closes all open handles. Later we may want to make this public. |
574 void CloseHandles(); | 584 void CloseHandles(); |
575 | 585 |
576 #if defined(OS_WIN) | 586 #if defined(OS_WIN) |
577 // MapFileToMemoryInternal calls this function. It provides the ability to | 587 // MapFileToMemoryInternal calls this function. It provides the ability to |
(...skipping 24 matching lines...) Expand all Loading... |
602 FILE_SYSTEM_SMB, | 612 FILE_SYSTEM_SMB, |
603 FILE_SYSTEM_CODA, | 613 FILE_SYSTEM_CODA, |
604 FILE_SYSTEM_MEMORY, // in-memory file system | 614 FILE_SYSTEM_MEMORY, // in-memory file system |
605 FILE_SYSTEM_CGROUP, // cgroup control. | 615 FILE_SYSTEM_CGROUP, // cgroup control. |
606 FILE_SYSTEM_OTHER, // any other value. | 616 FILE_SYSTEM_OTHER, // any other value. |
607 FILE_SYSTEM_TYPE_COUNT | 617 FILE_SYSTEM_TYPE_COUNT |
608 }; | 618 }; |
609 | 619 |
610 // Attempts determine the FileSystemType for |path|. | 620 // Attempts determine the FileSystemType for |path|. |
611 // Returns false if |path| doesn't exist. | 621 // Returns false if |path| doesn't exist. |
612 BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type); | 622 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, |
| 623 FileSystemType* type); |
613 #endif | 624 #endif |
614 | 625 |
615 } // namespace file_util | 626 } // namespace file_util |
616 | 627 |
617 #endif // BASE_FILE_UTIL_H_ | 628 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |