1 // Copyright (c) 2012 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_
8 #include "build/build_config.h"
15 #include "base/base_export.h"
16 #include "base/basictypes.h"
17 #include "base/files/file_path.h"
18 #include "base/time.h"
22 // PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
23 // exactly one of the five (possibly combining with other flags) when opening
24 // or creating a file.
25 enum PlatformFileFlags
{
26 PLATFORM_FILE_OPEN
= 1 << 0, // Opens a file, only if it exists.
27 PLATFORM_FILE_CREATE
= 1 << 1, // Creates a new file, only if it
28 // does not already exist.
29 PLATFORM_FILE_OPEN_ALWAYS
= 1 << 2, // May create a new file.
30 PLATFORM_FILE_CREATE_ALWAYS
= 1 << 3, // May overwrite an old file.
31 PLATFORM_FILE_OPEN_TRUNCATED
= 1 << 4, // Opens a file and truncates it,
33 PLATFORM_FILE_READ
= 1 << 5,
34 PLATFORM_FILE_WRITE
= 1 << 6,
35 PLATFORM_FILE_EXCLUSIVE_READ
= 1 << 7, // EXCLUSIVE is opposite of Windows
37 PLATFORM_FILE_EXCLUSIVE_WRITE
= 1 << 8,
38 PLATFORM_FILE_ASYNC
= 1 << 9,
39 PLATFORM_FILE_TEMPORARY
= 1 << 10, // Used on Windows only
40 PLATFORM_FILE_HIDDEN
= 1 << 11, // Used on Windows only
41 PLATFORM_FILE_DELETE_ON_CLOSE
= 1 << 12,
43 PLATFORM_FILE_WRITE_ATTRIBUTES
= 1 << 13, // Used on Windows only
44 PLATFORM_FILE_ENUMERATE
= 1 << 14, // May enumerate directory
46 PLATFORM_FILE_SHARE_DELETE
= 1 << 15, // Used on Windows only
48 PLATFORM_FILE_TERMINAL_DEVICE
= 1 << 16, // Serial port flags
49 PLATFORM_FILE_BACKUP_SEMANTICS
= 1 << 17, // Used on Windows only
51 PLATFORM_FILE_EXECUTE
= 1 << 18, // Used on Windows only
54 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
55 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
56 // browser policy doesn't allow the operation to be executed.
57 enum PlatformFileError
{
59 PLATFORM_FILE_ERROR_FAILED
= -1,
60 PLATFORM_FILE_ERROR_IN_USE
= -2,
61 PLATFORM_FILE_ERROR_EXISTS
= -3,
62 PLATFORM_FILE_ERROR_NOT_FOUND
= -4,
63 PLATFORM_FILE_ERROR_ACCESS_DENIED
= -5,
64 PLATFORM_FILE_ERROR_TOO_MANY_OPENED
= -6,
65 PLATFORM_FILE_ERROR_NO_MEMORY
= -7,
66 PLATFORM_FILE_ERROR_NO_SPACE
= -8,
67 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY
= -9,
68 PLATFORM_FILE_ERROR_INVALID_OPERATION
= -10,
69 PLATFORM_FILE_ERROR_SECURITY
= -11,
70 PLATFORM_FILE_ERROR_ABORT
= -12,
71 PLATFORM_FILE_ERROR_NOT_A_FILE
= -13,
72 PLATFORM_FILE_ERROR_NOT_EMPTY
= -14,
73 PLATFORM_FILE_ERROR_INVALID_URL
= -15,
74 PLATFORM_FILE_ERROR_IO
= -16,
75 // Put new entries here and increment PLATFORM_FILE_ERROR_MAX.
76 PLATFORM_FILE_ERROR_MAX
= -17
79 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
80 enum PlatformFileWhence
{
81 PLATFORM_FILE_FROM_BEGIN
= 0,
82 PLATFORM_FILE_FROM_CURRENT
= 1,
83 PLATFORM_FILE_FROM_END
= 2
86 // Used to hold information about a given file.
87 // If you add more fields to this structure (platform-specific fields are OK),
88 // make sure to update all functions that use it in file_util_{win|posix}.cc
89 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
90 // chrome/common/common_param_traits.cc.
91 struct BASE_EXPORT PlatformFileInfo
{
95 // The size of the file in bytes. Undefined when is_directory is true.
98 // True if the file corresponds to a directory.
101 // True if the file corresponds to a symbolic link.
102 bool is_symbolic_link
;
104 // The last modified time of a file.
105 base::Time last_modified
;
107 // The last accessed time of a file.
108 base::Time last_accessed
;
110 // The creation time of a file.
111 base::Time creation_time
;
115 typedef HANDLE PlatformFile
;
116 const PlatformFile kInvalidPlatformFileValue
= INVALID_HANDLE_VALUE
;
117 PlatformFileError
LastErrorToPlatformFileError(DWORD saved_errno
);
118 #elif defined(OS_POSIX)
119 typedef int PlatformFile
;
120 const PlatformFile kInvalidPlatformFileValue
= -1;
121 PlatformFileError
ErrnoToPlatformFileError(int saved_errno
);
124 // Creates or opens the given file. If |created| is provided, it will be set to
125 // true if a new file was created [or an old one truncated to zero length to
126 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
127 // false otherwise. |error| can be NULL.
129 // This function fails with 'access denied' if the |name| contains path
130 // traversal ('..') components.
131 BASE_EXPORT PlatformFile
CreatePlatformFile(const FilePath
& name
,
134 PlatformFileError
* error
);
136 // Same as CreatePlatformFile but allows paths with traversal (like \..\)
137 // components. Use only with extreme care.
138 BASE_EXPORT PlatformFile
CreatePlatformFileUnsafe(const FilePath
& name
,
141 PlatformFileError
* error
);
143 BASE_EXPORT
FILE* FdopenPlatformFile(PlatformFile file
, const char* mode
);
145 // Closes a file handle. Returns |true| on success and |false| otherwise.
146 BASE_EXPORT
bool ClosePlatformFile(PlatformFile file
);
148 // Changes current position in the file to an |offset| relative to an origin
149 // defined by |whence|. Returns the resultant current position in the file
150 // (relative to the start) or -1 in case of error.
151 BASE_EXPORT int64
SeekPlatformFile(PlatformFile file
,
152 PlatformFileWhence whence
,
155 // Reads the given number of bytes (or until EOF is reached) starting with the
156 // given offset. Returns the number of bytes read, or -1 on error. Note that
157 // this function makes a best effort to read all data on all platforms, so it is
158 // not intended for stream oriented files but instead for cases when the normal
159 // expectation is that actually |size| bytes are read unless there is an error.
160 BASE_EXPORT
int ReadPlatformFile(PlatformFile file
, int64 offset
,
161 char* data
, int size
);
163 // Same as above but without seek.
164 BASE_EXPORT
int ReadPlatformFileAtCurrentPos(PlatformFile file
,
165 char* data
, int size
);
167 // Reads the given number of bytes (or until EOF is reached) starting with the
168 // given offset, but does not make any effort to read all data on all platforms.
169 // Returns the number of bytes read, or -1 on error.
170 BASE_EXPORT
int ReadPlatformFileNoBestEffort(PlatformFile file
, int64 offset
,
171 char* data
, int size
);
173 // Same as above but without seek.
174 BASE_EXPORT
int ReadPlatformFileCurPosNoBestEffort(PlatformFile file
,
175 char* data
, int size
);
177 // Writes the given buffer into the file at the given offset, overwritting any
178 // data that was previously there. Returns the number of bytes written, or -1
179 // on error. Note that this function makes a best effort to write all data on
181 BASE_EXPORT
int WritePlatformFile(PlatformFile file
, int64 offset
,
182 const char* data
, int size
);
184 // Save as above but without seek.
185 BASE_EXPORT
int WritePlatformFileAtCurrentPos(PlatformFile file
,
186 const char* data
, int size
);
188 // Save as above but does not make any effort to write all data on all
189 // platforms. Returns the number of bytes written, or -1 on error.
190 BASE_EXPORT
int WritePlatformFileCurPosNoBestEffort(PlatformFile file
,
191 const char* data
, int size
);
193 // Truncates the given file to the given length. If |length| is greater than
194 // the current size of the file, the file is extended with zeros. If the file
195 // doesn't exist, |false| is returned.
196 BASE_EXPORT
bool TruncatePlatformFile(PlatformFile file
, int64 length
);
198 // Flushes the buffers of the given file.
199 BASE_EXPORT
bool FlushPlatformFile(PlatformFile file
);
201 // Touches the given file.
202 BASE_EXPORT
bool TouchPlatformFile(PlatformFile file
,
203 const Time
& last_access_time
,
204 const Time
& last_modified_time
);
206 // Returns some information for the given file.
207 BASE_EXPORT
bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
);
209 // Use this class to pass ownership of a PlatformFile to a receiver that may or
210 // may not want to accept it. This class does not own the storage for the
215 // void MaybeProcessFile(PassPlatformFile pass_file) {
217 // PlatformFile file = pass_file.ReleaseValue();
218 // // Now, we are responsible for closing |file|.
222 // void OpenAndMaybeProcessFile(const FilePath& path) {
223 // PlatformFile file = CreatePlatformFile(path, ...);
224 // MaybeProcessFile(PassPlatformFile(&file));
225 // if (file != kInvalidPlatformFileValue)
226 // ClosePlatformFile(file);
229 class BASE_EXPORT PassPlatformFile
{
231 explicit PassPlatformFile(PlatformFile
* value
) : value_(value
) {
234 // Called to retrieve the PlatformFile stored in this object. The caller
235 // gains ownership of the PlatformFile and is now responsible for closing it.
236 // Any subsequent calls to this method will return an invalid PlatformFile.
237 PlatformFile
ReleaseValue() {
238 PlatformFile temp
= *value_
;
239 *value_
= kInvalidPlatformFileValue
;
244 PlatformFile
* value_
;
249 #endif // BASE_PLATFORM_FILE_H_