OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
15 #elif defined(OS_POSIX) | 15 #elif defined(OS_POSIX) |
16 #include <fts.h> | 16 #include <fts.h> |
17 #endif | 17 #endif |
18 | 18 |
19 #include <stdio.h> | 19 #include <stdio.h> |
20 | 20 |
21 #include <stack> | 21 #include <stack> |
22 #include <string> | 22 #include <string> |
23 #include <vector> | 23 #include <vector> |
24 | 24 |
25 #include "base/basictypes.h" | 25 #include "base/basictypes.h" |
26 | 26 |
| 27 class FilePath; |
| 28 |
27 namespace file_util { | 29 namespace file_util { |
28 | 30 |
29 //----------------------------------------------------------------------------- | 31 //----------------------------------------------------------------------------- |
30 // Constants | 32 // Constants |
31 | 33 |
32 extern const wchar_t kPathSeparator; | 34 extern const wchar_t kPathSeparator; |
33 | 35 |
34 | 36 |
35 //----------------------------------------------------------------------------- | 37 //----------------------------------------------------------------------------- |
36 // Functions that operate purely on a path string w/o touching the filesystem: | 38 // Functions that operate purely on a path string w/o touching the filesystem: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // path == "C:\pics\jojo.jpg", returns "C:\pics" | 78 // path == "C:\pics\jojo.jpg", returns "C:\pics" |
77 // path == "C:\Windows\system32\", returns "C:\Windows\system32" | 79 // path == "C:\Windows\system32\", returns "C:\Windows\system32" |
78 // path == "C:\Windows\system32", returns "C:\Windows" | 80 // path == "C:\Windows\system32", returns "C:\Windows" |
79 std::wstring GetDirectoryFromPath(const std::wstring& path); | 81 std::wstring GetDirectoryFromPath(const std::wstring& path); |
80 | 82 |
81 // Appends new_ending to path, adding a separator between the two if necessary. | 83 // Appends new_ending to path, adding a separator between the two if necessary. |
82 void AppendToPath(std::wstring* path, const std::wstring& new_ending); | 84 void AppendToPath(std::wstring* path, const std::wstring& new_ending); |
83 | 85 |
84 // Convert provided relative path into an absolute path. Returns false on | 86 // Convert provided relative path into an absolute path. Returns false on |
85 // error. | 87 // error. |
| 88 bool AbsolutePath(FilePath* path); |
| 89 // Deprecated temporary compatibility function. |
86 bool AbsolutePath(std::wstring* path); | 90 bool AbsolutePath(std::wstring* path); |
87 | 91 |
88 // Inserts |suffix| after the file name portion of |path| but before the | 92 // Inserts |suffix| after the file name portion of |path| but before the |
89 // extension. | 93 // extension. |
90 // Examples: | 94 // Examples: |
91 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" | 95 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" |
92 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" | 96 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" |
93 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" | 97 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" |
94 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" | 98 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" |
95 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix); | 99 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix); |
(...skipping 24 matching lines...) Expand all Loading... |
120 #endif // defined(OS_WIN) | 124 #endif // defined(OS_WIN) |
121 | 125 |
122 // Deletes the given path, whether it's a file or a directory. | 126 // Deletes the given path, whether it's a file or a directory. |
123 // If it's a directory, it's perfectly happy to delete all of the | 127 // If it's a directory, it's perfectly happy to delete all of the |
124 // directory's contents. Passing true to recursive deletes | 128 // directory's contents. Passing true to recursive deletes |
125 // subdirectories and their contents as well. | 129 // subdirectories and their contents as well. |
126 // Returns true if successful, false otherwise. | 130 // Returns true if successful, false otherwise. |
127 // | 131 // |
128 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 132 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
129 // TO "rm -rf", SO USE WITH CAUTION. | 133 // TO "rm -rf", SO USE WITH CAUTION. |
| 134 bool Delete(const FilePath& path, bool recursive); |
| 135 // Deprecated temporary compatibility function. |
130 bool Delete(const std::wstring& path, bool recursive); | 136 bool Delete(const std::wstring& path, bool recursive); |
131 | 137 |
132 // Moves the given path, whether it's a file or a directory. | 138 // Moves the given path, whether it's a file or a directory. |
133 // Returns true if successful, false otherwise. | 139 // Returns true if successful, false otherwise. |
| 140 bool Move(const FilePath& from_path, const FilePath& to_path); |
| 141 // Deprecated temporary compatibility function. |
134 bool Move(const std::wstring& from_path, const std::wstring& to_path); | 142 bool Move(const std::wstring& from_path, const std::wstring& to_path); |
135 | 143 |
136 // Copies a single file. Use CopyDirectory to copy directories. | 144 // Copies a single file. Use CopyDirectory to copy directories. |
| 145 bool CopyFile(const FilePath& from_path, const FilePath& to_path); |
| 146 // Deprecated temporary compatibility function. |
137 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path); | 147 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path); |
138 | 148 |
139 // Copies the given path, and optionally all subdirectories and their contents | 149 // Copies the given path, and optionally all subdirectories and their contents |
140 // as well. | 150 // as well. |
141 // If there are files existing under to_path, always overwrite. | 151 // If there are files existing under to_path, always overwrite. |
142 // Returns true if successful, false otherwise. | 152 // Returns true if successful, false otherwise. |
143 // Dont't use wildcards on the names, it may stop working without notice. | 153 // Dont't use wildcards on the names, it may stop working without notice. |
144 // | 154 // |
145 // If you only need to copy a file use CopyFile, it's faster. | 155 // If you only need to copy a file use CopyFile, it's faster. |
| 156 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| 157 bool recursive); |
| 158 // Deprecated temporary compatibility function. |
146 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, | 159 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
147 bool recursive); | 160 bool recursive); |
148 | 161 |
149 // Returns true if the given path exists on the local filesystem, | 162 // Returns true if the given path exists on the local filesystem, |
150 // false otherwise. | 163 // false otherwise. |
| 164 bool PathExists(const FilePath& path); |
| 165 // Deprecated temporary compatibility function. |
151 bool PathExists(const std::wstring& path); | 166 bool PathExists(const std::wstring& path); |
152 | 167 |
153 // Returns true if the given path is writable by the user, false otherwise. | 168 // Returns true if the given path is writable by the user, false otherwise. |
154 bool PathIsWritable(const std::wstring& path); | 169 bool PathIsWritable(const std::wstring& path); |
155 | 170 |
156 // Returns true if the given path exists and is a directory, false otherwise. | 171 // Returns true if the given path exists and is a directory, false otherwise. |
| 172 bool DirectoryExists(const FilePath& path); |
| 173 // Deprecated temporary compatibility function. |
157 bool DirectoryExists(const std::wstring& path); | 174 bool DirectoryExists(const std::wstring& path); |
158 | 175 |
159 #if defined(OS_WIN) | 176 #if defined(OS_WIN) |
160 // Gets the creation time of the given file (expressed in the local timezone), | 177 // Gets the creation time of the given file (expressed in the local timezone), |
161 // and returns it via the creation_time parameter. Returns true if successful, | 178 // and returns it via the creation_time parameter. Returns true if successful, |
162 // false otherwise. | 179 // false otherwise. |
163 bool GetFileCreationLocalTime(const std::wstring& filename, | 180 bool GetFileCreationLocalTime(const std::wstring& filename, |
164 LPSYSTEMTIME creation_time); | 181 LPSYSTEMTIME creation_time); |
165 | 182 |
166 // Same as above, but takes a previously-opened file handle instead of a name. | 183 // Same as above, but takes a previously-opened file handle instead of a name. |
167 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, | 184 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, |
168 LPSYSTEMTIME creation_time); | 185 LPSYSTEMTIME creation_time); |
169 #endif // defined(OS_WIN) | 186 #endif // defined(OS_WIN) |
170 | 187 |
171 // Returns true if the contents of the two files given are equal, false | 188 // Returns true if the contents of the two files given are equal, false |
172 // otherwise. If either file can't be read, returns false. | 189 // otherwise. If either file can't be read, returns false. |
| 190 bool ContentsEqual(const FilePath& filename1, |
| 191 const FilePath& filename2); |
| 192 // Deprecated temporary compatibility function. |
173 bool ContentsEqual(const std::wstring& filename1, | 193 bool ContentsEqual(const std::wstring& filename1, |
174 const std::wstring& filename2); | 194 const std::wstring& filename2); |
175 | 195 |
176 // Read the file at |path| into |contents|, returning true on success. | 196 // Read the file at |path| into |contents|, returning true on success. |
177 // Useful for unit tests. | 197 // Useful for unit tests. |
178 bool ReadFileToString(const std::wstring& path, std::string* contents); | 198 bool ReadFileToString(const std::wstring& path, std::string* contents); |
179 | 199 |
180 #if defined(OS_WIN) | 200 #if defined(OS_WIN) |
181 // Resolve Windows shortcut (.LNK file) | 201 // Resolve Windows shortcut (.LNK file) |
182 // Argument path specifies a valid LNK file. On success, return true and put | 202 // Argument path specifies a valid LNK file. On success, return true and put |
(...skipping 26 matching lines...) Expand all Loading... |
209 const wchar_t *description, const wchar_t *icon, | 229 const wchar_t *description, const wchar_t *icon, |
210 int icon_index); | 230 int icon_index); |
211 | 231 |
212 // Return true if the given directory is empty | 232 // Return true if the given directory is empty |
213 bool IsDirectoryEmpty(const std::wstring& dir_path); | 233 bool IsDirectoryEmpty(const std::wstring& dir_path); |
214 | 234 |
215 #endif | 235 #endif |
216 | 236 |
217 | 237 |
218 // Get the temporary directory provided by the system. | 238 // Get the temporary directory provided by the system. |
| 239 bool GetTempDir(FilePath* path); |
| 240 // Deprecated temporary compatibility function. |
219 bool GetTempDir(std::wstring* path); | 241 bool GetTempDir(std::wstring* path); |
220 | 242 |
221 // Creates a temporary file. The full path is placed in 'temp_file', and the | 243 // Creates a temporary file. The full path is placed in 'temp_file', and the |
222 // function returns true if was successful in creating the file. The file will | 244 // function returns true if was successful in creating the file. The file will |
223 // be empty and all handles closed after this function returns. | 245 // be empty and all handles closed after this function returns. |
224 // TODO(erikkay): rename this function and track down all of the callers. | 246 // TODO(erikkay): rename this function and track down all of the callers. |
225 bool CreateTemporaryFileName(std::wstring* temp_file); | 247 bool CreateTemporaryFileName(std::wstring* temp_file); |
226 | 248 |
227 // Same as CreateTemporaryFileName but the file is created in |dir|. | 249 // Same as CreateTemporaryFileName but the file is created in |dir|. |
228 bool CreateTemporaryFileNameInDir(const std::wstring& dir, | 250 bool CreateTemporaryFileNameInDir(const std::wstring& dir, |
229 std::wstring* temp_file); | 251 std::wstring* temp_file); |
230 | 252 |
231 // Create a new directory under TempPath. If prefix is provided, the new | 253 // Create a new directory under TempPath. If prefix is provided, the new |
232 // directory name is in the format of prefixyyyy. | 254 // directory name is in the format of prefixyyyy. |
233 // If success, return true and output the full path of the directory created. | 255 // If success, return true and output the full path of the directory created. |
234 bool CreateNewTempDirectory(const std::wstring& prefix, | 256 bool CreateNewTempDirectory(const std::wstring& prefix, |
235 std::wstring* new_temp_path); | 257 std::wstring* new_temp_path); |
236 | 258 |
237 // Creates a directory, as well as creating any parent directories, if they | 259 // Creates a directory, as well as creating any parent directories, if they |
238 // don't exist. Returns 'true' on successful creation, or if the directory | 260 // don't exist. Returns 'true' on successful creation, or if the directory |
239 // already exists. | 261 // already exists. |
| 262 bool CreateDirectory(const FilePath& full_path); |
| 263 // Deprecated temporary compatibility function. |
240 bool CreateDirectory(const std::wstring& full_path); | 264 bool CreateDirectory(const std::wstring& full_path); |
241 | 265 |
242 // Returns the file size. Returns true on success. | 266 // Returns the file size. Returns true on success. |
243 bool GetFileSize(const std::wstring& file_path, int64* file_size); | 267 bool GetFileSize(const std::wstring& file_path, int64* file_size); |
244 | 268 |
245 // Used to hold information about a given file path. See GetFileInfo below. | 269 // Used to hold information about a given file path. See GetFileInfo below. |
246 struct FileInfo { | 270 struct FileInfo { |
247 // The size of the file in bytes. Undefined when is_directory is true. | 271 // The size of the file in bytes. Undefined when is_directory is true. |
248 int64 size; | 272 int64 size; |
249 | 273 |
250 // True if the file corresponds to a directory. | 274 // True if the file corresponds to a directory. |
251 bool is_directory; | 275 bool is_directory; |
252 | 276 |
253 // Add additional fields here as needed. | 277 // Add additional fields here as needed. |
254 }; | 278 }; |
255 | 279 |
256 // Returns information about the given file path. | 280 // Returns information about the given file path. |
257 bool GetFileInfo(const std::wstring& file_path, FileInfo* info); | 281 bool GetFileInfo(const std::wstring& file_path, FileInfo* info); |
258 | 282 |
259 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. | 283 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
| 284 FILE* OpenFile(const FilePath& filename, const char* mode); |
| 285 // Deprecated temporary compatibility functions. |
260 FILE* OpenFile(const std::string& filename, const char* mode); | 286 FILE* OpenFile(const std::string& filename, const char* mode); |
261 FILE* OpenFile(const std::wstring& filename, const char* mode); | 287 FILE* OpenFile(const std::wstring& filename, const char* mode); |
262 | 288 |
263 // Closes file opened by OpenFile. Returns true on success. | 289 // Closes file opened by OpenFile. Returns true on success. |
264 bool CloseFile(FILE* file); | 290 bool CloseFile(FILE* file); |
265 | 291 |
266 // Reads the given number of bytes from the file into the buffer. Returns | 292 // Reads the given number of bytes from the file into the buffer. Returns |
267 // the number of read bytes, or -1 on error. | 293 // the number of read bytes, or -1 on error. |
268 int ReadFile(const std::wstring& filename, char* data, int size); | 294 int ReadFile(const std::wstring& filename, char* data, int size); |
269 | 295 |
270 // Writes the given buffer into the file, overwriting any data that was | 296 // Writes the given buffer into the file, overwriting any data that was |
271 // previously there. Returns the number of bytes written, or -1 on error. | 297 // previously there. Returns the number of bytes written, or -1 on error. |
272 int WriteFile(const std::wstring& filename, const char* data, int size); | 298 int WriteFile(const std::wstring& filename, const char* data, int size); |
273 | 299 |
274 // Gets the current working directory for the process. | 300 // Gets the current working directory for the process. |
| 301 bool GetCurrentDirectory(FilePath* path); |
| 302 // Deprecated temporary compatibility function. |
275 bool GetCurrentDirectory(std::wstring* path); | 303 bool GetCurrentDirectory(std::wstring* path); |
276 | 304 |
277 // Sets the current working directory for the process. | 305 // Sets the current working directory for the process. |
278 bool SetCurrentDirectory(const std::wstring& current_directory); | 306 bool SetCurrentDirectory(const std::wstring& current_directory); |
279 | 307 |
280 // A class for enumerating the files in a provided path. The order of the | 308 // A class for enumerating the files in a provided path. The order of the |
281 // results is not guaranteed. | 309 // results is not guaranteed. |
282 // | 310 // |
283 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | 311 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
284 // program where latency does not matter. This class is blocking. | 312 // program where latency does not matter. This class is blocking. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 376 |
349 // Renames a file using the SHFileOperation API to ensure that the target file | 377 // Renames a file using the SHFileOperation API to ensure that the target file |
350 // gets the correct default security descriptor in the new path. | 378 // gets the correct default security descriptor in the new path. |
351 bool RenameFileAndResetSecurityDescriptor( | 379 bool RenameFileAndResetSecurityDescriptor( |
352 const std::wstring& source_file_path, | 380 const std::wstring& source_file_path, |
353 const std::wstring& target_file_path); | 381 const std::wstring& target_file_path); |
354 | 382 |
355 } // namespace file_util | 383 } // namespace file_util |
356 | 384 |
357 #endif // BASE_FILE_UTIL_H_ | 385 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |