file_util.h 20 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains utility functions for dealing with the local
// filesystem.

8 9
#ifndef BUTIL_FILE_UTIL_H_
#define BUTIL_FILE_UTIL_H_
gejun's avatar
gejun committed
10

11
#include "butil/build_config.h"
gejun's avatar
gejun committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25

#if defined(OS_WIN)
#include <windows.h>
#elif defined(OS_POSIX)
#include <sys/stat.h>
#include <unistd.h>
#endif

#include <stdio.h>

#include <set>
#include <string>
#include <vector>

26 27 28 29 30 31
#include "butil/base_export.h"
#include "butil/basictypes.h"
#include "butil/files/file.h"
#include "butil/files/file_path.h"
#include "butil/memory/scoped_ptr.h"
#include "butil/strings/string16.h"
gejun's avatar
gejun committed
32 33

#if defined(OS_POSIX)
34 35 36
#include "butil/file_descriptor_posix.h"
#include "butil/logging.h"
#include "butil/posix/eintr_wrapper.h"
gejun's avatar
gejun committed
37 38
#endif

39
namespace butil {
gejun's avatar
gejun committed
40 41 42 43 44 45 46 47 48

class Time;

//-----------------------------------------------------------------------------
// Functions that involve filesystem access or modification:

// Returns an absolute version of a relative path. Returns an empty path on
// error. On POSIX, this function fails if the path does not exist. This
// function can result in I/O so it can be slow.
49
BUTIL_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
gejun's avatar
gejun committed
50 51 52 53 54 55

// Returns the total number of bytes used by all the files under |root_path|.
// If the path does not exist the function returns 0.
//
// This function is implemented using the FileEnumerator class so it is not
// particularly speedy in any platform.
56
BUTIL_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path);
gejun's avatar
gejun committed
57 58 59 60 61 62 63 64 65 66 67 68 69

// Deletes the given path, whether it's a file or a directory.
// If it's a directory, it's perfectly happy to delete all of the
// directory's contents.  Passing true to recursive deletes
// subdirectories and their contents as well.
// Returns true if successful, false otherwise. It is considered successful
// to attempt to delete a file that does not exist.
//
// In posix environment and if |path| is a symbolic link, this deletes only
// the symlink. (even if the symlink points to a non-existent file)
//
// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
//          TO "rm -rf", SO USE WITH CAUTION.
70
BUTIL_EXPORT bool DeleteFile(const FilePath& path, bool recursive);
gejun's avatar
gejun committed
71 72 73 74 75 76 77

#if defined(OS_WIN)
// Schedules to delete the given path, whether it's a file or a directory, until
// the operating system is restarted.
// Note:
// 1) The file/directory to be deleted should exist in a temp folder.
// 2) The directory to be deleted must be empty.
78
BUTIL_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
gejun's avatar
gejun committed
79 80 81 82 83 84 85
#endif

// Moves the given path, whether it's a file or a directory.
// If a simple rename is not possible, such as in the case where the paths are
// on different volumes, this will attempt to copy and delete. Returns
// true for success.
// This function fails if either path contains traversal components ('..').
86
BUTIL_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path);
gejun's avatar
gejun committed
87 88 89 90 91 92 93

// Renames file |from_path| to |to_path|. Both paths must be on the same
// volume, or the function will fail. Destination file will be created
// if it doesn't exist. Prefer this function over Move when dealing with
// temporary files. On Windows it preserves attributes of the target file.
// Returns true on success, leaving *error unchanged.
// Returns false on failure and sets *error appropriately, if it is non-NULL.
94
BUTIL_EXPORT bool ReplaceFile(const FilePath& from_path,
gejun's avatar
gejun committed
95 96 97 98 99 100 101 102
                             const FilePath& to_path,
                             File::Error* error);

// Copies a single file. Use CopyDirectory to copy directories.
// This function fails if either path contains traversal components ('..').
//
// This function keeps the metadata on Windows. The read only bit on Windows is
// not kept.
103
BUTIL_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path);
gejun's avatar
gejun committed
104 105 106 107 108 109 110 111 112 113 114

