blob: afe909a5960ca6f06c087d11d643db3b63398c90 [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]21da6eb2008-11-03 17:18:142// 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_PLATFORM_FILE_H_
6#define BASE_PLATFORM_FILE_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]21da6eb2008-11-03 17:18:148
9#include "build/build_config.h"
10#if defined(OS_WIN)
11#include <windows.h>
12#endif
13
14#include <string>
15
[email protected]90509cb2011-03-25 18:46:3816#include "base/base_api.h"
17#include "base/basictypes.h"
18#include "base/file_path.h"
19#include "base/time.h"
20
[email protected]21da6eb2008-11-03 17:18:1421namespace base {
22
23#if defined(OS_WIN)
24typedef HANDLE PlatformFile;
25const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
26#elif defined(OS_POSIX)
27typedef int PlatformFile;
28const PlatformFile kInvalidPlatformFileValue = -1;
29#endif
30
[email protected]b2f2308d2011-05-23 22:00:0431// PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
32// exactly one of the five (possibly combining with other flags) when opening
33// or creating a file.
[email protected]21da6eb2008-11-03 17:18:1434enum PlatformFileFlags {
[email protected]b2f2308d2011-05-23 22:00:0435 PLATFORM_FILE_OPEN = 1, // Opens a file, only if it exists.
36 PLATFORM_FILE_CREATE = 2, // Creates a new file, only if it does not
37 // already exist.
38 PLATFORM_FILE_OPEN_ALWAYS = 4, // May create a new file.
39 PLATFORM_FILE_CREATE_ALWAYS = 8, // May overwrite an old file.
40 PLATFORM_FILE_OPEN_TRUNCATED = 16, // Opens a file and truncates it, only if
41 // it exists.
42 PLATFORM_FILE_READ = 32,
43 PLATFORM_FILE_WRITE = 64,
44 PLATFORM_FILE_EXCLUSIVE_READ = 128, // EXCLUSIVE is opposite of Windows SHARE
45 PLATFORM_FILE_EXCLUSIVE_WRITE = 256,
46 PLATFORM_FILE_ASYNC = 512,
47 PLATFORM_FILE_TEMPORARY = 1024, // Used on Windows only
48 PLATFORM_FILE_HIDDEN = 2048, // Used on Windows only
49 PLATFORM_FILE_DELETE_ON_CLOSE = 4096,
50
[email protected]600ea402011-04-12 00:01:5151 PLATFORM_FILE_WRITE_ATTRIBUTES = 8192, // Used on Windows only
52 PLATFORM_FILE_ENUMERATE = 16384, // May enumerate directory
[email protected]ad0f81dd2011-06-22 19:29:2353
54 PLATFORM_FILE_SHARE_DELETE = 32768, // Used on Windows only
[email protected]21da6eb2008-11-03 17:18:1455};
56
[email protected]57466402010-09-22 03:29:1457// PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
58// a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
59// browser policy doesn't allow the operation to be executed.
[email protected]ed65fec2010-08-31 19:30:2760enum PlatformFileError {
[email protected]d480a3e2010-08-24 20:26:2361 PLATFORM_FILE_OK = 0,
[email protected]ed65fec2010-08-31 19:30:2762 PLATFORM_FILE_ERROR_FAILED = -1,
63 PLATFORM_FILE_ERROR_IN_USE = -2,
64 PLATFORM_FILE_ERROR_EXISTS = -3,
65 PLATFORM_FILE_ERROR_NOT_FOUND = -4,
66 PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
67 PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
68 PLATFORM_FILE_ERROR_NO_MEMORY = -7,
[email protected]61da1db2010-09-02 03:43:3669 PLATFORM_FILE_ERROR_NO_SPACE = -8,
70 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
[email protected]57466402010-09-22 03:29:1471 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
72 PLATFORM_FILE_ERROR_SECURITY = -11,
[email protected]81b18ca2010-10-07 08:35:0973 PLATFORM_FILE_ERROR_ABORT = -12,
74 PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
75 PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
[email protected]24dceaf2011-04-20 09:05:5276 PLATFORM_FILE_ERROR_INVALID_URL = -15,
[email protected]d480a3e2010-08-24 20:26:2377};
78
[email protected]2f0193c22010-09-03 02:28:3779// Used to hold information about a given file.
80// If you add more fields to this structure (platform-specific fields are OK),
81// make sure to update all functions that use it in file_util_{win|posix}.cc
82// too, and the ParamTraits<base::PlatformFileInfo> implementation in
83// chrome/common/common_param_traits.cc.
[email protected]90509cb2011-03-25 18:46:3884struct BASE_API PlatformFileInfo {
[email protected]a502bbe72011-01-07 18:06:4585 PlatformFileInfo();
86 ~PlatformFileInfo();
87
[email protected]2f0193c22010-09-03 02:28:3788 // The size of the file in bytes. Undefined when is_directory is true.
89 int64 size;
90
91 // True if the file corresponds to a directory.
92 bool is_directory;
93
[email protected]2e733d102010-11-30 00:43:3794 // True if the file corresponds to a symbolic link.
95 bool is_symbolic_link;
96
[email protected]2f0193c22010-09-03 02:28:3797 // The last modified time of a file.
98 base::Time last_modified;
99
100 // The last accessed time of a file.
101 base::Time last_accessed;
102
103 // The creation time of a file.
104 base::Time creation_time;
105};
106
[email protected]d4905e2e2011-05-13 21:56:32107// Creates or opens the given file. If |created| is provided, it will be set to
108// true if a new file was created [or an old one truncated to zero length to
109// simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
110// false otherwise. |error_code| can be NULL.
[email protected]90509cb2011-03-25 18:46:38111BASE_API PlatformFile CreatePlatformFile(const FilePath& name,
112 int flags,
113 bool* created,
114 PlatformFileError* error_code);
[email protected]21da6eb2008-11-03 17:18:14115
[email protected]50184482010-09-27 18:51:20116// Closes a file handle. Returns |true| on success and |false| otherwise.
[email protected]90509cb2011-03-25 18:46:38117BASE_API bool ClosePlatformFile(PlatformFile file);
[email protected]ee8d4c82009-08-28 21:58:28118
[email protected]3fb43ed12010-09-10 03:01:14119// Reads the given number of bytes (or until EOF is reached) starting with the
120// given offset. Returns the number of bytes read, or -1 on error.
[email protected]90509cb2011-03-25 18:46:38121BASE_API int ReadPlatformFile(PlatformFile file, int64 offset,
122 char* data, int size);
[email protected]3fb43ed12010-09-10 03:01:14123
124// Writes the given buffer into the file at the given offset, overwritting any
125// data that was previously there. Returns the number of bytes written, or -1
126// on error.
[email protected]90509cb2011-03-25 18:46:38127BASE_API int WritePlatformFile(PlatformFile file, int64 offset,
128 const char* data, int size);
[email protected]3fb43ed12010-09-10 03:01:14129
130// Truncates the given file to the given length. If |length| is greater than
131// the current size of the file, the file is extended with zeros. If the file
132// doesn't exist, |false| is returned.
[email protected]90509cb2011-03-25 18:46:38133BASE_API bool TruncatePlatformFile(PlatformFile file, int64 length);
[email protected]3fb43ed12010-09-10 03:01:14134
135// Flushes the buffers of the given file.
[email protected]90509cb2011-03-25 18:46:38136BASE_API bool FlushPlatformFile(PlatformFile file);
[email protected]3fb43ed12010-09-10 03:01:14137
138// Touches the given file.
[email protected]90509cb2011-03-25 18:46:38139BASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time,
140 const Time& last_modified_time);
[email protected]3fb43ed12010-09-10 03:01:14141
142// Returns some information for the given file.
[email protected]90509cb2011-03-25 18:46:38143BASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
[email protected]3fb43ed12010-09-10 03:01:14144
[email protected]21ae1f92010-07-30 23:05:15145// Use this class to pass ownership of a PlatformFile to a receiver that may or
146// may not want to accept it. This class does not own the storage for the
147// PlatformFile.
148//
149// EXAMPLE:
150//
151// void MaybeProcessFile(PassPlatformFile pass_file) {
152// if (...) {
153// PlatformFile file = pass_file.ReleaseValue();
154// // Now, we are responsible for closing |file|.
155// }
156// }
157//
158// void OpenAndMaybeProcessFile(const FilePath& path) {
159// PlatformFile file = CreatePlatformFile(path, ...);
160// MaybeProcessFile(PassPlatformFile(&file));
161// if (file != kInvalidPlatformFileValue)
162// ClosePlatformFile(file);
163// }
164//
[email protected]90509cb2011-03-25 18:46:38165class BASE_API PassPlatformFile {
[email protected]21ae1f92010-07-30 23:05:15166 public:
167 explicit PassPlatformFile(PlatformFile* value) : value_(value) {
168 }
169
170 // Called to retrieve the PlatformFile stored in this object. The caller
171 // gains ownership of the PlatformFile and is now responsible for closing it.
172 // Any subsequent calls to this method will return an invalid PlatformFile.
173 PlatformFile ReleaseValue() {
174 PlatformFile temp = *value_;
175 *value_ = kInvalidPlatformFileValue;
176 return temp;
177 }
178
179 private:
180 PlatformFile* value_;
181};
182
[email protected]21da6eb2008-11-03 17:18:14183} // namespace base
184
185#endif // BASE_PLATFORM_FILE_H_