Update .DEPS.git
[chromium-blink-merge.git] / base / platform_file.h
blob07d993479177604bfee9c717440418b4d6c3e372
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/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
60 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
61 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
62 // browser policy doesn't allow the operation to be executed.
63 enum PlatformFileError {
64 PLATFORM_FILE_OK = 0,
65 PLATFORM_FILE_ERROR_FAILED = -1,
66 PLATFORM_FILE_ERROR_IN_USE = -2,
67 PLATFORM_FILE_ERROR_EXISTS = -3,
68 PLATFORM_FILE_ERROR_NOT_FOUND = -4,
69 PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
70 PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
71 PLATFORM_FILE_ERROR_NO_MEMORY = -7,
72 PLATFORM_FILE_ERROR_NO_SPACE = -8,
73 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
74 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
75 PLATFORM_FILE_ERROR_SECURITY = -11,
76 PLATFORM_FILE_ERROR_ABORT = -12,
77 PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
78 PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
79 PLATFORM_FILE_ERROR_INVALID_URL = -15,
80 // Put new entries here and increment PLATFORM_FILE_ERROR_MAX.
81 PLATFORM_FILE_ERROR_MAX = -16
84 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
85 enum PlatformFileWhence {
86 PLATFORM_FILE_FROM_BEGIN = 0,
87 PLATFORM_FILE_FROM_CURRENT = 1,
88 PLATFORM_FILE_FROM_END = 2
91 // Used to hold information about a given file.
92 // If you add more fields to this structure (platform-specific fields are OK),
93 // make sure to update all functions that use it in file_util_{win|posix}.cc
94 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
95 // chrome/common/common_param_traits.cc.
96 struct BASE_EXPORT PlatformFileInfo {
97 PlatformFileInfo();
98 ~PlatformFileInfo();
100 // The size of the file in bytes. Undefined when is_directory is true.
101 int64 size;
103 // True if the file corresponds to a directory.
104 bool is_directory;
106 // True if the file corresponds to a symbolic link.
107 bool is_symbolic_link;
109 // The last modified time of a file.
110 base::Time last_modified;
112 // The last accessed time of a file.
113 base::Time last_accessed;
115 // The creation time of a file.
116 base::Time creation_time;
119 // Creates or opens the given file. If |created| is provided, it will be set to
120 // true if a new file was created [or an old one truncated to zero length to
121 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
122 // false otherwise. |error_code| can be NULL.
123 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name,
124 int flags,
125 bool* created,
126 PlatformFileError* error_code);
128 // Closes a file handle. Returns |true| on success and |false| otherwise.
129 BASE_EXPORT bool ClosePlatformFile(PlatformFile file);
131 // Changes current position in the file to an |offset| relative to an origin
132 // defined by |whence|. Returns the resultant current position in the file
133 // (relative to the start) or -1 in case of error.
134 BASE_EXPORT int64 SeekPlatformFile(PlatformFile file,
135 PlatformFileWhence whence,
136 int64 offset);
138 // Reads the given number of bytes (or until EOF is reached) starting with the
139 // given offset. Returns the number of bytes read, or -1 on error. Note that
140 // this function makes a best effort to read all data on all platforms, so it is
141 // not intended for stream oriented files but instead for cases when the normal
142 // expectation is that actually |size| bytes are read unless there is an error.
143 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset,
144 char* data, int size);
146 // Same as above but without seek.
147 BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file,
148 char* data, int size);
150 // Reads the given number of bytes (or until EOF is reached) starting with the
151 // given offset, but does not make any effort to read all data on all platforms.
152 // Returns the number of bytes read, or -1 on error.
153 BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
154 char* data, int size);
156 // Same as above but without seek.
157 BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
158 char* data, int size);
160 // Writes the given buffer into the file at the given offset, overwritting any
161 // data that was previously there. Returns the number of bytes written, or -1
162 // on error. Note that this function makes a best effort to write all data on
163 // all platforms.
164 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
165 const char* data, int size);
167 // Save as above but without seek.
168 BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file,
169 const char* data, int size);
171 // Save as above but does not make any effort to write all data on all
172 // platforms. Returns the number of bytes written, or -1 on error.
173 BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
174 const char* data, int size);
176 // Truncates the given file to the given length. If |length| is greater than
177 // the current size of the file, the file is extended with zeros. If the file
178 // doesn't exist, |false| is returned.
179 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length);
181 // Flushes the buffers of the given file.
182 BASE_EXPORT bool FlushPlatformFile(PlatformFile file);
184 // Touches the given file.
185 BASE_EXPORT bool TouchPlatformFile(PlatformFile file,
186 const Time& last_access_time,
187 const Time& last_modified_time);
189 // Returns some information for the given file.
190 BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
192 // Use this class to pass ownership of a PlatformFile to a receiver that may or
193 // may not want to accept it. This class does not own the storage for the
194 // PlatformFile.
196 // EXAMPLE:
198 // void MaybeProcessFile(PassPlatformFile pass_file) {
199 // if (...) {
200 // PlatformFile file = pass_file.ReleaseValue();
201 // // Now, we are responsible for closing |file|.
202 // }
203 // }
205 // void OpenAndMaybeProcessFile(const FilePath& path) {
206 // PlatformFile file = CreatePlatformFile(path, ...);
207 // MaybeProcessFile(PassPlatformFile(&file));
208 // if (file != kInvalidPlatformFileValue)
209 // ClosePlatformFile(file);
210 // }
212 class BASE_EXPORT PassPlatformFile {
213 public:
214 explicit PassPlatformFile(PlatformFile* value) : value_(value) {
217 // Called to retrieve the PlatformFile stored in this object. The caller
218 // gains ownership of the PlatformFile and is now responsible for closing it.
219 // Any subsequent calls to this method will return an invalid PlatformFile.
220 PlatformFile ReleaseValue() {
221 PlatformFile temp = *value_;
222 *value_ = kInvalidPlatformFileValue;
223 return temp;
226 private:
227 PlatformFile* value_;
230 } // namespace base
232 #endif // BASE_PLATFORM_FILE_H_