// Copies the given path, and optionally all subdirectories and their contents
// as well.
//
// If there are files existing under to_path, always overwrite. Returns true
// if successful, false otherwise. Wildcards on the names are not supported.
//
// This function calls into CopyFile() so the same behavior w.r.t. metadata
// applies.
//
// If you only need to copy a file use CopyFile, it's faster.
115
BUTIL_EXPORT bool CopyDirectory(const FilePath& from_path,
gejun's avatar
gejun committed
116 117 118 119 120
                               const FilePath& to_path,
                               bool recursive);

// Returns true if the given path exists on the local filesystem,
// false otherwise.
121
BUTIL_EXPORT bool PathExists(const FilePath& path);
gejun's avatar
gejun committed
122 123

// Returns true if the given path is writable by the user, false otherwise.
124
BUTIL_EXPORT bool PathIsWritable(const FilePath& path);
gejun's avatar
gejun committed
125 126

// Returns true if the given path exists and is a directory, false otherwise.
127
BUTIL_EXPORT bool DirectoryExists(const FilePath& path);
gejun's avatar
gejun committed
128 129 130

// Returns true if the contents of the two files given are equal, false
// otherwise.  If either file can't be read, returns false.
131
BUTIL_EXPORT bool ContentsEqual(const FilePath& filename1,
gejun's avatar
gejun committed
132 133 134 135
                               const FilePath& filename2);

// Returns true if the contents of the two text files given are equal, false
// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
136
BUTIL_EXPORT bool TextContentsEqual(const FilePath& filename1,
gejun's avatar
gejun committed
137 138 139 140 141 142 143 144 145
                                   const FilePath& filename2);

// Reads the file at |path| into |contents| and returns true on success and
// false on error.  For security reasons, a |path| containing path traversal
// components ('..') is treated as a read error and |contents| is set to empty.
// In case of I/O error, |contents| holds the data that could be read from the
// file before the error occurred.
// |contents| may be NULL, in which case this function is useful for its side
// effect of priming the disk cache (could be used for unit tests).
146
BUTIL_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents);
gejun's avatar
gejun committed
147 148 149 150 151 152 153 154 155 156

// Reads the file at |path| into |contents| and returns true on success and
// false on error.  For security reasons, a |path| containing path traversal
// components ('..') is treated as a read error and |contents| is set to empty.
// In case of I/O error, |contents| holds the data that could be read from the
// file before the error occurred.  When the file size exceeds |max_size|, the
// function returns false with |contents| holding the file truncated to
// |max_size|.
// |contents| may be NULL, in which case this function is useful for its side
// effect of priming the disk cache (could be used for unit tests).
157
BUTIL_EXPORT bool ReadFileToString(const FilePath& path,
gejun's avatar
gejun committed
158 159 160 161 162 163 164 165
                                  std::string* contents,
                                  size_t max_size);

#if defined(OS_POSIX)

// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
// in |buffer|. This function is protected against EINTR and partial reads.
// Returns true iff |bytes| bytes have been successfully read from |fd|.
166
BUTIL_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes);
gejun's avatar
gejun committed
167 168 169

// Creates a symbolic link at |symlink| pointing to |target|.  Returns
// false on failure.
170
BUTIL_EXPORT bool CreateSymbolicLink(const FilePath& target,
gejun's avatar
gejun committed
171 172 173 174
                                    const FilePath& symlink);

// Reads the given |symlink| and returns where it points to in |target|.
// Returns false upon failure.
175
BUTIL_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
gejun's avatar
gejun committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

// Bits and masks of the file permission.
enum FilePermissionBits {
  FILE_PERMISSION_MASK              = S_IRWXU | S_IRWXG | S_IRWXO,
  FILE_PERMISSION_USER_MASK         = S_IRWXU,
  FILE_PERMISSION_GROUP_MASK        = S_IRWXG,
  FILE_PERMISSION_OTHERS_MASK       = S_IRWXO,

  FILE_PERMISSION_READ_BY_USER      = S_IRUSR,
  FILE_PERMISSION_WRITE_BY_USER     = S_IWUSR,
  FILE_PERMISSION_EXECUTE_BY_USER   = S_IXUSR,
  FILE_PERMISSION_READ_BY_GROUP     = S_IRGRP,
  FILE_PERMISSION_WRITE_BY_GROUP    = S_IWGRP,
  FILE_PERMISSION_EXECUTE_BY_GROUP  = S_IXGRP,
  FILE_PERMISSION_READ_BY_OTHERS    = S_IROTH,
  FILE_PERMISSION_WRITE_BY_OTHERS   = S_IWOTH,
  FILE_PERMISSION_EXECUTE_BY_OTHERS = S_IXOTH,
};

