blob: 1d556722b8e9cfed5c7ab8b5bf40f5097bc057da [file] [log] [blame]
[email protected]e3177dd52014-08-13 20:22:141// 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
avi543540e2015-12-24 05:15:3211#include <stddef.h>
12#include <stdint.h>
13#include <stdio.h>
14
15#include <set>
16#include <string>
17#include <vector>
18
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3919#if defined(OS_POSIX) || defined(OS_FUCHSIA)
20#include <sys/stat.h>
21#include <unistd.h>
22#endif
23
avi543540e2015-12-24 05:15:3224#include "base/base_export.h"
25#include "base/files/file.h"
26#include "base/files/file_path.h"
avi543540e2015-12-24 05:15:3227#include "base/strings/string16.h"
[email protected]e3177dd52014-08-13 20:22:1428#include "build/build_config.h"
29
30#if defined(OS_WIN)
Bruce Dawsonbfdc3fd2018-01-03 20:32:3631#include "base/win/windows_types.h"
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3932#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]e3177dd52014-08-13 20:22:1433#include "base/file_descriptor_posix.h"
34#include "base/logging.h"
35#include "base/posix/eintr_wrapper.h"
36#endif
37
38namespace base {
39
thomasanderson1929bb22016-06-24 02:01:0040class Environment;
[email protected]e3177dd52014-08-13 20:22:1441class Time;
42
43//-----------------------------------------------------------------------------
44// Functions that involve filesystem access or modification:
45
46// Returns an absolute version of a relative path. Returns an empty path on
47// error. On POSIX, this function fails if the path does not exist. This
48// function can result in I/O so it can be slow.
49BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
50
51// Returns the total number of bytes used by all the files under |root_path|.
52// If the path does not exist the function returns 0.
53//
54// This function is implemented using the FileEnumerator class so it is not
55// particularly speedy in any platform.
avi543540e2015-12-24 05:15:3256BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path);
[email protected]e3177dd52014-08-13 20:22:1457
58// Deletes the given path, whether it's a file or a directory.
59// If it's a directory, it's perfectly happy to delete all of the
60// directory's contents. Passing true to recursive deletes
61// subdirectories and their contents as well.
62// Returns true if successful, false otherwise. It is considered successful
63// to attempt to delete a file that does not exist.
64//
65// In posix environment and if |path| is a symbolic link, this deletes only
66// the symlink. (even if the symlink points to a non-existent file)
67//
68// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
69// TO "rm -rf", SO USE WITH CAUTION.
70BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive);
71
72#if defined(OS_WIN)
73// Schedules to delete the given path, whether it's a file or a directory, until
74// the operating system is restarted.
75// Note:
76// 1) The file/directory to be deleted should exist in a temp folder.
77// 2) The directory to be deleted must be empty.
78BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
79#endif
80
81// Moves the given path, whether it's a file or a directory.
82// If a simple rename is not possible, such as in the case where the paths are
83// on different volumes, this will attempt to copy and delete. Returns
84// true for success.
85// This function fails if either path contains traversal components ('..').
86BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path);
87
88// Renames file |from_path| to |to_path|. Both paths must be on the same
89// volume, or the function will fail. Destination file will be created
90// if it doesn't exist. Prefer this function over Move when dealing with
91// temporary files. On Windows it preserves attributes of the target file.
92// Returns true on success, leaving *error unchanged.
93// Returns false on failure and sets *error appropriately, if it is non-NULL.
94BASE_EXPORT bool ReplaceFile(const FilePath& from_path,
95 const FilePath& to_path,
96 File::Error* error);
97
Lei Zhang6a0e47ca2017-10-02 23:02:1698// Copies a single file. Use CopyDirectory() to copy directories.
[email protected]e3177dd52014-08-13 20:22:1499// This function fails if either path contains traversal components ('..').
Lei Zhang6a0e47ca2017-10-02 23:02:16100// This function also fails if |to_path| is a directory.
[email protected]e3177dd52014-08-13 20:22:14101//
Lei Zhang6a0e47ca2017-10-02 23:02:16102// On POSIX, if |to_path| is a symlink, CopyFile() will follow the symlink. This
103// may have security implications. Use with care.
104//
105// If |to_path| already exists and is a regular file, it will be overwritten,
106// though its permissions will stay the same.
107//
108// If |to_path| does not exist, it will be created. The new file's permissions
109// varies per platform:
110//
111// - This function keeps the metadata on Windows. The read only bit is not kept.
112// - On Mac and iOS, |to_path| retains |from_path|'s permissions, except user
113// read/write permissions are always set.
114// - On Linux and Android, |to_path| has user read/write permissions only. i.e.
115// Always 0600.
116// - On ChromeOS, |to_path| has user read/write permissions and group/others
117// read permissions. i.e. Always 0644.
[email protected]e3177dd52014-08-13 20:22:14118BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path);
119
120// Copies the given path, and optionally all subdirectories and their contents
121// as well.
122//
123// If there are files existing under to_path, always overwrite. Returns true
124// if successful, false otherwise. Wildcards on the names are not supported.
125//
Eric Carusobca21fc582017-09-28 00:34:34126// This function has the same metadata behavior as CopyFile().
[email protected]e3177dd52014-08-13 20:22:14127//
128// If you only need to copy a file use CopyFile, it's faster.
129BASE_EXPORT bool CopyDirectory(const FilePath& from_path,
130 const FilePath& to_path,
131 bool recursive);
132
Eric Caruso128f0dd2017-12-16 01:32:49133// Like CopyDirectory() except trying to overwrite an existing file will not
134// work and will return false.
135BASE_EXPORT bool CopyDirectoryExcl(const FilePath& from_path,
136 const FilePath& to_path,
137 bool recursive);
138
[email protected]e3177dd52014-08-13 20:22:14139// Returns true if the given path exists on the local filesystem,
140// false otherwise.
141BASE_EXPORT bool PathExists(const FilePath& path);
142
143// Returns true if the given path is writable by the user, false otherwise.
144BASE_EXPORT bool PathIsWritable(const FilePath& path);
145
146// Returns true if the given path exists and is a directory, false otherwise.
147BASE_EXPORT bool DirectoryExists(const FilePath& path);
148
149// Returns true if the contents of the two files given are equal, false
150// otherwise. If either file can't be read, returns false.
151BASE_EXPORT bool ContentsEqual(const FilePath& filename1,
152 const FilePath& filename2);
153
154// Returns true if the contents of the two text files given are equal, false
155// otherwise. This routine treats "\r\n" and "\n" as equivalent.
156BASE_EXPORT bool TextContentsEqual(const FilePath& filename1,
157 const FilePath& filename2);
158
159// Reads the file at |path| into |contents| and returns true on success and
160// false on error. For security reasons, a |path| containing path traversal
161// components ('..') is treated as a read error and |contents| is set to empty.
162// In case of I/O error, |contents| holds the data that could be read from the
163// file before the error occurred.
164// |contents| may be NULL, in which case this function is useful for its side
165// effect of priming the disk cache (could be used for unit tests).
166BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents);
167
168// Reads the file at |path| into |contents| and returns true on success and
169// false on error. For security reasons, a |path| containing path traversal
170// components ('..') is treated as a read error and |contents| is set to empty.
171// In case of I/O error, |contents| holds the data that could be read from the
172// file before the error occurred. When the file size exceeds |max_size|, the
173// function returns false with |contents| holding the file truncated to
174// |max_size|.
175// |contents| may be NULL, in which case this function is useful for its side
176// effect of priming the disk cache (could be used for unit tests).
hashimoto6da2fef2016-02-24 03:39:58177BASE_EXPORT bool ReadFileToStringWithMaxSize(const FilePath& path,
178 std::string* contents,
179 size_t max_size);
[email protected]e3177dd52014-08-13 20:22:14180
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39181#if defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]e3177dd52014-08-13 20:22:14182
183// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
184// in |buffer|. This function is protected against EINTR and partial reads.
185// Returns true iff |bytes| bytes have been successfully read from |fd|.
186BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes);
187
Wez45a7b302018-02-22 22:42:39188// Performs the same function as CreateAndOpenTemporaryFileInDir(), but returns
Lei Zhangd22c9732019-08-02 16:31:38189// the file-descriptor wrapped in a ScopedFD, rather than wrapped in a FILE.
190BASE_EXPORT ScopedFD CreateAndOpenFdForTemporaryFileInDir(const FilePath& dir,
191 FilePath* path);
Wez45a7b302018-02-22 22:42:39192
Lei Zhangd22c9732019-08-02 16:31:38193#endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39194
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18195#if defined(OS_POSIX)
Kevin Marshalla9f05ec2017-07-14 02:10:05196
[email protected]e3177dd52014-08-13 20:22:14197// Creates a symbolic link at |symlink| pointing to |target|. Returns
198// false on failure.
199BASE_EXPORT bool CreateSymbolicLink(const FilePath& target,
200 const FilePath& symlink);
201
202// Reads the given |symlink| and returns where it points to in |target|.
203// Returns false upon failure.
204BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
205
206// Bits and masks of the file permission.
207enum FilePermissionBits {
208 FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO,
209 FILE_PERMISSION_USER_MASK = S_IRWXU,
210 FILE_PERMISSION_GROUP_MASK = S_IRWXG,
211 FILE_PERMISSION_OTHERS_MASK = S_IRWXO,
212
213 FILE_PERMISSION_READ_BY_USER = S_IRUSR,
214 FILE_PERMISSION_WRITE_BY_USER = S_IWUSR,
215 FILE_PERMISSION_EXECUTE_BY_USER = S_IXUSR,
216 FILE_PERMISSION_READ_BY_GROUP = S_IRGRP,
217 FILE_PERMISSION_WRITE_BY_GROUP = S_IWGRP,
218 FILE_PERMISSION_EXECUTE_BY_GROUP = S_IXGRP,
219 FILE_PERMISSION_READ_BY_OTHERS = S_IROTH,
220 FILE_PERMISSION_WRITE_BY_OTHERS = S_IWOTH,
221 FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH,
222};
223
224// Reads the permission of the given |path|, storing the file permission
225// bits in |mode|. If |path| is symbolic link, |mode| is the permission of
226// a file which the symlink points to.
227BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode);
228// Sets the permission of the given |path|. If |path| is symbolic link, sets
229// the permission of a file which the symlink points to.
230BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode);
231
thomasanderson1929bb22016-06-24 02:01:00232// Returns true iff |executable| can be found in any directory specified by the
233// environment variable in |env|.
234BASE_EXPORT bool ExecutableExistsInPath(Environment* env,
235 const FilePath::StringType& executable);
236
Sergey Volk76166f5b2018-06-15 17:38:02237#if defined(OS_LINUX) || defined(OS_AIX)
238// Determine if files under a given |path| can be mapped and then mprotect'd
239// PROT_EXEC. This depends on the mount options used for |path|, which vary
240// among different Linux distributions and possibly local configuration. It also
241// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
242// but its kernel allows mprotect with PROT_EXEC anyway.
243BASE_EXPORT bool IsPathExecutable(const FilePath& path);
244#endif // OS_LINUX || OS_AIX
245
Fabrice de Gans-Riberi65421f62018-05-22 23:16:18246#endif // OS_POSIX
[email protected]e3177dd52014-08-13 20:22:14247
248// Returns true if the given directory is empty
249BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path);
250
251// Get the temporary directory provided by the system.
252//
253// WARNING: In general, you should use CreateTemporaryFile variants below
254// instead of this function. Those variants will ensure that the proper
255// permissions are set so that other users on the system can't edit them while
256// they're open (which can lead to security issues).
257BASE_EXPORT bool GetTempDir(FilePath* path);
258
259// Get the home directory. This is more complicated than just getenv("HOME")
260// as it knows to fall back on getpwent() etc.
261//
262// You should not generally call this directly. Instead use DIR_HOME with the
263// path service which will use this function but cache the value.
264// Path service may also override DIR_HOME.
265BASE_EXPORT FilePath GetHomeDir();
266
267// Creates a temporary file. The full path is placed in |path|, and the
268// function returns true if was successful in creating the file. The file will
269// be empty and all handles closed after this function returns.
270BASE_EXPORT bool CreateTemporaryFile(FilePath* path);
271
272// Same as CreateTemporaryFile but the file is created in |dir|.
273BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir,
274 FilePath* temp_file);
275
276// Create and open a temporary file. File is opened for read/write.
277// The full path is placed in |path|.
278// Returns a handle to the opened file or NULL if an error occurred.
279BASE_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path);
280
281// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
282BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
283 FilePath* path);
284
285// Create a new directory. If prefix is provided, the new directory name is in
286// the format of prefixyyyy.
287// NOTE: prefix is ignored in the POSIX implementation.
288// If success, return true and output the full path of the directory created.
289BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix,
290 FilePath* new_temp_path);
291
292// Create a directory within another directory.
293// Extra characters will be appended to |prefix| to ensure that the
294// new directory does not have the same name as an existing directory.
295BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir,
296 const FilePath::StringType& prefix,
297 FilePath* new_dir);
298
299// Creates a directory, as well as creating any parent directories, if they
300// don't exist. Returns 'true' on successful creation, or if the directory
301// already exists. The directory is only readable by the current user.
302// Returns true on success, leaving *error unchanged.
303// Returns false on failure and sets *error appropriately, if it is non-NULL.
304BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
305 File::Error* error);
306
307// Backward-compatible convenience method for the above.
308BASE_EXPORT bool CreateDirectory(const FilePath& full_path);
309
310// Returns the file size. Returns true on success.
avi543540e2015-12-24 05:15:32311BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64_t* file_size);
[email protected]e3177dd52014-08-13 20:22:14312
313// Sets |real_path| to |path| with symbolic links and junctions expanded.
314// On windows, make sure the path starts with a lettered drive.
315// |path| must reference a file. Function will fail if |path| points to
316// a directory or to a nonexistent path. On windows, this function will
Alex Gough84ee78c2019-06-06 22:46:23317// fail if |real_path| would be longer than MAX_PATH characters.
[email protected]e3177dd52014-08-13 20:22:14318BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
319
320#if defined(OS_WIN)
321
322// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
323// return in |drive_letter_path| the equivalent path that starts with
324// a drive letter ("C:\..."). Return false if no such path exists.
325BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path,
326 FilePath* drive_letter_path);
327
Bruce Longc343f7e2019-05-11 02:20:12328// Method that wraps the win32 GetLongPathName API, normalizing the specified
329// path to its long form. An example where this is needed is when comparing
330// temp file paths. If a username isn't a valid 8.3 short file name (even just a
331// lengthy name like "user with long name"), Windows will set the TMP and TEMP
332// environment variables to be 8.3 paths. ::GetTempPath (called in
333// base::GetTempDir) just uses the value specified by TMP or TEMP, and so can
334// return a short path. Returns an empty path on error.
335BASE_EXPORT FilePath MakeLongFilePath(const FilePath& input);
[email protected]e3177dd52014-08-13 20:22:14336#endif
337
338// This function will return if the given file is a symlink or not.
339BASE_EXPORT bool IsLink(const FilePath& file_path);
340
341// Returns information about the given file path.
342BASE_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info);
343
344// Sets the time of the last access and the time of the last modification.
345BASE_EXPORT bool TouchFile(const FilePath& path,
346 const Time& last_accessed,
347 const Time& last_modified);
348
grtd0bc44f2017-02-13 17:52:17349// Wrapper for fopen-like calls. Returns non-NULL FILE* on success. The
350// underlying file descriptor (POSIX) or handle (Windows) is unconditionally
351// configured to not be propagated to child processes.
[email protected]e3177dd52014-08-13 20:22:14352BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode);
353
354// Closes file opened by OpenFile. Returns true on success.
355BASE_EXPORT bool CloseFile(FILE* file);
356
357// Associates a standard FILE stream with an existing File. Note that this
358// functions take ownership of the existing File.
359BASE_EXPORT FILE* FileToFILE(File file, const char* mode);
360
361// Truncates an open file to end at the location of the current file pointer.
362// This is a cross-platform analog to Windows' SetEndOfFile() function.
363BASE_EXPORT bool TruncateFile(FILE* file);
364
365// Reads at most the given number of bytes from the file into the buffer.
366// Returns the number of read bytes, or -1 on error.
367BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int max_size);
368
369// Writes the given buffer into the file, overwriting any data that was
370// previously there. Returns the number of bytes written, or -1 on error.
371BASE_EXPORT int WriteFile(const FilePath& filename, const char* data,
372 int size);
373
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39374#if defined(OS_POSIX) || defined(OS_FUCHSIA)
chirantan75ea2fd2014-10-07 23:15:30375// Appends |data| to |fd|. Does not close |fd| when done. Returns true iff
376// |size| bytes of |data| were written to |fd|.
377BASE_EXPORT bool WriteFileDescriptor(const int fd, const char* data, int size);
Alex Ilin925adf02019-04-26 11:03:25378
379// Allocates disk space for the file referred to by |fd| for the byte range
380// starting at |offset| and continuing for |size| bytes. The file size will be
381// changed if |offset|+|len| is greater than the file size. Zeros will fill the
382// new space.
383// After a successful call, subsequent writes into the specified range are
384// guaranteed not to fail because of lack of disk space.
385BASE_EXPORT bool AllocateFileRegion(File* file, int64_t offset, size_t size);
[email protected]e3177dd52014-08-13 20:22:14386#endif
387
chirantan75ea2fd2014-10-07 23:15:30388// Appends |data| to |filename|. Returns true iff |size| bytes of |data| were
389// written to |filename|.
390BASE_EXPORT bool AppendToFile(const FilePath& filename,
391 const char* data,
392 int size);
[email protected]e3177dd52014-08-13 20:22:14393
394// Gets the current working directory for the process.
395BASE_EXPORT bool GetCurrentDirectory(FilePath* path);
396
397// Sets the current working directory for the process.
398BASE_EXPORT bool SetCurrentDirectory(const FilePath& path);
399
Greg Thompsonb4abcb42019-08-23 01:42:56400// The largest value attempted by GetUniquePath{Number,}.
401enum { kMaxUniqueFiles = 100 };
[email protected]e3177dd52014-08-13 20:22:14402
Greg Thompsonb4abcb42019-08-23 01:42:56403// Returns the number N that makes |path| unique when formatted as " (N)" in a
404// suffix to its basename before any file extension, where N is a number between
405// 1 and 100 (inclusive). Returns 0 if |path| does not exist (meaning that it is
406// unique as-is), or -1 if no such number can be found.
407BASE_EXPORT int GetUniquePathNumber(const FilePath& path);
408
409// Returns |path| if it does not exist. Otherwise, returns |path| with the
410// suffix " (N)" appended to its basename before any file extension, where N is
411// a number between 1 and 100 (inclusive). Returns an empty path if no such
412// number can be found.
Bruce Longd096e4892019-02-28 17:50:00413BASE_EXPORT FilePath GetUniquePath(const FilePath& path);
414
tfarina060df7e2015-12-16 05:15:32415// Sets the given |fd| to non-blocking mode.
416// Returns true if it was able to set it in the non-blocking mode, otherwise
417// false.
418BASE_EXPORT bool SetNonBlocking(int fd);
419
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39420#if defined(OS_POSIX) || defined(OS_FUCHSIA)
Nick Diego Yamane61bf28ba2019-03-19 14:56:37421
422// Creates a pipe. Returns true on success, otherwise false.
423// On success, |read_fd| will be set to the fd of the read side, and
424// |write_fd| will be set to the one of write side. If |non_blocking|
425// is set the pipe will be created with O_NONBLOCK|O_CLOEXEC flags set
426// otherwise flag is set to zero (default).
427BASE_EXPORT bool CreatePipe(ScopedFD* read_fd,
428 ScopedFD* write_fd,
429 bool non_blocking = false);
430
lhchavez80c77bfd2016-10-08 00:50:53431// Creates a non-blocking, close-on-exec pipe.
432// This creates a non-blocking pipe that is not intended to be shared with any
433// child process. This will be done atomically if the operating system supports
434// it. Returns true if it was able to create the pipe, otherwise false.
435BASE_EXPORT bool CreateLocalNonBlockingPipe(int fds[2]);
436
437// Sets the given |fd| to close-on-exec mode.
438// Returns true if it was able to set it in the close-on-exec mode, otherwise
439// false.
440BASE_EXPORT bool SetCloseOnExec(int fd);
441
[email protected]e3177dd52014-08-13 20:22:14442// Test that |path| can only be changed by a given user and members of
443// a given set of groups.
444// Specifically, test that all parts of |path| under (and including) |base|:
445// * Exist.
446// * Are owned by a specific user.
447// * Are not writable by all users.
448// * Are owned by a member of a given set of groups, or are not writable by
449// their group.
450// * Are not symbolic links.
451// This is useful for checking that a config file is administrator-controlled.
452// |base| must contain |path|.
453BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base,
454 const base::FilePath& path,
455 uid_t owner_uid,
456 const std::set<gid_t>& group_gids);
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39457#endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]e3177dd52014-08-13 20:22:14458
459#if defined(OS_MACOSX) && !defined(OS_IOS)
460// Is |path| writable only by a user with administrator privileges?
461// This function uses Mac OS conventions. The super user is assumed to have
462// uid 0, and the administrator group is assumed to be named "admin".
463// Testing that |path|, and every parent directory including the root of
464// the filesystem, are owned by the superuser, controlled by the group
465// "admin", are not writable by all users, and contain no symbolic links.
466// Will return false if |path| does not exist.
467BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path);
468#endif // defined(OS_MACOSX) && !defined(OS_IOS)
469
470// Returns the maximum length of path component on the volume containing
471// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
472BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path);
473
rayb0088ee52017-04-26 22:35:08474#if defined(OS_LINUX) || defined(OS_AIX)
[email protected]e3177dd52014-08-13 20:22:14475// Broad categories of file systems as returned by statfs() on Linux.
476enum FileSystemType {
477 FILE_SYSTEM_UNKNOWN, // statfs failed.
478 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
479 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
480 FILE_SYSTEM_NFS,
481 FILE_SYSTEM_SMB,
482 FILE_SYSTEM_CODA,
483 FILE_SYSTEM_MEMORY, // in-memory file system
484 FILE_SYSTEM_CGROUP, // cgroup control.
485 FILE_SYSTEM_OTHER, // any other value.
486 FILE_SYSTEM_TYPE_COUNT
487};
488
489// Attempts determine the FileSystemType for |path|.
490// Returns false if |path| doesn't exist.
491BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type);
492#endif
493
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39494#if defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]e3177dd52014-08-13 20:22:14495// Get a temporary directory for shared memory files. The directory may depend
496// on whether the destination is intended for executable files, which in turn
497// depends on how /dev/shmem was mounted. As a result, you must supply whether
498// you intend to create executable shmem segments so this function can find
499// an appropriate location.
500BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
501#endif
502
503// Internal --------------------------------------------------------------------
504
505namespace internal {
506
507// Same as Move but allows paths with traversal components.
508// Use only with extreme care.
509BASE_EXPORT bool MoveUnsafe(const FilePath& from_path,
510 const FilePath& to_path);
511
[email protected]e3177dd52014-08-13 20:22:14512#if defined(OS_WIN)
513// Copy from_path to to_path recursively and then delete from_path recursively.
514// Returns true if all operations succeed.
515// This function simulates Move(), but unlike Move() it works across volumes.
516// This function is not transactional.
517BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
518 const FilePath& to_path);
519#endif // defined(OS_WIN)
520
521} // namespace internal
522} // namespace base
523
524#endif // BASE_FILES_FILE_UTIL_H_