~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/chromium/src/base/files/file.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
 
5
 
#ifndef BASE_FILES_FILE_H_
6
 
#define BASE_FILES_FILE_H_
7
 
 
8
 
#include "build/build_config.h"
9
 
#if defined(OS_WIN)
10
 
#include <windows.h>
11
 
#endif
12
 
 
13
 
#include "base/base_export.h"
14
 
#include "base/basictypes.h"
15
 
#include "base/move.h"
16
 
#include "base/time/time.h"
17
 
 
18
 
#if defined(OS_WIN)
19
 
#include "base/win/scoped_handle.h"
20
 
#endif
21
 
 
22
 
namespace base {
23
 
 
24
 
class FilePath;
25
 
 
26
 
#if defined(OS_WIN)
27
 
typedef HANDLE PlatformFile;
28
 
#elif defined(OS_POSIX)
29
 
typedef int PlatformFile;
30
 
#endif
31
 
 
32
 
 
33
 
// Thin wrapper around an OS-level file.
34
 
// Note that this class does not provide any support for asynchronous IO, other
35
 
// than the ability to create asynchronous handles on Windows.
36
 
//
37
 
// Note about const: this class does not attempt to determine if the underlying
38
 
// file system object is affected by a particular method in order to consider
39
 
// that method const or not. Only methods that deal with member variables in an
40
 
// obvious non-modifying way are marked as const. Any method that forward calls
41
 
// to the OS is not considered const, even if there is no apparent change to
42
 
// member variables.
43
 
class BASE_EXPORT File {
44
 
  MOVE_ONLY_TYPE_FOR_CPP_03(File, RValue)
45
 
 
46
 
 public:
47
 
  // FLAG_(OPEN|CREATE).* are mutually exclusive. You should specify exactly one
48
 
  // of the five (possibly combining with other flags) when opening or creating
49
 
  // a file.
50
 
  // FLAG_(WRITE|APPEND) are mutually exclusive. This is so that APPEND behavior
51
 
  // will be consistent with O_APPEND on POSIX.
52
 
  // FLAG_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file on
53
 
  // creation on POSIX; for existing files, consider using Lock().
54
 
  enum Flags {
55
 
    FLAG_OPEN = 1 << 0,             // Opens a file, only if it exists.
56
 
    FLAG_CREATE = 1 << 1,           // Creates a new file, only if it does not
57
 
                                    // already exist.
58
 
    FLAG_OPEN_ALWAYS = 1 << 2,      // May create a new file.
59
 
    FLAG_CREATE_ALWAYS = 1 << 3,    // May overwrite an old file.
60
 
    FLAG_OPEN_TRUNCATED = 1 << 4,   // Opens a file and truncates it, only if it
61
 
                                    // exists.
62
 
    FLAG_READ = 1 << 5,
63
 
    FLAG_WRITE = 1 << 6,
64
 
    FLAG_APPEND = 1 << 7,
65
 
    FLAG_EXCLUSIVE_READ = 1 << 8,   // EXCLUSIVE is opposite of Windows SHARE.
66
 
    FLAG_EXCLUSIVE_WRITE = 1 << 9,
67
 
    FLAG_ASYNC = 1 << 10,
68
 
    FLAG_TEMPORARY = 1 << 11,       // Used on Windows only.
69
 
    FLAG_HIDDEN = 1 << 12,          // Used on Windows only.
70
 
    FLAG_DELETE_ON_CLOSE = 1 << 13,
71
 
    FLAG_WRITE_ATTRIBUTES = 1 << 14,  // Used on Windows only.
72
 
    FLAG_SHARE_DELETE = 1 << 15,      // Used on Windows only.
73
 
    FLAG_TERMINAL_DEVICE = 1 << 16,   // Serial port flags.
74
 
    FLAG_BACKUP_SEMANTICS = 1 << 17,  // Used on Windows only.
75
 
    FLAG_EXECUTE = 1 << 18,           // Used on Windows only.
76
 
  };
77
 
 
78
 
  // This enum has been recorded in multiple histograms. If the order of the
79
 
  // fields needs to change, please ensure that those histograms are obsolete or
80
 
  // have been moved to a different enum.
81
 
  //
82
 
  // FILE_ERROR_ACCESS_DENIED is returned when a call fails because of a
83
 
  // filesystem restriction. FILE_ERROR_SECURITY is returned when a browser
84
 
  // policy doesn't allow the operation to be executed.
85
 
  enum Error {
86
 
    FILE_OK = 0,
87
 
    FILE_ERROR_FAILED = -1,
88
 
