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