Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(549)

Side by Side Diff: base/file_util.h

Issue 6733031: Base: A few more files using BASE_API (for base.dll) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_path.h ('k') | base/file_util_deprecated.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #pragma once 10 #pragma once
11 11
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 13
14 #if defined(OS_WIN) 14 #if defined(OS_WIN)
15 #include <windows.h> 15 #include <windows.h>
16 #if defined(UNIT_TEST) 16 #if defined(UNIT_TEST)
17 #include <aclapi.h> 17 #include <aclapi.h>
18 #endif 18 #endif
19 #elif defined(OS_POSIX) 19 #elif defined(OS_POSIX)
20 #include <sys/stat.h> 20 #include <sys/stat.h>
21 #endif 21 #endif
22 22
23 #include <stdio.h> 23 #include <stdio.h>
24 24
25 #include <stack> 25 #include <stack>
26 #include <string> 26 #include <string>
27 #include <vector> 27 #include <vector>
28 28
29 #include "base/base_api.h"
29 #include "base/basictypes.h" 30 #include "base/basictypes.h"
30 #include "base/file_path.h" 31 #include "base/file_path.h"
31 #include "base/platform_file.h" 32 #include "base/platform_file.h"
32 #include "base/scoped_ptr.h" 33 #include "base/scoped_ptr.h"
33 #include "base/string16.h" 34 #include "base/string16.h"
34 35
35 #if defined(OS_POSIX) 36 #if defined(OS_POSIX)
36 #include "base/eintr_wrapper.h" 37 #include "base/eintr_wrapper.h"
37 #include "base/file_descriptor_posix.h" 38 #include "base/file_descriptor_posix.h"
38 #include "base/logging.h" 39 #include "base/logging.h"
39 #endif 40 #endif
40 41
41 namespace base { 42 namespace base {
42 class Time; 43 class Time;
43 } 44 }
44 45
45 namespace file_util { 46 namespace file_util {
46 47
47 //----------------------------------------------------------------------------- 48 //-----------------------------------------------------------------------------
48 // 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:
49 50
50 // 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.
51 bool EndsWithSeparator(const FilePath& path); 52 BASE_API bool EndsWithSeparator(const FilePath& path);
52 53
53 // 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
54 // exists. Returns true if |path| is an existing directory, false otherwise. 55 // exists. Returns true if |path| is an existing directory, false otherwise.
55 bool EnsureEndsWithSeparator(FilePath* path); 56 BASE_API bool EnsureEndsWithSeparator(FilePath* path);
56 57
57 // Convert provided relative path into an absolute path. Returns false on 58 // Convert provided relative path into an absolute path. Returns false on
58 // 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.
59 bool AbsolutePath(FilePath* path); 60 BASE_API bool AbsolutePath(FilePath* path);
60 61
61 // Returns true if |parent| contains |child|. Both paths are converted to 62 // Returns true if |parent| contains |child|. Both paths are converted to
62 // absolute paths before doing the comparison. 63 // absolute paths before doing the comparison.
63 bool ContainsPath(const FilePath& parent, const FilePath& child); 64 BASE_API bool ContainsPath(const FilePath& parent, const FilePath& child);
64 65
65 //----------------------------------------------------------------------------- 66 //-----------------------------------------------------------------------------
66 // Functions that involve filesystem access or modification: 67 // Functions that involve filesystem access or modification:
67 68
68 // Returns the number of files matching the current path that were 69 // Returns the number of files matching the current path that were
69 // created on or after the given |file_time|. Doesn't count ".." or ".". 70 // created on or after the given |file_time|. Doesn't count ".." or ".".
70 // 71 //
71 // Note for POSIX environments: a file created before |file_time| 72 // Note for POSIX environments: a file created before |file_time|
72 // can be mis-detected as a newer file due to low precision of 73 // can be mis-detected as a newer file due to low precision of
73 // timestmap of file creation time. If you need to avoid such 74 // timestmap of file creation time. If you need to avoid such
74 // mis-detection perfectly, you should wait one second before 75 // mis-detection perfectly, you should wait one second before
75 // obtaining |file_time|. 76 // obtaining |file_time|.
76 int CountFilesCreatedAfter(const FilePath& path, 77 BASE_API int CountFilesCreatedAfter(const FilePath& path,
77 const base::Time& file_time); 78 const base::Time& file_time);
78 79
79 // Returns the total number of bytes used by all the files under |root_path|. 80 // Returns the total number of bytes used by all the files under |root_path|.
80 // If the path does not exist the function returns 0. 81 // If the path does not exist the function returns 0.
81 // 82 //
82 // This function is implemented using the FileEnumerator class so it is not 83 // This function is implemented using the FileEnumerator class so it is not
83 // particularly speedy in any platform. 84 // particularly speedy in any platform.
84 int64 ComputeDirectorySize(const FilePath& root_path); 85 BASE_API int64 ComputeDirectorySize(const FilePath& root_path);
85 86
86 // Returns the total number of bytes used by all files matching the provided 87 // Returns the total number of bytes used by all files matching the provided
87 // |pattern|, on this |directory| (without recursion). If the path does not 88 // |pattern|, on this |directory| (without recursion). If the path does not
88 // exist the function returns 0. 89 // exist the function returns 0.
89 // 90 //
90 // This function is implemented using the FileEnumerator class so it is not 91 // This function is implemented using the FileEnumerator class so it is not
91 // particularly speedy in any platform. 92 // particularly speedy in any platform.
92 int64 ComputeFilesSize(const FilePath& directory, 93 BASE_API int64 ComputeFilesSize(const FilePath& directory,
93 const FilePath::StringType& pattern); 94 const FilePath::StringType& pattern);
94 95
95 // Deletes the given path, whether it's a file or a directory. 96 // Deletes the given path, whether it's a file or a directory.
96 // If it's a directory, it's perfectly happy to delete all of the 97 // If it's a directory, it's perfectly happy to delete all of the
97 // directory's contents. Passing true to recursive deletes 98 // directory's contents. Passing true to recursive deletes
98 // subdirectories and their contents as well. 99 // subdirectories and their contents as well.
99 // Returns true if successful, false otherwise. 100 // Returns true if successful, false otherwise.
100 // 101 //
101 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT 102 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT
102 // TO "rm -rf", SO USE WITH CAUTION. 103 // TO "rm -rf", SO USE WITH CAUTION.
103 bool Delete(const FilePath& path, bool recursive); 104 BASE_API bool Delete(const FilePath& path, bool recursive);
104 105
105 #if defined(OS_WIN) 106 #if defined(OS_WIN)
106 // Schedules to delete the given path, whether it's a file or a directory, until 107 // Schedules to delete the given path, whether it's a file or a directory, until
107 // the operating system is restarted. 108 // the operating system is restarted.
108 // Note: 109 // Note:
109 // 1) The file/directory to be deleted should exist in a temp folder. 110 // 1) The file/directory to be deleted should exist in a temp folder.
110 // 2) The directory to be deleted must be empty. 111 // 2) The directory to be deleted must be empty.
111 bool DeleteAfterReboot(const FilePath& path); 112 BASE_API bool DeleteAfterReboot(const FilePath& path);
112 #endif 113 #endif
113 114
114 // Moves the given path, whether it's a file or a directory. 115 // Moves the given path, whether it's a file or a directory.
115 // If a simple rename is not possible, such as in the case where the paths are 116 // If a simple rename is not possible, such as in the case where the paths are
116 // on different volumes, this will attempt to copy and delete. Returns 117 // on different volumes, this will attempt to copy and delete. Returns
117 // true for success. 118 // true for success.
118 bool Move(const FilePath& from_path, const FilePath& to_path); 119 BASE_API bool Move(const FilePath& from_path, const FilePath& to_path);
119 120
120 // Renames file |from_path| to |to_path|. Both paths must be on the same 121 // Renames file |from_path| to |to_path|. Both paths must be on the same
121 // volume, or the function will fail. Destination file will be created 122 // volume, or the function will fail. Destination file will be created
122 // if it doesn't exist. Prefer this function over Move when dealing with 123 // if it doesn't exist. Prefer this function over Move when dealing with
123 // temporary files. On Windows it preserves attributes of the target file. 124 // temporary files. On Windows it preserves attributes of the target file.
124 // Returns true on success. 125 // Returns true on success.
125 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path); 126 BASE_API bool ReplaceFile(const FilePath& from_path, const FilePath& to_path);
126 127
127 // Copies a single file. Use CopyDirectory to copy directories. 128 // Copies a single file. Use CopyDirectory to copy directories.
128 bool CopyFile(const FilePath& from_path, const FilePath& to_path); 129 BASE_API bool CopyFile(const FilePath& from_path, const FilePath& to_path);
129 130
130 // Copies the given path, and optionally all subdirectories and their contents 131 // Copies the given path, and optionally all subdirectories and their contents
131 // as well. 132 // as well.
132 // If there are files existing under to_path, always overwrite. 133 // If there are files existing under to_path, always overwrite.
133 // Returns true if successful, false otherwise. 134 // Returns true if successful, false otherwise.
134 // Don't use wildcards on the names, it may stop working without notice. 135 // Don't use wildcards on the names, it may stop working without notice.
135 // 136 //
136 // If you only need to copy a file use CopyFile, it's faster. 137 // If you only need to copy a file use CopyFile, it's faster.
137 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, 138 BASE_API bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
138 bool recursive); 139 bool recursive);
139 140
140 // Returns true if the given path exists on the local filesystem, 141 // Returns true if the given path exists on the local filesystem,
141 // false otherwise. 142 // false otherwise.
142 bool PathExists(const FilePath& path); 143 BASE_API bool PathExists(const FilePath& path);
143 144
144 // Returns true if the given path is writable by the user, false otherwise. 145 // Returns true if the given path is writable by the user, false otherwise.
145 bool PathIsWritable(const FilePath& path); 146 BASE_API bool PathIsWritable(const FilePath& path);
146 147
147 // Returns true if the given path exists and is a directory, false otherwise. 148 // Returns true if the given path exists and is a directory, false otherwise.
148 bool DirectoryExists(const FilePath& path); 149 BASE_API bool DirectoryExists(const FilePath& path);
149 150
150 #if defined(OS_WIN) 151 #if defined(OS_WIN)
151 // Gets the creation time of the given file (expressed in the local timezone), 152 // Gets the creation time of the given file (expressed in the local timezone),
152 // and returns it via the creation_time parameter. Returns true if successful, 153 // and returns it via the creation_time parameter. Returns true if successful,
153 // false otherwise. 154 // false otherwise.
154 bool GetFileCreationLocalTime(const std::wstring& filename, 155 BASE_API bool GetFileCreationLocalTime(const std::wstring& filename,
155 LPSYSTEMTIME creation_time); 156 LPSYSTEMTIME creation_time);
156 157
157 // Same as above, but takes a previously-opened file handle instead of a name. 158 // Same as above, but takes a previously-opened file handle instead of a name.
158 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, 159 BASE_API bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,
159 LPSYSTEMTIME creation_time); 160 LPSYSTEMTIME creation_time);
160 #endif // defined(OS_WIN) 161 #endif // defined(OS_WIN)
161 162
162 // Returns true if the contents of the two files given are equal, false 163 // Returns true if the contents of the two files given are equal, false
163 // otherwise. If either file can't be read, returns false. 164 // otherwise. If either file can't be read, returns false.
164 bool ContentsEqual(const FilePath& filename1, 165 BASE_API bool ContentsEqual(const FilePath& filename1,
165 const FilePath& filename2); 166 const FilePath& filename2);
166 167
167 // Returns true if the contents of the two text files given are equal, false 168 // Returns true if the contents of the two text files given are equal, false
168 // otherwise. This routine treats "\r\n" and "\n" as equivalent. 169 // otherwise. This routine treats "\r\n" and "\n" as equivalent.
169 bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2); 170 BASE_API bool TextContentsEqual(const FilePath& filename1,
171 const FilePath& filename2);
170 172
171 // Read the file at |path| into |contents|, returning true on success. 173 // Read the file at |path| into |contents|, returning true on success.
172 // |contents| may be NULL, in which case this function is useful for its 174 // |contents| may be NULL, in which case this function is useful for its
173 // side effect of priming the disk cache. 175 // side effect of priming the disk cache.
174 // Useful for unit tests. 176 // Useful for unit tests.
175 bool ReadFileToString(const FilePath& path, std::string* contents); 177 BASE_API bool ReadFileToString(const FilePath& path, std::string* contents);
176 178
177 #if defined(OS_POSIX) 179 #if defined(OS_POSIX)
178 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result 180 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result
179 // in |buffer|. This function is protected against EINTR and partial reads. 181 // in |buffer|. This function is protected against EINTR and partial reads.
180 // Returns true iff |bytes| bytes have been successfuly read from |fd|. 182 // Returns true iff |bytes| bytes have been successfuly read from |fd|.
181 bool ReadFromFD(int fd, char* buffer, size_t bytes); 183 bool ReadFromFD(int fd, char* buffer, size_t bytes);
182 184
183 // Creates a symbolic link at |symlink| pointing to |target|. Returns 185 // Creates a symbolic link at |symlink| pointing to |target|. Returns
184 // false on failure. 186 // false on failure.
185 bool CreateSymbolicLink(const FilePath& target, const FilePath& symlink); 187 bool CreateSymbolicLink(const FilePath& target, const FilePath& symlink);
186 188
187 // Reads the given |symlink| and returns where it points to in |target|. 189 // Reads the given |symlink| and returns where it points to in |target|.
188 // Returns false upon failure. 190 // Returns false upon failure.
189 bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); 191 bool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
190 #endif // defined(OS_POSIX) 192 #endif // defined(OS_POSIX)
191 193
192 #if defined(OS_WIN) 194 #if defined(OS_WIN)
193 // Resolve Windows shortcut (.LNK file) 195 // Resolve Windows shortcut (.LNK file)
194 // This methods tries to resolve a shortcut .LNK file. If the |path| is valid 196 // This methods tries to resolve a shortcut .LNK file. If the |path| is valid
195 // returns true and puts the target into the |path|, otherwise returns 197 // returns true and puts the target into the |path|, otherwise returns
196 // false leaving the path as it is. 198 // false leaving the path as it is.
197 bool ResolveShortcut(FilePath* path); 199 BASE_API bool ResolveShortcut(FilePath* path);
198 200
199 // Create a Windows shortcut (.LNK file) 201 // Create a Windows shortcut (.LNK file)
200 // This method creates a shortcut link using the information given. Ensure 202 // This method creates a shortcut link using the information given. Ensure
201 // you have initialized COM before calling into this function. 'source' 203 // you have initialized COM before calling into this function. 'source'
202 // and 'destination' parameters are required, everything else can be NULL. 204 // and 'destination' parameters are required, everything else can be NULL.
203 // 'source' is the existing file, 'destination' is the new link file to be 205 // 'source' is the existing file, 'destination' is the new link file to be
204 // created; for best results pass the filename with the .lnk extension. 206 // created; for best results pass the filename with the .lnk extension.
205 // The 'icon' can specify a dll or exe in which case the icon index is the 207 // The 'icon' can specify a dll or exe in which case the icon index is the
206 // resource id. 'app_id' is the app model id for the shortcut on Win7. 208 // resource id. 'app_id' is the app model id for the shortcut on Win7.
207 // Note that if the shortcut exists it will overwrite it. 209 // Note that if the shortcut exists it will overwrite it.
208 bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, 210 BASE_API bool CreateShortcutLink(const wchar_t *source,
209 const wchar_t *working_dir, const wchar_t *arguments, 211 const wchar_t *destination,
210 const wchar_t *description, const wchar_t *icon, 212 const wchar_t *working_dir,
211 int icon_index, const wchar_t* app_id); 213 const wchar_t *arguments,
214 const wchar_t *description,
215 const wchar_t *icon,
216 int icon_index,
217 const wchar_t* app_id);
212 218
213 // Update a Windows shortcut (.LNK file). This method assumes the shortcut 219 // Update a Windows shortcut (.LNK file). This method assumes the shortcut
214 // link already exists (otherwise false is returned). Ensure you have 220 // link already exists (otherwise false is returned). Ensure you have
215 // initialized COM before calling into this function. Only 'destination' 221 // initialized COM before calling into this function. Only 'destination'
216 // parameter is required, everything else can be NULL (but if everything else 222 // parameter is required, everything else can be NULL (but if everything else
217 // is NULL no changes are made to the shortcut). 'destination' is the link 223 // is NULL no changes are made to the shortcut). 'destination' is the link
218 // file to be updated. 'app_id' is the app model id for the shortcut on Win7. 224 // file to be updated. 'app_id' is the app model id for the shortcut on Win7.
219 // For best results pass the filename with the .lnk extension. 225 // For best results pass the filename with the .lnk extension.
220 bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination, 226 BASE_API bool UpdateShortcutLink(const wchar_t *source,
221 const wchar_t *working_dir, const wchar_t *arguments, 227 const wchar_t *destination,
222 const wchar_t *description, const wchar_t *icon, 228 const wchar_t *working_dir,
223 int icon_index, const wchar_t* app_id); 229 const wchar_t *arguments,
230 const wchar_t *description,
231 const wchar_t *icon,
232 int icon_index,
233 const wchar_t* app_id);
224 234
225 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already 235 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already
226 // exist and be a shortcut that points to an executable. 236 // exist and be a shortcut that points to an executable.
227 bool TaskbarPinShortcutLink(const wchar_t* shortcut); 237 BASE_API bool TaskbarPinShortcutLink(const wchar_t* shortcut);
228 238
229 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and 239 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and
230 // already be pinned to the taskbar. 240 // already be pinned to the taskbar.
231 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut); 241 BASE_API bool TaskbarUnpinShortcutLink(const wchar_t* shortcut);
232 242
233 // Copy from_path to to_path recursively and then delete from_path recursively. 243 // Copy from_path to to_path recursively and then delete from_path recursively.
234 // Returns true if all operations succeed. 244 // Returns true if all operations succeed.
235 // This function simulates Move(), but unlike Move() it works across volumes. 245 // This function simulates Move(), but unlike Move() it works across volumes.
236 // This fuction is not transactional. 246 // This fuction is not transactional.
237 bool CopyAndDeleteDirectory(const FilePath& from_path, 247 BASE_API bool CopyAndDeleteDirectory(const FilePath& from_path,
238 const FilePath& to_path); 248 const FilePath& to_path);
239 #endif // defined(OS_WIN) 249 #endif // defined(OS_WIN)
240 250
241 // Return true if the given directory is empty 251 // Return true if the given directory is empty
242 bool IsDirectoryEmpty(const FilePath& dir_path); 252 BASE_API bool IsDirectoryEmpty(const FilePath& dir_path);
243 253
244 // Get the temporary directory provided by the system. 254 // Get the temporary directory provided by the system.
245 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of 255 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
246 // the functions below. 256 // the functions below.
247 bool GetTempDir(FilePath* path); 257 BASE_API bool GetTempDir(FilePath* path);
248 // Get a temporary directory for shared memory files. 258 // Get a temporary directory for shared memory files.
249 // Only useful on POSIX; redirects to GetTempDir() on Windows. 259 // Only useful on POSIX; redirects to GetTempDir() on Windows.
250 bool GetShmemTempDir(FilePath* path); 260 BASE_API bool GetShmemTempDir(FilePath* path);
251 261
252 // Get the home directory. This is more complicated than just getenv("HOME") 262 // Get the home directory. This is more complicated than just getenv("HOME")
253 // as it knows to fall back on getpwent() etc. 263 // as it knows to fall back on getpwent() etc.
254 FilePath GetHomeDir(); 264 BASE_API FilePath GetHomeDir();
255 265
256 // Creates a temporary file. The full path is placed in |path|, and the 266 // Creates a temporary file. The full path is placed in |path|, and the
257 // function returns true if was successful in creating the file. The file will 267 // function returns true if was successful in creating the file. The file will
258 // be empty and all handles closed after this function returns. 268 // be empty and all handles closed after this function returns.
259 bool CreateTemporaryFile(FilePath* path); 269 BASE_API bool CreateTemporaryFile(FilePath* path);
260 270
261 // Same as CreateTemporaryFile but the file is created in |dir|. 271 // Same as CreateTemporaryFile but the file is created in |dir|.
262 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file); 272 BASE_API bool CreateTemporaryFileInDir(const FilePath& dir,
273 FilePath* temp_file);
263 274
264 // Create and open a temporary file. File is opened for read/write. 275 // Create and open a temporary file. File is opened for read/write.
265 // The full path is placed in |path|. 276 // The full path is placed in |path|.
266 // Returns a handle to the opened file or NULL if an error occured. 277 // Returns a handle to the opened file or NULL if an error occured.
267 FILE* CreateAndOpenTemporaryFile(FilePath* path); 278 BASE_API FILE* CreateAndOpenTemporaryFile(FilePath* path);
268 // Like above but for shmem files. Only useful for POSIX. 279 // Like above but for shmem files. Only useful for POSIX.
269 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path); 280 BASE_API FILE* CreateAndOpenTemporaryShmemFile(FilePath* path);
270 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. 281 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
271 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path); 282 BASE_API FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
283 FilePath* path);
272 284
273 // Create a new directory. If prefix is provided, the new directory name is in 285 // Create a new directory. If prefix is provided, the new directory name is in
274 // the format of prefixyyyy. 286 // the format of prefixyyyy.
275 // NOTE: prefix is ignored in the POSIX implementation. 287 // NOTE: prefix is ignored in the POSIX implementation.
276 // If success, return true and output the full path of the directory created. 288 // If success, return true and output the full path of the directory created.
277 bool CreateNewTempDirectory(const FilePath::StringType& prefix, 289 BASE_API bool CreateNewTempDirectory(const FilePath::StringType& prefix,
278 FilePath* new_temp_path); 290 FilePath* new_temp_path);
279 291
280 // Create a directory within another directory. 292 // Create a directory within another directory.
281 // Extra characters will be appended to |prefix| to ensure that the 293 // Extra characters will be appended to |prefix| to ensure that the
282 // new directory does not have the same name as an existing directory. 294 // new directory does not have the same name as an existing directory.
283 bool CreateTemporaryDirInDir(const FilePath& base_dir, 295 BASE_API bool CreateTemporaryDirInDir(const FilePath& base_dir,
284 const FilePath::StringType& prefix, 296 const FilePath::StringType& prefix,
285 FilePath* new_dir); 297 FilePath* new_dir);
286 298
287 // Creates a directory, as well as creating any parent directories, if they 299 // Creates a directory, as well as creating any parent directories, if they
288 // don't exist. Returns 'true' on successful creation, or if the directory 300 // don't exist. Returns 'true' on successful creation, or if the directory
289 // already exists. The directory is only readable by the current user. 301 // already exists. The directory is only readable by the current user.
290 bool CreateDirectory(const FilePath& full_path); 302 BASE_API bool CreateDirectory(const FilePath& full_path);
291 303
292 // Returns the file size. Returns true on success. 304 // Returns the file size. Returns true on success.
293 bool GetFileSize(const FilePath& file_path, int64* file_size); 305 BASE_API bool GetFileSize(const FilePath& file_path, int64* file_size);
294 306
295 // Returns true if the given path's base name is ".". 307 // Returns true if the given path's base name is ".".
296 bool IsDot(const FilePath& path); 308 BASE_API bool IsDot(const FilePath& path);
297 309
298 // Returns true if the given path's base name is "..". 310 // Returns true if the given path's base name is "..".
299 bool IsDotDot(const FilePath& path); 311 BASE_API bool IsDotDot(const FilePath& path);
300 312
301 // Sets |real_path| to |path| with symbolic links and junctions expanded. 313 // Sets |real_path| to |path| with symbolic links and junctions expanded.
302 // On windows, make sure the path starts with a lettered drive. 314 // On windows, make sure the path starts with a lettered drive.
303 // |path| must reference a file. Function will fail if |path| points to 315 // |path| must reference a file. Function will fail if |path| points to
304 // a directory or to a nonexistent path. On windows, this function will 316 // a directory or to a nonexistent path. On windows, this function will
305 // fail if |path| is a junction or symlink that points to an empty file, 317 // fail if |path| is a junction or symlink that points to an empty file,
306 // or if |real_path| would be longer than MAX_PATH characters. 318 // or if |real_path| would be longer than MAX_PATH characters.
307 bool NormalizeFilePath(const FilePath& path, FilePath* real_path); 319 BASE_API bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
308 320
309 #if defined(OS_WIN) 321 #if defined(OS_WIN)
310 // Given an existing file in |path|, it returns in |real_path| the path 322 // Given an existing file in |path|, it returns in |real_path| the path
311 // in the native NT format, of the form "\Device\HarddiskVolumeXX\..". 323 // in the native NT format, of the form "\Device\HarddiskVolumeXX\..".
312 // Returns false it it fails. Empty files cannot be resolved with this 324 // Returns false it it fails. Empty files cannot be resolved with this
313 // function. 325 // function.
314 bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path); 326 BASE_API bool NormalizeToNativeFilePath(const FilePath& path,
327 FilePath* nt_path);
315 #endif 328 #endif
316 329
317 // Returns information about the given file path. 330 // Returns information about the given file path.
318 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* info); 331 BASE_API bool GetFileInfo(const FilePath& file_path,
332 base::PlatformFileInfo* info);
319 333
320 // Sets the time of the last access and the time of the last modification. 334 // Sets the time of the last access and the time of the last modification.
321 bool TouchFile(const FilePath& path, 335 BASE_API bool TouchFile(const FilePath& path,
322 const base::Time& last_accessed, 336 const base::Time& last_accessed,
323 const base::Time& last_modified); 337 const base::Time& last_modified);
324 338
325 // Set the time of the last modification. Useful for unit tests. 339 // Set the time of the last modification. Useful for unit tests.
326 bool SetLastModifiedTime(const FilePath& path, 340 BASE_API bool SetLastModifiedTime(const FilePath& path,
327 const base::Time& last_modified); 341 const base::Time& last_modified);
328 342
329 #if defined(OS_POSIX) 343 #if defined(OS_POSIX)
330 // Store inode number of |path| in |inode|. Return true on success. 344 // Store inode number of |path| in |inode|. Return true on success.
331 bool GetInode(const FilePath& path, ino_t* inode); 345 bool GetInode(const FilePath& path, ino_t* inode);
332 #endif 346 #endif
333 347
334 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. 348 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
335 FILE* OpenFile(const FilePath& filename, const char* mode); 349 BASE_API FILE* OpenFile(const FilePath& filename, const char* mode);
336 350
337 // Closes file opened by OpenFile. Returns true on success. 351 // Closes file opened by OpenFile. Returns true on success.
338 bool CloseFile(FILE* file); 352 BASE_API bool CloseFile(FILE* file);
339 353
340 // Truncates an open file to end at the location of the current file pointer. 354 // Truncates an open file to end at the location of the current file pointer.
341 // This is a cross-platform analog to Windows' SetEndOfFile() function. 355 // This is a cross-platform analog to Windows' SetEndOfFile() function.
342 bool TruncateFile(FILE* file); 356 BASE_API bool TruncateFile(FILE* file);
343 357
344 // Reads the given number of bytes from the file into the buffer. Returns 358 // Reads the given number of bytes from the file into the buffer. Returns
345 // the number of read bytes, or -1 on error. 359 // the number of read bytes, or -1 on error.
346 int ReadFile(const FilePath& filename, char* data, int size); 360 BASE_API int ReadFile(const FilePath& filename, char* data, int size);
347 361
348 // Writes the given buffer into the file, overwriting any data that was 362 // Writes the given buffer into the file, overwriting any data that was
349 // previously there. Returns the number of bytes written, or -1 on error. 363 // previously there. Returns the number of bytes written, or -1 on error.
350 int WriteFile(const FilePath& filename, const char* data, int size); 364 BASE_API int WriteFile(const FilePath& filename, const char* data, int size);
351 #if defined(OS_POSIX) 365 #if defined(OS_POSIX)
352 // Append the data to |fd|. Does not close |fd| when done. 366 // Append the data to |fd|. Does not close |fd| when done.
353 int WriteFileDescriptor(const int fd, const char* data, int size); 367 int WriteFileDescriptor(const int fd, const char* data, int size);
354 #endif 368 #endif
355 369
356 // Gets the current working directory for the process. 370 // Gets the current working directory for the process.
357 bool GetCurrentDirectory(FilePath* path); 371 BASE_API bool GetCurrentDirectory(FilePath* path);
358 372
359 // Sets the current working directory for the process. 373 // Sets the current working directory for the process.
360 bool SetCurrentDirectory(const FilePath& path); 374 BASE_API bool SetCurrentDirectory(const FilePath& path);
361 375
362 // A class to handle auto-closing of FILE*'s. 376 // A class to handle auto-closing of FILE*'s.
363 class ScopedFILEClose { 377 class ScopedFILEClose {
364 public: 378 public:
365 inline void operator()(FILE* x) const { 379 inline void operator()(FILE* x) const {
366 if (x) { 380 if (x) {
367 fclose(x); 381 fclose(x);
368 } 382 }
369 } 383 }
370 }; 384 };
(...skipping 13 matching lines...) Expand all
384 }; 398 };
385 399
386 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; 400 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
387 #endif // OS_POSIX 401 #endif // OS_POSIX
388 402
389 // A class for enumerating the files in a provided path. The order of the 403 // A class for enumerating the files in a provided path. The order of the
390 // results is not guaranteed. 404 // results is not guaranteed.
391 // 405 //
392 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test 406 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
393 // program where latency does not matter. This class is blocking. 407 // program where latency does not matter. This class is blocking.
394 class FileEnumerator { 408 class BASE_API FileEnumerator {
395 public: 409 public:
396 #if defined(OS_WIN) 410 #if defined(OS_WIN)
397 typedef WIN32_FIND_DATA FindInfo; 411 typedef WIN32_FIND_DATA FindInfo;
398 #elif defined(OS_POSIX) 412 #elif defined(OS_POSIX)
399 typedef struct { 413 typedef struct {
400 struct stat stat; 414 struct stat stat;
401 std::string filename; 415 std::string filename;
402 } FindInfo; 416 } FindInfo;
403 #endif 417 #endif
404 418
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 FILE_TYPE file_type_; 497 FILE_TYPE file_type_;
484 FilePath::StringType pattern_; // Empty when we want to find everything. 498 FilePath::StringType pattern_; // Empty when we want to find everything.
485 499
486 // A stack that keeps track of which subdirectories we still need to 500 // A stack that keeps track of which subdirectories we still need to
487 // enumerate in the breadth-first search. 501 // enumerate in the breadth-first search.
488 std::stack<FilePath> pending_paths_; 502 std::stack<FilePath> pending_paths_;
489 503
490 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); 504 DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
491 }; 505 };
492 506
493 class MemoryMappedFile { 507 class BASE_API MemoryMappedFile {
494 public: 508 public:
495 // The default constructor sets all members to invalid/null values. 509 // The default constructor sets all members to invalid/null values.
496 MemoryMappedFile(); 510 MemoryMappedFile();
497 ~MemoryMappedFile(); 511 ~MemoryMappedFile();
498 512
499 // Opens an existing file and maps it into memory. Access is restricted to 513 // Opens an existing file and maps it into memory. Access is restricted to
500 // read only. If this object already points to a valid memory mapped file 514 // read only. If this object already points to a valid memory mapped file
501 // then this method will fail and return false. If it cannot open the file, 515 // then this method will fail and return false. If it cannot open the file,
502 // the file does not exist, or the memory mapping fails, it will return false. 516 // the file does not exist, or the memory mapping fails, it will return false.
503 // Later we may want to allow the user to specify access. 517 // Later we may want to allow the user to specify access.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 #endif 552 #endif
539 base::PlatformFile file_; 553 base::PlatformFile file_;
540 uint8* data_; 554 uint8* data_;
541 size_t length_; 555 size_t length_;
542 556
543 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); 557 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile);
544 }; 558 };
545 559
546 // Renames a file using the SHFileOperation API to ensure that the target file 560 // Renames a file using the SHFileOperation API to ensure that the target file
547 // gets the correct default security descriptor in the new path. 561 // gets the correct default security descriptor in the new path.
548 bool RenameFileAndResetSecurityDescriptor( 562 BASE_API bool RenameFileAndResetSecurityDescriptor(
549 const FilePath& source_file_path, 563 const FilePath& source_file_path,
550 const FilePath& target_file_path); 564 const FilePath& target_file_path);
551 565
552 // Returns whether the file has been modified since a particular date. 566 // Returns whether the file has been modified since a particular date.
553 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, 567 BASE_API bool HasFileBeenModifiedSince(
554 const base::Time& cutoff_time); 568 const FileEnumerator::FindInfo& find_info,
569 const base::Time& cutoff_time);
555 570
556 #ifdef UNIT_TEST 571 #ifdef UNIT_TEST
557 572
558 inline bool MakeFileUnreadable(const FilePath& path) { 573 inline bool MakeFileUnreadable(const FilePath& path) {
559 #if defined(OS_POSIX) 574 #if defined(OS_POSIX)
560 struct stat stat_buf; 575 struct stat stat_buf;
561 if (stat(path.value().c_str(), &stat_buf) != 0) 576 if (stat(path.value().c_str(), &stat_buf) != 0)
562 return false; 577 return false;
563 stat_buf.st_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH); 578 stat_buf.st_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
564 579
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 } 619 }
605 620
606 #endif // UNIT_TEST 621 #endif // UNIT_TEST
607 622
608 #if defined(OS_WIN) 623 #if defined(OS_WIN)
609 // Loads the file passed in as an image section and touches pages to avoid 624 // Loads the file passed in as an image section and touches pages to avoid
610 // subsequent hard page faults during LoadLibrary. The size to be pre read 625 // subsequent hard page faults during LoadLibrary. The size to be pre read
611 // is passed in. If it is 0 then the whole file is paged in. The step size 626 // is passed in. If it is 0 then the whole file is paged in. The step size
612 // which indicates the number of bytes to skip after every page touched is 627 // which indicates the number of bytes to skip after every page touched is
613 // also passed in. 628 // also passed in.
614 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, 629 bool BASE_API PreReadImage(const wchar_t* file_path, size_t size_to_read,
615 size_t step_size); 630 size_t step_size);
616 #endif // OS_WIN 631 #endif // OS_WIN
617 632
618 #if defined(OS_LINUX) 633 #if defined(OS_LINUX)
619 // Broad categories of file systems as returned by statfs() on Linux. 634 // Broad categories of file systems as returned by statfs() on Linux.
620 enum FileSystemType { 635 enum FileSystemType {
621 FILE_SYSTEM_UNKNOWN, // statfs failed. 636 FILE_SYSTEM_UNKNOWN, // statfs failed.
622 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. 637 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
623 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 638 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
624 FILE_SYSTEM_NFS, 639 FILE_SYSTEM_NFS,
625 FILE_SYSTEM_SMB, 640 FILE_SYSTEM_SMB,
626 FILE_SYSTEM_CODA, 641 FILE_SYSTEM_CODA,
627 FILE_SYSTEM_MEMORY, // in-memory file system 642 FILE_SYSTEM_MEMORY, // in-memory file system
628 FILE_SYSTEM_CGROUP, // cgroup control. 643 FILE_SYSTEM_CGROUP, // cgroup control.
629 FILE_SYSTEM_OTHER, // any other value. 644 FILE_SYSTEM_OTHER, // any other value.
630 FILE_SYSTEM_TYPE_COUNT 645 FILE_SYSTEM_TYPE_COUNT
631 }; 646 };
632 647
633 // Attempts determine the FileSystemType for |path|. 648 // Attempts determine the FileSystemType for |path|.
634 // Returns false if |path| doesn't exist. 649 // Returns false if |path| doesn't exist.
635 bool GetFileSystemType(const FilePath& path, FileSystemType* type); 650 bool GetFileSystemType(const FilePath& path, FileSystemType* type);
636 #endif 651 #endif
637 652
638 } // namespace file_util 653 } // namespace file_util
639 654
640 // Deprecated functions have been moved to this separate header file, 655 // Deprecated functions have been moved to this separate header file,
641 // which must be included last after all the above definitions. 656 // which must be included last after all the above definitions.
642 #include "base/file_util_deprecated.h" 657 #include "base/file_util_deprecated.h"
643 658
644 #endif // BASE_FILE_UTIL_H_ 659 #endif // BASE_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « base/file_path.h ('k') | base/file_util_deprecated.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698