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