123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- #ifndef BASE_FILES_FILE_UTIL_H_
- #define BASE_FILES_FILE_UTIL_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <limits>
- #include <set>
- #include <string>
- #include <vector>
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- #include <sys/stat.h>
- #include <unistd.h>
- #endif
- #include "base/base_export.h"
- #include "base/callback_forward.h"
- #include "base/containers/span.h"
- #include "base/files/file.h"
- #include "base/files/file_path.h"
- #include "base/files/scoped_file.h"
- #include "base/strings/string16.h"
- #include "build/build_config.h"
- #if defined(OS_WIN)
- #include "base/win/windows_types.h"
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- #include "base/file_descriptor_posix.h"
- #include "base/posix/eintr_wrapper.h"
- #endif
- namespace base {
- class Environment;
- class Time;
- BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
- BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path);
- BASE_EXPORT bool DeleteFile(const FilePath& path);
- BASE_EXPORT bool DeletePathRecursively(const FilePath& path);
- BASE_EXPORT OnceCallback<void(const FilePath&)> GetDeleteFileCallback();
- BASE_EXPORT OnceCallback<void(const FilePath&)>
- GetDeletePathRecursivelyCallback();
- #if defined(OS_WIN)
- BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
- #endif
- BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path);
- BASE_EXPORT bool ReplaceFile(const FilePath& from_path,
- const FilePath& to_path,
- File::Error* error);
- BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path);
- BASE_EXPORT bool CopyDirectory(const FilePath& from_path,
- const FilePath& to_path,
- bool recursive);
- BASE_EXPORT bool CopyDirectoryExcl(const FilePath& from_path,
- const FilePath& to_path,
- bool recursive);
- BASE_EXPORT bool PathExists(const FilePath& path);
- BASE_EXPORT bool PathIsReadable(const FilePath& path);
- BASE_EXPORT bool PathIsWritable(const FilePath& path);
- BASE_EXPORT bool DirectoryExists(const FilePath& path);
- BASE_EXPORT bool ContentsEqual(const FilePath& filename1,
- const FilePath& filename2);
- BASE_EXPORT bool TextContentsEqual(const FilePath& filename1,
- const FilePath& filename2);
- BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents);
- BASE_EXPORT bool ReadFileToStringWithMaxSize(const FilePath& path,
- std::string* contents,
- size_t max_size);
- BASE_EXPORT bool ReadStreamToString(FILE* stream, std::string* contents);
- BASE_EXPORT bool ReadStreamToStringWithMaxSize(FILE* stream,
- size_t max_size,
- std::string* contents);
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes);
- BASE_EXPORT ScopedFD CreateAndOpenFdForTemporaryFileInDir(const FilePath& dir,
- FilePath* path);
- #endif
- #if defined(OS_POSIX)
- BASE_EXPORT bool ReadFileToStringNonBlocking(const base::FilePath& file,
- std::string* ret);
- BASE_EXPORT bool CreateSymbolicLink(const FilePath& target,
- const FilePath& symlink);
- BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
- 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,
- };
- BASE_EXPORT bool GetPosixFilePermissions(const FilePath& path, int* mode);
- BASE_EXPORT bool SetPosixFilePermissions(const FilePath& path, int mode);
- BASE_EXPORT bool ExecutableExistsInPath(Environment* env,
- const FilePath::StringType& executable);
- #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_AIX)
- BASE_EXPORT bool IsPathExecutable(const FilePath& path);
- #endif
- #endif
- BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path);
- BASE_EXPORT bool GetTempDir(FilePath* path);
- BASE_EXPORT FilePath GetHomeDir();
- BASE_EXPORT File CreateAndOpenTemporaryFileInDir(const FilePath& dir,
- FilePath* temp_file);
- BASE_EXPORT bool CreateTemporaryFile(FilePath* path);
- BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir,
- FilePath* temp_file);
- BASE_EXPORT ScopedFILE CreateAndOpenTemporaryStream(FilePath* path);
- BASE_EXPORT ScopedFILE CreateAndOpenTemporaryStreamInDir(const FilePath& dir,
- FilePath* path);
- BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix,
- FilePath* new_temp_path);
- BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir,
- const FilePath::StringType& prefix,
- FilePath* new_dir);
- BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
- File::Error* error);
- BASE_EXPORT bool CreateDirectory(const FilePath& full_path);
- BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64_t* file_size);
- BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
- #if defined(OS_WIN)
- BASE_EXPORT bool DevicePathToDriveLetterPath(const FilePath& device_path,
- FilePath* drive_letter_path);
- BASE_EXPORT FilePath MakeLongFilePath(const FilePath& input);
- BASE_EXPORT bool CreateWinHardLink(const FilePath& to_file,
- const FilePath& from_file);
- #endif
- BASE_EXPORT bool IsLink(const FilePath& file_path);
- BASE_EXPORT bool GetFileInfo(const FilePath& file_path, File::Info* info);
- BASE_EXPORT bool TouchFile(const FilePath& path,
- const Time& last_accessed,
- const Time& last_modified);
- BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode);
- BASE_EXPORT bool CloseFile(FILE* file);
- BASE_EXPORT FILE* FileToFILE(File file, const char* mode);
- BASE_EXPORT File FILEToFile(FILE* file_stream);
- BASE_EXPORT bool TruncateFile(FILE* file);
- BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int max_size);
- BASE_EXPORT int WriteFile(const FilePath& filename, const char* data,
- int size);
- BASE_EXPORT bool WriteFile(const FilePath& filename, span<const uint8_t> data);
- BASE_EXPORT bool WriteFile(const FilePath& filename, StringPiece data);
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- BASE_EXPORT bool WriteFileDescriptor(const int fd, const char* data, int size);
- BASE_EXPORT bool AllocateFileRegion(File* file, int64_t offset, size_t size);
- #endif
- BASE_EXPORT bool AppendToFile(const FilePath& filename,
- const char* data,
- int size);
- BASE_EXPORT bool GetCurrentDirectory(FilePath* path);
- BASE_EXPORT bool SetCurrentDirectory(const FilePath& path);
- enum { kMaxUniqueFiles = 100 };
- BASE_EXPORT int GetUniquePathNumber(const FilePath& path);
- BASE_EXPORT FilePath GetUniquePath(const FilePath& path);
- BASE_EXPORT bool SetNonBlocking(int fd);
- enum class PrefetchResultCode {
- kSuccess = 0,
- kInvalidFile = 1,
- kSlowSuccess = 2,
- kSlowFailed = 3,
- kMemoryMapFailedSlowUsed = 4,
- kMemoryMapFailedSlowFailed = 5,
- kFastFailed = 6,
- kFastFailedSlowUsed = 7,
- kFastFailedSlowFailed = 8,
- kMaxValue = kFastFailedSlowFailed
- };
- struct PrefetchResult {
- bool succeeded() const {
- return code_ == PrefetchResultCode::kSuccess ||
- code_ == PrefetchResultCode::kSlowSuccess;
- }
- const PrefetchResultCode code_;
- };
- BASE_EXPORT PrefetchResult
- PreReadFile(const FilePath& file_path,
- bool is_executable,
- int64_t max_bytes = std::numeric_limits<int64_t>::max());
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- BASE_EXPORT bool CreatePipe(ScopedFD* read_fd,
- ScopedFD* write_fd,
- bool non_blocking = false);
- BASE_EXPORT bool CreateLocalNonBlockingPipe(int fds[2]);
- BASE_EXPORT bool SetCloseOnExec(int fd);
- BASE_EXPORT bool VerifyPathControlledByUser(const base::FilePath& base,
- const base::FilePath& path,
- uid_t owner_uid,
- const std::set<gid_t>& group_gids);
- #endif
- #if defined(OS_MAC)
- BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path);
- #endif
- BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path);
- #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_AIX)
- enum FileSystemType {
- FILE_SYSTEM_UNKNOWN,
- FILE_SYSTEM_0,
- FILE_SYSTEM_ORDINARY,
- FILE_SYSTEM_NFS,
- FILE_SYSTEM_SMB,
- FILE_SYSTEM_CODA,
- FILE_SYSTEM_MEMORY,
- FILE_SYSTEM_CGROUP,
- FILE_SYSTEM_OTHER,
- FILE_SYSTEM_TYPE_COUNT
- };
- BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type);
- #endif
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- BASE_EXPORT bool GetShmemTempDir(bool executable, FilePath* path);
- #endif
- namespace internal {
- BASE_EXPORT bool MoveUnsafe(const FilePath& from_path,
- const FilePath& to_path);
- #if defined(OS_WIN)
- BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
- const FilePath& to_path);
- #endif
- bool PreReadFileSlow(const FilePath& file_path, int64_t max_bytes);
- }
- }
- #endif
|