// Reads the permission of the given |path|, storing the file permission
// bits in |mode|. If |path| is symbolic link, |mode| is the permission of
// a file which the symlink points to.
198
BUTIL_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode);
gejun's avatar
gejun committed
199 200
// Sets the permission of the given |path|. If |path| is symbolic link, sets
// the permission of a file which the symlink points to.
201
BUTIL_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode);
gejun's avatar
gejun committed
202 203 204 205

#endif  // OS_POSIX

// Returns true if the given directory is empty
206
BUTIL_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path);
gejun's avatar
gejun committed
207 208 209 210 211 212 213

// Get the temporary directory provided by the system.
//
// WARNING: In general, you should use CreateTemporaryFile variants below
// instead of this function. Those variants will ensure that the proper
// permissions are set so that other users on the system can't edit them while
// they're open (which can lead to security issues).
214
BUTIL_EXPORT bool GetTempDir(FilePath* path);
gejun's avatar
gejun committed
215 216 217 218 219 220 221

// Get the home directory. This is more complicated than just getenv("HOME")
// as it knows to fall back on getpwent() etc.
//
// You should not generally call this directly. Instead use DIR_HOME with the
// path service which will use this function but cache the value.
// Path service may also override DIR_HOME.
222
BUTIL_EXPORT FilePath GetHomeDir();
gejun's avatar
gejun committed
223 224 225 226

// Creates a temporary file. The full path is placed in |path|, and the
// function returns true if was successful in creating the file. The file will
// be empty and all handles closed after this function returns.
227
BUTIL_EXPORT bool CreateTemporaryFile(FilePath* path);
gejun's avatar
gejun committed
228 229

// Same as CreateTemporaryFile but the file is created in |dir|.
230
BUTIL_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir,
gejun's avatar
gejun committed
231 232 233 234 235
                                          FilePath* temp_file);

// Create and open a temporary file.  File is opened for read/write.
// The full path is placed in |path|.
// Returns a handle to the opened file or NULL if an error occurred.
236
BUTIL_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path);
gejun's avatar
gejun committed
237 238

// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
239
BUTIL_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
gejun's avatar
gejun committed
240 241 242 243 244 245
                                                  FilePath* path);

// Create a new directory. If prefix is provided, the new directory name is in
// the format of prefixyyyy.
// NOTE: prefix is ignored in the POSIX implementation.
// If success, return true and output the full path of the directory created.
246
BUTIL_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix,
gejun's avatar
gejun committed
247 248 249 250 251
                                        FilePath* new_temp_path);

// Create a directory within another directory.
// Extra characters will be appended to |prefix| to ensure that the
// new directory does not have the same name as an existing directory.
252
BUTIL_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir,
gejun's avatar
gejun committed
253 254 255 256 257 258 259 260 261 262
                                         const FilePath::StringType& prefix,
                                         FilePath* new_dir);

// Creates a directory, as well as creating any parent directories by default,
// if they don't exist by default. If |create_parents| is false and the parent
// doesn't exists, false would be returned.
// Returns 'true' on successful creation, or if the directory already exists.
// The directory is readable for all the users.
// Returns true on success, leaving *error unchanged.
// Returns false on failure and sets *error appropriately, if it is non-NULL.
263
BUTIL_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
gejun's avatar
gejun committed
264
                                            File::Error* error);
265
BUTIL_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
gejun's avatar
gejun committed
266 267 268 269
                                            File::Error* error,
                                            bool create_parents);

// Backward-compatible convenience method for the above.
270 271
BUTIL_EXPORT bool CreateDirectory(const FilePath& full_path);
BUTIL_EXPORT bool CreateDirectory(const FilePath& full_path, bool create_parents);
gejun's avatar
gejun committed
272 273 274


// Returns the file size. Returns true on success.
275
BUTIL_EXPORT bool GetFileSize(const FilePath& file_path, int64_t* file_size);
gejun's avatar
gejun committed
276 277 278 279 280 281 282

