Bluetooth: fix header sentry comment style
[chromium-blink-merge.git] / base / platform_file.h
blobb1efaaa5f706de0a0e43b298c0ba4b79ef9a83a9
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"
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
13 #include <string>
15 #include "base/base_export.h"
16 #include "base/basictypes.h"
17 #include "base/files/file_path.h"
18 #include "base/time.h"
20 namespace base {
22 #if defined(OS_WIN)
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;
28 #endif
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,
40 // only if it exists.
41 PLATFORM_FILE_READ = 1 << 5,
42 PLATFORM_FILE_WRITE = 1 << 6,
43 PLATFORM_FILE_EXCLUSIVE_READ = 1 << 7, // EXCLUSIVE is opposite of Windows
44 // SHARE
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 {
66 PLATFORM_FILE_OK = 0,
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 {
99 PlatformFileInfo();
100 ~PlatformFileInfo();
102 // The size of the file in bytes. Undefined when is_directory is true.
103 int64 size;
105 // True if the file corresponds to a directory.
106 bool is_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,
129 int flags,
130 bool* created,
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,
136 int flags,
137 bool* created,
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,
150 int64 offset);
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
177 // all platforms.
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
208 // PlatformFile.
210 // EXAMPLE:
212 // void MaybeProcessFile(PassPlatformFile pass_file) {
213 // if (...) {
214 // PlatformFile file = pass_file.ReleaseValue();
215 // // Now, we are responsible for closing |file|.
216 // }
217 // }
219 // void OpenAndMaybeProcessFile(const FilePath& path) {
220 // PlatformFile file = CreatePlatformFile(path, ...);
221 // MaybeProcessFile(PassPlatformFile(&file));
222 // if (file != kInvalidPlatformFileValue)
223 // ClosePlatformFile(file);
224 // }
226 class BASE_EXPORT PassPlatformFile {
227 public:
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;
237 return temp;
240 private:
241 PlatformFile* value_;
244 } // namespace base
246 #endif // BASE_PLATFORM_FILE_H_