    FILE_ERROR_IN_USE = -2,
89
 
    FILE_ERROR_EXISTS = -3,
90
 
    FILE_ERROR_NOT_FOUND = -4,
91
 
    FILE_ERROR_ACCESS_DENIED = -5,
92
 
    FILE_ERROR_TOO_MANY_OPENED = -6,
93
 
    FILE_ERROR_NO_MEMORY = -7,
94
 
    FILE_ERROR_NO_SPACE = -8,
95
 
    FILE_ERROR_NOT_A_DIRECTORY = -9,
96
 
    FILE_ERROR_INVALID_OPERATION = -10,
97
 
    FILE_ERROR_SECURITY = -11,
98
 
    FILE_ERROR_ABORT = -12,
99
 
    FILE_ERROR_NOT_A_FILE = -13,
100
 
    FILE_ERROR_NOT_EMPTY = -14,
101
 
    FILE_ERROR_INVALID_URL = -15,
102
 
    FILE_ERROR_IO = -16,
103
 
    // Put new entries here and increment FILE_ERROR_MAX.
104
 
    FILE_ERROR_MAX = -17
105
 
  };
106
 
 
107
 
  // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
108
 
  enum Whence {
109
 
    FROM_BEGIN   = 0,
110
 
    FROM_CURRENT = 1,
111
 
    FROM_END     = 2
112
 
  };
113
 
 
114
 
  // Used to hold information about a given file.
115
 
  // If you add more fields to this structure (platform-specific fields are OK),
116
 
  // make sure to update all functions that use it in file_util_{win|posix}.cc
117
 
  // too, and the ParamTraits<base::PlatformFileInfo> implementation in
118
 
  // chrome/common/common_param_traits.cc.
119
 
  struct BASE_EXPORT Info {
120
 
    Info();
121
 
    ~Info();
122
 
 
123
 
    // The size of the file in bytes.  Undefined when is_directory is true.
124
 
    int64 size;
125
 
 
126
 
    // True if the file corresponds to a directory.
127
 
    bool is_directory;
128
 
 
129
 
    // True if the file corresponds to a symbolic link.
130
 
    bool is_symbolic_link;
131
 
 
132
 
    // The last modified time of a file.
133
 
    base::Time last_modified;
134
 
 
135
 
    // The last accessed time of a file.
136
 
    base::Time last_accessed;
137
 
 
138
 
    // The creation time of a file.
139
 
    base::Time creation_time;
140
 
  };
141
 
 
142
 
  File();
143
 
 
144
 
  // Creates or opens the given file. This will fail with 'access denied' if the
145
 
  // |name| contains path traversal ('..') components.
146
 
  File(const FilePath& name, uint32 flags);
147
 
 
148
 
  // Takes ownership of |platform_file|.
149
 
  explicit File(PlatformFile platform_file);
150
 
 
151
 
  // Move constructor for C++03 move emulation of this type.
152
 
  File(RValue other);
153
 
 
154
 
  ~File();
155
 
 
156
 
  // Move operator= for C++03 move emulation of this type.
157
 
  File& operator=(RValue other);
158
 
 
159
 
  // Creates or opens the given file.
160
 
  void Initialize(const FilePath& name, uint32 flags);
161
 
 
162
 
  // Creates or opens the given file, allowing paths with traversal ('..')
163
 
  // components. Use only with extreme care.
164
 
  void InitializeUnsafe(const FilePath& name, uint32 flags);
165
 
 
166
 
  bool IsValid() const;
167
 
 
168
 
  // Returns true if a new file was created (or an old one truncated to zero
169
 
  // length to simulate a new file, which can happen with
170
 
  // FLAG_CREATE_ALWAYS), and false otherwise.
171
 
  bool created() const { return created_; }
172
 
 
173
 
  // Returns the OS result of opening this file. Note that the way to verify
174
 
  // the success of the operation is to use IsValid(), not this method:
175
 
  //   File file(name, flags);
176
 
  //   if (!file.IsValid())
177
 
  //     return;
178
 
  Error error_details() const { return error_details_; }
179
 
 
180
 
  PlatformFile GetPlatformFile() const { return file_; }
181
 
  PlatformFile TakePlatformFile();
182
 
 
183
 
  // Destroying this object closes the file automatically.
184
 
  void Close();
185
 
 
186
 
  // Changes current position in the file to an |offset| relative to an origin
187
 
  // defined by |whence|. Returns the resultant current position in the file
188
 
  // (relative to the start) or -1 in case of error.
189
 
  int64 Seek(Whence whence, int64 offset);
190
 
 
191
 
