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 // PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
32 // exactly one of the five (possibly combining with other flags) when opening
33 // or creating a file.
34 enum PlatformFileFlags
{
35 PLATFORM_FILE_OPEN
= 1, // Opens a file, only if it exists.
36 PLATFORM_FILE_CREATE
= 2, // Creates a new file, only if it does not
38 PLATFORM_FILE_OPEN_ALWAYS
= 4, // May create a new file.
39 PLATFORM_FILE_CREATE_ALWAYS
= 8, // May overwrite an old file.
40 PLATFORM_FILE_OPEN_TRUNCATED
= 16, // Opens a file and truncates it, only if
42 PLATFORM_FILE_READ
= 32,
43 PLATFORM_FILE_WRITE
= 64,
44 PLATFORM_FILE_EXCLUSIVE_READ
= 128, // EXCLUSIVE is opposite of Windows SHARE
45 PLATFORM_FILE_EXCLUSIVE_WRITE
= 256,
46 PLATFORM_FILE_ASYNC
= 512,
47 PLATFORM_FILE_TEMPORARY
= 1024, // Used on Windows only
48 PLATFORM_FILE_HIDDEN
= 2048, // Used on Windows only
49 PLATFORM_FILE_DELETE_ON_CLOSE
= 4096,
51 PLATFORM_FILE_WRITE_ATTRIBUTES
= 8192, // Used on Windows only
52 PLATFORM_FILE_ENUMERATE
= 16384, // May enumerate directory
54 PLATFORM_FILE_SHARE_DELETE
= 32768, // Used on Windows only
57 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
58 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
59 // browser policy doesn't allow the operation to be executed.
60 enum PlatformFileError
{
62 PLATFORM_FILE_ERROR_FAILED
= -1,
63 PLATFORM_FILE_ERROR_IN_USE
= -2,
64 PLATFORM_FILE_ERROR_EXISTS
= -3,
65 PLATFORM_FILE_ERROR_NOT_FOUND
= -4,
66 PLATFORM_FILE_ERROR_ACCESS_DENIED
= -5,
67 PLATFORM_FILE_ERROR_TOO_MANY_OPENED
= -6,
68 PLATFORM_FILE_ERROR_NO_MEMORY
= -7,
69 PLATFORM_FILE_ERROR_NO_SPACE
= -8,
70 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY
= -9,
71 PLATFORM_FILE_ERROR_INVALID_OPERATION
= -10,
72 PLATFORM_FILE_ERROR_SECURITY
= -11,
73 PLATFORM_FILE_ERROR_ABORT
= -12,
74 PLATFORM_FILE_ERROR_NOT_A_FILE
= -13,
75 PLATFORM_FILE_ERROR_NOT_EMPTY
= -14,
76 PLATFORM_FILE_ERROR_INVALID_URL
= -15,
79 // Used to hold information about a given file.
80 // If you add more fields to this structure (platform-specific fields are OK),
81 // make sure to update all functions that use it in file_util_{win|posix}.cc
82 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
83 // chrome/common/common_param_traits.cc.
84 struct BASE_API PlatformFileInfo
{
88 // The size of the file in bytes. Undefined when is_directory is true.
91 // True if the file corresponds to a directory.
94 // True if the file corresponds to a symbolic link.
95 bool is_symbolic_link
;
97 // The last modified time of a file.
98 base::Time last_modified
;
100 // The last accessed time of a file.
101 base::Time last_accessed
;
103 // The creation time of a file.
104 base::Time creation_time
;
107 // Creates or opens the given file. If |created| is provided, it will be set to
108 // true if a new file was created [or an old one truncated to zero length to
109 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
110 // false otherwise. |error_code| can be NULL.
111 BASE_API PlatformFile
CreatePlatformFile(const FilePath
& name
,
114 PlatformFileError
* error_code
);
116 // Closes a file handle. Returns |true| on success and |false| otherwise.
117 BASE_API
bool ClosePlatformFile(PlatformFile file
);
119 // Reads the given number of bytes (or until EOF is reached) starting with the
120 // given offset. Returns the number of bytes read, or -1 on error.
121 BASE_API
int ReadPlatformFile(PlatformFile file
, int64 offset
,
122 char* data
, int size
);
124 // Writes the given buffer into the file at the given offset, overwritting any
125 // data that was previously there. Returns the number of bytes written, or -1
127 BASE_API
int WritePlatformFile(PlatformFile file
, int64 offset
,
128 const char* data
, int size
);
130 // Truncates the given file to the given length. If |length| is greater than
131 // the current size of the file, the file is extended with zeros. If the file
132 // doesn't exist, |false| is returned.
133 BASE_API
bool TruncatePlatformFile(PlatformFile file
, int64 length
);
135 // Flushes the buffers of the given file.
136 BASE_API
bool FlushPlatformFile(PlatformFile file
);
138 // Touches the given file.
139 BASE_API
bool TouchPlatformFile(PlatformFile file
, const Time
& last_access_time
,
140 const Time
& last_modified_time
);
142 // Returns some information for the given file.
143 BASE_API
bool GetPlatformFileInfo(PlatformFile file
, PlatformFileInfo
* info
);
145 // Use this class to pass ownership of a PlatformFile to a receiver that may or
146 // may not want to accept it. This class does not own the storage for the
151 // void MaybeProcessFile(PassPlatformFile pass_file) {
153 // PlatformFile file = pass_file.ReleaseValue();
154 // // Now, we are responsible for closing |file|.
158 // void OpenAndMaybeProcessFile(const FilePath& path) {
159 // PlatformFile file = CreatePlatformFile(path, ...);
160 // MaybeProcessFile(PassPlatformFile(&file));
161 // if (file != kInvalidPlatformFileValue)
162 // ClosePlatformFile(file);
165 class BASE_API PassPlatformFile
{
167 explicit PassPlatformFile(PlatformFile
* value
) : value_(value
) {
170 // Called to retrieve the PlatformFile stored in this object. The caller
171 // gains ownership of the PlatformFile and is now responsible for closing it.
172 // Any subsequent calls to this method will return an invalid PlatformFile.
173 PlatformFile
ReleaseValue() {
174 PlatformFile temp
= *value_
;
175 *value_
= kInvalidPlatformFileValue
;
180 PlatformFile
* value_
;
185 #endif // BASE_PLATFORM_FILE_H_