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 #include <sys/stat.h> | 17 #include <sys/stat.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #include <stdio.h> | 20 #include <stdio.h> |
21 | 21 |
22 #include <stack> | 22 #include <stack> |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
27 | 27 #include "base/file_path.h" |
28 class FilePath; | |
29 | 28 |
30 namespace file_util { | 29 namespace file_util { |
31 | 30 |
32 //----------------------------------------------------------------------------- | 31 //----------------------------------------------------------------------------- |
33 // Constants | 32 // Constants |
34 | 33 |
| 34 #if defined(OS_WIN) |
| 35 // The use of this constant is deprecated. Instead use file_util or FilePath |
| 36 // functions (Append, TrimTrailingSeparator, etc.), or use |
| 37 // FilePath::kSeparator[0]. |
35 extern const wchar_t kPathSeparator; | 38 extern const wchar_t kPathSeparator; |
36 | 39 #endif |
37 | 40 |
38 //----------------------------------------------------------------------------- | 41 //----------------------------------------------------------------------------- |
39 // Functions that operate purely on a path string w/o touching the filesystem: | 42 // Functions that operate purely on a path string w/o touching the filesystem: |
40 | 43 |
41 // Returns a vector of all of the components of the provided path. | 44 // Returns a vector of all of the components of the provided path. |
| 45 void PathComponents(const FilePath& path, |
| 46 std::vector<FilePath::StringType>* components); |
| 47 #if defined(OS_WIN) |
| 48 // Deprecated temporary compatibility function. |
42 void PathComponents(const std::wstring& path, | 49 void PathComponents(const std::wstring& path, |
43 std::vector<std::wstring>* components); | 50 std::vector<std::wstring>* components); |
| 51 #endif |
44 | 52 |
45 // Returns true if the given path ends with a path separator character. | 53 // Returns true if the given path ends with a path separator character. |
46 bool EndsWithSeparator(const FilePath& path); | 54 bool EndsWithSeparator(const FilePath& path); |
47 // These two versions are both deprecated. TODO(estade): remove them. | 55 // These two versions are both deprecated. TODO(estade): remove them. |
48 bool EndsWithSeparator(std::wstring* path); | 56 bool EndsWithSeparator(std::wstring* path); |
49 bool EndsWithSeparator(const std::wstring& path); | 57 bool EndsWithSeparator(const std::wstring& path); |
50 | 58 |
51 // Makes sure that |path| ends with a separator IFF path is a directory that | 59 // Makes sure that |path| ends with a separator IFF path is a directory that |
52 // exists. Returns true if |path| is an existing directory, false otherwise. | 60 // exists. Returns true if |path| is an existing directory, false otherwise. |
53 bool EnsureEndsWithSeparator(FilePath* path); | 61 bool EnsureEndsWithSeparator(FilePath* path); |
54 | 62 |
55 // Modifies a string by trimming all trailing separators from the end. | 63 // Modifies a string by trimming all trailing separators from the end. |
56 void TrimTrailingSeparator(std::wstring* dir); | 64 void TrimTrailingSeparator(std::wstring* dir); |
57 | 65 |
58 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not | 66 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not |
59 // refer to a file. | 67 // refer to a file. |
60 // If 'dir' is a root directory, return without change. | 68 // If 'dir' is a root directory, return without change. |
| 69 // Deprecated. Use FilePath::DirName instead. |
61 void UpOneDirectory(std::wstring* dir); | 70 void UpOneDirectory(std::wstring* dir); |
62 | 71 |
63 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not | 72 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not |
64 // refer to a file. | 73 // refer to a file. |
65 // If 'dir' is a root directory, the result becomes empty string. | 74 // If 'dir' is a root directory, the result becomes empty string. |
| 75 // Deprecated. Use FilePath::DirName instead. |
66 void UpOneDirectoryOrEmpty(std::wstring* dir); | 76 void UpOneDirectoryOrEmpty(std::wstring* dir); |
67 | 77 |
68 // Strips the filename component from the end of 'path'. | 78 // Strips the filename component from the end of 'path'. If path ends with a |
| 79 // separator, then just drop the separator. |
| 80 // Deprecated. Use FilePath::DirName instead. |
69 void TrimFilename(std::wstring* path); | 81 void TrimFilename(std::wstring* path); |
70 | 82 |
71 // Returns the filename portion of 'path', without any leading \'s or /'s. | 83 // Returns the filename portion of 'path', without any leading \'s or /'s. |
72 std::wstring GetFilenameFromPath(const std::wstring& path); | 84 std::wstring GetFilenameFromPath(const std::wstring& path); |
73 | 85 |
74 // Returns "jpg" for path "C:\pics\jojo.jpg", or an empty string if | 86 // Returns "jpg" for path "C:\pics\jojo.jpg", or an empty string if |
75 // the file has no extension. | 87 // the file has no extension. |
76 std::wstring GetFileExtensionFromPath(const std::wstring& path); | 88 std::wstring GetFileExtensionFromPath(const std::wstring& path); |
77 | 89 |
78 // Returns 'jojo' for path "C:\pics\jojo.jpg". | 90 // Returns 'jojo' for path "C:\pics\jojo.jpg". |
(...skipping 12 matching lines...) Expand all Loading... |
91 | 103 |
92 // Appends new_ending to path, adding a separator between the two if necessary. | 104 // Appends new_ending to path, adding a separator between the two if necessary. |
93 void AppendToPath(std::wstring* path, const std::wstring& new_ending); | 105 void AppendToPath(std::wstring* path, const std::wstring& new_ending); |
94 | 106 |
95 // Convert provided relative path into an absolute path. Returns false on | 107 // Convert provided relative path into an absolute path. Returns false on |
96 // error. | 108 // error. |
97 bool AbsolutePath(FilePath* path); | 109 bool AbsolutePath(FilePath* path); |
98 // Deprecated temporary compatibility function. | 110 // Deprecated temporary compatibility function. |
99 bool AbsolutePath(std::wstring* path); | 111 bool AbsolutePath(std::wstring* path); |
100 | 112 |
| 113 // TODO(port): create FilePath versions of these functions, and remove this |
| 114 // platform define. |
| 115 #if defined(OS_WIN) |
101 // Inserts |suffix| after the file name portion of |path| but before the | 116 // Inserts |suffix| after the file name portion of |path| but before the |
102 // extension. | 117 // extension. |
103 // Examples: | 118 // Examples: |
104 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" | 119 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" |
105 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" | 120 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" |
106 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" | 121 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" |
107 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" | 122 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" |
108 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix); | 123 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix); |
109 | 124 |
| 125 // Replaces the extension of |file_name| with |extension|. If |file_name| |
| 126 // does not have an extension, them |extension| is added. If |extension| is |
| 127 // empty, then the extension is removed from |file_name|. |
| 128 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension); |
| 129 #endif |
| 130 |
110 // Replaces characters in 'file_name' that are illegal for file names with | 131 // Replaces characters in 'file_name' that are illegal for file names with |
111 // 'replace_char'. 'file_name' must not be a full or relative path, but just the | 132 // 'replace_char'. 'file_name' must not be a full or relative path, but just the |
112 // file name component. Any leading or trailing whitespace in 'file_name' is | 133 // file name component. Any leading or trailing whitespace in 'file_name' is |
113 // removed. | 134 // removed. |
114 // Example: | 135 // Example: |
115 // file_name == "bad:file*name?.txt", changed to: "bad-file-name-.txt" when | 136 // file_name == "bad:file*name?.txt", changed to: "bad-file-name-.txt" when |
116 // 'replace_char' is '-'. | 137 // 'replace_char' is '-'. |
117 void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char); | 138 void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char); |
118 | 139 |
119 // Replaces the extension of |file_name| with |extension|. If |file_name| | |
120 // does not have an extension, them |extension| is added. If |extention| is | |
121 // empty, then the extension is removed from |file_name|. | |
122 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension); | |
123 | |
124 //----------------------------------------------------------------------------- | 140 //----------------------------------------------------------------------------- |
125 // Functions that involve filesystem access or modification: | 141 // Functions that involve filesystem access or modification: |
126 | 142 |
127 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
128 // Returns the number of files matching the current path that were | 144 // Returns the number of files matching the current path that were |
129 // created on or after the given FILETIME. Doesn't count ".." or ".". | 145 // created on or after the given FILETIME. Doesn't count ".." or ".". |
130 // Filetime is UTC filetime, not LocalFiletime. | 146 // Filetime is UTC filetime, not LocalFiletime. |
131 int CountFilesCreatedAfter(const std::wstring& path, | 147 int CountFilesCreatedAfter(const std::wstring& path, |
132 const FILETIME& file_time); | 148 const FILETIME& file_time); |
133 #endif // defined(OS_WIN) | 149 #endif // defined(OS_WIN) |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 422 |
407 // Renames a file using the SHFileOperation API to ensure that the target file | 423 // Renames a file using the SHFileOperation API to ensure that the target file |
408 // gets the correct default security descriptor in the new path. | 424 // gets the correct default security descriptor in the new path. |
409 bool RenameFileAndResetSecurityDescriptor( | 425 bool RenameFileAndResetSecurityDescriptor( |
410 const std::wstring& source_file_path, | 426 const std::wstring& source_file_path, |
411 const std::wstring& target_file_path); | 427 const std::wstring& target_file_path); |
412 | 428 |
413 } // namespace file_util | 429 } // namespace file_util |
414 | 430 |
415 #endif // BASE_FILE_UTIL_H_ | 431 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |