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_FILES_FILE_UTIL_H_ | 8 #ifndef BASE_FILES_FILE_UTIL_H_ |
9 #define BASE_FILES_FILE_UTIL_H_ | 9 #define BASE_FILES_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_POSIX) |
14 #include <windows.h> | |
15 #elif defined(OS_POSIX) | |
16 #include <sys/stat.h> | 14 #include <sys/stat.h> |
17 #include <unistd.h> | 15 #include <unistd.h> |
18 #endif | 16 #endif |
19 | 17 |
20 #include <stdio.h> | 18 #include <stdio.h> |
21 | 19 |
22 #include <set> | 20 #include <set> |
23 #include <string> | 21 #include <string> |
24 #include <vector> | 22 #include <vector> |
25 | 23 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // Returns true if successful, false otherwise. It is considered successful | 60 // Returns true if successful, false otherwise. It is considered successful |
63 // to attempt to delete a file that does not exist. | 61 // to attempt to delete a file that does not exist. |
64 // | 62 // |
65 // In posix environment and if |path| is a symbolic link, this deletes only | 63 // In posix environment and if |path| is a symbolic link, this deletes only |
66 // the symlink. (even if the symlink points to a non-existent file) | 64 // the symlink. (even if the symlink points to a non-existent file) |
67 // | 65 // |
68 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 66 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
69 // TO "rm -rf", SO USE WITH CAUTION. | 67 // TO "rm -rf", SO USE WITH CAUTION. |
70 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); | 68 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); |
71 | 69 |
72 #if defined(OS_WIN) | |
73 // Schedules to delete the given path, whether it's a file or a directory, until | |
74 // the operating system is restarted. | |
75 // Note: | |
76 // 1) The file/directory to be deleted should exist in a temp folder. | |
77 // 2) The directory to be deleted must be empty. | |
78 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); | |
79 #endif | |
80 | |
81 // Moves the given path, whether it's a file or a directory. | 70 // Moves the given path, whether it's a file or a directory. |
82 // If a simple rename is not possible, such as in the case where the paths are | 71 // If a simple rename is not possible, such as in the case where the paths are |
83 // on different volumes, this will attempt to copy and delete. Returns | 72 // on different volumes, this will attempt to copy and delete. Returns |
84 // true for success. | 73 // true for success. |
85 // This function fails if either path contains traversal components ('..'). | 74 // This function fails if either path contains traversal components ('..'). |
86 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); | 75 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); |
87 | 76 |
88 // Renames file |from_path| to |to_path|. Both paths must be on the same | 77 // Renames file |from_path| to |to_path|. Both paths must be on the same |
89 // volume, or the function will fail. Destination file will be created | 78 // volume, or the function will fail. Destination file will be created |
90 // if it doesn't exist. Prefer this function over Move when dealing with | 79 // if it doesn't exist. Prefer this function over Move when dealing with |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size); | 257 BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size); |
269 | 258 |
270 // Sets |real_path| to |path| with symbolic links and junctions expanded. | 259 // Sets |real_path| to |path| with symbolic links and junctions expanded. |
271 // On windows, make sure the path starts with a lettered drive. | 260 // On windows, make sure the path starts with a lettered drive. |
272 // |path| must reference a file. Function will fail if |path| points to | 261 // |path| must reference a file. Function will fail if |path| points to |
273 // a directory or to a nonexistent path. On windows, this function will | 262 // a directory or to a nonexistent path. On windows, this function will |
274 // fail if |path| is a junction or symlink that points to an empty file, | 263 // fail if |path| is a junction or symlink that points to an empty file, |
275 // or if |real_path| would be longer than MAX_PATH characters. | 264 // or if |real_path| would be longer than MAX_PATH characters. |
276 BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path); | 265 BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path); |
277 | 266 |
278 #if defined(OS_WIN) | |
279 | |
280 // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."), | |
281 // return in |drive_letter_path| the equivalent path that starts with | |
282 // a drive letter ("C:\..."). Return false if no such path exists. | |
283 BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path, | |
284 FilePath* drive_letter_path); | |
285 | |
286 // Given an existing file in |path|, set |real_path| to the path | |
287 // in native NT format, of the form "\Device\HarddiskVolumeXX\..". | |
288 // Returns false if the path can not be found. Empty files cannot | |
289 // be resolved with this function. | |
290 BASE_EXPORT bool NormalizeToNativeFilePath(const FilePath& path, | |
291 FilePath* nt_path); | |
292 #endif | |
293 | |
294 // This function will return if the given file is a symlink or not. | 267 // This function will return if the given file is a symlink or not. |
295 BASE_EXPORT bool IsLink(const FilePath& file_path); | 268 BASE_EXPORT bool IsLink(const FilePath& file_path); |
296 | 269 |
297 // Returns information about the given file path. | 270 // Returns information about the given file path. |
298 BASE_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info); | 271 BASE_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info); |
299 | 272 |
300 // Sets the time of the last access and the time of the last modification. | 273 // Sets the time of the last access and the time of the last modification. |
301 BASE_EXPORT bool TouchFile(const FilePath& path, | 274 BASE_EXPORT bool TouchFile(const FilePath& path, |
302 const Time& last_accessed, | 275 const Time& last_accessed, |
303 const Time& last_modified); | 276 const Time& last_modified); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 387 |
415 // Internal -------------------------------------------------------------------- | 388 // Internal -------------------------------------------------------------------- |
416 | 389 |
417 namespace internal { | 390 namespace internal { |
418 | 391 |
419 // Same as Move but allows paths with traversal components. | 392 // Same as Move but allows paths with traversal components. |
420 // Use only with extreme care. | 393 // Use only with extreme care. |
421 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, | 394 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, |
422 const FilePath& to_path); | 395 const FilePath& to_path); |
423 | 396 |
424 #if defined(OS_WIN) | |
425 // Copy from_path to to_path recursively and then delete from_path recursively. | |
426 // Returns true if all operations succeed. | |
427 // This function simulates Move(), but unlike Move() it works across volumes. | |
428 // This function is not transactional. | |
429 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | |
430 const FilePath& to_path); | |
431 #endif // defined(OS_WIN) | |
432 | |
433 } // namespace internal | 397 } // namespace internal |
434 } // namespace base | 398 } // namespace base |
435 | 399 |
436 #endif // BASE_FILES_FILE_UTIL_H_ | 400 #endif // BASE_FILES_FILE_UTIL_H_ |
OLD | NEW |