// Sets |real_path| to |path| with symbolic links and junctions expanded.
// On windows, make sure the path starts with a lettered drive.
// |path| must reference a file.  Function will fail if |path| points to
// a directory or to a nonexistent path.  On windows, this function will
// fail if |path| is a junction or symlink that points to an empty file,
// or if |real_path| would be longer than MAX_PATH characters.
283
BUTIL_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
gejun's avatar
gejun committed
284 285 286 287 288 289

#if defined(OS_WIN)

// Given a path in NT native form ("\Device\HarddiskVolumeXX\..."),
// return in |drive_letter_path| the equivalent path that starts with
// a drive letter ("C:\...").  Return false if no such path exists.
290
BUTIL_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path,
gejun's avatar
gejun committed
291 292 293 294 295 296
                                             FilePath* drive_letter_path);

// Given an existing file in |path|, set |real_path| to the path
// in native NT format, of the form "\Device\HarddiskVolumeXX\..".
// Returns false if the path can not be found. Empty files cannot
// be resolved with this function.
297
BUTIL_EXPORT bool NormalizeToNativeFilePath(const FilePath& path,
gejun's avatar
gejun committed
298 299 300 301
                                           FilePath* nt_path);
#endif

// This function will return if the given file is a symlink or not.
302
BUTIL_EXPORT bool IsLink(const FilePath& file_path);
gejun's avatar
gejun committed
303 304

// Returns information about the given file path.
305
BUTIL_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info);
gejun's avatar
gejun committed
306 307

// Sets the time of the last access and the time of the last modification.
308
BUTIL_EXPORT bool TouchFile(const FilePath& path,
gejun's avatar
gejun committed
309 310 311 312
                           const Time& last_accessed,
                           const Time& last_modified);

// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
313
BUTIL_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode);
gejun's avatar
gejun committed
314 315

// Closes file opened by OpenFile. Returns true on success.
316
BUTIL_EXPORT bool CloseFile(FILE* file);
gejun's avatar
gejun committed
317 318 319

// Associates a standard FILE stream with an existing File. Note that this
// functions take ownership of the existing File.
320
BUTIL_EXPORT FILE* FileToFILE(File file, const char* mode);
gejun's avatar
gejun committed
321 322 323

// Truncates an open file to end at the location of the current file pointer.
// This is a cross-platform analog to Windows' SetEndOfFile() function.
324
BUTIL_EXPORT bool TruncateFile(FILE* file);
gejun's avatar
gejun committed
325 326 327

// Reads at most the given number of bytes from the file into the buffer.
// Returns the number of read bytes, or -1 on error.
328
BUTIL_EXPORT int ReadFile(const FilePath& filename, char* data, int max_size);
gejun's avatar
gejun committed
329 330 331

// Writes the given buffer into the file, overwriting any data that was
// previously there.  Returns the number of bytes written, or -1 on error.
332
BUTIL_EXPORT int WriteFile(const FilePath& filename, const char* data,
gejun's avatar
gejun committed
333 334 335 336
                          int size);

#if defined(OS_POSIX)
// Append the data to |fd|. Does not close |fd| when done.
337
BUTIL_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size);
gejun's avatar
gejun committed
338 339 340 341
#endif

// Append the given buffer into the file. Returns the number of bytes written,
// or -1 on error.
342
BUTIL_EXPORT int AppendToFile(const FilePath& filename,
gejun's avatar
gejun committed
343 344 345
                             const char* data, int size);

// Gets the current working directory for the process.
346
BUTIL_EXPORT bool GetCurrentDirectory(FilePath* path);
gejun's avatar
gejun committed
347 348

// Sets the current working directory for the process.
349
BUTIL_EXPORT bool SetCurrentDirectory(const FilePath& path);
gejun's avatar
gejun committed
350 351 352 353 354

// Attempts to find a number that can be appended to the |path| to make it
// unique. If |path| does not exist, 0 is returned.  If it fails to find such
// a number, -1 is returned. If |suffix| is not empty, also checks the
// existence of it with the given suffix.
355
BUTIL_EXPORT int GetUniquePathNumber(const FilePath& path,
gejun's avatar
gejun committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369
                                    const FilePath::StringType& suffix);

