diff options
Diffstat (limited to 'chromium/base/files/file.h')
-rw-r--r-- | chromium/base/files/file.h | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/chromium/base/files/file.h b/chromium/base/files/file.h index d1e0e8ca587..1913bc7f6c4 100644 --- a/chromium/base/files/file.h +++ b/chromium/base/files/file.h @@ -10,11 +10,15 @@ #include <windows.h> #endif +#if defined(OS_POSIX) +#include <sys/stat.h> +#endif + #include <string> #include "base/base_export.h" #include "base/basictypes.h" -#include "base/files/file_path.h" +#include "base/files/scoped_file.h" #include "base/move.h" #include "base/time/time.h" @@ -24,12 +28,19 @@ namespace base { +class FilePath; + #if defined(OS_WIN) typedef HANDLE PlatformFile; #elif defined(OS_POSIX) typedef int PlatformFile; -#endif +#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) +typedef struct stat stat_wrapper_t; +#else +typedef struct stat64 stat_wrapper_t; +#endif +#endif // defined(OS_POSIX) // Thin wrapper around an OS-level file. // Note that this class does not provide any support for asynchronous IO, other @@ -120,6 +131,10 @@ class BASE_EXPORT File { struct BASE_EXPORT Info { Info(); ~Info(); +#if defined(OS_POSIX) + // Fills this struct with values from |stat_info|. + void FromStat(const stat_wrapper_t& stat_info); +#endif // The size of the file in bytes. Undefined when is_directory is true. int64 size; @@ -149,6 +164,9 @@ class BASE_EXPORT File { // Takes ownership of |platform_file|. explicit File(PlatformFile platform_file); + // Creates an object with a specific error_details code. + explicit File(Error error_details); + // Move constructor for C++03 move emulation of this type. File(RValue other); @@ -157,9 +175,12 @@ class BASE_EXPORT File { // Move operator= for C++03 move emulation of this type. File& operator=(RValue other); + // Creates or opens the given file. + void Initialize(const FilePath& name, uint32 flags); + // Creates or opens the given file, allowing paths with traversal ('..') // components. Use only with extreme care. - void CreateBaseFileUnsafe(const FilePath& name, uint32 flags); + void InitializeUnsafe(const FilePath& name, uint32 flags); bool IsValid() const; @@ -168,10 +189,14 @@ class BASE_EXPORT File { // FLAG_CREATE_ALWAYS), and false otherwise. bool created() const { return created_; } - // Returns the OS result of opening this file. - Error error() const { return error_; } + // Returns the OS result of opening this file. Note that the way to verify + // the success of the operation is to use IsValid(), not this method: + // File file(name, flags); + // if (!file.IsValid()) + // return; + Error error_details() const { return error_details_; } - PlatformFile GetPlatformFile() const { return file_; } + PlatformFile GetPlatformFile() const; PlatformFile TakePlatformFile(); // Destroying this object closes the file automatically. @@ -216,10 +241,13 @@ class BASE_EXPORT File { // platforms. Returns the number of bytes written, or -1 on error. int WriteAtCurrentPosNoBestEffort(const char* data, int size); + // Returns the current size of this file, or a negative number on failure. + int64 GetLength(); + // Truncates the file to the given length. If |length| is greater than the // current size of the file, the file is extended with zeros. If the file // doesn't exist, |false| is returned. - bool Truncate(int64 length); + bool SetLength(int64 length); // Flushes the buffers. bool Flush(); @@ -255,22 +283,27 @@ class BASE_EXPORT File { // Unlock a file previously locked. Error Unlock(); + bool async() const { return async_; } + #if defined(OS_WIN) static Error OSErrorToFileError(DWORD last_error); #elif defined(OS_POSIX) static Error OSErrorToFileError(int saved_errno); #endif + // Converts an error value to a human-readable form. Used for logging. + static std::string ErrorToString(Error error); + private: void SetPlatformFile(PlatformFile file); #if defined(OS_WIN) win::ScopedHandle file_; #elif defined(OS_POSIX) - PlatformFile file_; + ScopedFD file_; #endif - Error error_; + Error error_details_; bool created_; bool async_; }; |