  // Reads the given number of bytes (or until EOF is reached) starting with the
192
 
  // given offset. Returns the number of bytes read, or -1 on error. Note that
193
 
  // this function makes a best effort to read all data on all platforms, so it
194
 
  // is not intended for stream oriented files but instead for cases when the
195
 
  // normal expectation is that actually |size| bytes are read unless there is
196
 
  // an error.
197
 
  int Read(int64 offset, char* data, int size);
198
 
 
199
 
  // Same as above but without seek.
200
 
  int ReadAtCurrentPos(char* data, int size);
201
 
 
202
 
  // Reads the given number of bytes (or until EOF is reached) starting with the
203
 
  // given offset, but does not make any effort to read all data on all
204
 
  // platforms. Returns the number of bytes read, or -1 on error.
205
 
  int ReadNoBestEffort(int64 offset, char* data, int size);
206
 
 
207
 
  // Same as above but without seek.
208
 
  int ReadAtCurrentPosNoBestEffort(char* data, int size);
209
 
 
210
 
  // Writes the given buffer into the file at the given offset, overwritting any
211
 
  // data that was previously there. Returns the number of bytes written, or -1
212
 
  // on error. Note that this function makes a best effort to write all data on
213
 
  // all platforms.
214
 
  // Ignores the offset and writes to the end of the file if the file was opened
215
 
  // with FLAG_APPEND.
216
 
  int Write(int64 offset, const char* data, int size);
217
 
 
218
 
  // Save as above but without seek.
219
 
  int WriteAtCurrentPos(const char* data, int size);
220
 
 
221
 
  // Save as above but does not make any effort to write all data on all
222
 
  // platforms. Returns the number of bytes written, or -1 on error.
223
 
  int WriteAtCurrentPosNoBestEffort(const char* data, int size);
224
 
 
225
 
  // Returns the current size of this file, or a negative number on failure.
226
 
  int64 GetLength();
227
 
 
228
 
  // Truncates the file to the given length. If |length| is greater than the
229
 
  // current size of the file, the file is extended with zeros. If the file
230
 
  // doesn't exist, |false| is returned.
231
 
  bool SetLength(int64 length);
232
 
 
233
 
  // Flushes the buffers.
234
 
  bool Flush();
235
 
 
236
 
  // Updates the file times.
237
 
  bool SetTimes(Time last_access_time, Time last_modified_time);
238
 
 
239
 
  // Returns some basic information for the given file.
240
 
  bool GetInfo(Info* info);
241
 
 
242
 
  // Attempts to take an exclusive write lock on the file. Returns immediately
243
 
  // (i.e. does not wait for another process to unlock the file). If the lock
244
 
  // was obtained, the result will be FILE_OK. A lock only guarantees
245
 
  // that other processes may not also take a lock on the same file with the
246
 
  // same API - it may still be opened, renamed, unlinked, etc.
247
 
  //
248
 
  // Common semantics:
249
 
  //  * Locks are held by processes, but not inherited by child processes.
250
 
  //  * Locks are released by the OS on file close or process termination.
251
 
  //  * Locks are reliable only on local filesystems.
252
 
  //  * Duplicated file handles may also write to locked files.
253
 
  // Windows-specific semantics:
254
 
  //  * Locks are mandatory for read/write APIs, advisory for mapping APIs.
255
 
  //  * Within a process, locking the same file (by the same or new handle)
256
 
  //    will fail.
257
 
  // POSIX-specific semantics:
258
 
  //  * Locks are advisory only.
259
 
  //  * Within a process, locking the same file (by the same or new handle)
260
 
  //    will succeed.
261
 
  //  * Closing any descriptor on a given file releases the lock.
262
 
  Error Lock();
263
 
 
264
 
  // Unlock a file previously locked.
265
 
  Error Unlock();
266
 
 
267
 
#if defined(OS_WIN)
268
 
  static Error OSErrorToFileError(DWORD last_error);
269
 
#elif defined(OS_POSIX)
270
 
  static Error OSErrorToFileError(int saved_errno);
271
 
#endif
272
 
 
273
 
 private:
274
 
  void SetPlatformFile(PlatformFile file);
275
 
 
276
 
#if defined(OS_WIN)
277
 
  win::ScopedHandle file_;
278
 
#elif defined(OS_POSIX)
279
 
  PlatformFile file_;
280
 
#endif
281
 
 
282
 
  Error error_details_;
283
 
  bool created_;
284
 
  bool async_;
285
 
};
286
 
 
287
 
}  // namespace base
288
 
 
289
 
#endif  // BASE_FILES_FILE_H_