skerner@chromium.org | d05e16c | 2012-01-18 07:44:31 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 2 | // 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.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 10 | |
| 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.org | 8078414 | 2011-10-18 06:30:29 +0900 | [diff] [blame] | 22 | #include <set> |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "base/base_export.h" |
| 27 | #include "base/basictypes.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 28 | #include "base/files/file_path.h" |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 29 | #include "base/memory/scoped_ptr.h" |
| 30 | #include "base/platform_file.h" |
avi@chromium.org | 94bd573 | 2013-06-11 22:36:37 +0900 | [diff] [blame] | 31 | #include "base/strings/string16.h" |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 32 | |
| 33 | #if defined(OS_POSIX) |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 34 | #include "base/file_descriptor_posix.h" |
| 35 | #include "base/logging.h" |
brettw@chromium.org | b1788fb | 2012-11-15 05:54:35 +0900 | [diff] [blame] | 36 | #include "base/posix/eintr_wrapper.h" |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | namespace base { |
brettw@chromium.org | 55d5ee6 | 2013-06-23 07:15:46 +0900 | [diff] [blame] | 40 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 41 | class Time; |
brettw@chromium.org | 99b198e | 2013-04-12 14:17:15 +0900 | [diff] [blame] | 42 | |
aa@chromium.org | 8dac1e4 | 2012-02-05 12:22:38 +0900 | [diff] [blame] | 43 | extern bool g_bug108724_debug; |
| 44 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 45 | //----------------------------------------------------------------------------- |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 46 | // Functions that involve filesystem access or modification: |
| 47 | |
brettw@chromium.org | 55d5ee6 | 2013-06-23 07:15:46 +0900 | [diff] [blame] | 48 | // Returns an absolute version of a relative path. Returns an empty path on |
| 49 | // error. On POSIX, this function fails if the path does not exist. This |
| 50 | // function can result in I/O so it can be slow. |
| 51 | BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input); |
| 52 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 53 | // Returns the total number of bytes used by all the files under |root_path|. |
| 54 | // If the path does not exist the function returns 0. |
| 55 | // |
| 56 | // This function is implemented using the FileEnumerator class so it is not |
| 57 | // particularly speedy in any platform. |
brettw@chromium.org | 55d5ee6 | 2013-06-23 07:15:46 +0900 | [diff] [blame] | 58 | BASE_EXPORT int64 ComputeDirectorySize(const FilePath& root_path); |
| 59 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 60 | // Deletes the given path, whether it's a file or a directory. |
| 61 | // If it's a directory, it's perfectly happy to delete all of the |
| 62 | // directory's contents. Passing true to recursive deletes |
| 63 | // subdirectories and their contents as well. |
gavinp@chromium.org | e5281ba | 2013-04-12 07:50:23 +0900 | [diff] [blame] | 64 | // Returns true if successful, false otherwise. It is considered successful |
| 65 | // to attempt to delete a file that does not exist. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 66 | // |
yoshiki@chromium.org | 45cbd63 | 2012-06-30 14:26:59 +0900 | [diff] [blame] | 67 | // In posix environment and if |path| is a symbolic link, this deletes only |
yoshiki@chromium.org | 670a38f | 2012-07-11 10:24:02 +0900 | [diff] [blame] | 68 | // the symlink. (even if the symlink points to a non-existent file) |
yoshiki@chromium.org | 45cbd63 | 2012-06-30 14:26:59 +0900 | [diff] [blame] | 69 | // |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 70 | // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
| 71 | // TO "rm -rf", SO USE WITH CAUTION. |
brettw@chromium.org | e9f9948 | 2013-07-02 04:41:02 +0900 | [diff] [blame] | 72 | BASE_EXPORT bool Delete(const FilePath& path, bool recursive); |
| 73 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 74 | #if defined(OS_WIN) |
| 75 | // Schedules to delete the given path, whether it's a file or a directory, until |
| 76 | // the operating system is restarted. |
| 77 | // Note: |
| 78 | // 1) The file/directory to be deleted should exist in a temp folder. |
| 79 | // 2) The directory to be deleted must be empty. |
brettw@chromium.org | 0878fea | 2013-07-02 08:07:36 +0900 | [diff] [blame] | 80 | BASE_EXPORT bool DeleteAfterReboot(const FilePath& path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | // Moves the given path, whether it's a file or a directory. |
| 84 | // If a simple rename is not possible, such as in the case where the paths are |
| 85 | // on different volumes, this will attempt to copy and delete. Returns |
| 86 | // true for success. |
cevans@chromium.org | 007dbe2 | 2013-02-07 05:38:07 +0900 | [diff] [blame] | 87 | // This function fails if either path contains traversal components ('..'). |
brettw@chromium.org | 0878fea | 2013-07-02 08:07:36 +0900 | [diff] [blame] | 88 | BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 89 | |
| 90 | // Renames file |from_path| to |to_path|. Both paths must be on the same |
| 91 | // volume, or the function will fail. Destination file will be created |
| 92 | // if it doesn't exist. Prefer this function over Move when dealing with |
| 93 | // temporary files. On Windows it preserves attributes of the target file. |
dgrogan@chromium.org | 38fc56d | 2013-05-09 07:02:36 +0900 | [diff] [blame] | 94 | // Returns true on success, leaving *error unchanged. |
| 95 | // Returns false on failure and sets *error appropriately, if it is non-NULL. |
brettw@chromium.org | 0878fea | 2013-07-02 08:07:36 +0900 | [diff] [blame] | 96 | BASE_EXPORT bool ReplaceFile(const FilePath& from_path, |
| 97 | const FilePath& to_path, |
| 98 | PlatformFileError* error); |
dgrogan@chromium.org | 38fc56d | 2013-05-09 07:02:36 +0900 | [diff] [blame] | 99 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 100 | // Copies a single file. Use CopyDirectory to copy directories. |
cevans@chromium.org | 007dbe2 | 2013-02-07 05:38:07 +0900 | [diff] [blame] | 101 | // This function fails if either path contains traversal components ('..'). |
brettw@chromium.org | aecf7a3 | 2013-07-10 02:42:26 +0900 | [diff] [blame] | 102 | BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path); |
cevans@chromium.org | 007dbe2 | 2013-02-07 05:38:07 +0900 | [diff] [blame] | 103 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 104 | // Copies the given path, and optionally all subdirectories and their contents |
| 105 | // as well. |
brettw@chromium.org | 5694672 | 2013-06-08 13:53:36 +0900 | [diff] [blame] | 106 | // |
| 107 | // If there are files existing under to_path, always overwrite. Returns true |
| 108 | // if successful, false otherwise. Wildcards on the names are not supported. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 109 | // |
| 110 | // If you only need to copy a file use CopyFile, it's faster. |
brettw@chromium.org | aecf7a3 | 2013-07-10 02:42:26 +0900 | [diff] [blame] | 111 | BASE_EXPORT bool CopyDirectory(const FilePath& from_path, |
| 112 | const FilePath& to_path, |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 113 | bool recursive); |
| 114 | |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 115 | // Returns true if the given path exists on the local filesystem, |
| 116 | // false otherwise. |
| 117 | BASE_EXPORT bool PathExists(const FilePath& path); |
| 118 | |
brettw@chromium.org | aecf7a3 | 2013-07-10 02:42:26 +0900 | [diff] [blame] | 119 | } // namespace base |
| 120 | |
| 121 | // ----------------------------------------------------------------------------- |
| 122 | |
| 123 | namespace file_util { |
| 124 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 125 | // Returns true if the given path is writable by the user, false otherwise. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 126 | BASE_EXPORT bool PathIsWritable(const base::FilePath& path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 127 | |
| 128 | // Returns true if the given path exists and is a directory, false otherwise. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 129 | BASE_EXPORT bool DirectoryExists(const base::FilePath& path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 130 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 131 | // Returns true if the contents of the two files given are equal, false |
| 132 | // otherwise. If either file can't be read, returns false. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 133 | BASE_EXPORT bool ContentsEqual(const base::FilePath& filename1, |
| 134 | const base::FilePath& filename2); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 135 | |
| 136 | // Returns true if the contents of the two text files given are equal, false |
| 137 | // otherwise. This routine treats "\r\n" and "\n" as equivalent. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 138 | BASE_EXPORT bool TextContentsEqual(const base::FilePath& filename1, |
| 139 | const base::FilePath& filename2); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 140 | |
| 141 | // Read the file at |path| into |contents|, returning true on success. |
cpu@chromium.org | 93db53b | 2013-01-09 09:38:59 +0900 | [diff] [blame] | 142 | // This function fails if the |path| contains path traversal components ('..'). |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 143 | // |contents| may be NULL, in which case this function is useful for its |
| 144 | // side effect of priming the disk cache. |
| 145 | // Useful for unit tests. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 146 | BASE_EXPORT bool ReadFileToString(const base::FilePath& path, |
| 147 | std::string* contents); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 148 | |
| 149 | #if defined(OS_POSIX) |
| 150 | // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
| 151 | // in |buffer|. This function is protected against EINTR and partial reads. |
| 152 | // Returns true iff |bytes| bytes have been successfuly read from |fd|. |
| 153 | BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
| 154 | |
| 155 | // Creates a symbolic link at |symlink| pointing to |target|. Returns |
| 156 | // false on failure. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 157 | BASE_EXPORT bool CreateSymbolicLink(const base::FilePath& target, |
| 158 | const base::FilePath& symlink); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 159 | |
| 160 | // Reads the given |symlink| and returns where it points to in |target|. |
| 161 | // Returns false upon failure. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 162 | BASE_EXPORT bool ReadSymbolicLink(const base::FilePath& symlink, |
| 163 | base::FilePath* target); |
yoshiki@chromium.org | 670a38f | 2012-07-11 10:24:02 +0900 | [diff] [blame] | 164 | |
| 165 | // Bits ans masks of the file permission. |
| 166 | enum FilePermissionBits { |
| 167 | FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, |
| 168 | FILE_PERMISSION_USER_MASK = S_IRWXU, |
| 169 | FILE_PERMISSION_GROUP_MASK = S_IRWXG, |
| 170 | FILE_PERMISSION_OTHERS_MASK = S_IRWXO, |
| 171 | |
| 172 | FILE_PERMISSION_READ_BY_USER = S_IRUSR, |
| 173 | FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, |
| 174 | FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, |
| 175 | FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, |
| 176 | FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, |
| 177 | FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, |
| 178 | FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, |
| 179 | FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, |
| 180 | FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, |
| 181 | }; |
| 182 | |
| 183 | // Reads the permission of the given |path|, storing the file permission |
| 184 | // bits in |mode|. If |path| is symbolic link, |mode| is the permission of |
| 185 | // a file which the symlink points to. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 186 | BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path, |
yoshiki@chromium.org | 670a38f | 2012-07-11 10:24:02 +0900 | [diff] [blame] | 187 | int* mode); |
| 188 | // Sets the permission of the given |path|. If |path| is symbolic link, sets |
| 189 | // the permission of a file which the symlink points to. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 190 | BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path, |
yoshiki@chromium.org | 670a38f | 2012-07-11 10:24:02 +0900 | [diff] [blame] | 191 | int mode); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 192 | #endif // defined(OS_POSIX) |
| 193 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 194 | // Return true if the given directory is empty |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 195 | BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 196 | |
| 197 | // Get the temporary directory provided by the system. |
| 198 | // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of |
| 199 | // the functions below. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 200 | BASE_EXPORT bool GetTempDir(base::FilePath* path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 201 | // Get a temporary directory for shared memory files. |
| 202 | // Only useful on POSIX; redirects to GetTempDir() on Windows. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 203 | BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 204 | |
| 205 | // Get the home directory. This is more complicated than just getenv("HOME") |
| 206 | // as it knows to fall back on getpwent() etc. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 207 | BASE_EXPORT base::FilePath GetHomeDir(); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 208 | |
| 209 | // Creates a temporary file. The full path is placed in |path|, and the |
| 210 | // function returns true if was successful in creating the file. The file will |
| 211 | // be empty and all handles closed after this function returns. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 212 | BASE_EXPORT bool CreateTemporaryFile(base::FilePath* path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 213 | |
| 214 | // Same as CreateTemporaryFile but the file is created in |dir|. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 215 | BASE_EXPORT bool CreateTemporaryFileInDir(const base::FilePath& dir, |
| 216 | base::FilePath* temp_file); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 217 | |
| 218 | // Create and open a temporary file. File is opened for read/write. |
| 219 | // The full path is placed in |path|. |
| 220 | // Returns a handle to the opened file or NULL if an error occured. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 221 | BASE_EXPORT FILE* CreateAndOpenTemporaryFile(base::FilePath* path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 222 | // Like above but for shmem files. Only useful for POSIX. |
mcgrathr@chromium.org | 569a423 | 2011-12-07 03:07:05 +0900 | [diff] [blame] | 223 | // The executable flag says the file needs to support using |
| 224 | // mprotect with PROT_EXEC after mapping. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 225 | BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(base::FilePath* path, |
mcgrathr@chromium.org | 569a423 | 2011-12-07 03:07:05 +0900 | [diff] [blame] | 226 | bool executable); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 227 | // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 228 | BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const base::FilePath& dir, |
| 229 | base::FilePath* path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 230 | |
| 231 | // Create a new directory. If prefix is provided, the new directory name is in |
| 232 | // the format of prefixyyyy. |
| 233 | // NOTE: prefix is ignored in the POSIX implementation. |
| 234 | // If success, return true and output the full path of the directory created. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 235 | BASE_EXPORT bool CreateNewTempDirectory( |
| 236 | const base::FilePath::StringType& prefix, |
| 237 | base::FilePath* new_temp_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 238 | |
| 239 | // Create a directory within another directory. |
| 240 | // Extra characters will be appended to |prefix| to ensure that the |
| 241 | // new directory does not have the same name as an existing directory. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 242 | BASE_EXPORT bool CreateTemporaryDirInDir( |
| 243 | const base::FilePath& base_dir, |
| 244 | const base::FilePath::StringType& prefix, |
| 245 | base::FilePath* new_dir); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 246 | |
| 247 | // Creates a directory, as well as creating any parent directories, if they |
| 248 | // don't exist. Returns 'true' on successful creation, or if the directory |
| 249 | // already exists. The directory is only readable by the current user. |
dgrogan@chromium.org | f772813 | 2013-06-11 12:50:25 +0900 | [diff] [blame] | 250 | // Returns true on success, leaving *error unchanged. |
| 251 | // Returns false on failure and sets *error appropriately, if it is non-NULL. |
| 252 | BASE_EXPORT bool CreateDirectoryAndGetError(const base::FilePath& full_path, |
| 253 | base::PlatformFileError* error); |
| 254 | |
| 255 | // Backward-compatible convenience method for the above. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 256 | BASE_EXPORT bool CreateDirectory(const base::FilePath& full_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 257 | |
| 258 | // Returns the file size. Returns true on success. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 259 | BASE_EXPORT bool GetFileSize(const base::FilePath& file_path, int64* file_size); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 260 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 261 | // Sets |real_path| to |path| with symbolic links and junctions expanded. |
| 262 | // On windows, make sure the path starts with a lettered drive. |
| 263 | // |path| must reference a file. Function will fail if |path| points to |
| 264 | // a directory or to a nonexistent path. On windows, this function will |
| 265 | // fail if |path| is a junction or symlink that points to an empty file, |
| 266 | // or if |real_path| would be longer than MAX_PATH characters. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 267 | BASE_EXPORT bool NormalizeFilePath(const base::FilePath& path, |
| 268 | base::FilePath* real_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 269 | |
| 270 | #if defined(OS_WIN) |
skerner@chromium.org | d05e16c | 2012-01-18 07:44:31 +0900 | [diff] [blame] | 271 | |
| 272 | // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."), |
| 273 | // return in |drive_letter_path| the equivalent path that starts with |
| 274 | // a drive letter ("C:\..."). Return false if no such path exists. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 275 | BASE_EXPORT bool DevicePathToDriveLetterPath(const base::FilePath& device_path, |
| 276 | base::FilePath* drive_letter_path); |
skerner@chromium.org | d05e16c | 2012-01-18 07:44:31 +0900 | [diff] [blame] | 277 | |
| 278 | // Given an existing file in |path|, set |real_path| to the path |
| 279 | // in native NT format, of the form "\Device\HarddiskVolumeXX\..". |
| 280 | // Returns false if the path can not be found. Empty files cannot |
| 281 | // be resolved with this function. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 282 | BASE_EXPORT bool NormalizeToNativeFilePath(const base::FilePath& path, |
| 283 | base::FilePath* nt_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 284 | #endif |
| 285 | |
| 286 | // This function will return if the given file is a symlink or not. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 287 | BASE_EXPORT bool IsLink(const base::FilePath& file_path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 288 | |
| 289 | // Returns information about the given file path. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 290 | BASE_EXPORT bool GetFileInfo(const base::FilePath& file_path, |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 291 | base::PlatformFileInfo* info); |
| 292 | |
| 293 | // Sets the time of the last access and the time of the last modification. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 294 | BASE_EXPORT bool TouchFile(const base::FilePath& path, |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 295 | const base::Time& last_accessed, |
| 296 | const base::Time& last_modified); |
| 297 | |
| 298 | // Set the time of the last modification. Useful for unit tests. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 299 | BASE_EXPORT bool SetLastModifiedTime(const base::FilePath& path, |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 300 | const base::Time& last_modified); |
| 301 | |
| 302 | #if defined(OS_POSIX) |
| 303 | // Store inode number of |path| in |inode|. Return true on success. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 304 | BASE_EXPORT bool GetInode(const base::FilePath& path, ino_t* inode); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 305 | #endif |
| 306 | |
| 307 | // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 308 | BASE_EXPORT FILE* OpenFile(const base::FilePath& filename, const char* mode); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 309 | |
| 310 | // Closes file opened by OpenFile. Returns true on success. |
| 311 | BASE_EXPORT bool CloseFile(FILE* file); |
| 312 | |
| 313 | // Truncates an open file to end at the location of the current file pointer. |
| 314 | // This is a cross-platform analog to Windows' SetEndOfFile() function. |
| 315 | BASE_EXPORT bool TruncateFile(FILE* file); |
| 316 | |
| 317 | // Reads the given number of bytes from the file into the buffer. Returns |
| 318 | // the number of read bytes, or -1 on error. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 319 | BASE_EXPORT int ReadFile(const base::FilePath& filename, char* data, int size); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 320 | |
| 321 | // Writes the given buffer into the file, overwriting any data that was |
| 322 | // previously there. Returns the number of bytes written, or -1 on error. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 323 | BASE_EXPORT int WriteFile(const base::FilePath& filename, const char* data, |
| 324 | int size); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 325 | #if defined(OS_POSIX) |
| 326 | // Append the data to |fd|. Does not close |fd| when done. |
| 327 | BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size); |
| 328 | #endif |
loislo@chromium.org | eae0dcb | 2012-04-29 21:57:10 +0900 | [diff] [blame] | 329 | // Append the given buffer into the file. Returns the number of bytes written, |
| 330 | // or -1 on error. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 331 | BASE_EXPORT int AppendToFile(const base::FilePath& filename, |
loislo@chromium.org | eae0dcb | 2012-04-29 21:57:10 +0900 | [diff] [blame] | 332 | const char* data, int size); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 333 | |
| 334 | // Gets the current working directory for the process. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 335 | BASE_EXPORT bool GetCurrentDirectory(base::FilePath* path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 336 | |
| 337 | // Sets the current working directory for the process. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 338 | BASE_EXPORT bool SetCurrentDirectory(const base::FilePath& path); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 339 | |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 340 | // Attempts to find a number that can be appended to the |path| to make it |
| 341 | // unique. If |path| does not exist, 0 is returned. If it fails to find such |
| 342 | // a number, -1 is returned. If |suffix| is not empty, also checks the |
| 343 | // existence of it with the given suffix. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 344 | BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path, |
| 345 | const base::FilePath::StringType& suffix); |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 346 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 347 | #if defined(OS_POSIX) |
jeremya@chromium.org | 0560966 | 2013-04-04 18:05:21 +0900 | [diff] [blame] | 348 | // Creates a directory with a guaranteed unique name based on |path|, returning |
| 349 | // the pathname if successful, or an empty path if there was an error creating |
| 350 | // the directory. Does not create parent directories. |
| 351 | BASE_EXPORT base::FilePath MakeUniqueDirectory(const base::FilePath& path); |
| 352 | #endif |
| 353 | |
| 354 | #if defined(OS_POSIX) |
skerner@chromium.org | 8078414 | 2011-10-18 06:30:29 +0900 | [diff] [blame] | 355 | // Test that |path| can only be changed by a given user and members of |
| 356 | // a given set of groups. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 357 | // Specifically, test that all parts of |path| under (and including) |base|: |
| 358 | // * Exist. |
skerner@chromium.org | 8078414 | 2011-10-18 06:30:29 +0900 | [diff] [blame] | 359 | // * Are owned by a specific user. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 360 | // * Are not writable by all users. |
skerner@chromium.org | 8078414 | 2011-10-18 06:30:29 +0900 | [diff] [blame] | 361 | // * Are owned by a memeber of a given set of groups, or are not writable by |
| 362 | // their group. |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 363 | // * Are not symbolic links. |
| 364 | // This is useful for checking that a config file is administrator-controlled. |
| 365 | // |base| must contain |path|. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 366 | BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base, |
| 367 | const base::FilePath& path, |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 368 | uid_t owner_uid, |
skerner@chromium.org | 8078414 | 2011-10-18 06:30:29 +0900 | [diff] [blame] | 369 | const std::set<gid_t>& group_gids); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 370 | #endif // defined(OS_POSIX) |
| 371 | |
qsr@chromium.org | 4ab5de9 | 2012-07-09 23:40:39 +0900 | [diff] [blame] | 372 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 373 | // Is |path| writable only by a user with administrator privileges? |
| 374 | // This function uses Mac OS conventions. The super user is assumed to have |
| 375 | // uid 0, and the administrator group is assumed to be named "admin". |
| 376 | // Testing that |path|, and every parent directory including the root of |
| 377 | // the filesystem, are owned by the superuser, controlled by the group |
| 378 | // "admin", are not writable by all users, and contain no symbolic links. |
| 379 | // Will return false if |path| does not exist. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 380 | BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path); |
stuartmorgan@chromium.org | 925e0b7 | 2012-07-24 20:23:32 +0900 | [diff] [blame] | 381 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 382 | |
kinaba@chromium.org | bbe80ba | 2013-02-21 12:24:08 +0900 | [diff] [blame] | 383 | // Returns the maximum length of path component on the volume containing |
| 384 | // the directory |path|, in the number of FilePath::CharType, or -1 on failure. |
| 385 | BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path); |
| 386 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 387 | // A class to handle auto-closing of FILE*'s. |
| 388 | class ScopedFILEClose { |
| 389 | public: |
| 390 | inline void operator()(FILE* x) const { |
| 391 | if (x) { |
| 392 | fclose(x); |
| 393 | } |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE; |
| 398 | |
| 399 | #if defined(OS_POSIX) |
| 400 | // A class to handle auto-closing of FDs. |
| 401 | class ScopedFDClose { |
| 402 | public: |
| 403 | inline void operator()(int* x) const { |
| 404 | if (x && *x >= 0) { |
| 405 | if (HANDLE_EINTR(close(*x)) < 0) |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 406 | DPLOG(ERROR) << "close"; |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | }; |
| 410 | |
| 411 | typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; |
| 412 | #endif // OS_POSIX |
| 413 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 414 | #if defined(OS_LINUX) |
| 415 | // Broad categories of file systems as returned by statfs() on Linux. |
| 416 | enum FileSystemType { |
| 417 | FILE_SYSTEM_UNKNOWN, // statfs failed. |
| 418 | FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
| 419 | FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
| 420 | FILE_SYSTEM_NFS, |
| 421 | FILE_SYSTEM_SMB, |
| 422 | FILE_SYSTEM_CODA, |
| 423 | FILE_SYSTEM_MEMORY, // in-memory file system |
| 424 | FILE_SYSTEM_CGROUP, // cgroup control. |
| 425 | FILE_SYSTEM_OTHER, // any other value. |
| 426 | FILE_SYSTEM_TYPE_COUNT |
| 427 | }; |
| 428 | |
| 429 | // Attempts determine the FileSystemType for |path|. |
| 430 | // Returns false if |path| doesn't exist. |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 431 | BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, |
| 432 | FileSystemType* type); |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 433 | #endif |
| 434 | |
| 435 | } // namespace file_util |
| 436 | |
brettw@chromium.org | aecf7a3 | 2013-07-10 02:42:26 +0900 | [diff] [blame] | 437 | // Internal -------------------------------------------------------------------- |
| 438 | |
| 439 | namespace base { |
| 440 | namespace internal { |
| 441 | |
| 442 | // Same as Move but allows paths with traversal components. |
| 443 | // Use only with extreme care. |
| 444 | BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, |
| 445 | const FilePath& to_path); |
| 446 | |
| 447 | // Same as CopyFile but allows paths with traversal components. |
| 448 | // Use only with extreme care. |
| 449 | BASE_EXPORT bool CopyFileUnsafe(const FilePath& from_path, |
| 450 | const FilePath& to_path); |
| 451 | |
| 452 | #if defined(OS_WIN) |
| 453 | // Copy from_path to to_path recursively and then delete from_path recursively. |
| 454 | // Returns true if all operations succeed. |
| 455 | // This function simulates Move(), but unlike Move() it works across volumes. |
| 456 | // This fuction is not transactional. |
| 457 | BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 458 | const FilePath& to_path); |
| 459 | #endif // defined(OS_WIN) |
| 460 | |
| 461 | } // namespace internal |
| 462 | } // namespace base |
| 463 | |
grt@chromium.org | 683b29f | 2011-10-15 05:48:05 +0900 | [diff] [blame] | 464 | #endif // BASE_FILE_UTIL_H_ |