blob: 00be5c92f95a06caed585deea0020e507ff35969 [file] [log] [blame]
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
grt@chromium.org683b29f2011-10-15 05:48:05 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains utility functions for dealing with the local
6// filesystem.
7
8#ifndef BASE_FILE_UTIL_H_
9#define BASE_FILE_UTIL_H_
grt@chromium.org683b29f2011-10-15 05:48:05 +090010
11#include "build/build_config.h"
12
13#if defined(OS_WIN)
14#include <windows.h>
15#elif defined(OS_POSIX)
16#include <sys/stat.h>
17#include <unistd.h>
18#endif
19
20#include <stdio.h>
21
skerner@chromium.org80784142011-10-18 06:30:29 +090022#include <set>
grt@chromium.org683b29f2011-10-15 05:48:05 +090023#include <stack>
24#include <string>
25#include <vector>
26
27#include "base/base_export.h"
28#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090029#include "base/files/file_path.h"
grt@chromium.org683b29f2011-10-15 05:48:05 +090030#include "base/memory/scoped_ptr.h"
31#include "base/platform_file.h"
32#include "base/string16.h"
33
34#if defined(OS_POSIX)
grt@chromium.org683b29f2011-10-15 05:48:05 +090035#include "base/file_descriptor_posix.h"
36#include "base/logging.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090037#include "base/posix/eintr_wrapper.h"
grt@chromium.org683b29f2011-10-15 05:48:05 +090038#endif
39
40namespace base {
41class Time;
42}
43
44namespace file_util {
45
aa@chromium.org8dac1e42012-02-05 12:22:38 +090046extern bool g_bug108724_debug;
47
grt@chromium.org683b29f2011-10-15 05:48:05 +090048//-----------------------------------------------------------------------------
49// Functions that operate purely on a path string w/o touching the filesystem:
50
51// Returns true if the given path ends with a path separator character.
brettw@chromium.orga7086942013-02-02 14:12:33 +090052BASE_EXPORT bool EndsWithSeparator(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +090053
54// Makes sure that |path| ends with a separator IFF path is a directory that
55// exists. Returns true if |path| is an existing directory, false otherwise.
brettw@chromium.orga7086942013-02-02 14:12:33 +090056BASE_EXPORT bool EnsureEndsWithSeparator(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +090057
58// Convert provided relative path into an absolute path. Returns false on
59// error. On POSIX, this function fails if the path does not exist.
brettw@chromium.orga7086942013-02-02 14:12:33 +090060BASE_EXPORT bool AbsolutePath(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +090061
62// Returns true if |parent| contains |child|. Both paths are converted to
63// absolute paths before doing the comparison.
brettw@chromium.orga7086942013-02-02 14:12:33 +090064BASE_EXPORT bool ContainsPath(const base::FilePath& parent,
65 const base::FilePath& child);
grt@chromium.org683b29f2011-10-15 05:48:05 +090066
67//-----------------------------------------------------------------------------
68// Functions that involve filesystem access or modification:
69
grt@chromium.org683b29f2011-10-15 05:48:05 +090070// Returns the total number of bytes used by all the files under |root_path|.
71// If the path does not exist the function returns 0.
72//
73// This function is implemented using the FileEnumerator class so it is not
74// particularly speedy in any platform.
brettw@chromium.orga7086942013-02-02 14:12:33 +090075BASE_EXPORT int64 ComputeDirectorySize(const base::FilePath& root_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +090076
grt@chromium.org683b29f2011-10-15 05:48:05 +090077// Deletes the given path, whether it's a file or a directory.
78// If it's a directory, it's perfectly happy to delete all of the
79// directory's contents. Passing true to recursive deletes
80// subdirectories and their contents as well.
81// Returns true if successful, false otherwise.
82//
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +090083// In posix environment and if |path| is a symbolic link, this deletes only
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +090084// the symlink. (even if the symlink points to a non-existent file)
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +090085//
grt@chromium.org683b29f2011-10-15 05:48:05 +090086// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
87// TO "rm -rf", SO USE WITH CAUTION.
brettw@chromium.orga7086942013-02-02 14:12:33 +090088BASE_EXPORT bool Delete(const base::FilePath& path, bool recursive);
grt@chromium.org683b29f2011-10-15 05:48:05 +090089
90#if defined(OS_WIN)
91// Schedules to delete the given path, whether it's a file or a directory, until
92// the operating system is restarted.
93// Note:
94// 1) The file/directory to be deleted should exist in a temp folder.
95// 2) The directory to be deleted must be empty.
brettw@chromium.orga7086942013-02-02 14:12:33 +090096BASE_EXPORT bool DeleteAfterReboot(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +090097#endif
98
99// Moves the given path, whether it's a file or a directory.
100// If a simple rename is not possible, such as in the case where the paths are
101// on different volumes, this will attempt to copy and delete. Returns
102// true for success.
cevans@chromium.org007dbe22013-02-07 05:38:07 +0900103// This function fails if either path contains traversal components ('..').
brettw@chromium.orga7086942013-02-02 14:12:33 +0900104BASE_EXPORT bool Move(const base::FilePath& from_path,
105 const base::FilePath& to_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900106
cevans@chromium.org007dbe22013-02-07 05:38:07 +0900107// Same as Move but allows paths with traversal components.
108// Use only with extreme care.
109BASE_EXPORT bool MoveUnsafe(const base::FilePath& from_path,
110 const base::FilePath& to_path);
111
grt@chromium.org683b29f2011-10-15 05:48:05 +0900112// Renames file |from_path| to |to_path|. Both paths must be on the same
113// volume, or the function will fail. Destination file will be created
114// if it doesn't exist. Prefer this function over Move when dealing with
115// temporary files. On Windows it preserves attributes of the target file.
116// Returns true on success.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900117BASE_EXPORT bool ReplaceFile(const base::FilePath& from_path,
118 const base::FilePath& to_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900119
120// Copies a single file. Use CopyDirectory to copy directories.
cevans@chromium.org007dbe22013-02-07 05:38:07 +0900121// This function fails if either path contains traversal components ('..').
brettw@chromium.orga7086942013-02-02 14:12:33 +0900122BASE_EXPORT bool CopyFile(const base::FilePath& from_path,
123 const base::FilePath& to_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900124
cevans@chromium.org007dbe22013-02-07 05:38:07 +0900125// Same as CopyFile but allows paths with traversal components.
126// Use only with extreme care.
127BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path,
128 const base::FilePath& to_path);
129
grt@chromium.org683b29f2011-10-15 05:48:05 +0900130// Copies the given path, and optionally all subdirectories and their contents
131// as well.
132// If there are files existing under to_path, always overwrite.
133// Returns true if successful, false otherwise.
134// Don't use wildcards on the names, it may stop working without notice.
135//
136// If you only need to copy a file use CopyFile, it's faster.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900137BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path,
138 const base::FilePath& to_path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900139 bool recursive);
140
141// Returns true if the given path exists on the local filesystem,
142// false otherwise.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900143BASE_EXPORT bool PathExists(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900144
145// Returns true if the given path is writable by the user, false otherwise.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900146BASE_EXPORT bool PathIsWritable(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900147
148// Returns true if the given path exists and is a directory, false otherwise.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900149BASE_EXPORT bool DirectoryExists(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900150
grt@chromium.org683b29f2011-10-15 05:48:05 +0900151// Returns true if the contents of the two files given are equal, false
152// otherwise. If either file can't be read, returns false.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900153BASE_EXPORT bool ContentsEqual(const base::FilePath& filename1,
154 const base::FilePath& filename2);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900155
156// Returns true if the contents of the two text files given are equal, false
157// otherwise. This routine treats "\r\n" and "\n" as equivalent.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900158BASE_EXPORT bool TextContentsEqual(const base::FilePath& filename1,
159 const base::FilePath& filename2);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900160
161// Read the file at |path| into |contents|, returning true on success.
cpu@chromium.org93db53b2013-01-09 09:38:59 +0900162// This function fails if the |path| contains path traversal components ('..').
grt@chromium.org683b29f2011-10-15 05:48:05 +0900163// |contents| may be NULL, in which case this function is useful for its
164// side effect of priming the disk cache.
165// Useful for unit tests.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900166BASE_EXPORT bool ReadFileToString(const base::FilePath& path,
167 std::string* contents);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900168
169#if defined(OS_POSIX)
170// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
171// in |buffer|. This function is protected against EINTR and partial reads.
172// Returns true iff |bytes| bytes have been successfuly read from |fd|.
173BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes);
174
175// Creates a symbolic link at |symlink| pointing to |target|. Returns
176// false on failure.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900177BASE_EXPORT bool CreateSymbolicLink(const base::FilePath& target,
178 const base::FilePath& symlink);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900179
180// Reads the given |symlink| and returns where it points to in |target|.
181// Returns false upon failure.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900182BASE_EXPORT bool ReadSymbolicLink(const base::FilePath& symlink,
183 base::FilePath* target);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900184
185// Bits ans masks of the file permission.
186enum FilePermissionBits {
187 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO,
188 FILE_PERMISSION_USER_MASK = S_IRWXU,
189 FILE_PERMISSION_GROUP_MASK = S_IRWXG,
190 FILE_PERMISSION_OTHERS_MASK = S_IRWXO,
191
192 FILE_PERMISSION_READ_BY_USER = S_IRUSR,
193 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR,
194 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR,
195 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP,
196 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP,
197 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP,
198 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH,
199 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH,
200 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH,
201};
202
203// Reads the permission of the given |path|, storing the file permission
204// bits in |mode|. If |path| is symbolic link, |mode| is the permission of
205// a file which the symlink points to.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900206BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900207 int* mode);
208// Sets the permission of the given |path|. If |path| is symbolic link, sets
209// the permission of a file which the symlink points to.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900210BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900211 int mode);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900212#endif // defined(OS_POSIX)
213
214#if defined(OS_WIN)
grt@chromium.org683b29f2011-10-15 05:48:05 +0900215// Copy from_path to to_path recursively and then delete from_path recursively.
216// Returns true if all operations succeed.
217// This function simulates Move(), but unlike Move() it works across volumes.
218// This fuction is not transactional.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900219BASE_EXPORT bool CopyAndDeleteDirectory(const base::FilePath& from_path,
220 const base::FilePath& to_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900221#endif // defined(OS_WIN)
222
223// Return true if the given directory is empty
brettw@chromium.orga7086942013-02-02 14:12:33 +0900224BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900225
226// Get the temporary directory provided by the system.
227// WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
228// the functions below.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900229BASE_EXPORT bool GetTempDir(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900230// Get a temporary directory for shared memory files.
231// Only useful on POSIX; redirects to GetTempDir() on Windows.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900232BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900233
234// Get the home directory. This is more complicated than just getenv("HOME")
235// as it knows to fall back on getpwent() etc.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900236BASE_EXPORT base::FilePath GetHomeDir();
grt@chromium.org683b29f2011-10-15 05:48:05 +0900237
238// Creates a temporary file. The full path is placed in |path|, and the
239// function returns true if was successful in creating the file. The file will
240// be empty and all handles closed after this function returns.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900241BASE_EXPORT bool CreateTemporaryFile(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900242
243// Same as CreateTemporaryFile but the file is created in |dir|.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900244BASE_EXPORT bool CreateTemporaryFileInDir(const base::FilePath& dir,
245 base::FilePath* temp_file);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900246
247// Create and open a temporary file. File is opened for read/write.
248// The full path is placed in |path|.
249// Returns a handle to the opened file or NULL if an error occured.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900250BASE_EXPORT FILE* CreateAndOpenTemporaryFile(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900251// Like above but for shmem files. Only useful for POSIX.
mcgrathr@chromium.org569a4232011-12-07 03:07:05 +0900252// The executable flag says the file needs to support using
253// mprotect with PROT_EXEC after mapping.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900254BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(base::FilePath* path,
mcgrathr@chromium.org569a4232011-12-07 03:07:05 +0900255 bool executable);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900256// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900257BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const base::FilePath& dir,
258 base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900259
260// Create a new directory. If prefix is provided, the new directory name is in
261// the format of prefixyyyy.
262// NOTE: prefix is ignored in the POSIX implementation.
263// If success, return true and output the full path of the directory created.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900264BASE_EXPORT bool CreateNewTempDirectory(
265 const base::FilePath::StringType& prefix,
266 base::FilePath* new_temp_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900267
268// Create a directory within another directory.
269// Extra characters will be appended to |prefix| to ensure that the
270// new directory does not have the same name as an existing directory.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900271BASE_EXPORT bool CreateTemporaryDirInDir(
272 const base::FilePath& base_dir,
273 const base::FilePath::StringType& prefix,
274 base::FilePath* new_dir);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900275
276// Creates a directory, as well as creating any parent directories, if they
277// don't exist. Returns 'true' on successful creation, or if the directory
278// already exists. The directory is only readable by the current user.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900279BASE_EXPORT bool CreateDirectory(const base::FilePath& full_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900280
281// Returns the file size. Returns true on success.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900282BASE_EXPORT bool GetFileSize(const base::FilePath& file_path, int64* file_size);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900283
284// Returns true if the given path's base name is ".".
brettw@chromium.orga7086942013-02-02 14:12:33 +0900285BASE_EXPORT bool IsDot(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900286
287// Returns true if the given path's base name is "..".
brettw@chromium.orga7086942013-02-02 14:12:33 +0900288BASE_EXPORT bool IsDotDot(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900289
290// Sets |real_path| to |path| with symbolic links and junctions expanded.
291// On windows, make sure the path starts with a lettered drive.
292// |path| must reference a file. Function will fail if |path| points to
293// a directory or to a nonexistent path. On windows, this function will
294// fail if |path| is a junction or symlink that points to an empty file,
295// or if |real_path| would be longer than MAX_PATH characters.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900296BASE_EXPORT bool NormalizeFilePath(const base::FilePath& path,
297 base::FilePath* real_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900298
299#if defined(OS_WIN)
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900300
301// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
302// return in |drive_letter_path| the equivalent path that starts with
303// a drive letter ("C:\..."). Return false if no such path exists.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900304BASE_EXPORT bool DevicePathToDriveLetterPath(const base::FilePath& device_path,
305 base::FilePath* drive_letter_path);
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900306
307// Given an existing file in |path|, set |real_path| to the path
308// in native NT format, of the form "\Device\HarddiskVolumeXX\..".
309// Returns false if the path can not be found. Empty files cannot
310// be resolved with this function.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900311BASE_EXPORT bool NormalizeToNativeFilePath(const base::FilePath& path,
312 base::FilePath* nt_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900313#endif
314
315// This function will return if the given file is a symlink or not.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900316BASE_EXPORT bool IsLink(const base::FilePath& file_path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900317
318// Returns information about the given file path.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900319BASE_EXPORT bool GetFileInfo(const base::FilePath& file_path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900320 base::PlatformFileInfo* info);
321
322// Sets the time of the last access and the time of the last modification.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900323BASE_EXPORT bool TouchFile(const base::FilePath& path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900324 const base::Time& last_accessed,
325 const base::Time& last_modified);
326
327// Set the time of the last modification. Useful for unit tests.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900328BASE_EXPORT bool SetLastModifiedTime(const base::FilePath& path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900329 const base::Time& last_modified);
330
331#if defined(OS_POSIX)
332// Store inode number of |path| in |inode|. Return true on success.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900333BASE_EXPORT bool GetInode(const base::FilePath& path, ino_t* inode);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900334#endif
335
336// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900337BASE_EXPORT FILE* OpenFile(const base::FilePath& filename, const char* mode);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900338
339// Closes file opened by OpenFile. Returns true on success.
340BASE_EXPORT bool CloseFile(FILE* file);
341
342// Truncates an open file to end at the location of the current file pointer.
343// This is a cross-platform analog to Windows' SetEndOfFile() function.
344BASE_EXPORT bool TruncateFile(FILE* file);
345
346// Reads the given number of bytes from the file into the buffer. Returns
347// the number of read bytes, or -1 on error.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900348BASE_EXPORT int ReadFile(const base::FilePath& filename, char* data, int size);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900349
350// Writes the given buffer into the file, overwriting any data that was
351// previously there. Returns the number of bytes written, or -1 on error.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900352BASE_EXPORT int WriteFile(const base::FilePath& filename, const char* data,
353 int size);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900354#if defined(OS_POSIX)
355// Append the data to |fd|. Does not close |fd| when done.
356BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size);
357#endif
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +0900358// Append the given buffer into the file. Returns the number of bytes written,
359// or -1 on error.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900360BASE_EXPORT int AppendToFile(const base::FilePath& filename,
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +0900361 const char* data, int size);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900362
363// Gets the current working directory for the process.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900364BASE_EXPORT bool GetCurrentDirectory(base::FilePath* path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900365
366// Sets the current working directory for the process.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900367BASE_EXPORT bool SetCurrentDirectory(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900368
jam@chromium.orgc693fe32012-02-01 08:16:39 +0900369// Attempts to find a number that can be appended to the |path| to make it
370// unique. If |path| does not exist, 0 is returned. If it fails to find such
371// a number, -1 is returned. If |suffix| is not empty, also checks the
372// existence of it with the given suffix.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900373BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path,
374 const base::FilePath::StringType& suffix);
jam@chromium.orgc693fe32012-02-01 08:16:39 +0900375
grt@chromium.org683b29f2011-10-15 05:48:05 +0900376#if defined(OS_POSIX)
jeremya@chromium.org05609662013-04-04 18:05:21 +0900377// Creates a directory with a guaranteed unique name based on |path|, returning
378// the pathname if successful, or an empty path if there was an error creating
379// the directory. Does not create parent directories.
380BASE_EXPORT base::FilePath MakeUniqueDirectory(const base::FilePath& path);
381#endif
382
383#if defined(OS_POSIX)
skerner@chromium.org80784142011-10-18 06:30:29 +0900384// Test that |path| can only be changed by a given user and members of
385// a given set of groups.
grt@chromium.org683b29f2011-10-15 05:48:05 +0900386// Specifically, test that all parts of |path| under (and including) |base|:
387// * Exist.
skerner@chromium.org80784142011-10-18 06:30:29 +0900388// * Are owned by a specific user.
grt@chromium.org683b29f2011-10-15 05:48:05 +0900389// * Are not writable by all users.
skerner@chromium.org80784142011-10-18 06:30:29 +0900390// * Are owned by a memeber of a given set of groups, or are not writable by
391// their group.
grt@chromium.org683b29f2011-10-15 05:48:05 +0900392// * Are not symbolic links.
393// This is useful for checking that a config file is administrator-controlled.
394// |base| must contain |path|.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900395BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base,
396 const base::FilePath& path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900397 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900398 const std::set<gid_t>& group_gids);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900399#endif // defined(OS_POSIX)
400
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900401#if defined(OS_MACOSX) && !defined(OS_IOS)
grt@chromium.org683b29f2011-10-15 05:48:05 +0900402// Is |path| writable only by a user with administrator privileges?
403// This function uses Mac OS conventions. The super user is assumed to have
404// uid 0, and the administrator group is assumed to be named "admin".
405// Testing that |path|, and every parent directory including the root of
406// the filesystem, are owned by the superuser, controlled by the group
407// "admin", are not writable by all users, and contain no symbolic links.
408// Will return false if |path| does not exist.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900409BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path);
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900410#endif // defined(OS_MACOSX) && !defined(OS_IOS)
grt@chromium.org683b29f2011-10-15 05:48:05 +0900411
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900412// Returns the maximum length of path component on the volume containing
413// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
414BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path);
415
grt@chromium.org683b29f2011-10-15 05:48:05 +0900416// A class to handle auto-closing of FILE*'s.
417class ScopedFILEClose {
418 public:
419 inline void operator()(FILE* x) const {
420 if (x) {
421 fclose(x);
422 }
423 }
424};
425
426typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE;
427
428#if defined(OS_POSIX)
429// A class to handle auto-closing of FDs.
430class ScopedFDClose {
431 public:
432 inline void operator()(int* x) const {
433 if (x && *x >= 0) {
434 if (HANDLE_EINTR(close(*x)) < 0)
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900435 DPLOG(ERROR) << "close";
grt@chromium.org683b29f2011-10-15 05:48:05 +0900436 }
437 }
438};
439
440typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
441#endif // OS_POSIX
442
443// A class for enumerating the files in a provided path. The order of the
444// results is not guaranteed.
445//
446// DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
447// program where latency does not matter. This class is blocking.
448class BASE_EXPORT FileEnumerator {
449 public:
450#if defined(OS_WIN)
451 typedef WIN32_FIND_DATA FindInfo;
452#elif defined(OS_POSIX)
453 typedef struct {
454 struct stat stat;
455 std::string filename;
456 } FindInfo;
457#endif
458
459 enum FileType {
460 FILES = 1 << 0,
461 DIRECTORIES = 1 << 1,
462 INCLUDE_DOT_DOT = 1 << 2,
463#if defined(OS_POSIX)
464 SHOW_SYM_LINKS = 1 << 4,
465#endif
466 };
467
468 // |root_path| is the starting directory to search for. It may or may not end
469 // in a slash.
470 //
471 // If |recursive| is true, this will enumerate all matches in any
472 // subdirectories matched as well. It does a breadth-first search, so all
473 // files in one directory will be returned before any files in a
474 // subdirectory.
475 //
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900476 // |file_type|, a bit mask of FileType, specifies whether the enumerator
477 // should match files, directories, or both.
grt@chromium.org683b29f2011-10-15 05:48:05 +0900478 //
479 // |pattern| is an optional pattern for which files to match. This
480 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
481 // However, be careful in specifying patterns that aren't cross platform
482 // since the underlying code uses OS-specific matching routines. In general,
483 // Windows matching is less featureful than others, so test there first.
484 // If unspecified, this will match all files.
485 // NOTE: the pattern only matches the contents of root_path, not files in
486 // recursive subdirectories.
487 // TODO(erikkay): Fix the pattern matching to work at all levels.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900488 FileEnumerator(const base::FilePath& root_path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900489 bool recursive,
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900490 int file_type);
brettw@chromium.orga7086942013-02-02 14:12:33 +0900491 FileEnumerator(const base::FilePath& root_path,
grt@chromium.org683b29f2011-10-15 05:48:05 +0900492 bool recursive,
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900493 int file_type,
brettw@chromium.orga7086942013-02-02 14:12:33 +0900494 const base::FilePath::StringType& pattern);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900495 ~FileEnumerator();
496
497 // Returns an empty string if there are no more results.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900498 base::FilePath Next();
grt@chromium.org683b29f2011-10-15 05:48:05 +0900499
500 // Write the file info into |info|.
501 void GetFindInfo(FindInfo* info);
502
503 // Looks inside a FindInfo and determines if it's a directory.
504 static bool IsDirectory(const FindInfo& info);
505
brettw@chromium.orga7086942013-02-02 14:12:33 +0900506 static base::FilePath GetFilename(const FindInfo& find_info);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900507 static int64 GetFilesize(const FindInfo& find_info);
508 static base::Time GetLastModifiedTime(const FindInfo& find_info);
509
510 private:
511 // Returns true if the given path should be skipped in enumeration.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900512 bool ShouldSkip(const base::FilePath& path);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900513
514
515#if defined(OS_WIN)
516 // True when find_data_ is valid.
517 bool has_find_data_;
518 WIN32_FIND_DATA find_data_;
519 HANDLE find_handle_;
520#elif defined(OS_POSIX)
521 struct DirectoryEntryInfo {
brettw@chromium.orga7086942013-02-02 14:12:33 +0900522 base::FilePath filename;
grt@chromium.org683b29f2011-10-15 05:48:05 +0900523 struct stat stat;
524 };
525
526 // Read the filenames in source into the vector of DirectoryEntryInfo's
527 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries,
brettw@chromium.orga7086942013-02-02 14:12:33 +0900528 const base::FilePath& source, bool show_links);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900529
530 // The files in the current directory
531 std::vector<DirectoryEntryInfo> directory_entries_;
532
533 // The next entry to use from the directory_entries_ vector
534 size_t current_directory_entry_;
535#endif
536
brettw@chromium.orga7086942013-02-02 14:12:33 +0900537 base::FilePath root_path_;
grt@chromium.org683b29f2011-10-15 05:48:05 +0900538 bool recursive_;
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900539 int file_type_;
brettw@chromium.orga7086942013-02-02 14:12:33 +0900540 base::FilePath::StringType pattern_; // Empty when we want to find
541 // everything.
grt@chromium.org683b29f2011-10-15 05:48:05 +0900542
543 // A stack that keeps track of which subdirectories we still need to
544 // enumerate in the breadth-first search.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900545 std::stack<base::FilePath> pending_paths_;
grt@chromium.org683b29f2011-10-15 05:48:05 +0900546
547 DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
548};
549
grt@chromium.org683b29f2011-10-15 05:48:05 +0900550#if defined(OS_LINUX)
551// Broad categories of file systems as returned by statfs() on Linux.
552enum FileSystemType {
553 FILE_SYSTEM_UNKNOWN, // statfs failed.
554 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
555 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
556 FILE_SYSTEM_NFS,
557 FILE_SYSTEM_SMB,
558 FILE_SYSTEM_CODA,
559 FILE_SYSTEM_MEMORY, // in-memory file system
560 FILE_SYSTEM_CGROUP, // cgroup control.
561 FILE_SYSTEM_OTHER, // any other value.
562 FILE_SYSTEM_TYPE_COUNT
563};
564
565// Attempts determine the FileSystemType for |path|.
566// Returns false if |path| doesn't exist.
brettw@chromium.orga7086942013-02-02 14:12:33 +0900567BASE_EXPORT bool GetFileSystemType(const base::FilePath& path,
568 FileSystemType* type);
grt@chromium.org683b29f2011-10-15 05:48:05 +0900569#endif
570
571} // namespace file_util
572
grt@chromium.org683b29f2011-10-15 05:48:05 +0900573#endif // BASE_FILE_UTIL_H_