[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 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. |
| 4 | |
| 5 | // This file contains utility functions for dealing with the local |
| 6 | // filesystem. |
| 7 | |
| 8 | #ifndef BASE_FILES_FILE_UTIL_H_ |
| 9 | #define BASE_FILES_FILE_UTIL_H_ |
| 10 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 15 | #include <limits> |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 20 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 21 | #include <sys/stat.h> |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 25 | #include "base/base_export.h" |
Lei Zhang | e3aa0b9a | 2020-06-11 08:59:23 | [diff] [blame] | 26 | #include "base/callback_forward.h" |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 27 | #include "base/containers/span.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 28 | #include "base/files/file.h" |
| 29 | #include "base/files/file_path.h" |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 30 | #include "base/files/scoped_file.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 31 | #include "base/strings/string16.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 32 | #include "build/build_config.h" |
| 33 | |
| 34 | #if defined(OS_WIN) |
Bruce Dawson | bfdc3fd | 2018-01-03 20:32:36 | [diff] [blame] | 35 | #include "base/win/windows_types.h" |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 36 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 37 | #include "base/file_descriptor_posix.h" |
| 38 | #include "base/logging.h" |
| 39 | #include "base/posix/eintr_wrapper.h" |
| 40 | #endif |
| 41 | |
| 42 | namespace base { |
| 43 | |
thomasanderson | 1929bb2 | 2016-06-24 02:01:00 | [diff] [blame] | 44 | class Environment; |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 45 | class Time; |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | // Functions that involve filesystem access or modification: |
| 49 | |
| 50 | // Returns an absolute version of a relative path. Returns an empty path on |
| 51 | // error. On POSIX, this function fails if the path does not exist. This |
| 52 | // function can result in I/O so it can be slow. |
| 53 | BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input); |
| 54 | |
| 55 | // Returns the total number of bytes used by all the files under |root_path|. |
| 56 | // If the path does not exist the function returns 0. |
| 57 | // |
| 58 | // This function is implemented using the FileEnumerator class so it is not |
| 59 | // particularly speedy in any platform. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 60 | BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 61 | |
| 62 | // Deletes the given path, whether it's a file or a directory. |
Lei Zhang | f9696375 | 2020-06-18 20:34:10 | [diff] [blame^] | 63 | // If it's a directory, it's perfectly happy to delete all of the directory's |
| 64 | // contents, but it will not recursively delete subdirectories and their |
| 65 | // contents. |
| 66 | // Returns true if successful, false otherwise. It is considered successful to |
| 67 | // attempt to delete a file that does not exist. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 68 | // |
Lei Zhang | aeb72b5 | 2019-10-14 22:39:40 | [diff] [blame] | 69 | // In POSIX environment and if |path| is a symbolic link, this deletes only |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 70 | // the symlink. (even if the symlink points to a non-existent file) |
Lei Zhang | f9696375 | 2020-06-18 20:34:10 | [diff] [blame^] | 71 | BASE_EXPORT bool DeleteFile(const FilePath& path); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 72 | |
Lei Zhang | aeb72b5 | 2019-10-14 22:39:40 | [diff] [blame] | 73 | // Deletes the given path, whether it's a file or a directory. |
| 74 | // If it's a directory, it's perfectly happy to delete all of the |
| 75 | // directory's contents, including subdirectories and their contents. |
| 76 | // Returns true if successful, false otherwise. It is considered successful |
| 77 | // to attempt to delete a file that does not exist. |
| 78 | // |
| 79 | // In POSIX environment and if |path| is a symbolic link, this deletes only |
| 80 | // the symlink. (even if the symlink points to a non-existent file) |
| 81 | // |
| 82 | // WARNING: USING THIS EQUIVALENT TO "rm -rf", SO USE WITH CAUTION. |
Lei Zhang | 3190449f7 | 2020-06-12 05:14:04 | [diff] [blame] | 83 | // TODO(thestig): Rename to DeletePathRecursively(). |
Lei Zhang | aeb72b5 | 2019-10-14 22:39:40 | [diff] [blame] | 84 | BASE_EXPORT bool DeleteFileRecursively(const FilePath& path); |
| 85 | |
Lei Zhang | f9696375 | 2020-06-18 20:34:10 | [diff] [blame^] | 86 | // DEPRECATED. Please use the functions immediately above. |
| 87 | // https://crbug.com/1009837 |
| 88 | // |
| 89 | // Deletes the given path, whether it's a file or a directory. |
| 90 | // If it's a directory, it's perfectly happy to delete all of the |
| 91 | // directory's contents. Passing true to recursively delete |
| 92 | // subdirectories and their contents as well. |
| 93 | // Returns true if successful, false otherwise. It is considered successful |
| 94 | // to attempt to delete a file that does not exist. |
| 95 | // |
| 96 | // In POSIX environment and if |path| is a symbolic link, this deletes only |
| 97 | // the symlink. (even if the symlink points to a non-existent file) |
| 98 | // |
| 99 | // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
| 100 | // TO "rm -rf", SO USE WITH CAUTION. |
| 101 | BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); |
| 102 | |
| 103 | // Simplified way to get a callback to do DeleteFile(path) and ignore the |
Lei Zhang | e3aa0b9a | 2020-06-11 08:59:23 | [diff] [blame] | 104 | // DeleteFile() result. |
| 105 | BASE_EXPORT OnceCallback<void(const FilePath&)> GetDeleteFileCallback(); |
| 106 | |
Lei Zhang | 3190449f7 | 2020-06-12 05:14:04 | [diff] [blame] | 107 | // Simplified way to get a callback to do DeleteFileRecursively(path) and ignore |
| 108 | // the DeleteFileRecursively() result. |
| 109 | BASE_EXPORT OnceCallback<void(const FilePath&)> |
| 110 | GetDeletePathRecursivelyCallback(); |
| 111 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 112 | #if defined(OS_WIN) |
| 113 | // Schedules to delete the given path, whether it's a file or a directory, until |
| 114 | // the operating system is restarted. |
| 115 | // Note: |
| 116 | // 1) The file/directory to be deleted should exist in a temp folder. |
| 117 | // 2) The directory to be deleted must be empty. |
| 118 | BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); |
| 119 | #endif |
| 120 | |
| 121 | // Moves the given path, whether it's a file or a directory. |
| 122 | // If a simple rename is not possible, such as in the case where the paths are |
| 123 | // on different volumes, this will attempt to copy and delete. Returns |
| 124 | // true for success. |
| 125 | // This function fails if either path contains traversal components ('..'). |
| 126 | BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); |
| 127 | |
| 128 | // Renames file |from_path| to |to_path|. Both paths must be on the same |
| 129 | // volume, or the function will fail. Destination file will be created |
| 130 | // if it doesn't exist. Prefer this function over Move when dealing with |
| 131 | // temporary files. On Windows it preserves attributes of the target file. |
| 132 | // Returns true on success, leaving *error unchanged. |
| 133 | // Returns false on failure and sets *error appropriately, if it is non-NULL. |
| 134 | BASE_EXPORT bool ReplaceFile(const FilePath& from_path, |
| 135 | const FilePath& to_path, |
| 136 | File::Error* error); |
| 137 | |
Lei Zhang | 6a0e47ca | 2017-10-02 23:02:16 | [diff] [blame] | 138 | // Copies a single file. Use CopyDirectory() to copy directories. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 139 | // This function fails if either path contains traversal components ('..'). |
Lei Zhang | 6a0e47ca | 2017-10-02 23:02:16 | [diff] [blame] | 140 | // This function also fails if |to_path| is a directory. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 141 | // |
Lei Zhang | 6a0e47ca | 2017-10-02 23:02:16 | [diff] [blame] | 142 | // On POSIX, if |to_path| is a symlink, CopyFile() will follow the symlink. This |
| 143 | // may have security implications. Use with care. |
| 144 | // |
| 145 | // If |to_path| already exists and is a regular file, it will be overwritten, |
| 146 | // though its permissions will stay the same. |
| 147 | // |
| 148 | // If |to_path| does not exist, it will be created. The new file's permissions |
| 149 | // varies per platform: |
| 150 | // |
| 151 | // - This function keeps the metadata on Windows. The read only bit is not kept. |
| 152 | // - On Mac and iOS, |to_path| retains |from_path|'s permissions, except user |
| 153 | // read/write permissions are always set. |
| 154 | // - On Linux and Android, |to_path| has user read/write permissions only. i.e. |
| 155 | // Always 0600. |
| 156 | // - On ChromeOS, |to_path| has user read/write permissions and group/others |
| 157 | // read permissions. i.e. Always 0644. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 158 | BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path); |
| 159 | |
| 160 | // Copies the given path, and optionally all subdirectories and their contents |
| 161 | // as well. |
| 162 | // |
| 163 | // If there are files existing under to_path, always overwrite. Returns true |
| 164 | // if successful, false otherwise. Wildcards on the names are not supported. |
| 165 | // |
Eric Caruso | bca21fc58 | 2017-09-28 00:34:34 | [diff] [blame] | 166 | // This function has the same metadata behavior as CopyFile(). |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 167 | // |
| 168 | // If you only need to copy a file use CopyFile, it's faster. |
| 169 | BASE_EXPORT bool CopyDirectory(const FilePath& from_path, |
| 170 | const FilePath& to_path, |
| 171 | bool recursive); |
| 172 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 173 | // Like CopyDirectory() except trying to overwrite an existing file will not |
| 174 | // work and will return false. |
| 175 | BASE_EXPORT bool CopyDirectoryExcl(const FilePath& from_path, |
| 176 | const FilePath& to_path, |
| 177 | bool recursive); |
| 178 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 179 | // Returns true if the given path exists on the local filesystem, |
| 180 | // false otherwise. |
| 181 | BASE_EXPORT bool PathExists(const FilePath& path); |
| 182 | |
| 183 | // Returns true if the given path is writable by the user, false otherwise. |
| 184 | BASE_EXPORT bool PathIsWritable(const FilePath& path); |
| 185 | |
| 186 | // Returns true if the given path exists and is a directory, false otherwise. |
| 187 | BASE_EXPORT bool DirectoryExists(const FilePath& path); |
| 188 | |
| 189 | // Returns true if the contents of the two files given are equal, false |
| 190 | // otherwise. If either file can't be read, returns false. |
| 191 | BASE_EXPORT bool ContentsEqual(const FilePath& filename1, |
| 192 | const FilePath& filename2); |
| 193 | |
| 194 | // Returns true if the contents of the two text files given are equal, false |
| 195 | // otherwise. This routine treats "\r\n" and "\n" as equivalent. |
| 196 | BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, |
| 197 | const FilePath& filename2); |
| 198 | |
| 199 | // Reads the file at |path| into |contents| and returns true on success and |
| 200 | // false on error. For security reasons, a |path| containing path traversal |
| 201 | // components ('..') is treated as a read error and |contents| is set to empty. |
| 202 | // In case of I/O error, |contents| holds the data that could be read from the |
| 203 | // file before the error occurred. |
| 204 | // |contents| may be NULL, in which case this function is useful for its side |
| 205 | // effect of priming the disk cache (could be used for unit tests). |
| 206 | BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); |
| 207 | |
| 208 | // Reads the file at |path| into |contents| and returns true on success and |
| 209 | // false on error. For security reasons, a |path| containing path traversal |
| 210 | // components ('..') is treated as a read error and |contents| is set to empty. |
| 211 | // In case of I/O error, |contents| holds the data that could be read from the |
| 212 | // file before the error occurred. When the file size exceeds |max_size|, the |
| 213 | // function returns false with |contents| holding the file truncated to |
| 214 | // |max_size|. |
| 215 | // |contents| may be NULL, in which case this function is useful for its side |
| 216 | // effect of priming the disk cache (could be used for unit tests). |
hashimoto | 6da2fef | 2016-02-24 03:39:58 | [diff] [blame] | 217 | BASE_EXPORT bool ReadFileToStringWithMaxSize(const FilePath& path, |
| 218 | std::string* contents, |
| 219 | size_t max_size); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 220 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 221 | // As ReadFileToString, but reading from an open stream after seeking to its |
| 222 | // start (if supported by the stream). |
| 223 | BASE_EXPORT bool ReadStreamToString(FILE* stream, std::string* contents); |
| 224 | |
| 225 | // As ReadFileToStringWithMaxSize, but reading from an open stream after seeking |
| 226 | // to its start (if supported by the stream). |
| 227 | BASE_EXPORT bool ReadStreamToStringWithMaxSize(FILE* stream, |
| 228 | size_t max_size, |
| 229 | std::string* contents); |
| 230 | |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 231 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 232 | |
| 233 | // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
| 234 | // in |buffer|. This function is protected against EINTR and partial reads. |
| 235 | // Returns true iff |bytes| bytes have been successfully read from |fd|. |
| 236 | BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
| 237 | |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 238 | // Performs the same function as CreateAndOpenTemporaryStreamInDir(), but |
| 239 | // returns the file-descriptor wrapped in a ScopedFD, rather than the stream |
| 240 | // wrapped in a ScopedFILE. |
Lei Zhang | d22c973 | 2019-08-02 16:31:38 | [diff] [blame] | 241 | BASE_EXPORT ScopedFD CreateAndOpenFdForTemporaryFileInDir(const FilePath& dir, |
| 242 | FilePath* path); |
Wez | 45a7b30 | 2018-02-22 22:42:39 | [diff] [blame] | 243 | |
Lei Zhang | d22c973 | 2019-08-02 16:31:38 | [diff] [blame] | 244 | #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 245 | |
Fabrice de Gans-Riberi | 65421f6 | 2018-05-22 23:16:18 | [diff] [blame] | 246 | #if defined(OS_POSIX) |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 247 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 248 | // Creates a symbolic link at |symlink| pointing to |target|. Returns |
| 249 | // false on failure. |
| 250 | BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, |
| 251 | const FilePath& symlink); |
| 252 | |
| 253 | // Reads the given |symlink| and returns where it points to in |target|. |
| 254 | // Returns false upon failure. |
| 255 | BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); |
| 256 | |
| 257 | // Bits and masks of the file permission. |
| 258 | enum FilePermissionBits { |
| 259 | FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO, |
| 260 | FILE_PERMISSION_USER_MASK = S_IRWXU, |
| 261 | FILE_PERMISSION_GROUP_MASK = S_IRWXG, |
| 262 | FILE_PERMISSION_OTHERS_MASK = S_IRWXO, |
| 263 | |
| 264 | FILE_PERMISSION_READ_BY_USER = S_IRUSR, |
| 265 | FILE_PERMISSION_WRITE_BY_USER = S_IWUSR, |
| 266 | FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR, |
| 267 | FILE_PERMISSION_READ_BY_GROUP = S_IRGRP, |
| 268 | FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP, |
| 269 | FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP, |
| 270 | FILE_PERMISSION_READ_BY_OTHERS = S_IROTH, |
| 271 | FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH, |
| 272 | FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH, |
| 273 | }; |
| 274 | |
| 275 | // Reads the permission of the given |path|, storing the file permission |
| 276 | // bits in |mode|. If |path| is symbolic link, |mode| is the permission of |
| 277 | // a file which the symlink points to. |
| 278 | BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode); |
| 279 | // Sets the permission of the given |path|. If |path| is symbolic link, sets |
| 280 | // the permission of a file which the symlink points to. |
| 281 | BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode); |
| 282 | |
thomasanderson | 1929bb2 | 2016-06-24 02:01:00 | [diff] [blame] | 283 | // Returns true iff |executable| can be found in any directory specified by the |
| 284 | // environment variable in |env|. |
| 285 | BASE_EXPORT bool ExecutableExistsInPath(Environment* env, |
| 286 | const FilePath::StringType& executable); |
| 287 | |
Sergey Volk | 76166f5b | 2018-06-15 17:38:02 | [diff] [blame] | 288 | #if defined(OS_LINUX) || defined(OS_AIX) |
| 289 | // Determine if files under a given |path| can be mapped and then mprotect'd |
| 290 | // PROT_EXEC. This depends on the mount options used for |path|, which vary |
| 291 | // among different Linux distributions and possibly local configuration. It also |
| 292 | // depends on details of kernel--ChromeOS uses the noexec option for /dev/shm |
| 293 | // but its kernel allows mprotect with PROT_EXEC anyway. |
| 294 | BASE_EXPORT bool IsPathExecutable(const FilePath& path); |
| 295 | #endif // OS_LINUX || OS_AIX |
| 296 | |
Fabrice de Gans-Riberi | 65421f6 | 2018-05-22 23:16:18 | [diff] [blame] | 297 | #endif // OS_POSIX |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 298 | |
| 299 | // Returns true if the given directory is empty |
| 300 | BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path); |
| 301 | |
| 302 | // Get the temporary directory provided by the system. |
| 303 | // |
| 304 | // WARNING: In general, you should use CreateTemporaryFile variants below |
| 305 | // instead of this function. Those variants will ensure that the proper |
| 306 | // permissions are set so that other users on the system can't edit them while |
| 307 | // they're open (which can lead to security issues). |
| 308 | BASE_EXPORT bool GetTempDir(FilePath* path); |
| 309 | |
| 310 | // Get the home directory. This is more complicated than just getenv("HOME") |
| 311 | // as it knows to fall back on getpwent() etc. |
| 312 | // |
| 313 | // You should not generally call this directly. Instead use DIR_HOME with the |
| 314 | // path service which will use this function but cache the value. |
| 315 | // Path service may also override DIR_HOME. |
| 316 | BASE_EXPORT FilePath GetHomeDir(); |
| 317 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 318 | // Returns a new temporary file in |dir| with a unique name. The file is opened |
| 319 | // for exclusive read, write, and delete access (note: exclusivity is unique to |
| 320 | // Windows). On Windows, the returned file supports File::DeleteOnClose. |
| 321 | // On success, |temp_file| is populated with the full path to the created file. |
| 322 | BASE_EXPORT File CreateAndOpenTemporaryFileInDir(const FilePath& dir, |
| 323 | FilePath* temp_file); |
| 324 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 325 | // Creates a temporary file. The full path is placed in |path|, and the |
| 326 | // function returns true if was successful in creating the file. The file will |
| 327 | // be empty and all handles closed after this function returns. |
| 328 | BASE_EXPORT bool CreateTemporaryFile(FilePath* path); |
| 329 | |
| 330 | // Same as CreateTemporaryFile but the file is created in |dir|. |
| 331 | BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir, |
| 332 | FilePath* temp_file); |
| 333 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 334 | // Create and open a temporary file stream for exclusive read, write, and delete |
| 335 | // access (note: exclusivity is unique to Windows). The full path is placed in |
| 336 | // |path|. Returns the opened file stream, or null in case of error. |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 337 | BASE_EXPORT ScopedFILE CreateAndOpenTemporaryStream(FilePath* path); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 338 | |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 339 | // Similar to CreateAndOpenTemporaryStream, but the file is created in |dir|. |
| 340 | BASE_EXPORT ScopedFILE CreateAndOpenTemporaryStreamInDir(const FilePath& dir, |
| 341 | FilePath* path); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 342 | |
| 343 | // Create a new directory. If prefix is provided, the new directory name is in |
| 344 | // the format of prefixyyyy. |
| 345 | // NOTE: prefix is ignored in the POSIX implementation. |
| 346 | // If success, return true and output the full path of the directory created. |
| 347 | BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 348 | FilePath* new_temp_path); |
| 349 | |
| 350 | // Create a directory within another directory. |
| 351 | // Extra characters will be appended to |prefix| to ensure that the |
| 352 | // new directory does not have the same name as an existing directory. |
| 353 | BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 354 | const FilePath::StringType& prefix, |
| 355 | FilePath* new_dir); |
| 356 | |
| 357 | // Creates a directory, as well as creating any parent directories, if they |
| 358 | // don't exist. Returns 'true' on successful creation, or if the directory |
| 359 | // already exists. The directory is only readable by the current user. |
| 360 | // Returns true on success, leaving *error unchanged. |
| 361 | // Returns false on failure and sets *error appropriately, if it is non-NULL. |
| 362 | BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path, |
| 363 | File::Error* error); |
| 364 | |
| 365 | // Backward-compatible convenience method for the above. |
| 366 | BASE_EXPORT bool CreateDirectory(const FilePath& full_path); |
| 367 | |
| 368 | // Returns the file size. Returns true on success. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 369 | BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64_t* file_size); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 370 | |
| 371 | // Sets |real_path| to |path| with symbolic links and junctions expanded. |
| 372 | // On windows, make sure the path starts with a lettered drive. |
| 373 | // |path| must reference a file. Function will fail if |path| points to |
| 374 | // a directory or to a nonexistent path. On windows, this function will |
Alex Gough | 84ee78c | 2019-06-06 22:46:23 | [diff] [blame] | 375 | // fail if |real_path| would be longer than MAX_PATH characters. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 376 | BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path); |
| 377 | |
| 378 | #if defined(OS_WIN) |
| 379 | |
| 380 | // Given a path in NT native form ("\Device\HarddiskVolumeXX\..."), |
| 381 | // return in |drive_letter_path| the equivalent path that starts with |
| 382 | // a drive letter ("C:\..."). Return false if no such path exists. |
| 383 | BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path, |
| 384 | FilePath* drive_letter_path); |
| 385 | |
Bruce Long | c343f7e | 2019-05-11 02:20:12 | [diff] [blame] | 386 | // Method that wraps the win32 GetLongPathName API, normalizing the specified |
| 387 | // path to its long form. An example where this is needed is when comparing |
| 388 | // temp file paths. If a username isn't a valid 8.3 short file name (even just a |
| 389 | // lengthy name like "user with long name"), Windows will set the TMP and TEMP |
| 390 | // environment variables to be 8.3 paths. ::GetTempPath (called in |
| 391 | // base::GetTempDir) just uses the value specified by TMP or TEMP, and so can |
| 392 | // return a short path. Returns an empty path on error. |
| 393 | BASE_EXPORT FilePath MakeLongFilePath(const FilePath& input); |
David Bienvenu | f863412f | 2019-12-10 21:55:16 | [diff] [blame] | 394 | |
| 395 | // Creates a hard link named |to_file| to the file |from_file|. Both paths |
| 396 | // must be on the same volume, and |from_file| may not name a directory. |
| 397 | // Returns true if the hard link is created, false if it fails. |
| 398 | BASE_EXPORT bool CreateWinHardLink(const FilePath& to_file, |
| 399 | const FilePath& from_file); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 400 | #endif |
| 401 | |
| 402 | // This function will return if the given file is a symlink or not. |
| 403 | BASE_EXPORT bool IsLink(const FilePath& file_path); |
| 404 | |
| 405 | // Returns information about the given file path. |
| 406 | BASE_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info); |
| 407 | |
| 408 | // Sets the time of the last access and the time of the last modification. |
| 409 | BASE_EXPORT bool TouchFile(const FilePath& path, |
| 410 | const Time& last_accessed, |
| 411 | const Time& last_modified); |
| 412 | |
grt | d0bc44f | 2017-02-13 17:52:17 | [diff] [blame] | 413 | // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. The |
| 414 | // underlying file descriptor (POSIX) or handle (Windows) is unconditionally |
| 415 | // configured to not be propagated to child processes. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 416 | BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode); |
| 417 | |
| 418 | // Closes file opened by OpenFile. Returns true on success. |
| 419 | BASE_EXPORT bool CloseFile(FILE* file); |
| 420 | |
| 421 | // Associates a standard FILE stream with an existing File. Note that this |
| 422 | // functions take ownership of the existing File. |
| 423 | BASE_EXPORT FILE* FileToFILE(File file, const char* mode); |
| 424 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 425 | // Returns a new handle to the file underlying |file_stream|. |
| 426 | BASE_EXPORT File FILEToFile(FILE* file_stream); |
| 427 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 428 | // Truncates an open file to end at the location of the current file pointer. |
| 429 | // This is a cross-platform analog to Windows' SetEndOfFile() function. |
| 430 | BASE_EXPORT bool TruncateFile(FILE* file); |
| 431 | |
| 432 | // Reads at most the given number of bytes from the file into the buffer. |
| 433 | // Returns the number of read bytes, or -1 on error. |
| 434 | BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int max_size); |
| 435 | |
| 436 | // Writes the given buffer into the file, overwriting any data that was |
| 437 | // previously there. Returns the number of bytes written, or -1 on error. |
Andrew | 091a47b1 | 2019-12-12 00:24:00 | [diff] [blame] | 438 | // If file doesn't exist, it gets created with read/write permissions for all. |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 439 | // Note that the other variants of WriteFile() below may be easier to use. |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 440 | BASE_EXPORT int WriteFile(const FilePath& filename, const char* data, |
| 441 | int size); |
| 442 | |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 443 | // Writes |data| into the file, overwriting any data that was previously there. |
| 444 | // Returns true if and only if all of |data| was written. If the file does not |
| 445 | // exist, it gets created with read/write permissions for all. |
| 446 | BASE_EXPORT bool WriteFile(const FilePath& filename, span<const uint8_t> data); |
| 447 | |
| 448 | // Another WriteFile() variant that takes a StringPiece so callers don't have to |
| 449 | // do manual conversions from a char span to a uint8_t span. |
| 450 | BASE_EXPORT bool WriteFile(const FilePath& filename, StringPiece data); |
| 451 | |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 452 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 453 | // Appends |data| to |fd|. Does not close |fd| when done. Returns true iff |
| 454 | // |size| bytes of |data| were written to |fd|. |
| 455 | BASE_EXPORT bool WriteFileDescriptor(const int fd, const char* data, int size); |
Alex Ilin | 925adf0 | 2019-04-26 11:03:25 | [diff] [blame] | 456 | |
| 457 | // Allocates disk space for the file referred to by |fd| for the byte range |
| 458 | // starting at |offset| and continuing for |size| bytes. The file size will be |
| 459 | // changed if |offset|+|len| is greater than the file size. Zeros will fill the |
| 460 | // new space. |
| 461 | // After a successful call, subsequent writes into the specified range are |
| 462 | // guaranteed not to fail because of lack of disk space. |
| 463 | BASE_EXPORT bool AllocateFileRegion(File* file, int64_t offset, size_t size); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 464 | #endif |
| 465 | |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 466 | // Appends |data| to |filename|. Returns true iff |size| bytes of |data| were |
| 467 | // written to |filename|. |
| 468 | BASE_EXPORT bool AppendToFile(const FilePath& filename, |
| 469 | const char* data, |
| 470 | int size); |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 471 | |
| 472 | // Gets the current working directory for the process. |
| 473 | BASE_EXPORT bool GetCurrentDirectory(FilePath* path); |
| 474 | |
| 475 | // Sets the current working directory for the process. |
| 476 | BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); |
| 477 | |
Greg Thompson | b4abcb4 | 2019-08-23 01:42:56 | [diff] [blame] | 478 | // The largest value attempted by GetUniquePath{Number,}. |
| 479 | enum { kMaxUniqueFiles = 100 }; |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 480 | |
Greg Thompson | b4abcb4 | 2019-08-23 01:42:56 | [diff] [blame] | 481 | // Returns the number N that makes |path| unique when formatted as " (N)" in a |
| 482 | // suffix to its basename before any file extension, where N is a number between |
| 483 | // 1 and 100 (inclusive). Returns 0 if |path| does not exist (meaning that it is |
| 484 | // unique as-is), or -1 if no such number can be found. |
| 485 | BASE_EXPORT int GetUniquePathNumber(const FilePath& path); |
| 486 | |
| 487 | // Returns |path| if it does not exist. Otherwise, returns |path| with the |
| 488 | // suffix " (N)" appended to its basename before any file extension, where N is |
| 489 | // a number between 1 and 100 (inclusive). Returns an empty path if no such |
| 490 | // number can be found. |
Bruce Long | d096e489 | 2019-02-28 17:50:00 | [diff] [blame] | 491 | BASE_EXPORT FilePath GetUniquePath(const FilePath& path); |
| 492 | |
tfarina | 060df7e | 2015-12-16 05:15:32 | [diff] [blame] | 493 | // Sets the given |fd| to non-blocking mode. |
| 494 | // Returns true if it was able to set it in the non-blocking mode, otherwise |
| 495 | // false. |
| 496 | BASE_EXPORT bool SetNonBlocking(int fd); |
| 497 | |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 498 | // Hints the OS to prefetch the first |max_bytes| of |file_path| into its cache. |
| 499 | // |
| 500 | // If called at the appropriate time, this can reduce the latency incurred by |
| 501 | // feature code that needs to read the file. |
| 502 | // |
| 503 | // |max_bytes| specifies how many bytes should be pre-fetched. It may exceed the |
| 504 | // file's size. Passing in std::numeric_limits<int64_t>::max() is a convenient |
| 505 | // way to get the entire file pre-fetched. |
| 506 | // |
| 507 | // |is_executable| specifies whether the file is to be prefetched as |
| 508 | // executable code or as data. Windows treats the file backed pages in RAM |
| 509 | // differently, and specifying the wrong value results in two copies in RAM. |
| 510 | // |
| 511 | // Returns false if prefetching definitely failed. A return value of true does |
| 512 | // not guarantee that the entire desired range was prefetched. |
| 513 | // |
| 514 | // Calling this before using ::LoadLibrary() on Windows is more efficient memory |
| 515 | // wise, but we must be sure no other threads try to LoadLibrary() the file |
| 516 | // while we are doing the mapping and prefetching, or the process will get a |
| 517 | // private copy of the DLL via COW. |
| 518 | BASE_EXPORT bool PreReadFile( |
| 519 | const FilePath& file_path, |
| 520 | bool is_executable, |
| 521 | int64_t max_bytes = std::numeric_limits<int64_t>::max()); |
| 522 | |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 523 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
Nick Diego Yamane | 61bf28ba | 2019-03-19 14:56:37 | [diff] [blame] | 524 | |
| 525 | // Creates a pipe. Returns true on success, otherwise false. |
| 526 | // On success, |read_fd| will be set to the fd of the read side, and |
| 527 | // |write_fd| will be set to the one of write side. If |non_blocking| |
| 528 | // is set the pipe will be created with O_NONBLOCK|O_CLOEXEC flags set |
| 529 | // otherwise flag is set to zero (default). |
| 530 | BASE_EXPORT bool CreatePipe(ScopedFD* read_fd, |
| 531 | ScopedFD* write_fd, |
| 532 | bool non_blocking = false); |
| 533 | |
lhchavez | 80c77bfd | 2016-10-08 00:50:53 | [diff] [blame] | 534 | // Creates a non-blocking, close-on-exec pipe. |
| 535 | // This creates a non-blocking pipe that is not intended to be shared with any |
| 536 | // child process. This will be done atomically if the operating system supports |
| 537 | // it. Returns true if it was able to create the pipe, otherwise false. |
| 538 | BASE_EXPORT bool CreateLocalNonBlockingPipe(int fds[2]); |
| 539 | |
| 540 | // Sets the given |fd| to close-on-exec mode. |
| 541 | // Returns true if it was able to set it in the close-on-exec mode, otherwise |
| 542 | // false. |
| 543 | BASE_EXPORT bool SetCloseOnExec(int fd); |
| 544 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 545 | // Test that |path| can only be changed by a given user and members of |
| 546 | // a given set of groups. |
| 547 | // Specifically, test that all parts of |path| under (and including) |base|: |
| 548 | // * Exist. |
| 549 | // * Are owned by a specific user. |
| 550 | // * Are not writable by all users. |
| 551 | // * Are owned by a member of a given set of groups, or are not writable by |
| 552 | // their group. |
| 553 | // * Are not symbolic links. |
| 554 | // This is useful for checking that a config file is administrator-controlled. |
| 555 | // |base| must contain |path|. |
| 556 | BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base, |
| 557 | const base::FilePath& path, |
| 558 | uid_t owner_uid, |
| 559 | const std::set<gid_t>& group_gids); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 560 | #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 561 | |
| 562 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 563 | // Is |path| writable only by a user with administrator privileges? |
| 564 | // This function uses Mac OS conventions. The super user is assumed to have |
| 565 | // uid 0, and the administrator group is assumed to be named "admin". |
| 566 | // Testing that |path|, and every parent directory including the root of |
| 567 | // the filesystem, are owned by the superuser, controlled by the group |
| 568 | // "admin", are not writable by all users, and contain no symbolic links. |
| 569 | // Will return false if |path| does not exist. |
| 570 | BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path); |
| 571 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 572 | |
| 573 | // Returns the maximum length of path component on the volume containing |
| 574 | // the directory |path|, in the number of FilePath::CharType, or -1 on failure. |
| 575 | BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path); |
| 576 | |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 577 | #if defined(OS_LINUX) || defined(OS_AIX) |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 578 | // Broad categories of file systems as returned by statfs() on Linux. |
| 579 | enum FileSystemType { |
| 580 | FILE_SYSTEM_UNKNOWN, // statfs failed. |
| 581 | FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
| 582 | FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
| 583 | FILE_SYSTEM_NFS, |
| 584 | FILE_SYSTEM_SMB, |
| 585 | FILE_SYSTEM_CODA, |
| 586 | FILE_SYSTEM_MEMORY, // in-memory file system |
| 587 | FILE_SYSTEM_CGROUP, // cgroup control. |
| 588 | FILE_SYSTEM_OTHER, // any other value. |
| 589 | FILE_SYSTEM_TYPE_COUNT |
| 590 | }; |
| 591 | |
| 592 | // Attempts determine the FileSystemType for |path|. |
| 593 | // Returns false if |path| doesn't exist. |
| 594 | BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type); |
| 595 | #endif |
| 596 | |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 597 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 598 | // Get a temporary directory for shared memory files. The directory may depend |
| 599 | // on whether the destination is intended for executable files, which in turn |
| 600 | // depends on how /dev/shmem was mounted. As a result, you must supply whether |
| 601 | // you intend to create executable shmem segments so this function can find |
| 602 | // an appropriate location. |
| 603 | BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path); |
| 604 | #endif |
| 605 | |
| 606 | // Internal -------------------------------------------------------------------- |
| 607 | |
| 608 | namespace internal { |
| 609 | |
| 610 | // Same as Move but allows paths with traversal components. |
| 611 | // Use only with extreme care. |
| 612 | BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, |
| 613 | const FilePath& to_path); |
| 614 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 615 | #if defined(OS_WIN) |
| 616 | // Copy from_path to to_path recursively and then delete from_path recursively. |
| 617 | // Returns true if all operations succeed. |
| 618 | // This function simulates Move(), but unlike Move() it works across volumes. |
| 619 | // This function is not transactional. |
| 620 | BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 621 | const FilePath& to_path); |
| 622 | #endif // defined(OS_WIN) |
| 623 | |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 624 | // Used by PreReadFile() when no kernel support for prefetching is available. |
| 625 | bool PreReadFileSlow(const FilePath& file_path, int64_t max_bytes); |
| 626 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 627 | } // namespace internal |
| 628 | } // namespace base |
| 629 | |
| 630 | #endif // BASE_FILES_FILE_UTIL_H_ |