Skip to content
Newer
Older
100644 386 lines (312 sloc) 15.6 KB
1457f56 Use a more compact license header in source files.
license.bot authored
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
3 // found in the LICENSE file.
d7cae12 Add base to the repository.
initial.commit authored
4
5 // This file contains utility functions for dealing with the local
6 // filesystem.
7
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
8 #ifndef BASE_FILE_UTIL_H_
9 #define BASE_FILE_UTIL_H_
d7cae12 Add base to the repository.
initial.commit authored
10
bf14fc5 Refactoring file_util into file_util and file_util_win. Also fix wind…
erikkay@google.com authored
11 #include "build/build_config.h"
12
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
13 #if defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
14 #include <windows.h>
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
15 #elif defined(OS_POSIX)
16 #include <fts.h>
bf14fc5 Refactoring file_util into file_util and file_util_win. Also fix wind…
erikkay@google.com authored
17 #endif
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
18
ee3eba3 Cross-platform wrappers for fopen, _wfopen_s, etc.
mark@chromium.org authored
19 #include <stdio.h>
20
d7cae12 Add base to the repository.
initial.commit authored
21 #include <stack>
22 #include <string>
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
23 #include <vector>
d7cae12 Add base to the repository.
initial.commit authored
24
25 #include "base/basictypes.h"
26
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
27 class FilePath;
28
d7cae12 Add base to the repository.
initial.commit authored
29 namespace file_util {
30
31 //-----------------------------------------------------------------------------
32 // Constants
33
34 extern const wchar_t kPathSeparator;
35
36
37 //-----------------------------------------------------------------------------
38 // Functions that operate purely on a path string w/o touching the filesystem:
39
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
40 // Returns a vector of all of the components of the provided path.
41 void PathComponents(const std::wstring& path,
42 std::vector<std::wstring>* components);
43
d7cae12 Add base to the repository.
initial.commit authored
44 // Returns true if the given path ends with a path separator character.
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
45 // TODO(erikkay): remove this pointer version
d7cae12 Add base to the repository.
initial.commit authored
46 bool EndsWithSeparator(std::wstring* path);
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
47 bool EndsWithSeparator(const std::wstring& path);
48
d7cae12 Add base to the repository.
initial.commit authored
49 // Modifies a string by trimming all trailing separators from the end.
50 void TrimTrailingSeparator(std::wstring* dir);
51
52 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
53 // refer to a file.
54 // If 'dir' is a root directory, return without change.
55 void UpOneDirectory(std::wstring* dir);
56
57 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
58 // refer to a file.
59 // If 'dir' is a root directory, the result becomes empty string.
60 void UpOneDirectoryOrEmpty(std::wstring* dir);
61
62 // Strips the filename component from the end of 'path'.
63 void TrimFilename(std::wstring* path);
64
65 // Returns the filename portion of 'path', without any leading \'s or /'s.
66 std::wstring GetFilenameFromPath(const std::wstring& path);
67
68 // Returns "jpg" for path "C:\pics\jojo.jpg", or an empty string if
69 // the file has no extension.
70 std::wstring GetFileExtensionFromPath(const std::wstring& path);
71
72 // Returns the directory component of a path, without the trailing
73 // path separator, or an empty string on error. The function does not
74 // check for the existence of the path, so if it is passed a directory
75 // without the trailing \, it will interpret the last component of the
76 // path as a file and chomp it. This does not support relative paths.
77 // Examples:
78 // path == "C:\pics\jojo.jpg", returns "C:\pics"
79 // path == "C:\Windows\system32\", returns "C:\Windows\system32"
80 // path == "C:\Windows\system32", returns "C:\Windows"
81 std::wstring GetDirectoryFromPath(const std::wstring& path);
82
83 // Appends new_ending to path, adding a separator between the two if necessary.
84 void AppendToPath(std::wstring* path, const std::wstring& new_ending);
85
99c03e5 I accidentally committed file_version_info. Since comments were relat…
erikkay@google.com authored
86 // Convert provided relative path into an absolute path. Returns false on
87 // error.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
88 bool AbsolutePath(FilePath* path);
89 // Deprecated temporary compatibility function.
99c03e5 I accidentally committed file_version_info. Since comments were relat…
erikkay@google.com authored
90 bool AbsolutePath(std::wstring* path);
91
d7cae12 Add base to the repository.
initial.commit authored
92 // Inserts |suffix| after the file name portion of |path| but before the
93 // extension.
94 // Examples:
95 // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
96 // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg"
97 // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)"
98 // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
99 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix);
100
101 // Replaces characters in 'file_name' that are illegal for file names with
102 // 'replace_char'. 'file_name' must not be a full or relative path, but just the
103 // file name component. Any leading or trailing whitespace in 'file_name' is
104 // removed.
105 // Example:
106 // file_name == "bad:file*name?.txt", changed to: "bad-file-name-.txt" when
107 // 'replace_char' is '-'.
108 void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char);
109
110 // Replaces the extension of |file_name| with |extension|. If |file_name|
111 // does not have an extension, them |extension| is added. If |extention| is
112 // empty, then the extension is removed from |file_name|.
113 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
114
115 //-----------------------------------------------------------------------------
116 // Functions that involve filesystem access or modification:
117
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
118 #if defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
119 // Returns the number of files matching the current path that were
120 // created on or after the given FILETIME. Doesn't count ".." or ".".
121 // Filetime is UTC filetime, not LocalFiletime.
122 int CountFilesCreatedAfter(const std::wstring& path,
123 const FILETIME& file_time);
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
124 #endif // defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
125
126 // Deletes the given path, whether it's a file or a directory.
127 // If it's a directory, it's perfectly happy to delete all of the
128 // directory's contents. Passing true to recursive deletes
129 // subdirectories and their contents as well.
130 // Returns true if successful, false otherwise.
131 //
132 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT
133 // TO "rm -rf", SO USE WITH CAUTION.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
134 bool Delete(const FilePath& path, bool recursive);
135 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
136 bool Delete(const std::wstring& path, bool recursive);
137
138 // Moves the given path, whether it's a file or a directory.
139 // Returns true if successful, false otherwise.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
140 bool Move(const FilePath& from_path, const FilePath& to_path);
141 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
142 bool Move(const std::wstring& from_path, const std::wstring& to_path);
143
144 // Copies a single file. Use CopyDirectory to copy directories.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
145 bool CopyFile(const FilePath& from_path, const FilePath& to_path);
146 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
147 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path);
148
149 // Copies the given path, and optionally all subdirectories and their contents
150 // as well.
151 // If there are files existing under to_path, always overwrite.
152 // Returns true if successful, false otherwise.
153 // Dont't use wildcards on the names, it may stop working without notice.
154 //
155 // If you only need to copy a file use CopyFile, it's faster.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
156 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
157 bool recursive);
158 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
159 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
160 bool recursive);
161
162 // Returns true if the given path exists on the local filesystem,
163 // false otherwise.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
164 bool PathExists(const FilePath& path);
165 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
166 bool PathExists(const std::wstring& path);
167
168 // Returns true if the given path is writable by the user, false otherwise.
169 bool PathIsWritable(const std::wstring& path);
170
a12e489 CreateDirectory() should check if an existing path is actually a dire…
mmoss@google.com authored
171 // Returns true if the given path exists and is a directory, false otherwise.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
172 bool DirectoryExists(const FilePath& path);
173 // Deprecated temporary compatibility function.
a12e489 CreateDirectory() should check if an existing path is actually a dire…
mmoss@google.com authored
174 bool DirectoryExists(const std::wstring& path);
175
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
176 #if defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
177 // Gets the creation time of the given file (expressed in the local timezone),
178 // and returns it via the creation_time parameter. Returns true if successful,
179 // false otherwise.
180 bool GetFileCreationLocalTime(const std::wstring& filename,
181 LPSYSTEMTIME creation_time);
182
183 // Same as above, but takes a previously-opened file handle instead of a name.
184 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,
185 LPSYSTEMTIME creation_time);
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
186 #endif // defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
187
188 // Returns true if the contents of the two files given are equal, false
189 // otherwise. If either file can't be read, returns false.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
190 bool ContentsEqual(const FilePath& filename1,
191 const FilePath& filename2);
192 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
193 bool ContentsEqual(const std::wstring& filename1,
194 const std::wstring& filename2);
195
196 // Read the file at |path| into |contents|, returning true on success.
197 // Useful for unit tests.
198 bool ReadFileToString(const std::wstring& path, std::string* contents);
199
d647b1f one more try before revert
erikkay@google.com authored
200 #if defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
201 // Resolve Windows shortcut (.LNK file)
202 // Argument path specifies a valid LNK file. On success, return true and put
203 // the URL into path. If path is a invalid .LNK file, return false.
204 bool ResolveShortcut(std::wstring* path);
205
206 // Create a Windows shortcut (.LNK file)
207 // This method creates a shortcut link using the information given. Ensure
208 // you have initialized COM before calling into this function. 'source'
209 // and 'destination' parameters are required, everything else can be NULL.
210 // 'source' is the existing file, 'destination' is the new link file to be
df40e82 Cleanup some comment typos.
ericroman@google.com authored
211 // created; for best results pass the filename with the .lnk extension.
d7cae12 Add base to the repository.
initial.commit authored
212 // The 'icon' can specify a dll or exe in which case the icon index is the
213 // resource id.
214 // Note that if the shortcut exists it will overwrite it.
215 bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination,
216 const wchar_t *working_dir, const wchar_t *arguments,
217 const wchar_t *description, const wchar_t *icon,
218 int icon_index);
219
220 // Update a Windows shortcut (.LNK file). This method assumes the shortcut
221 // link already exists (otherwise false is returned). Ensure you have
222 // initialized COM before calling into this function. Only 'destination'
df40e82 Cleanup some comment typos.
ericroman@google.com authored
223 // parameter is required, everything else can be NULL (but if everything else
d7cae12 Add base to the repository.
initial.commit authored
224 // is NULL no changes are made to the shortcut). 'destination' is the link
225 // file to be updated. For best results pass the filename with the .lnk
226 // extension.
227 bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination,
228 const wchar_t *working_dir, const wchar_t *arguments,
229 const wchar_t *description, const wchar_t *icon,
230 int icon_index);
f96ba6c Fix some problems with machine level install.
kuchhal@chromium.org authored
231
232 // Return true if the given directory is empty
233 bool IsDirectoryEmpty(const std::wstring& dir_path);
234
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
235 #endif
d7cae12 Add base to the repository.
initial.commit authored
236
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
237
d7cae12 Add base to the repository.
initial.commit authored
238 // Get the temporary directory provided by the system.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
239 bool GetTempDir(FilePath* path);
240 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
241 bool GetTempDir(std::wstring* path);
242
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
243 // Creates a temporary file. The full path is placed in 'temp_file', and the
244 // function returns true if was successful in creating the file. The file will
245 // be empty and all handles closed after this function returns.
246 // TODO(erikkay): rename this function and track down all of the callers.
d7cae12 Add base to the repository.
initial.commit authored
247 bool CreateTemporaryFileName(std::wstring* temp_file);
248
74aae27 This CL adds prompting for dangerous types of files (executable) when…
jcampan@chromium.org authored
249 // Same as CreateTemporaryFileName but the file is created in |dir|.
250 bool CreateTemporaryFileNameInDir(const std::wstring& dir,
251 std::wstring* temp_file);
252
d7cae12 Add base to the repository.
initial.commit authored
253 // Create a new directory under TempPath. If prefix is provided, the new
254 // directory name is in the format of prefixyyyy.
255 // If success, return true and output the full path of the directory created.
256 bool CreateNewTempDirectory(const std::wstring& prefix,
257 std::wstring* new_temp_path);
258
259 // Creates a directory, as well as creating any parent directories, if they
3adf15e Make CreateDirectory consistent on Win and Posix when directory exists.
mmoss@google.com authored
260 // don't exist. Returns 'true' on successful creation, or if the directory
261 // already exists.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
262 bool CreateDirectory(const FilePath& full_path);
263 // Deprecated temporary compatibility function.
d7cae12 Add base to the repository.
initial.commit authored
264 bool CreateDirectory(const std::wstring& full_path);
265
266 // Returns the file size. Returns true on success.
267 bool GetFileSize(const std::wstring& file_path, int64* file_size);
268
3df3338 Add file_util::GetFileInfo as a means to provide more information abo…
darin@google.com authored
269 // Used to hold information about a given file path. See GetFileInfo below.
270 struct FileInfo {
271 // The size of the file in bytes. Undefined when is_directory is true.
272 int64 size;
273
274 // True if the file corresponds to a directory.
275 bool is_directory;
276
277 // Add additional fields here as needed.
278 };
279
280 // Returns information about the given file path.
281 bool GetFileInfo(const std::wstring& file_path, FileInfo* info);
282
ee3eba3 Cross-platform wrappers for fopen, _wfopen_s, etc.
mark@chromium.org authored
283 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
284 FILE* OpenFile(const FilePath& filename, const char* mode);
285 // Deprecated temporary compatibility functions.
ee3eba3 Cross-platform wrappers for fopen, _wfopen_s, etc.
mark@chromium.org authored
286 FILE* OpenFile(const std::string& filename, const char* mode);
287 FILE* OpenFile(const std::wstring& filename, const char* mode);
288
289 // Closes file opened by OpenFile. Returns true on success.
290 bool CloseFile(FILE* file);
291
d7cae12 Add base to the repository.
initial.commit authored
292 // Reads the given number of bytes from the file into the buffer. Returns
293 // the number of read bytes, or -1 on error.
294 int ReadFile(const std::wstring& filename, char* data, int size);
295
296 // Writes the given buffer into the file, overwriting any data that was
297 // previously there. Returns the number of bytes written, or -1 on error.
298 int WriteFile(const std::wstring& filename, const char* data, int size);
299
bf14fc5 Refactoring file_util into file_util and file_util_win. Also fix wind…
erikkay@google.com authored
300 // Gets the current working directory for the process.
a59cef1 Begin the first small step towards using FilePath everywhere:
evanm@google.com authored
301 bool GetCurrentDirectory(FilePath* path);
302 // Deprecated temporary compatibility function.
bf14fc5 Refactoring file_util into file_util and file_util_win. Also fix wind…
erikkay@google.com authored
303 bool GetCurrentDirectory(std::wstring* path);
304
305 // Sets the current working directory for the process.
306 bool SetCurrentDirectory(const std::wstring& current_directory);
307
d7cae12 Add base to the repository.
initial.commit authored
308 // A class for enumerating the files in a provided path. The order of the
309 // results is not guaranteed.
310 //
311 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
312 // program where latency does not matter. This class is blocking.
313 class FileEnumerator {
314 public:
315 enum FILE_TYPE {
316 FILES = 0x1,
317 DIRECTORIES = 0x2,
318 FILES_AND_DIRECTORIES = 0x3
319 };
320
321 // |root_path| is the starting directory to search for. It may or may not end
322 // in a slash.
323 //
324 // If |recursive| is true, this will enumerate all matches in any
325 // subdirectories matched as well. It does a breadth-first search, so all
326 // files in one directory will be returned before any files in a
327 // subdirectory.
328 //
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
329 // |file_type| specifies whether the enumerator should match files,
330 // directories, or both.
331 //
332 // |pattern| is an optional pattern for which files to match. This
333 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
334 // However, be careful in specifying patterns that aren't cross platform
335 // since the underlying code uses OS-specific matching routines. In general,
336 // Windows matching is less featureful than others, so test there first.
d7cae12 Add base to the repository.
initial.commit authored
337 // If unspecified, this will match all files.
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
338 // NOTE: the pattern only matches the contents of root_path, not files in
339 // recursive subdirectories.
340 // TODO(erikkay): Fix the pattern matching to work at all levels.
d7cae12 Add base to the repository.
initial.commit authored
341 FileEnumerator(const std::wstring& root_path,
342 bool recursive,
343 FileEnumerator::FILE_TYPE file_type);
344 FileEnumerator(const std::wstring& root_path,
345 bool recursive,
346 FileEnumerator::FILE_TYPE file_type,
347 const std::wstring& pattern);
348 ~FileEnumerator();
349
350 // Returns an empty string if there are no more results.
351 std::wstring Next();
352
353 private:
354 std::wstring root_path_;
355 bool recursive_;
356 FILE_TYPE file_type_;
357 std::wstring pattern_; // Empty when we want to find everything.
358
359 // Set to true when there is a find operation open. This way, we can lazily
360 // start the operations when the caller calls Next().
361 bool is_in_find_op_;
362
363 // A stack that keeps track of which subdirectories we still need to
364 // enumerate in the breadth-first search.
365 std::stack<std::wstring> pending_paths_;
366
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
367 #if defined(OS_WIN)
d7cae12 Add base to the repository.
initial.commit authored
368 WIN32_FIND_DATA find_data_;
369 HANDLE find_handle_;
85cd27a Part two of file_util porting. Almost all of the functionality has be…
erikkay@google.com authored
370 #elif defined(OS_POSIX)
371 FTS* fts_;
372 #endif
d7cae12 Add base to the repository.
initial.commit authored
373
374 DISALLOW_EVIL_CONSTRUCTORS(FileEnumerator);
375 };
376
bbc00cd Make sure it's possible to download to a network share.
nsylvain@chromium.org authored
377 // Renames a file using the SHFileOperation API to ensure that the target file
378 // gets the correct default security descriptor in the new path.
d7cae12 Add base to the repository.
initial.commit authored
379 bool RenameFileAndResetSecurityDescriptor(
380 const std::wstring& source_file_path,
381 const std::wstring& target_file_path);
382
383 } // namespace file_util
384
a0c9c8b Add defines for the size of wchar_t to build_config.h. Use this in pl…
brettw@google.com authored
385 #endif // BASE_FILE_UTIL_H_
Something went wrong with that request. Please try again.