1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_PLATFORM_FILE_H_
6 #define BASE_PLATFORM_FILE_H_
9 #include "build/build_config.h"
16 #include "base/base_api.h"
17 #include "base/basictypes.h"
18 #include "base/file_path.h"
19 #include "base/time.h"
24 typedef HANDLE PlatformFile
;
25 const PlatformFile kInvalidPlatformFileValue
= INVALID_HANDLE_VALUE
;
26 #elif defined(OS_POSIX)
27 typedef int PlatformFile
;
28 const PlatformFile kInvalidPlatformFileValue
= -1;
31 enum PlatformFileFlags
{
32 PLATFORM_FILE_OPEN
= 1,
33 PLATFORM_FILE_CREATE
= 2,
34 PLATFORM_FILE_OPEN_ALWAYS
= 4, // May create a new file.
35 PLATFORM_FILE_CREATE_ALWAYS
= 8, // May overwrite an old file.
36 PLATFORM_FILE_READ
= 16,
37 PLATFORM_FILE_WRITE
= 32,
38 PLATFORM_FILE_EXCLUSIVE_READ
= 64, // EXCLUSIVE is opposite of Windows SHARE
39 PLATFORM_FILE_EXCLUSIVE_WRITE
= 128,
40 PLATFORM_FILE_ASYNC
= 256,
41 PLATFORM_FILE_TEMPORARY
= 512, // Used on Windows only
42 PLATFORM_FILE_HIDDEN
= 1024, // Used on Windows only
43 PLATFORM_FILE_DELETE_ON_CLOSE
= 2048,
44 PLATFORM_FILE_TRUNCATE
= 4096,
45 PLATFORM_FILE_WRITE_ATTRIBUTES
= 8192, // Used on Windows only
46 PLATFORM_FILE_ENUMERATE
= 16384, // May enumerate directory
49 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
50 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
51 // browser policy doesn't allow the operation to be executed.
52 enum PlatformFileError
{
54 PLATFORM_FILE_ERROR_FAILED
= -1,
55 PLATFORM_FILE_ERROR_IN_USE
= -2,
56 PLATFORM_FILE_ERROR_EXISTS
= -3,
57 PLATFORM_FILE_ERROR_NOT_FOUND
= -4,
58 PLATFORM_FILE_ERROR_ACCESS_DENIED
= -5,
59 PLATFORM_FILE_ERROR_TOO_MANY_OPENED
= -6,
60 PLATFORM_FILE_ERROR_NO_MEMORY
= -7,
61 PLATFORM_FILE_ERROR_NO_SPACE
= -8,
62 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY
= -9,
63 PLATFORM_FILE_ERROR_INVALID_OPERATION
= -10,
64 PLATFORM_FILE_ERROR_SECURITY
= -11,
65 PLATFORM_FILE_ERROR_ABORT
= -12,
66 PLATFORM_FILE_ERROR_NOT_A_FILE
= -13,
67 PLATFORM_FILE_ERROR_NOT_EMPTY
= -14,
70 // Used to hold information about a given file.
71 // If you add more fields to this structure (platform-specific fields are OK),
72 // make sure to update all functions that use it in file_util_{win|posix}.cc
73 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
74 // chrome/common/common_param_traits.cc.
75 struct BASE_API PlatformFileInfo
{
79 // The size of the file in bytes. Undefined when is_directory is true.
82 // True if the file corresponds to a directory.
85 // True if the file corresponds to a symbolic link.
86 bool is_symbolic_link
;
88 // The last modified time of a file.
89 base::Time last_modified
;
91 // The last accessed time of a file.
92 base::Time last_accessed
;
94 // The creation time of a file.
95 base::Time creation_time
;
98 // Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and
99 // |created| is provided, |created| will be set to true if the file was created
100 // or to false in case the file was just opened. |error_code| can be NULL.
101 BASE_API PlatformFile
CreatePlatformFile(const FilePath
& name
,
104 PlatformFileError
* error_code
);
106 // Closes a file handle. Returns |true| on success and |false| otherwise.
107 BASE_API
bool ClosePlatformFile(PlatformFile file
);
109 // Reads the given number of bytes (or until EOF is reached) starting with the
110 // given offset. Returns the number of bytes read, or -1 on error.
111 BASE_API
int ReadPlatformFile(PlatformFile file
, int64 offset
,
112 char* data
, int size
);
114 // Writes the given buffer into the file at the given offset, overwritting any
115 // data that was previously there. Returns the number of bytes written, or -1
117 BASE_API
int WritePlatformFile(PlatformFile file
, int64 offset
,
118 const char* data
, int size
);
120 // Truncates the given file to the given length. If |length| is greater than
121 // the current size of the file, the file is extended with zeros. If the file
122 // doesn't exist, |false| is returned.
123 BASE_API
bool TruncatePlatformFile(PlatformFile file
, int64 length
);
125 // Flushes the buffers of the given file.
126 BASE_API
bool FlushPlatformFile(PlatformFile file
);
128 // Touches the given file.
129 BASE_API
bool TouchPlatformFile(PlatformFile file
, const Time
& last_access_time
,
130 const Time
& last_modified_time
);
132 // Returns some information for the given file.
133 BASE_API
bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
);
135 // Use this class to pass ownership of a PlatformFile to a receiver that may or
136 // may not want to accept it. This class does not own the storage for the
141 // void MaybeProcessFile(PassPlatformFile pass_file) {
143 // PlatformFile file = pass_file.ReleaseValue();
144 // // Now, we are responsible for closing |file|.
148 // void OpenAndMaybeProcessFile(const FilePath& path) {
149 // PlatformFile file = CreatePlatformFile(path, ...);
150 // MaybeProcessFile(PassPlatformFile(&file));
151 // if (file != kInvalidPlatformFileValue)
152 // ClosePlatformFile(file);
155 class BASE_API PassPlatformFile
{
157 explicit PassPlatformFile(PlatformFile
* value
) : value_(value
) {
160 // Called to retrieve the PlatformFile stored in this object. The caller
161 // gains ownership of the PlatformFile and is now responsible for closing it.
162 // Any subsequent calls to this method will return an invalid PlatformFile.
163 PlatformFile
ReleaseValue() {
164 PlatformFile temp
= *value_
;
165 *value_
= kInvalidPlatformFileValue
;
170 PlatformFile
* value_
;
175 #endif // BASE_PLATFORM_FILE_H_