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
3 // found in the LICENSE file.
5 // This file contains utility functions for dealing with the local
8 #ifndef BASE_FILE_UTIL_H_
9 #define BASE_FILE_UTIL_H_
12 #include "build/build_config.h"
16 #elif defined(OS_POSIX)
28 #include "base/base_export.h"
29 #include "base/basictypes.h"
30 #include "base/file_path.h"
31 #include "base/memory/scoped_ptr.h"
32 #include "base/platform_file.h"
33 #include "base/string16.h"
36 #include "base/eintr_wrapper.h"
37 #include "base/file_descriptor_posix.h"
38 #include "base/logging.h"
47 extern bool g_bug108724_debug
;
49 //-----------------------------------------------------------------------------
50 // Functions that operate purely on a path string w/o touching the filesystem:
52 // Returns true if the given path ends with a path separator character.
53 BASE_EXPORT
bool EndsWithSeparator(const FilePath
& path
);
55 // Makes sure that |path| ends with a separator IFF path is a directory that
56 // exists. Returns true if |path| is an existing directory, false otherwise.
57 BASE_EXPORT
bool EnsureEndsWithSeparator(FilePath
* path
);
59 // Convert provided relative path into an absolute path. Returns false on
60 // error. On POSIX, this function fails if the path does not exist.
61 BASE_EXPORT
bool AbsolutePath(FilePath
* path
);
63 // Returns true if |parent| contains |child|. Both paths are converted to
64 // absolute paths before doing the comparison.
65 BASE_EXPORT
bool ContainsPath(const FilePath
& parent
, const FilePath
& child
);
67 //-----------------------------------------------------------------------------
68 // Functions that involve filesystem access or modification:
70 // Returns the number of files matching the current path that were
71 // created on or after the given |file_time|. Doesn't count ".." or ".".
73 // Note for POSIX environments: a file created before |file_time|
74 // can be mis-detected as a newer file due to low precision of
75 // timestmap of file creation time. If you need to avoid such
76 // mis-detection perfectly, you should wait one second before
77 // obtaining |file_time|.
78 BASE_EXPORT
int CountFilesCreatedAfter(const FilePath
& path
,
79 const base::Time
& file_time
);
81 // Returns the total number of bytes used by all the files under |root_path|.
82 // If the path does not exist the function returns 0.
84 // This function is implemented using the FileEnumerator class so it is not
85 // particularly speedy in any platform.
86 BASE_EXPORT int64
ComputeDirectorySize(const FilePath
& root_path
);
88 // Returns the total number of bytes used by all files matching the provided
89 // |pattern|, on this |directory| (without recursion). If the path does not
90 // exist the function returns 0.
92 // This function is implemented using the FileEnumerator class so it is not
93 // particularly speedy in any platform.
94 BASE_EXPORT int64
ComputeFilesSize(const FilePath
& directory
,
95 const FilePath::StringType
& pattern
);
97 // Deletes the given path, whether it's a file or a directory.
98 // If it's a directory, it's perfectly happy to delete all of the
99 // directory's contents. Passing true to recursive deletes
100 // subdirectories and their contents as well.
101 // Returns true if successful, false otherwise.
103 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT
104 // TO "rm -rf", SO USE WITH CAUTION.
105 BASE_EXPORT
bool Delete(const FilePath
& path
, bool recursive
);
108 // Schedules to delete the given path, whether it's a file or a directory, until
109 // the operating system is restarted.
111 // 1) The file/directory to be deleted should exist in a temp folder.
112 // 2) The directory to be deleted must be empty.
113 BASE_EXPORT
bool DeleteAfterReboot(const FilePath
& path
);
116 // Moves the given path, whether it's a file or a directory.
117 // If a simple rename is not possible, such as in the case where the paths are
118 // on different volumes, this will attempt to copy and delete. Returns
120 BASE_EXPORT
bool Move(const FilePath
& from_path
, const FilePath
& to_path
);
122 // Renames file |from_path| to |to_path|. Both paths must be on the same
123 // volume, or the function will fail. Destination file will be created
124 // if it doesn't exist. Prefer this function over Move when dealing with
125 // temporary files. On Windows it preserves attributes of the target file.
126 // Returns true on success.
127 BASE_EXPORT
bool ReplaceFile(const FilePath
& from_path
,
128 const FilePath
& to_path
);
130 // Copies a single file. Use CopyDirectory to copy directories.
131 BASE_EXPORT
bool CopyFile(const FilePath
& from_path
, const FilePath
& to_path
);
133 // Copies the given path, and optionally all subdirectories and their contents
135 // If there are files existing under to_path, always overwrite.
136 // Returns true if successful, false otherwise.
137 // Don't use wildcards on the names, it may stop working without notice.
139 // If you only need to copy a file use CopyFile, it's faster.
140 BASE_EXPORT
bool CopyDirectory(const FilePath
& from_path
,
141 const FilePath
& to_path
,
144 // Returns true if the given path exists on the local filesystem,
146 BASE_EXPORT
bool PathExists(const FilePath
& path
);
148 // Returns true if the given path is writable by the user, false otherwise.
149 BASE_EXPORT
bool PathIsWritable(const FilePath
& path
);
151 // Returns true if the given path exists and is a directory, false otherwise.
152 BASE_EXPORT
bool DirectoryExists(const FilePath
& path
);
155 // Gets the creation time of the given file (expressed in the local timezone),
156 // and returns it via the creation_time parameter. Returns true if successful,
158 BASE_EXPORT
bool GetFileCreationLocalTime(const std::wstring
& filename
,
159 LPSYSTEMTIME creation_time
);
161 // Same as above, but takes a previously-opened file handle instead of a name.
162 BASE_EXPORT
bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle
,
163 LPSYSTEMTIME creation_time
);
164 #endif // defined(OS_WIN)
166 // Returns true if the contents of the two files given are equal, false
167 // otherwise. If either file can't be read, returns false.
168 BASE_EXPORT
bool ContentsEqual(const FilePath
& filename1
,
169 const FilePath
& filename2
);
171 // Returns true if the contents of the two text files given are equal, false
172 // otherwise. This routine treats "\r\n" and "\n" as equivalent.
173 BASE_EXPORT
bool TextContentsEqual(const FilePath
& filename1
,
174 const FilePath
& filename2
);
176 // Read the file at |path| into |contents|, returning true on success.
177 // |contents| may be NULL, in which case this function is useful for its
178 // side effect of priming the disk cache.
179 // Useful for unit tests.
180 BASE_EXPORT
bool ReadFileToString(const FilePath
& path
, std::string
* contents
);
182 #if defined(OS_POSIX)
183 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result
184 // in |buffer|. This function is protected against EINTR and partial reads.
185 // Returns true iff |bytes| bytes have been successfuly read from |fd|.
186 BASE_EXPORT
bool ReadFromFD(int fd
, char* buffer
, size_t bytes
);
188 // Creates a symbolic link at |symlink| pointing to |target|. Returns
190 BASE_EXPORT
bool CreateSymbolicLink(const FilePath
& target
,
191 const FilePath
& symlink
);
193 // Reads the given |symlink| and returns where it points to in |target|.
194 // Returns false upon failure.
195 BASE_EXPORT
bool ReadSymbolicLink(const FilePath
& symlink
, FilePath
* target
);
196 #endif // defined(OS_POSIX)
199 // Resolve Windows shortcut (.LNK file)
200 // This methods tries to resolve a shortcut .LNK file. If the |path| is valid
201 // returns true and puts the target into the |path|, otherwise returns
202 // false leaving the path as it is.
203 BASE_EXPORT
bool ResolveShortcut(FilePath
* path
);
205 // Create a Windows shortcut (.LNK file)
206 // This method creates a shortcut link using the information given. Ensure
207 // you have initialized COM before calling into this function. 'source'
208 // and 'destination' parameters are required, everything else can be NULL.
209 // 'source' is the existing file, 'destination' is the new link file to be
210 // created; for best results pass the filename with the .lnk extension.
211 // The 'icon' can specify a dll or exe in which case the icon index is the
212 // resource id. 'app_id' is the app model id for the shortcut on Win7.
213 // Note that if the shortcut exists it will overwrite it.
214 BASE_EXPORT
bool CreateShortcutLink(const wchar_t *source
,
215 const wchar_t *destination
,
216 const wchar_t *working_dir
,
217 const wchar_t *arguments
,
218 const wchar_t *description
,
221 const wchar_t* app_id
);
223 // Update a Windows shortcut (.LNK file). This method assumes the shortcut
224 // link already exists (otherwise false is returned). Ensure you have
225 // initialized COM before calling into this function. Only 'destination'
226 // parameter is required, everything else can be NULL (but if everything else
227 // is NULL no changes are made to the shortcut). 'destination' is the link
228 // file to be updated. 'app_id' is the app model id for the shortcut on Win7.
229 // For best results pass the filename with the .lnk extension.
230 BASE_EXPORT
bool UpdateShortcutLink(const wchar_t *source
,
231 const wchar_t *destination
,
232 const wchar_t *working_dir
,
233 const wchar_t *arguments
,
234 const wchar_t *description
,
237 const wchar_t* app_id
);
239 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already
240 // exist and be a shortcut that points to an executable.
241 BASE_EXPORT
bool TaskbarPinShortcutLink(const wchar_t* shortcut
);
243 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and
244 // already be pinned to the taskbar.
245 BASE_EXPORT
bool TaskbarUnpinShortcutLink(const wchar_t* shortcut
);
247 // Copy from_path to to_path recursively and then delete from_path recursively.
248 // Returns true if all operations succeed.
249 // This function simulates Move(), but unlike Move() it works across volumes.
250 // This fuction is not transactional.
251 BASE_EXPORT
bool CopyAndDeleteDirectory(const FilePath
& from_path
,
252 const FilePath
& to_path
);
253 #endif // defined(OS_WIN)
255 // Return true if the given directory is empty
256 BASE_EXPORT
bool IsDirectoryEmpty(const FilePath
& dir_path
);
258 // Get the temporary directory provided by the system.
259 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
260 // the functions below.
261 BASE_EXPORT
bool GetTempDir(FilePath
* path
);
262 // Get a temporary directory for shared memory files.
263 // Only useful on POSIX; redirects to GetTempDir() on Windows.
264 BASE_EXPORT
bool GetShmemTempDir(FilePath
* path
, bool executable
);
266 // Get the home directory. This is more complicated than just getenv("HOME")
267 // as it knows to fall back on getpwent() etc.
268 BASE_EXPORT FilePath
GetHomeDir();
270 // Creates a temporary file. The full path is placed in |path|, and the
271 // function returns true if was successful in creating the file. The file will
272 // be empty and all handles closed after this function returns.
273 BASE_EXPORT
bool CreateTemporaryFile(FilePath
* path
);
275 // Same as CreateTemporaryFile but the file is created in |dir|.
276 BASE_EXPORT
bool CreateTemporaryFileInDir(const FilePath
& dir
,
277 FilePath
* temp_file
);
279 // Create and open a temporary file. File is opened for read/write.
280 // The full path is placed in |path|.
281 // Returns a handle to the opened file or NULL if an error occured.
282 BASE_EXPORT
FILE* CreateAndOpenTemporaryFile(FilePath
* path
);
283 // Like above but for shmem files. Only useful for POSIX.
284 // The executable flag says the file needs to support using
285 // mprotect with PROT_EXEC after mapping.
286 BASE_EXPORT
FILE* CreateAndOpenTemporaryShmemFile(FilePath
* path
,
288 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
289 BASE_EXPORT
FILE* CreateAndOpenTemporaryFileInDir(const FilePath
& dir
,
292 // Create a new directory. If prefix is provided, the new directory name is in
293 // the format of prefixyyyy.
294 // NOTE: prefix is ignored in the POSIX implementation.
295 // If success, return true and output the full path of the directory created.
296 BASE_EXPORT
bool CreateNewTempDirectory(const FilePath::StringType
& prefix
,
297 FilePath
* new_temp_path
);
299 // Create a directory within another directory.
300 // Extra characters will be appended to |prefix| to ensure that the
301 // new directory does not have the same name as an existing directory.
302 BASE_EXPORT
bool CreateTemporaryDirInDir(const FilePath
& base_dir
,
303 const FilePath::StringType
& prefix
,
306 // Creates a directory, as well as creating any parent directories, if they
307 // don't exist. Returns 'true' on successful creation, or if the directory
308 // already exists. The directory is only readable by the current user.
309 BASE_EXPORT
bool CreateDirectory(const FilePath
& full_path
);
311 // Returns the file size. Returns true on success.
312 BASE_EXPORT
bool GetFileSize(const FilePath
& file_path
, int64
* file_size
);
314 // Returns true if the given path's base name is ".".
315 BASE_EXPORT
bool IsDot(const FilePath
& path
);
317 // Returns true if the given path's base name is "..".
318 BASE_EXPORT
bool IsDotDot(const FilePath
& path
);
320 // Sets |real_path| to |path| with symbolic links and junctions expanded.
321 // On windows, make sure the path starts with a lettered drive.
322 // |path| must reference a file. Function will fail if |path| points to
323 // a directory or to a nonexistent path. On windows, this function will
324 // fail if |path| is a junction or symlink that points to an empty file,
325 // or if |real_path| would be longer than MAX_PATH characters.
326 BASE_EXPORT
bool NormalizeFilePath(const FilePath
& path
, FilePath
* real_path
);
330 // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
331 // return in |drive_letter_path| the equivalent path that starts with
332 // a drive letter ("C:\..."). Return false if no such path exists.
333 BASE_EXPORT
bool DevicePathToDriveLetterPath(const FilePath
& device_path
,
334 FilePath
* drive_letter_path
);
336 // Given an existing file in |path|, set |real_path| to the path
337 // in native NT format, of the form "\Device\HarddiskVolumeXX\..".
338 // Returns false if the path can not be found. Empty files cannot
339 // be resolved with this function.
340 BASE_EXPORT
bool NormalizeToNativeFilePath(const FilePath
& path
,
344 // This function will return if the given file is a symlink or not.
345 BASE_EXPORT
bool IsLink(const FilePath
& file_path
);
347 // Returns information about the given file path.
348 BASE_EXPORT
bool GetFileInfo(const FilePath
& file_path
,
349 base::PlatformFileInfo
* info
);
351 // Sets the time of the last access and the time of the last modification.
352 BASE_EXPORT
bool TouchFile(const FilePath
& path
,
353 const base::Time
& last_accessed
,
354 const base::Time
& last_modified
);
356 // Set the time of the last modification. Useful for unit tests.
357 BASE_EXPORT
bool SetLastModifiedTime(const FilePath
& path
,
358 const base::Time
& last_modified
);
360 #if defined(OS_POSIX)
361 // Store inode number of |path| in |inode|. Return true on success.
362 BASE_EXPORT
bool GetInode(const FilePath
& path
, ino_t
* inode
);
365 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
366 BASE_EXPORT
FILE* OpenFile(const FilePath
& filename
, const char* mode
);
368 // Closes file opened by OpenFile. Returns true on success.
369 BASE_EXPORT
bool CloseFile(FILE* file
);
371 // Truncates an open file to end at the location of the current file pointer.
372 // This is a cross-platform analog to Windows' SetEndOfFile() function.
373 BASE_EXPORT
bool TruncateFile(FILE* file
);
375 // Reads the given number of bytes from the file into the buffer. Returns
376 // the number of read bytes, or -1 on error.
377 BASE_EXPORT
int ReadFile(const FilePath
& filename
, char* data
, int size
);
379 // Writes the given buffer into the file, overwriting any data that was
380 // previously there. Returns the number of bytes written, or -1 on error.
381 BASE_EXPORT
int WriteFile(const FilePath
& filename
, const char* data
, int size
);
382 #if defined(OS_POSIX)
383 // Append the data to |fd|. Does not close |fd| when done.
384 BASE_EXPORT
int WriteFileDescriptor(const int fd
, const char* data
, int size
);
387 // Gets the current working directory for the process.
388 BASE_EXPORT
bool GetCurrentDirectory(FilePath
* path
);
390 // Sets the current working directory for the process.
391 BASE_EXPORT
bool SetCurrentDirectory(const FilePath
& path
);
393 // Attempts to find a number that can be appended to the |path| to make it
394 // unique. If |path| does not exist, 0 is returned. If it fails to find such
395 // a number, -1 is returned. If |suffix| is not empty, also checks the
396 // existence of it with the given suffix.
397 BASE_EXPORT
int GetUniquePathNumber(const FilePath
& path
,
398 const FilePath::StringType
& suffix
);
400 #if defined(OS_POSIX)
401 // Test that |path| can only be changed by a given user and members of
402 // a given set of groups.
403 // Specifically, test that all parts of |path| under (and including) |base|:
405 // * Are owned by a specific user.
406 // * Are not writable by all users.
407 // * Are owned by a memeber of a given set of groups, or are not writable by
409 // * Are not symbolic links.
410 // This is useful for checking that a config file is administrator-controlled.
411 // |base| must contain |path|.
412 BASE_EXPORT
bool VerifyPathControlledByUser(const FilePath
& base
,
413 const FilePath
& path
,
415 const std::set
<gid_t
>& group_gids
);
416 #endif // defined(OS_POSIX)
418 #if defined(OS_MACOSX)
419 // Is |path| writable only by a user with administrator privileges?
420 // This function uses Mac OS conventions. The super user is assumed to have
421 // uid 0, and the administrator group is assumed to be named "admin".
422 // Testing that |path|, and every parent directory including the root of
423 // the filesystem, are owned by the superuser, controlled by the group
424 // "admin", are not writable by all users, and contain no symbolic links.
425 // Will return false if |path| does not exist.
426 BASE_EXPORT
bool VerifyPathControlledByAdmin(const FilePath
& path
);
427 #endif // defined(OS_MACOSX)
429 // A class to handle auto-closing of FILE*'s.
430 class ScopedFILEClose
{
432 inline void operator()(FILE* x
) const {
439 typedef scoped_ptr_malloc
<FILE, ScopedFILEClose
> ScopedFILE
;
441 #if defined(OS_POSIX)
442 // A class to handle auto-closing of FDs.
443 class ScopedFDClose
{
445 inline void operator()(int* x
) const {
447 if (HANDLE_EINTR(close(*x
)) < 0)
448 DPLOG(ERROR
) << "close";
453 typedef scoped_ptr_malloc
<int, ScopedFDClose
> ScopedFD
;
456 // A class for enumerating the files in a provided path. The order of the
457 // results is not guaranteed.
459 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
460 // program where latency does not matter. This class is blocking.
461 class BASE_EXPORT FileEnumerator
{
464 typedef WIN32_FIND_DATA FindInfo
;
465 #elif defined(OS_POSIX)
468 std::string filename
;
474 DIRECTORIES
= 1 << 1,
475 INCLUDE_DOT_DOT
= 1 << 2,
476 #if defined(OS_POSIX)
477 SHOW_SYM_LINKS
= 1 << 4,
481 // |root_path| is the starting directory to search for. It may or may not end
484 // If |recursive| is true, this will enumerate all matches in any
485 // subdirectories matched as well. It does a breadth-first search, so all
486 // files in one directory will be returned before any files in a
489 // |file_type| specifies whether the enumerator should match files,
490 // directories, or both.
492 // |pattern| is an optional pattern for which files to match. This
493 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
494 // However, be careful in specifying patterns that aren't cross platform
495 // since the underlying code uses OS-specific matching routines. In general,
496 // Windows matching is less featureful than others, so test there first.
497 // If unspecified, this will match all files.
498 // NOTE: the pattern only matches the contents of root_path, not files in
499 // recursive subdirectories.
500 // TODO(erikkay): Fix the pattern matching to work at all levels.
501 FileEnumerator(const FilePath
& root_path
,
504 FileEnumerator(const FilePath
& root_path
,
507 const FilePath::StringType
& pattern
);
510 // Returns an empty string if there are no more results.
513 // Write the file info into |info|.
514 void GetFindInfo(FindInfo
* info
);
516 // Looks inside a FindInfo and determines if it's a directory.
517 static bool IsDirectory(const FindInfo
& info
);
518 static bool IsLink(const FindInfo
& info
);
520 static FilePath
GetFilename(const FindInfo
& find_info
);
521 static int64
GetFilesize(const FindInfo
& find_info
);
522 static base::Time
GetLastModifiedTime(const FindInfo
& find_info
);
525 // Returns true if the given path should be skipped in enumeration.
526 bool ShouldSkip(const FilePath
& path
);
530 // True when find_data_ is valid.
532 WIN32_FIND_DATA find_data_
;
534 #elif defined(OS_POSIX)
535 struct DirectoryEntryInfo
{
540 // Read the filenames in source into the vector of DirectoryEntryInfo's
541 static bool ReadDirectory(std::vector
<DirectoryEntryInfo
>* entries
,
542 const FilePath
& source
, bool show_links
);
544 // The files in the current directory
545 std::vector
<DirectoryEntryInfo
> directory_entries_
;
547 // The next entry to use from the directory_entries_ vector
548 size_t current_directory_entry_
;
554 FilePath::StringType pattern_
; // Empty when we want to find everything.
556 // A stack that keeps track of which subdirectories we still need to
557 // enumerate in the breadth-first search.
558 std::stack
<FilePath
> pending_paths_
;
560 DISALLOW_COPY_AND_ASSIGN(FileEnumerator
);
563 class BASE_EXPORT MemoryMappedFile
{
565 // The default constructor sets all members to invalid/null values.
569 // Opens an existing file and maps it into memory. Access is restricted to
570 // read only. If this object already points to a valid memory mapped file
571 // then this method will fail and return false. If it cannot open the file,
572 // the file does not exist, or the memory mapping fails, it will return false.
573 // Later we may want to allow the user to specify access.
574 bool Initialize(const FilePath
& file_name
);
575 // As above, but works with an already-opened file. MemoryMappedFile will take
576 // ownership of |file| and close it when done.
577 bool Initialize(base::PlatformFile file
);
580 // Opens an existing file and maps it as an image section. Please refer to
581 // the Initialize function above for additional information.
582 bool InitializeAsImageSection(const FilePath
& file_name
);
585 const uint8
* data() const { return data_
; }
586 size_t length() const { return length_
; }
588 // Is file_ a valid file handle that points to an open, memory mapped file?
589 bool IsValid() const;
592 // Open the given file and pass it to MapFileToMemoryInternal().
593 bool MapFileToMemory(const FilePath
& file_name
);
595 // Map the file to memory, set data_ to that memory address. Return true on
596 // success, false on any kind of failure. This is a helper for Initialize().
597 bool MapFileToMemoryInternal();
599 // Closes all open handles. Later we may want to make this public.
603 // MapFileToMemoryInternal calls this function. It provides the ability to
604 // pass in flags which control the mapped section.
605 bool MapFileToMemoryInternalEx(int flags
);
607 HANDLE file_mapping_
;
609 base::PlatformFile file_
;
613 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile
);
616 // Returns whether the file has been modified since a particular date.
617 BASE_EXPORT
bool HasFileBeenModifiedSince(
618 const FileEnumerator::FindInfo
& find_info
,
619 const base::Time
& cutoff_time
);
621 #if defined(OS_LINUX)
622 // Broad categories of file systems as returned by statfs() on Linux.
623 enum FileSystemType
{
624 FILE_SYSTEM_UNKNOWN
, // statfs failed.
625 FILE_SYSTEM_0
, // statfs.f_type == 0 means unknown, may indicate AFS.
626 FILE_SYSTEM_ORDINARY
, // on-disk filesystem like ext2
630 FILE_SYSTEM_MEMORY
, // in-memory file system
631 FILE_SYSTEM_CGROUP
, // cgroup control.
632 FILE_SYSTEM_OTHER
, // any other value.
633 FILE_SYSTEM_TYPE_COUNT
636 // Attempts determine the FileSystemType for |path|.
637 // Returns false if |path| doesn't exist.
638 BASE_EXPORT
bool GetFileSystemType(const FilePath
& path
, FileSystemType
* type
);
641 } // namespace file_util
643 #endif // BASE_FILE_UTIL_H_