123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- #ifndef BASE_FILES_FILE_H_
- #define BASE_FILES_FILE_H_
- #include <stdint.h>
- #include <string>
- #include "base/base_export.h"
- #include "base/containers/span.h"
- #include "base/files/file_path.h"
- #include "base/files/file_tracing.h"
- #include "base/files/platform_file.h"
- #include "base/macros.h"
- #include "base/time/time.h"
- #include "build/build_config.h"
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- #include <sys/stat.h>
- #endif
- namespace base {
- #if defined(OS_BSD) || defined(OS_APPLE) || defined(OS_NACL) || \
- defined(OS_FUCHSIA) || (defined(OS_ANDROID) && __ANDROID_API__ < 21)
- typedef struct stat stat_wrapper_t;
- #elif defined(OS_POSIX)
- typedef struct stat64 stat_wrapper_t;
- #endif
- class BASE_EXPORT File {
- public:
-
-
-
-
-
-
-
- enum Flags {
- FLAG_OPEN = 1 << 0,
- FLAG_CREATE = 1 << 1,
-
- FLAG_OPEN_ALWAYS = 1 << 2,
- FLAG_CREATE_ALWAYS = 1 << 3,
- FLAG_OPEN_TRUNCATED = 1 << 4,
-
- FLAG_READ = 1 << 5,
- FLAG_WRITE = 1 << 6,
- FLAG_APPEND = 1 << 7,
- FLAG_EXCLUSIVE_READ = 1 << 8,
- FLAG_EXCLUSIVE_WRITE = 1 << 9,
- FLAG_ASYNC = 1 << 10,
- FLAG_TEMPORARY = 1 << 11,
- FLAG_HIDDEN = 1 << 12,
- FLAG_DELETE_ON_CLOSE = 1 << 13,
- FLAG_WRITE_ATTRIBUTES = 1 << 14,
- FLAG_SHARE_DELETE = 1 << 15,
- FLAG_TERMINAL_DEVICE = 1 << 16,
- FLAG_BACKUP_SEMANTICS = 1 << 17,
- FLAG_EXECUTE = 1 << 18,
- FLAG_SEQUENTIAL_SCAN = 1 << 19,
- FLAG_CAN_DELETE_ON_CLOSE = 1 << 20,
-
-
- };
-
-
-
-
-
-
-
- enum Error {
- FILE_OK = 0,
- FILE_ERROR_FAILED = -1,
- FILE_ERROR_IN_USE = -2,
- FILE_ERROR_EXISTS = -3,
- FILE_ERROR_NOT_FOUND = -4,
- FILE_ERROR_ACCESS_DENIED = -5,
- FILE_ERROR_TOO_MANY_OPENED = -6,
- FILE_ERROR_NO_MEMORY = -7,
- FILE_ERROR_NO_SPACE = -8,
- FILE_ERROR_NOT_A_DIRECTORY = -9,
- FILE_ERROR_INVALID_OPERATION = -10,
- FILE_ERROR_SECURITY = -11,
- FILE_ERROR_ABORT = -12,
- FILE_ERROR_NOT_A_FILE = -13,
- FILE_ERROR_NOT_EMPTY = -14,
- FILE_ERROR_INVALID_URL = -15,
- FILE_ERROR_IO = -16,
-
- FILE_ERROR_MAX = -17
- };
-
- enum Whence {
- FROM_BEGIN = 0,
- FROM_CURRENT = 1,
- FROM_END = 2
- };
-
-
-
-
-
- struct BASE_EXPORT Info {
- Info();
- ~Info();
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
-
- void FromStat(const stat_wrapper_t& stat_info);
- #endif
-
- int64_t size = 0;
-
- bool is_directory = false;
-
-
- bool is_symbolic_link = false;
-
- Time last_modified;
-
- Time last_accessed;
-
- Time creation_time;
- };
- File();
-
-
- File(const FilePath& path, uint32_t flags);
-
- explicit File(ScopedPlatformFile platform_file);
- explicit File(PlatformFile platform_file);
-
-
-
- File(ScopedPlatformFile platform_file, bool async);
- File(PlatformFile platform_file, bool async);
-
- explicit File(Error error_details);
- File(File&& other);
- ~File();
- File& operator=(File&& other);
-
- void Initialize(const FilePath& path, uint32_t flags);
-
-
-
- bool IsValid() const;
-
-
-
- bool created() const { return created_; }
-
-
-
-
-
- Error error_details() const { return error_details_; }
- PlatformFile GetPlatformFile() const;
- PlatformFile TakePlatformFile();
-
- void Close();
-
-
-
- int64_t Seek(Whence whence, int64_t offset);
-
-
-
- bool ReadAndCheck(int64_t offset, span<uint8_t> data);
- bool ReadAtCurrentPosAndCheck(span<uint8_t> data);
- bool WriteAndCheck(int64_t offset, span<const uint8_t> data);
- bool WriteAtCurrentPosAndCheck(span<const uint8_t> data);
-
-
-
-
-
-
- int Read(int64_t offset, char* data, int size);
-
- int ReadAtCurrentPos(char* data, int size);
-
-
-
- int ReadNoBestEffort(int64_t offset, char* data, int size);
-
- int ReadAtCurrentPosNoBestEffort(char* data, int size);
-
-
-
-
-
-
- int Write(int64_t offset, const char* data, int size);
-
- int WriteAtCurrentPos(const char* data, int size);
-
-
- int WriteAtCurrentPosNoBestEffort(const char* data, int size);
-
- int64_t GetLength();
-
-
-
- bool SetLength(int64_t length);
-
-
-
-
-
-
-
-
-
-
-
-
- bool Flush();
-
- bool SetTimes(Time last_access_time, Time last_modified_time);
-
- bool GetInfo(Info* info);
- #if !defined(OS_FUCHSIA)
- enum class LockMode {
- kShared,
- kExclusive,
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Error Lock(LockMode mode);
-
- Error Unlock();
- #endif
-
-
-
-
-
- File Duplicate() const;
- bool async() const { return async_; }
- #if defined(OS_WIN)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool DeleteOnClose(bool delete_on_close);
- #endif
- #if defined(OS_WIN)
- static Error OSErrorToFileError(DWORD last_error);
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- static Error OSErrorToFileError(int saved_errno);
- #endif
-
-
-
-
- static Error GetLastFileError();
-
- static std::string ErrorToString(Error error);
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
-
- static int Stat(const char* path, stat_wrapper_t* sb);
- static int Fstat(int fd, stat_wrapper_t* sb);
- static int Lstat(const char* path, stat_wrapper_t* sb);
- #endif
- private:
- friend class FileTracing::ScopedTrace;
-
-
- void DoInitialize(const FilePath& path, uint32_t flags);
- void SetPlatformFile(PlatformFile file);
- ScopedPlatformFile file_;
-
-
- FilePath tracing_path_;
-
- FileTracing::ScopedEnabler trace_enabler_;
- Error error_details_ = FILE_ERROR_FAILED;
- bool created_ = false;
- bool async_ = false;
- DISALLOW_COPY_AND_ASSIGN(File);
- };
- }
- #endif
|