15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file contains utility functions for dealing with the local
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// filesystem.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_FILE_UTIL_H_
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_FILE_UTIL_H_
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_POSIX)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/stat.h>
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/platform_file.h"
315e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string16.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_descriptor_posix.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/posix/eintr_wrapper.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass Time;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//-----------------------------------------------------------------------------
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Functions that involve filesystem access or modification:
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns an absolute version of a relative path. Returns an empty path on
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// error. On POSIX, this function fails if the path does not exist. This
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// function can result in I/O so it can be slow.
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the total number of bytes used by all the files under |root_path|.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If the path does not exist the function returns 0.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function is implemented using the FileEnumerator class so it is not
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// particularly speedy in any platform.
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT int64 ComputeDirectorySize(const FilePath& root_path);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Deletes the given path, whether it's a file or a directory.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If it's a directory, it's perfectly happy to delete all of the
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// directory's contents.  Passing true to recursive deletes
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// subdirectories and their contents as well.
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Returns true if successful, false otherwise. It is considered successful
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// to attempt to delete a file that does not exist.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In posix environment and if |path| is a symbolic link, this deletes only
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the symlink. (even if the symlink points to a non-existent file)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//          TO "rm -rf", SO USE WITH CAUTION.
707dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Schedules to delete the given path, whether it's a file or a directory, until
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the operating system is restarted.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note:
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1) The file/directory to be deleted should exist in a temp folder.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2) The directory to be deleted must be empty.
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Moves the given path, whether it's a file or a directory.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a simple rename is not possible, such as in the case where the paths are
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on different volumes, this will attempt to copy and delete. Returns
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// true for success.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This function fails if either path contains traversal components ('..').
86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Renames file |from_path| to |to_path|. Both paths must be on the same
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// volume, or the function will fail. Destination file will be created
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// if it doesn't exist. Prefer this function over Move when dealing with
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// temporary files. On Windows it preserves attributes of the target file.
92b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Returns true on success, leaving *error unchanged.
93b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Returns false on failure and sets *error appropriately, if it is non-NULL.
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochBASE_EXPORT bool ReplaceFile(const FilePath& from_path,
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             const FilePath& to_path,
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             PlatformFileError* error);
97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copies a single file. Use CopyDirectory to copy directories.
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This function fails if either path contains traversal components ('..').
1007dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copies the given path, and optionally all subdirectories and their contents
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as well.
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// If there are files existing under to_path, always overwrite. Returns true
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// if successful, false otherwise. Wildcards on the names are not supported.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If you only need to copy a file use CopyFile, it's faster.
1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool CopyDirectory(const FilePath& from_path,
1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                               const FilePath& to_path,
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               bool recursive);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the given path exists on the local filesystem,
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// false otherwise.
1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool PathExists(const FilePath& path);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the given path is writable by the user, false otherwise.
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool PathIsWritable(const FilePath& path);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the given path exists and is a directory, false otherwise.
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool DirectoryExists(const FilePath& path);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the contents of the two files given are equal, false
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// otherwise.  If either file can't be read, returns false.
1257dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool ContentsEqual(const FilePath& filename1,
1267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                               const FilePath& filename2);
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the contents of the two text files given are equal, false
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool TextContentsEqual(const FilePath& filename1,
1317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                   const FilePath& filename2);
1327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Read the file at |path| into |contents|, returning true on success.
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This function fails if the |path| contains path traversal components ('..').
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |contents| may be NULL, in which case this function is useful for its
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// side effect of priming the disk cache.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Useful for unit tests.
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents);
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in |buffer|. This function is protected against EINTR and partial reads.
144a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Returns true iff |bytes| bytes have been successfully read from |fd|.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates a symbolic link at |symlink| pointing to |target|.  Returns
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// false on failure.
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)BASE_EXPORT bool CreateSymbolicLink(const FilePath& target,
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                    const FilePath& symlink);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the given |symlink| and returns where it points to in |target|.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false upon failure.
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Bits ans masks of the file permission.
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum FilePermissionBits {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_MASK              = S_IRWXU | S_IRWXG | S_IRWXO,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_USER_MASK         = S_IRWXU,
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_GROUP_MASK        = S_IRWXG,
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_OTHERS_MASK       = S_IRWXO,
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_READ_BY_USER      = S_IRUSR,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_WRITE_BY_USER     = S_IWUSR,
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_EXECUTE_BY_USER   = S_IXUSR,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_READ_BY_GROUP     = S_IRGRP,
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_WRITE_BY_GROUP    = S_IWGRP,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_EXECUTE_BY_GROUP  = S_IXGRP,
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_READ_BY_OTHERS    = S_IROTH,
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_WRITE_BY_OTHERS   = S_IWOTH,
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH,
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the permission of the given |path|, storing the file permission
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bits in |mode|. If |path| is symbolic link, |mode| is the permission of
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a file which the symlink points to.
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the permission of the given |path|. If |path| is symbolic link, sets
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the permission of a file which the symlink points to.
180f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode);
181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // OS_POSIX
183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
184a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Returns true if the given directory is empty
185a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get the temporary directory provided by the system.
188a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
189a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// WARNING: In general, you should use CreateTemporaryFile variants below
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// instead of this function. Those variants will ensure that the proper
191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// permissions are set so that other users on the system can't edit them while
192a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// they're open (which can lead to security issues).
193a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool GetTempDir(FilePath* path);
194a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
195a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Get a temporary directory for shared memory files. The directory may depend
196a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// on whether the destination is intended for executable files, which in turn
197a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// depends on how /dev/shmem was mounted. As a result, you must supply whether
198a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// you intend to create executable shmem segments so this function can find
199a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// an appropriate location.
200a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Only useful on POSIX; redirects to GetTempDir() on Windows.
202a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
204a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if defined(OS_POSIX)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get the home directory.  This is more complicated than just getenv("HOME")
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as it knows to fall back on getpwent() etc.
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
208a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// This function is not currently implemented on Windows or Mac because we
209a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// don't use it. Generally you would use one of PathService's APP_DATA
210a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// directories on those platforms. If we need it, this could be implemented
211a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// there to return the appropriate directory.
212a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT FilePath GetHomeDir();
213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif  // OS_POSIX
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates a temporary file. The full path is placed in |path|, and the
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function returns true if was successful in creating the file. The file will
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be empty and all handles closed after this function returns.
218a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateTemporaryFile(FilePath* path);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same as CreateTemporaryFile but the file is created in |dir|.
221a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir,
222a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                          FilePath* temp_file);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Create and open a temporary file.  File is opened for read/write.
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The full path is placed in |path|.
226a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Returns a handle to the opened file or NULL if an error occurred.
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path);
228a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Like above but for shmem files.  Only useful for POSIX.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The executable flag says the file needs to support using
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// mprotect with PROT_EXEC after mapping.
232a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(FilePath* path,
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                  bool executable);
234a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
236a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
237a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                                  FilePath* path);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Create a new directory. If prefix is provided, the new directory name is in
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the format of prefixyyyy.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: prefix is ignored in the POSIX implementation.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If success, return true and output the full path of the directory created.
243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix,
244a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                        FilePath* new_temp_path);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Create a directory within another directory.
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Extra characters will be appended to |prefix| to ensure that the
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// new directory does not have the same name as an existing directory.
249a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir,
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         const FilePath::StringType& prefix,
251a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         FilePath* new_dir);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates a directory, as well as creating any parent directories, if they
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// don't exist. Returns 'true' on successful creation, or if the directory
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// already exists.  The directory is only readable by the current user.
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true on success, leaving *error unchanged.
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns false on failure and sets *error appropriately, if it is non-NULL.
258a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
259a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                            PlatformFileError* error);
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Backward-compatible convenience method for the above.
262a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool CreateDirectory(const FilePath& full_path);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the file size. Returns true on success.
265a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size);
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets |real_path| to |path| with symbolic links and junctions expanded.
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// On windows, make sure the path starts with a lettered drive.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |path| must reference a file.  Function will fail if |path| points to
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a directory or to a nonexistent path.  On windows, this function will
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// fail if |path| is a junction or symlink that points to an empty file,
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or if |real_path| would be longer than MAX_PATH characters.
273a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return in |drive_letter_path| the equivalent path that starts with
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a drive letter ("C:\...").  Return false if no such path exists.
280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path,
281a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                             FilePath* drive_letter_path);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Given an existing file in |path|, set |real_path| to the path
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in native NT format, of the form "\Device\HarddiskVolumeXX\..".
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false if the path can not be found. Empty files cannot
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be resolved with this function.
287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool NormalizeToNativeFilePath(const FilePath& path,
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                           FilePath* nt_path);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function will return if the given file is a symlink or not.
292a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool IsLink(const FilePath& file_path);
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns information about the given file path.
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* info);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the time of the last access and the time of the last modification.
298a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool TouchFile(const FilePath& path,
299a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           const Time& last_accessed,
300a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           const Time& last_modified);
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Closes file opened by OpenFile. Returns true on success.
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool CloseFile(FILE* file);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Truncates an open file to end at the location of the current file pointer.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is a cross-platform analog to Windows' SetEndOfFile() function.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool TruncateFile(FILE* file);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the given number of bytes from the file into the buffer.  Returns
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the number of read bytes, or -1 on error.
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT int ReadFile(const base::FilePath& filename, char* data, int size);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
316a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace base
317a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
318a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// -----------------------------------------------------------------------------
319a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
320a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace file_util {
321a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Writes the given buffer into the file, overwriting any data that was
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// previously there.  Returns the number of bytes written, or -1 on error.
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT int WriteFile(const base::FilePath& filename, const char* data,
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          int size);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Append the data to |fd|. Does not close |fd| when done.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Append the given buffer into the file. Returns the number of bytes written,
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or -1 on error.
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT int AppendToFile(const base::FilePath& filename,
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* data, int size);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the current working directory for the process.
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT bool GetCurrentDirectory(base::FilePath* path);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the current working directory for the process.
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT bool SetCurrentDirectory(const base::FilePath& path);
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Attempts to find a number that can be appended to the |path| to make it
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unique. If |path| does not exist, 0 is returned.  If it fails to find such
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a number, -1 is returned. If |suffix| is not empty, also checks the
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// existence of it with the given suffix.
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path,
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    const base::FilePath::StringType& suffix);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Creates a directory with a guaranteed unique name based on |path|, returning
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the pathname if successful, or an empty path if there was an error creating
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the directory. Does not create parent directories.
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT base::FilePath MakeUniqueDirectory(const base::FilePath& path);
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_POSIX)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that |path| can only be changed by a given user and members of
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a given set of groups.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specifically, test that all parts of |path| under (and including) |base|:
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Exist.
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Are owned by a specific user.
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Are not writable by all users.
362a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// * Are owned by a member of a given set of groups, or are not writable by
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   their group.
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Are not symbolic links.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is useful for checking that a config file is administrator-controlled.
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |base| must contain |path|.
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base,
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            const base::FilePath& path,
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            uid_t owner_uid,
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            const std::set<gid_t>& group_gids);
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_POSIX)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX) && !defined(OS_IOS)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Is |path| writable only by a user with administrator privileges?
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function uses Mac OS conventions.  The super user is assumed to have
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// uid 0, and the administrator group is assumed to be named "admin".
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Testing that |path|, and every parent directory including the root of
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the filesystem, are owned by the superuser, controlled by the group
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "admin", are not writable by all users, and contain no symbolic links.
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Will return false if |path| does not exist.
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path);
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns the maximum length of path component on the volume containing
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path);
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A class to handle auto-closing of FILE*'s.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ScopedFILEClose {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline void operator()(FILE* x) const {
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (x) {
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fclose(x);
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE;
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A class to handle auto-closing of FDs.
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ScopedFDClose {
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline void operator()(int* x) const {
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (x && *x >= 0) {
406a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (IGNORE_EINTR(close(*x)) < 0)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DPLOG(ERROR) << "close";
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_POSIX
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_LINUX)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Broad categories of file systems as returned by statfs() on Linux.
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum FileSystemType {
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_UNKNOWN,  // statfs failed.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_0,        // statfs.f_type == 0 means unknown, may indicate AFS.
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_ORDINARY,       // on-disk filesystem like ext2
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_NFS,
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_SMB,
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_CODA,
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_MEMORY,         // in-memory file system
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_CGROUP,         // cgroup control.
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_OTHER,          // any other value.
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILE_SYSTEM_TYPE_COUNT
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Attempts determine the FileSystemType for |path|.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns false if |path| doesn't exist.
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT bool GetFileSystemType(const base::FilePath& path,
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   FileSystemType* type);
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace file_util
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Internal --------------------------------------------------------------------
4397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochnamespace base {
4417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochnamespace internal {
4427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Same as Move but allows paths with traversal components.
4447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Use only with extreme care.
4457dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool MoveUnsafe(const FilePath& from_path,
4467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            const FilePath& to_path);
4477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Same as CopyFile but allows paths with traversal components.
4497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Use only with extreme care.
4507dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool CopyFileUnsafe(const FilePath& from_path,
4517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                const FilePath& to_path);
4527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_WIN)
4547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Copy from_path to to_path recursively and then delete from_path recursively.
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Returns true if all operations succeed.
4567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// This function simulates Move(), but unlike Move() it works across volumes.
457a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// This function is not transactional.
4587dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
4597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                        const FilePath& to_path);
4607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif  // defined(OS_WIN)
4617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace internal
4637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace base
4647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_FILE_UTIL_H_
466