16d528ed9Sopenharmony_ci// Copyright (c) 2012 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci// This file contains utility functions for dealing with the local
66d528ed9Sopenharmony_ci// filesystem.
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci#ifndef BASE_FILES_FILE_UTIL_H_
96d528ed9Sopenharmony_ci#define BASE_FILES_FILE_UTIL_H_
106d528ed9Sopenharmony_ci
116d528ed9Sopenharmony_ci#include <stddef.h>
126d528ed9Sopenharmony_ci#include <stdint.h>
136d528ed9Sopenharmony_ci#include <stdio.h>
146d528ed9Sopenharmony_ci
156d528ed9Sopenharmony_ci#include <set>
166d528ed9Sopenharmony_ci#include <string>
176d528ed9Sopenharmony_ci#include <vector>
186d528ed9Sopenharmony_ci
196d528ed9Sopenharmony_ci#if defined(OS_POSIX) || defined(OS_FUCHSIA)
206d528ed9Sopenharmony_ci#include <sys/stat.h>
216d528ed9Sopenharmony_ci#include <unistd.h>
226d528ed9Sopenharmony_ci#endif
236d528ed9Sopenharmony_ci
246d528ed9Sopenharmony_ci#include "base/files/file.h"
256d528ed9Sopenharmony_ci#include "base/files/file_path.h"
266d528ed9Sopenharmony_ci#include "util/build_config.h"
276d528ed9Sopenharmony_ci
286d528ed9Sopenharmony_ci#if defined(OS_WIN)
296d528ed9Sopenharmony_ci#include <windows.h>
306d528ed9Sopenharmony_ci#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
316d528ed9Sopenharmony_ci#include "base/logging.h"
326d528ed9Sopenharmony_ci#include "base/posix/eintr_wrapper.h"
336d528ed9Sopenharmony_ci#endif
346d528ed9Sopenharmony_ci
356d528ed9Sopenharmony_cinamespace base {
366d528ed9Sopenharmony_ci
376d528ed9Sopenharmony_ciclass Environment;
386d528ed9Sopenharmony_ci
396d528ed9Sopenharmony_ci//-----------------------------------------------------------------------------
406d528ed9Sopenharmony_ci// Functions that involve filesystem access or modification:
416d528ed9Sopenharmony_ci
426d528ed9Sopenharmony_ci// Returns an absolute version of a relative path. Returns an empty path on
436d528ed9Sopenharmony_ci// error. On POSIX, this function fails if the path does not exist. This
446d528ed9Sopenharmony_ci// function can result in I/O so it can be slow.
456d528ed9Sopenharmony_ciFilePath MakeAbsoluteFilePath(const FilePath& input);
466d528ed9Sopenharmony_ci
476d528ed9Sopenharmony_ci// Returns the total number of bytes used by all the files under |root_path|.
486d528ed9Sopenharmony_ci// If the path does not exist the function returns 0.
496d528ed9Sopenharmony_ci//
506d528ed9Sopenharmony_ci// This function is implemented using the FileEnumerator class so it is not
516d528ed9Sopenharmony_ci// particularly speedy in any platform.
526d528ed9Sopenharmony_ciint64_t ComputeDirectorySize(const FilePath& root_path);
536d528ed9Sopenharmony_ci
546d528ed9Sopenharmony_ci// Deletes the given path, whether it's a file or a directory.
556d528ed9Sopenharmony_ci// If it's a directory, it's perfectly happy to delete all of the
566d528ed9Sopenharmony_ci// directory's contents.  Passing true to recursive deletes
576d528ed9Sopenharmony_ci// subdirectories and their contents as well.
586d528ed9Sopenharmony_ci// Returns true if successful, false otherwise. It is considered successful
596d528ed9Sopenharmony_ci// to attempt to delete a file that does not exist.
606d528ed9Sopenharmony_ci//
616d528ed9Sopenharmony_ci// In posix environment and if |path| is a symbolic link, this deletes only
626d528ed9Sopenharmony_ci// the symlink. (even if the symlink points to a non-existent file)
636d528ed9Sopenharmony_ci//
646d528ed9Sopenharmony_ci// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
656d528ed9Sopenharmony_ci//          TO "rm -rf", SO USE WITH CAUTION.
666d528ed9Sopenharmony_cibool DeleteFile(const FilePath& path, bool recursive);
676d528ed9Sopenharmony_ci
686d528ed9Sopenharmony_ci#if defined(OS_WIN)
696d528ed9Sopenharmony_ci// Schedules to delete the given path, whether it's a file or a directory, until
706d528ed9Sopenharmony_ci// the operating system is restarted.
716d528ed9Sopenharmony_ci// Note:
726d528ed9Sopenharmony_ci// 1) The file/directory to be deleted should exist in a temp folder.
736d528ed9Sopenharmony_ci// 2) The directory to be deleted must be empty.
746d528ed9Sopenharmony_cibool DeleteFileAfterReboot(const FilePath& path);
756d528ed9Sopenharmony_ci#endif
766d528ed9Sopenharmony_ci
776d528ed9Sopenharmony_ci// Renames file |from_path| to |to_path|. Both paths must be on the same
786d528ed9Sopenharmony_ci// volume, or the function will fail. Destination file will be created
796d528ed9Sopenharmony_ci// if it doesn't exist. Prefer this function over Move when dealing with
806d528ed9Sopenharmony_ci// temporary files. On Windows it preserves attributes of the target file.
816d528ed9Sopenharmony_ci// Returns true on success, leaving *error unchanged.
826d528ed9Sopenharmony_ci// Returns false on failure and sets *error appropriately, if it is non-NULL.
836d528ed9Sopenharmony_cibool ReplaceFile(const FilePath& from_path,
846d528ed9Sopenharmony_ci                 const FilePath& to_path,
856d528ed9Sopenharmony_ci                 File::Error* error);
866d528ed9Sopenharmony_ci
876d528ed9Sopenharmony_ci// Returns true if the given path exists on the local filesystem,
886d528ed9Sopenharmony_ci// false otherwise.
896d528ed9Sopenharmony_cibool PathExists(const FilePath& path);
906d528ed9Sopenharmony_ci
916d528ed9Sopenharmony_ci// Returns true if the given path is writable by the user, false otherwise.
926d528ed9Sopenharmony_cibool PathIsWritable(const FilePath& path);
936d528ed9Sopenharmony_ci
946d528ed9Sopenharmony_ci// Returns true if the given path exists and is a directory, false otherwise.
956d528ed9Sopenharmony_cibool DirectoryExists(const FilePath& path);
966d528ed9Sopenharmony_ci
976d528ed9Sopenharmony_ci// Returns true if the contents of the two files given are equal, false
986d528ed9Sopenharmony_ci// otherwise.  If either file can't be read, returns false.
996d528ed9Sopenharmony_cibool ContentsEqual(const FilePath& filename1, const FilePath& filename2);
1006d528ed9Sopenharmony_ci
1016d528ed9Sopenharmony_ci// Returns true if the contents of the two text files given are equal, false
1026d528ed9Sopenharmony_ci// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
1036d528ed9Sopenharmony_cibool TextContentsEqual(const FilePath& filename1, const FilePath& filename2);
1046d528ed9Sopenharmony_ci
1056d528ed9Sopenharmony_ci// Reads the file at |path| into |contents| and returns true on success and
1066d528ed9Sopenharmony_ci// false on error.  For security reasons, a |path| containing path traversal
1076d528ed9Sopenharmony_ci// components ('..') is treated as a read error and |contents| is set to empty.
1086d528ed9Sopenharmony_ci// In case of I/O error, |contents| holds the data that could be read from the
1096d528ed9Sopenharmony_ci// file before the error occurred.
1106d528ed9Sopenharmony_ci// |contents| may be NULL, in which case this function is useful for its side
1116d528ed9Sopenharmony_ci// effect of priming the disk cache (could be used for unit tests).
1126d528ed9Sopenharmony_cibool ReadFileToString(const FilePath& path, std::string* contents);
1136d528ed9Sopenharmony_ci
1146d528ed9Sopenharmony_ci// Reads the file at |path| into |contents| and returns true on success and
1156d528ed9Sopenharmony_ci// false on error.  For security reasons, a |path| containing path traversal
1166d528ed9Sopenharmony_ci// components ('..') is treated as a read error and |contents| is set to empty.
1176d528ed9Sopenharmony_ci// In case of I/O error, |contents| holds the data that could be read from the
1186d528ed9Sopenharmony_ci// file before the error occurred.  When the file size exceeds |max_size|, the
1196d528ed9Sopenharmony_ci// function returns false with |contents| holding the file truncated to
1206d528ed9Sopenharmony_ci// |max_size|.
1216d528ed9Sopenharmony_ci// |contents| may be NULL, in which case this function is useful for its side
1226d528ed9Sopenharmony_ci// effect of priming the disk cache (could be used for unit tests).
1236d528ed9Sopenharmony_cibool ReadFileToStringWithMaxSize(const FilePath& path,
1246d528ed9Sopenharmony_ci                                 std::string* contents,
1256d528ed9Sopenharmony_ci                                 size_t max_size);
1266d528ed9Sopenharmony_ci
1276d528ed9Sopenharmony_ci#if defined(OS_POSIX) || defined(OS_FUCHSIA)
1286d528ed9Sopenharmony_ci
1296d528ed9Sopenharmony_ci// Performs the same function as CreateAndOpenTemporaryFileInDir(), but
1306d528ed9Sopenharmony_ci// returns the file-descriptor wrapped in a ScopedFD rather than a File.
1316d528ed9Sopenharmony_ciScopedFD CreateAndOpenFdForTemporaryFileInDir(const FilePath& dir,
1326d528ed9Sopenharmony_ci                                              FilePath* path);
1336d528ed9Sopenharmony_ci
1346d528ed9Sopenharmony_ci#endif
1356d528ed9Sopenharmony_ci
1366d528ed9Sopenharmony_ci#if defined(OS_POSIX)
1376d528ed9Sopenharmony_ci
1386d528ed9Sopenharmony_ci// Creates a symbolic link at |symlink| pointing to |target|.  Returns
1396d528ed9Sopenharmony_ci// false on failure.
1406d528ed9Sopenharmony_cibool CreateSymbolicLink(const FilePath& target, const FilePath& symlink);
1416d528ed9Sopenharmony_ci
1426d528ed9Sopenharmony_ci// Reads the given |symlink| and returns where it points to in |target|.
1436d528ed9Sopenharmony_ci// Returns false upon failure.
1446d528ed9Sopenharmony_cibool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
1456d528ed9Sopenharmony_ci
1466d528ed9Sopenharmony_ci// Bits and masks of the file permission.
1476d528ed9Sopenharmony_cienum FilePermissionBits {
1486d528ed9Sopenharmony_ci  FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO,
1496d528ed9Sopenharmony_ci  FILE_PERMISSION_USER_MASK = S_IRWXU,
1506d528ed9Sopenharmony_ci  FILE_PERMISSION_GROUP_MASK = S_IRWXG,
1516d528ed9Sopenharmony_ci  FILE_PERMISSION_OTHERS_MASK = S_IRWXO,
1526d528ed9Sopenharmony_ci
1536d528ed9Sopenharmony_ci  FILE_PERMISSION_READ_BY_USER = S_IRUSR,
1546d528ed9Sopenharmony_ci  FILE_PERMISSION_WRITE_BY_USER = S_IWUSR,
1556d528ed9Sopenharmony_ci  FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR,
1566d528ed9Sopenharmony_ci  FILE_PERMISSION_READ_BY_GROUP = S_IRGRP,
1576d528ed9Sopenharmony_ci  FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP,
1586d528ed9Sopenharmony_ci  FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP,
1596d528ed9Sopenharmony_ci  FILE_PERMISSION_READ_BY_OTHERS = S_IROTH,
1606d528ed9Sopenharmony_ci  FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH,
1616d528ed9Sopenharmony_ci  FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH,
1626d528ed9Sopenharmony_ci};
1636d528ed9Sopenharmony_ci
1646d528ed9Sopenharmony_ci// Reads the permission of the given |path|, storing the file permission
1656d528ed9Sopenharmony_ci// bits in |mode|. If |path| is symbolic link, |mode| is the permission of
1666d528ed9Sopenharmony_ci// a file which the symlink points to.
1676d528ed9Sopenharmony_cibool GetPosixFilePermissions(const FilePath& path, int* mode);
1686d528ed9Sopenharmony_ci// Sets the permission of the given |path|. If |path| is symbolic link, sets
1696d528ed9Sopenharmony_ci// the permission of a file which the symlink points to.
1706d528ed9Sopenharmony_cibool SetPosixFilePermissions(const FilePath& path, int mode);
1716d528ed9Sopenharmony_ci
1726d528ed9Sopenharmony_ci// Returns true iff |executable| can be found in any directory specified by the
1736d528ed9Sopenharmony_ci// environment variable in |env|.
1746d528ed9Sopenharmony_cibool ExecutableExistsInPath(Environment* env,
1756d528ed9Sopenharmony_ci                            const FilePath::StringType& executable);
1766d528ed9Sopenharmony_ci
1776d528ed9Sopenharmony_ci#endif  // OS_POSIX
1786d528ed9Sopenharmony_ci
1796d528ed9Sopenharmony_ci// Returns true if the given directory is empty
1806d528ed9Sopenharmony_cibool IsDirectoryEmpty(const FilePath& dir_path);
1816d528ed9Sopenharmony_ci
1826d528ed9Sopenharmony_ci// Get the temporary directory provided by the system.
1836d528ed9Sopenharmony_cibool GetTempDir(FilePath* path);
1846d528ed9Sopenharmony_ci
1856d528ed9Sopenharmony_ci// Returns a new temporary file in |dir| with a unique name. On success,
1866d528ed9Sopenharmony_ci// |temp_file| is populated with the full path to the created file.
1876d528ed9Sopenharmony_ciFile CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* temp_file);
1886d528ed9Sopenharmony_ci
1896d528ed9Sopenharmony_ci// Create a new directory. If prefix is provided, the new directory name is in
1906d528ed9Sopenharmony_ci// the format of prefixyyyy.
1916d528ed9Sopenharmony_ci// NOTE: prefix is ignored in the POSIX implementation.
1926d528ed9Sopenharmony_ci// If success, return true and output the full path of the directory created.
1936d528ed9Sopenharmony_cibool CreateNewTempDirectory(const FilePath::StringType& prefix,
1946d528ed9Sopenharmony_ci                            FilePath* new_temp_path);
1956d528ed9Sopenharmony_ci
1966d528ed9Sopenharmony_ci// Create a directory within another directory.
1976d528ed9Sopenharmony_ci// Extra characters will be appended to |prefix| to ensure that the
1986d528ed9Sopenharmony_ci// new directory does not have the same name as an existing directory.
1996d528ed9Sopenharmony_cibool CreateTemporaryDirInDir(const FilePath& base_dir,
2006d528ed9Sopenharmony_ci                             const FilePath::StringType& prefix,
2016d528ed9Sopenharmony_ci                             FilePath* new_dir);
2026d528ed9Sopenharmony_ci
2036d528ed9Sopenharmony_ci// Creates a directory, as well as creating any parent directories, if they
2046d528ed9Sopenharmony_ci// don't exist. Returns 'true' on successful creation, or if the directory
2056d528ed9Sopenharmony_ci// already exists.  The directory is only readable by the current user.
2066d528ed9Sopenharmony_ci// Returns true on success, leaving *error unchanged.
2076d528ed9Sopenharmony_ci// Returns false on failure and sets *error appropriately, if it is non-NULL.
2086d528ed9Sopenharmony_cibool CreateDirectoryAndGetError(const FilePath& full_path, File::Error* error);
2096d528ed9Sopenharmony_ci
2106d528ed9Sopenharmony_ci// Backward-compatible convenience method for the above.
2116d528ed9Sopenharmony_cibool CreateDirectory(const FilePath& full_path);
2126d528ed9Sopenharmony_ci
2136d528ed9Sopenharmony_ci// Returns the file size. Returns true on success.
2146d528ed9Sopenharmony_cibool GetFileSize(const FilePath& file_path, int64_t* file_size);
2156d528ed9Sopenharmony_ci
2166d528ed9Sopenharmony_ci// Sets |real_path| to |path| with symbolic links and junctions expanded.
2176d528ed9Sopenharmony_ci// On windows, make sure the path starts with a lettered drive.
2186d528ed9Sopenharmony_ci// |path| must reference a file.  Function will fail if |path| points to
2196d528ed9Sopenharmony_ci// a directory or to a nonexistent path.  On windows, this function will
2206d528ed9Sopenharmony_ci// fail if |path| is a junction or symlink that points to an empty file,
2216d528ed9Sopenharmony_ci// or if |real_path| would be longer than MAX_PATH characters.
2226d528ed9Sopenharmony_cibool NormalizeFilePath(const FilePath& path, FilePath* real_path);
2236d528ed9Sopenharmony_ci
2246d528ed9Sopenharmony_ci#if defined(OS_WIN)
2256d528ed9Sopenharmony_ci
2266d528ed9Sopenharmony_ci// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
2276d528ed9Sopenharmony_ci// return in |drive_letter_path| the equivalent path that starts with
2286d528ed9Sopenharmony_ci// a drive letter ("C:\...").  Return false if no such path exists.
2296d528ed9Sopenharmony_cibool DevicePathToDriveLetterPath(const FilePath& device_path,
2306d528ed9Sopenharmony_ci                                 FilePath* drive_letter_path);
2316d528ed9Sopenharmony_ci
2326d528ed9Sopenharmony_ci// Given an existing file in |path|, set |real_path| to the path
2336d528ed9Sopenharmony_ci// in native NT format, of the form "\Device\HarddiskVolumeXX\..".
2346d528ed9Sopenharmony_ci// Returns false if the path can not be found. Empty files cannot
2356d528ed9Sopenharmony_ci// be resolved with this function.
2366d528ed9Sopenharmony_cibool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path);
2376d528ed9Sopenharmony_ci#endif
2386d528ed9Sopenharmony_ci
2396d528ed9Sopenharmony_ci// This function will return if the given file is a symlink or not.
2406d528ed9Sopenharmony_cibool IsLink(const FilePath& file_path);
2416d528ed9Sopenharmony_ci
2426d528ed9Sopenharmony_ci// Returns information about the given file path.
2436d528ed9Sopenharmony_cibool GetFileInfo(const FilePath& file_path, File::Info* info);
2446d528ed9Sopenharmony_ci
2456d528ed9Sopenharmony_ci// Wrapper for fopen-like calls. Returns non-NULL FILE* on success. The
2466d528ed9Sopenharmony_ci// underlying file descriptor (POSIX) or handle (Windows) is unconditionally
2476d528ed9Sopenharmony_ci// configured to not be propagated to child processes.
2486d528ed9Sopenharmony_ciFILE* OpenFile(const FilePath& filename, const char* mode);
2496d528ed9Sopenharmony_ci
2506d528ed9Sopenharmony_ci// Closes file opened by OpenFile. Returns true on success.
2516d528ed9Sopenharmony_cibool CloseFile(FILE* file);
2526d528ed9Sopenharmony_ci
2536d528ed9Sopenharmony_ci// Associates a standard FILE stream with an existing File. Note that this
2546d528ed9Sopenharmony_ci// functions take ownership of the existing File.
2556d528ed9Sopenharmony_ciFILE* FileToFILE(File file, const char* mode);
2566d528ed9Sopenharmony_ci
2576d528ed9Sopenharmony_ci// Truncates an open file to end at the location of the current file pointer.
2586d528ed9Sopenharmony_ci// This is a cross-platform analog to Windows' SetEndOfFile() function.
2596d528ed9Sopenharmony_cibool TruncateFile(FILE* file);
2606d528ed9Sopenharmony_ci
2616d528ed9Sopenharmony_ci// Reads at most the given number of bytes from the file into the buffer.
2626d528ed9Sopenharmony_ci// Returns the number of read bytes, or -1 on error.
2636d528ed9Sopenharmony_ciint ReadFile(const FilePath& filename, char* data, int max_size);
2646d528ed9Sopenharmony_ci
2656d528ed9Sopenharmony_ci// Writes the given buffer into the file, overwriting any data that was
2666d528ed9Sopenharmony_ci// previously there.  Returns the number of bytes written, or -1 on error.
2676d528ed9Sopenharmony_ciint WriteFile(const FilePath& filename, const char* data, int size);
2686d528ed9Sopenharmony_ci
2696d528ed9Sopenharmony_ci#if defined(OS_POSIX) || defined(OS_FUCHSIA)
2706d528ed9Sopenharmony_ci// Appends |data| to |fd|. Does not close |fd| when done.  Returns true iff
2716d528ed9Sopenharmony_ci// |size| bytes of |data| were written to |fd|.
2726d528ed9Sopenharmony_cibool WriteFileDescriptor(const int fd, const char* data, int size);
2736d528ed9Sopenharmony_ci#endif
2746d528ed9Sopenharmony_ci
2756d528ed9Sopenharmony_ci// Appends |data| to |filename|.  Returns true iff |size| bytes of |data| were
2766d528ed9Sopenharmony_ci// written to |filename|.
2776d528ed9Sopenharmony_cibool AppendToFile(const FilePath& filename, const char* data, int size);
2786d528ed9Sopenharmony_ci
2796d528ed9Sopenharmony_ci// Gets the current working directory for the process.
2806d528ed9Sopenharmony_cibool GetCurrentDirectory(FilePath* path);
2816d528ed9Sopenharmony_ci
2826d528ed9Sopenharmony_ci// Sets the current working directory for the process.
2836d528ed9Sopenharmony_cibool SetCurrentDirectory(const FilePath& path);
2846d528ed9Sopenharmony_ci
2856d528ed9Sopenharmony_ci// Attempts to find a number that can be appended to the |path| to make it
2866d528ed9Sopenharmony_ci// unique. If |path| does not exist, 0 is returned.  If it fails to find such
2876d528ed9Sopenharmony_ci// a number, -1 is returned. If |suffix| is not empty, also checks the
2886d528ed9Sopenharmony_ci// existence of it with the given suffix.
2896d528ed9Sopenharmony_ciint GetUniquePathNumber(const FilePath& path,
2906d528ed9Sopenharmony_ci                        const FilePath::StringType& suffix);
2916d528ed9Sopenharmony_ci
2926d528ed9Sopenharmony_ci// Sets the given |fd| to non-blocking mode.
2936d528ed9Sopenharmony_ci// Returns true if it was able to set it in the non-blocking mode, otherwise
2946d528ed9Sopenharmony_ci// false.
2956d528ed9Sopenharmony_cibool SetNonBlocking(int fd);
2966d528ed9Sopenharmony_ci
2976d528ed9Sopenharmony_ci#if defined(OS_POSIX) || defined(OS_FUCHSIA)
2986d528ed9Sopenharmony_ci// Creates a non-blocking, close-on-exec pipe.
2996d528ed9Sopenharmony_ci// This creates a non-blocking pipe that is not intended to be shared with any
3006d528ed9Sopenharmony_ci// child process. This will be done atomically if the operating system supports
3016d528ed9Sopenharmony_ci// it. Returns true if it was able to create the pipe, otherwise false.
3026d528ed9Sopenharmony_cibool CreateLocalNonBlockingPipe(int fds[2]);
3036d528ed9Sopenharmony_ci
3046d528ed9Sopenharmony_ci// Sets the given |fd| to close-on-exec mode.
3056d528ed9Sopenharmony_ci// Returns true if it was able to set it in the close-on-exec mode, otherwise
3066d528ed9Sopenharmony_ci// false.
3076d528ed9Sopenharmony_cibool SetCloseOnExec(int fd);
3086d528ed9Sopenharmony_ci
3096d528ed9Sopenharmony_ci// Test that |path| can only be changed by a given user and members of
3106d528ed9Sopenharmony_ci// a given set of groups.
3116d528ed9Sopenharmony_ci// Specifically, test that all parts of |path| under (and including) |base|:
3126d528ed9Sopenharmony_ci// * Exist.
3136d528ed9Sopenharmony_ci// * Are owned by a specific user.
3146d528ed9Sopenharmony_ci// * Are not writable by all users.
3156d528ed9Sopenharmony_ci// * Are owned by a member of a given set of groups, or are not writable by
3166d528ed9Sopenharmony_ci//   their group.
3176d528ed9Sopenharmony_ci// * Are not symbolic links.
3186d528ed9Sopenharmony_ci// This is useful for checking that a config file is administrator-controlled.
3196d528ed9Sopenharmony_ci// |base| must contain |path|.
3206d528ed9Sopenharmony_cibool VerifyPathControlledByUser(const base::FilePath& base,
3216d528ed9Sopenharmony_ci                                const base::FilePath& path,
3226d528ed9Sopenharmony_ci                                uid_t owner_uid,
3236d528ed9Sopenharmony_ci                                const std::set<gid_t>& group_gids);
3246d528ed9Sopenharmony_ci#endif  // defined(OS_POSIX) || defined(OS_FUCHSIA)
3256d528ed9Sopenharmony_ci
3266d528ed9Sopenharmony_ci#if defined(OS_MACOSX) && !defined(OS_IOS)
3276d528ed9Sopenharmony_ci// Is |path| writable only by a user with administrator privileges?
3286d528ed9Sopenharmony_ci// This function uses Mac OS conventions.  The super user is assumed to have
3296d528ed9Sopenharmony_ci// uid 0, and the administrator group is assumed to be named "admin".
3306d528ed9Sopenharmony_ci// Testing that |path|, and every parent directory including the root of
3316d528ed9Sopenharmony_ci// the filesystem, are owned by the superuser, controlled by the group
3326d528ed9Sopenharmony_ci// "admin", are not writable by all users, and contain no symbolic links.
3336d528ed9Sopenharmony_ci// Will return false if |path| does not exist.
3346d528ed9Sopenharmony_cibool VerifyPathControlledByAdmin(const base::FilePath& path);
3356d528ed9Sopenharmony_ci#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
3366d528ed9Sopenharmony_ci
3376d528ed9Sopenharmony_ci// Returns the maximum length of path component on the volume containing
3386d528ed9Sopenharmony_ci// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
3396d528ed9Sopenharmony_ciint GetMaximumPathComponentLength(const base::FilePath& path);
3406d528ed9Sopenharmony_ci
3416d528ed9Sopenharmony_ci#if defined(OS_LINUX) || defined(OS_AIX) || defined(OS_BSD)
3426d528ed9Sopenharmony_ci// Broad categories of file systems as returned by statfs() on Linux.
3436d528ed9Sopenharmony_cienum FileSystemType {
3446d528ed9Sopenharmony_ci  FILE_SYSTEM_UNKNOWN,   // statfs failed.
3456d528ed9Sopenharmony_ci  FILE_SYSTEM_0,         // statfs.f_type == 0 means unknown, may indicate AFS.
3466d528ed9Sopenharmony_ci  FILE_SYSTEM_ORDINARY,  // on-disk filesystem like ext2
3476d528ed9Sopenharmony_ci  FILE_SYSTEM_NFS,
3486d528ed9Sopenharmony_ci  FILE_SYSTEM_SMB,
3496d528ed9Sopenharmony_ci  FILE_SYSTEM_CODA,
3506d528ed9Sopenharmony_ci  FILE_SYSTEM_MEMORY,  // in-memory file system
3516d528ed9Sopenharmony_ci  FILE_SYSTEM_CGROUP,  // cgroup control.
3526d528ed9Sopenharmony_ci  FILE_SYSTEM_OTHER,   // any other value.
3536d528ed9Sopenharmony_ci  FILE_SYSTEM_TYPE_COUNT
3546d528ed9Sopenharmony_ci};
3556d528ed9Sopenharmony_ci
3566d528ed9Sopenharmony_ci// Attempts determine the FileSystemType for |path|.
3576d528ed9Sopenharmony_ci// Returns false if |path| doesn't exist.
3586d528ed9Sopenharmony_cibool GetFileSystemType(const FilePath& path, FileSystemType* type);
3596d528ed9Sopenharmony_ci#endif
3606d528ed9Sopenharmony_ci
3616d528ed9Sopenharmony_ci}  // namespace base
3626d528ed9Sopenharmony_ci
3636d528ed9Sopenharmony_ci#endif  // BASE_FILES_FILE_UTIL_H_
364