OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_PLATFORM_FILE_H_ | 5 #ifndef BASE_PLATFORM_FILE_H_ |
6 #define BASE_PLATFORM_FILE_H_ | 6 #define BASE_PLATFORM_FILE_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 PLATFORM_FILE_ERROR_NO_SPACE = -8, | 71 PLATFORM_FILE_ERROR_NO_SPACE = -8, |
72 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, | 72 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, |
73 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, | 73 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, |
74 PLATFORM_FILE_ERROR_SECURITY = -11, | 74 PLATFORM_FILE_ERROR_SECURITY = -11, |
75 PLATFORM_FILE_ERROR_ABORT = -12, | 75 PLATFORM_FILE_ERROR_ABORT = -12, |
76 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, | 76 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, |
77 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, | 77 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, |
78 PLATFORM_FILE_ERROR_INVALID_URL = -15, | 78 PLATFORM_FILE_ERROR_INVALID_URL = -15, |
79 }; | 79 }; |
80 | 80 |
| 81 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
| 82 enum PlatformFileWhence { |
| 83 PLATFORM_FILE_FROM_BEGIN = 0, |
| 84 PLATFORM_FILE_FROM_CURRENT = 1, |
| 85 PLATFORM_FILE_FROM_END = 2 |
| 86 }; |
| 87 |
81 // Used to hold information about a given file. | 88 // Used to hold information about a given file. |
82 // If you add more fields to this structure (platform-specific fields are OK), | 89 // If you add more fields to this structure (platform-specific fields are OK), |
83 // make sure to update all functions that use it in file_util_{win|posix}.cc | 90 // make sure to update all functions that use it in file_util_{win|posix}.cc |
84 // too, and the ParamTraits<base::PlatformFileInfo> implementation in | 91 // too, and the ParamTraits<base::PlatformFileInfo> implementation in |
85 // chrome/common/common_param_traits.cc. | 92 // chrome/common/common_param_traits.cc. |
86 struct BASE_EXPORT PlatformFileInfo { | 93 struct BASE_EXPORT PlatformFileInfo { |
87 PlatformFileInfo(); | 94 PlatformFileInfo(); |
88 ~PlatformFileInfo(); | 95 ~PlatformFileInfo(); |
89 | 96 |
90 // The size of the file in bytes. Undefined when is_directory is true. | 97 // The size of the file in bytes. Undefined when is_directory is true. |
(...skipping 20 matching lines...) Expand all Loading... |
111 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and | 118 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and |
112 // false otherwise. |error_code| can be NULL. | 119 // false otherwise. |error_code| can be NULL. |
113 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name, | 120 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name, |
114 int flags, | 121 int flags, |
115 bool* created, | 122 bool* created, |
116 PlatformFileError* error_code); | 123 PlatformFileError* error_code); |
117 | 124 |
118 // Closes a file handle. Returns |true| on success and |false| otherwise. | 125 // Closes a file handle. Returns |true| on success and |false| otherwise. |
119 BASE_EXPORT bool ClosePlatformFile(PlatformFile file); | 126 BASE_EXPORT bool ClosePlatformFile(PlatformFile file); |
120 | 127 |
| 128 // Changes current position in the file to an |offset| relative to an origin |
| 129 // defined by |whence|. Returns the resultant current position in the file |
| 130 // (relative to the start) or -1 in case of error. |
| 131 BASE_EXPORT int64 SeekPlatformFile(PlatformFile file, |
| 132 PlatformFileWhence whence, |
| 133 int64 offset); |
| 134 |
121 // Reads the given number of bytes (or until EOF is reached) starting with the | 135 // Reads the given number of bytes (or until EOF is reached) starting with the |
122 // given offset. Returns the number of bytes read, or -1 on error. Note that | 136 // given offset. Returns the number of bytes read, or -1 on error. Note that |
123 // this function makes a best effort to read all data on all platforms, so it is | 137 // this function makes a best effort to read all data on all platforms, so it is |
124 // not intended for stream oriented files but instead for cases when the normal | 138 // not intended for stream oriented files but instead for cases when the normal |
125 // expectation is that actually |size| bytes are read unless there is an error. | 139 // expectation is that actually |size| bytes are read unless there is an error. |
126 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, | 140 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, |
127 char* data, int size); | 141 char* data, int size); |
128 | 142 |
129 // Same as above but without seek. | 143 // Same as above but without seek. |
130 BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file, | 144 BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file, |
131 char* data, int size); | 145 char* data, int size); |
132 | 146 |
133 // Reads the given number of bytes (or until EOF is reached) starting with the | 147 // Reads the given number of bytes (or until EOF is reached) starting with the |
134 // given offset, but does not make any effort to read all data on all platforms. | 148 // given offset, but does not make any effort to read all data on all platforms. |
135 // Returns the number of bytes read, or -1 on error. | 149 // Returns the number of bytes read, or -1 on error. |
136 BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, | 150 BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, |
137 char* data, int size); | 151 char* data, int size); |
138 | 152 |
| 153 // Same as above but without seek. |
| 154 BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, |
| 155 char* data, int size); |
| 156 |
139 // Writes the given buffer into the file at the given offset, overwritting any | 157 // Writes the given buffer into the file at the given offset, overwritting any |
140 // data that was previously there. Returns the number of bytes written, or -1 | 158 // data that was previously there. Returns the number of bytes written, or -1 |
141 // on error. Note that this function makes a best effort to write all data on | 159 // on error. Note that this function makes a best effort to write all data on |
142 // all platforms. | 160 // all platforms. |
143 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, | 161 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, |
144 const char* data, int size); | 162 const char* data, int size); |
145 | 163 |
146 // Save as above but without seek. | 164 // Save as above but without seek. |
147 BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file, | 165 BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file, |
148 const char* data, int size); | 166 const char* data, int size); |
149 | 167 |
| 168 // Save as above but does not make any effort to write all data on all |
| 169 // platforms. Returns the number of bytes written, or -1 on error. |
| 170 BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file, |
| 171 const char* data, int size); |
| 172 |
150 // Truncates the given file to the given length. If |length| is greater than | 173 // Truncates the given file to the given length. If |length| is greater than |
151 // the current size of the file, the file is extended with zeros. If the file | 174 // the current size of the file, the file is extended with zeros. If the file |
152 // doesn't exist, |false| is returned. | 175 // doesn't exist, |false| is returned. |
153 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); | 176 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); |
154 | 177 |
155 // Flushes the buffers of the given file. | 178 // Flushes the buffers of the given file. |
156 BASE_EXPORT bool FlushPlatformFile(PlatformFile file); | 179 BASE_EXPORT bool FlushPlatformFile(PlatformFile file); |
157 | 180 |
158 // Touches the given file. | 181 // Touches the given file. |
159 BASE_EXPORT bool TouchPlatformFile(PlatformFile file, | 182 BASE_EXPORT bool TouchPlatformFile(PlatformFile file, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 return temp; | 220 return temp; |
198 } | 221 } |
199 | 222 |
200 private: | 223 private: |
201 PlatformFile* value_; | 224 PlatformFile* value_; |
202 }; | 225 }; |
203 | 226 |
204 } // namespace base | 227 } // namespace base |
205 | 228 |
206 #endif // BASE_PLATFORM_FILE_H_ | 229 #endif // BASE_PLATFORM_FILE_H_ |
OLD | NEW |