#if defined(OS_POSIX)
// Test that |path| can only be changed by a given user and members of
// a given set of groups.
// Specifically, test that all parts of |path| under (and including) |base|:
// * Exist.
// * Are owned by a specific user.
// * Are not writable by all users.
// * Are owned by a member of a given set of groups, or are not writable by
//   their group.
// * Are not symbolic links.
// This is useful for checking that a config file is administrator-controlled.
// |base| must contain |path|.
370
BUTIL_EXPORT bool VerifyPathControlledByUser(const butil::FilePath& base,
371
                                            const butil::FilePath& path,
gejun's avatar
gejun committed
372 373 374 375 376 377 378 379 380 381 382 383
                                            uid_t owner_uid,
                                            const std::set<gid_t>& group_gids);
#endif  // defined(OS_POSIX)

#if defined(OS_MACOSX) && !defined(OS_IOS)
// Is |path| writable only by a user with administrator privileges?
// This function uses Mac OS conventions.  The super user is assumed to have
// uid 0, and the administrator group is assumed to be named "admin".
// Testing that |path|, and every parent directory including the root of
// the filesystem, are owned by the superuser, controlled by the group
// "admin", are not writable by all users, and contain no symbolic links.
// Will return false if |path| does not exist.
384
BUTIL_EXPORT bool VerifyPathControlledByAdmin(const butil::FilePath& path);
gejun's avatar
gejun committed
385 386 387 388
#endif  // defined(OS_MACOSX) && !defined(OS_IOS)

// Returns the maximum length of path component on the volume containing
// the directory |path|, in the number of FilePath::CharType, or -1 on failure.
389
BUTIL_EXPORT int GetMaximumPathComponentLength(const butil::FilePath& path);
gejun's avatar
gejun committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

#if defined(OS_LINUX)
// Broad categories of file systems as returned by statfs() on Linux.
enum FileSystemType {
  FILE_SYSTEM_UNKNOWN,  // statfs failed.
  FILE_SYSTEM_0,        // statfs.f_type == 0 means unknown, may indicate AFS.
  FILE_SYSTEM_ORDINARY,       // on-disk filesystem like ext2
  FILE_SYSTEM_NFS,
  FILE_SYSTEM_SMB,
  FILE_SYSTEM_CODA,
  FILE_SYSTEM_MEMORY,         // in-memory file system
  FILE_SYSTEM_CGROUP,         // cgroup control.
  FILE_SYSTEM_OTHER,          // any other value.
  FILE_SYSTEM_TYPE_COUNT
};

// Attempts determine the FileSystemType for |path|.
// Returns false if |path| doesn't exist.
408
BUTIL_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type);
gejun's avatar
gejun committed
409 410 411 412 413 414 415 416
#endif

#if defined(OS_POSIX)
// Get a temporary directory for shared memory files. The directory may depend
// on whether the destination is intended for executable files, which in turn
// depends on how /dev/shmem was mounted. As a result, you must supply whether
// you intend to create executable shmem segments so this function can find
// an appropriate location.
417
BUTIL_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
gejun's avatar
gejun committed
418 419
#endif

420
}  // namespace butil
gejun's avatar
gejun committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

// -----------------------------------------------------------------------------

namespace file_util {

// Functor for |ScopedFILE| (below).
struct ScopedFILEClose {
  inline void operator()(FILE* x) const {
    if (x)
      fclose(x);
  }
};

// Automatically closes |FILE*|s.
typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE;

}  // namespace file_util

// Internal --------------------------------------------------------------------

441
namespace butil {
gejun's avatar
gejun committed
442 443 444 445
namespace internal {

// Same as Move but allows paths with traversal components.
// Use only with extreme care.
446
BUTIL_EXPORT bool MoveUnsafe(const FilePath& from_path,
gejun's avatar
gejun committed
447 448 449 450
                            const FilePath& to_path);

// Same as CopyFile but allows paths with traversal components.
// Use only with extreme care.
451
BUTIL_EXPORT bool CopyFileUnsafe(const FilePath& from_path,
gejun's avatar
gejun committed
452 453 454 455 456 457 458
                                const FilePath& to_path);

#if defined(OS_WIN)
// Copy from_path to to_path recursively and then delete from_path recursively.
// Returns true if all operations succeed.
// This function simulates Move(), but unlike Move() it works across volumes.
// This function is not transactional.
459
BUTIL_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
gejun's avatar
gejun committed
460 461 462 463
                                        const FilePath& to_path);
#endif  // defined(OS_WIN)

}  // namespace internal
464
}  // namespace butil
gejun's avatar
gejun committed
465

466
#endif  // BUTIL_FILE_UTIL_H_