Disable crashing tests, my previous checkin to mark them flaky did not help.
[chromium-blink-merge.git] / base / platform_file.h
blobcf01188ea78e4194a28a23f11690a122fcd8b63b
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_
7 #pragma once
9 #include "build/build_config.h"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #endif
14 #include <string>
16 #include "base/base_api.h"
17 #include "base/basictypes.h"
18 #include "base/file_path.h"
19 #include "base/time.h"
21 namespace base {
23 #if defined(OS_WIN)
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;
29 #endif
31 enum PlatformFileFlags {
32 PLATFORM_FILE_OPEN = 1,
33 PLATFORM_FILE_CREATE = 2,
34 PLATFORM_FILE_OPEN_ALWAYS = 4, // May create a new file.
35 PLATFORM_FILE_CREATE_ALWAYS = 8, // May overwrite an old file.
36 PLATFORM_FILE_READ = 16,
37 PLATFORM_FILE_WRITE = 32,
38 PLATFORM_FILE_EXCLUSIVE_READ = 64, // EXCLUSIVE is opposite of Windows SHARE
39 PLATFORM_FILE_EXCLUSIVE_WRITE = 128,
40 PLATFORM_FILE_ASYNC = 256,
41 PLATFORM_FILE_TEMPORARY = 512, // Used on Windows only
42 PLATFORM_FILE_HIDDEN = 1024, // Used on Windows only
43 PLATFORM_FILE_DELETE_ON_CLOSE = 2048,
44 PLATFORM_FILE_TRUNCATE = 4096,
45 PLATFORM_FILE_WRITE_ATTRIBUTES = 8192, // Used on Windows only
46 PLATFORM_FILE_ENUMERATE = 16384, // May enumerate directory
49 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
50 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
51 // browser policy doesn't allow the operation to be executed.
52 enum PlatformFileError {
53 PLATFORM_FILE_OK = 0,
54 PLATFORM_FILE_ERROR_FAILED = -1,
55 PLATFORM_FILE_ERROR_IN_USE = -2,
56 PLATFORM_FILE_ERROR_EXISTS = -3,
57 PLATFORM_FILE_ERROR_NOT_FOUND = -4,
58 PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
59 PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
60 PLATFORM_FILE_ERROR_NO_MEMORY = -7,
61 PLATFORM_FILE_ERROR_NO_SPACE = -8,
62 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
63 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
64 PLATFORM_FILE_ERROR_SECURITY = -11,
65 PLATFORM_FILE_ERROR_ABORT = -12,
66 PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
67 PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
70 // Used to hold information about a given file.
71 // If you add more fields to this structure (platform-specific fields are OK),
72 // make sure to update all functions that use it in file_util_{win|posix}.cc
73 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
74 // chrome/common/common_param_traits.cc.
75 struct BASE_API PlatformFileInfo {
76 PlatformFileInfo();
77 ~PlatformFileInfo();
79 // The size of the file in bytes. Undefined when is_directory is true.
80 int64 size;
82 // True if the file corresponds to a directory.
83 bool is_directory;
85 // True if the file corresponds to a symbolic link.
86 bool is_symbolic_link;
88 // The last modified time of a file.
89 base::Time last_modified;
91 // The last accessed time of a file.
92 base::Time last_accessed;
94 // The creation time of a file.
95 base::Time creation_time;
98 // Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and
99 // |created| is provided, |created| will be set to true if the file was created
100 // or to false in case the file was just opened. |error_code| can be NULL.
101 BASE_API PlatformFile CreatePlatformFile(const FilePath& name,
102 int flags,
103 bool* created,
104 PlatformFileError* error_code);
106 // Closes a file handle. Returns |true| on success and |false| otherwise.
107 BASE_API bool ClosePlatformFile(PlatformFile file);
109 // Reads the given number of bytes (or until EOF is reached) starting with the
110 // given offset. Returns the number of bytes read, or -1 on error.
111 BASE_API int ReadPlatformFile(PlatformFile file, int64 offset,
112 char* data, int size);
114 // Writes the given buffer into the file at the given offset, overwritting any
115 // data that was previously there. Returns the number of bytes written, or -1
116 // on error.
117 BASE_API int WritePlatformFile(PlatformFile file, int64 offset,
118 const char* data, int size);
120 // Truncates the given file to the given length. If |length| is greater than
121 // the current size of the file, the file is extended with zeros. If the file
122 // doesn't exist, |false| is returned.
123 BASE_API bool TruncatePlatformFile(PlatformFile file, int64 length);
125 // Flushes the buffers of the given file.
126 BASE_API bool FlushPlatformFile(PlatformFile file);
128 // Touches the given file.
129 BASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time,
130 const Time& last_modified_time);
132 // Returns some information for the given file.
133 BASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
135 // Use this class to pass ownership of a PlatformFile to a receiver that may or
136 // may not want to accept it. This class does not own the storage for the
137 // PlatformFile.
139 // EXAMPLE:
141 // void MaybeProcessFile(PassPlatformFile pass_file) {
142 // if (...) {
143 // PlatformFile file = pass_file.ReleaseValue();
144 // // Now, we are responsible for closing |file|.
145 // }
146 // }
148 // void OpenAndMaybeProcessFile(const FilePath& path) {
149 // PlatformFile file = CreatePlatformFile(path, ...);
150 // MaybeProcessFile(PassPlatformFile(&file));
151 // if (file != kInvalidPlatformFileValue)
152 // ClosePlatformFile(file);
153 // }
155 class BASE_API PassPlatformFile {
156 public:
157 explicit PassPlatformFile(PlatformFile* value) : value_(value) {
160 // Called to retrieve the PlatformFile stored in this object. The caller
161 // gains ownership of the PlatformFile and is now responsible for closing it.
162 // Any subsequent calls to this method will return an invalid PlatformFile.
163 PlatformFile ReleaseValue() {
164 PlatformFile temp = *value_;
165 *value_ = kInvalidPlatformFileValue;
166 return temp;
169 private:
170 PlatformFile* value_;
173 } // namespace base
175 #endif // BASE_PLATFORM_FILE_H_