[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 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_PLATFORM_FILE_H_ |
| 6 | #define BASE_PLATFORM_FILE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 8 | |
| 9 | #include "build/build_config.h" |
| 10 | #if defined(OS_WIN) |
| 11 | #include <windows.h> |
| 12 | #endif |
| 13 | |
| 14 | #include <string> |
| 15 | |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 16 | #include "base/base_api.h" |
| 17 | #include "base/basictypes.h" |
| 18 | #include "base/file_path.h" |
| 19 | #include "base/time.h" |
| 20 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 21 | namespace base { |
| 22 | |
| 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 |
| 30 | |
[email protected] | b2f2308d | 2011-05-23 22:00:04 | [diff] [blame] | 31 | // 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] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 34 | enum PlatformFileFlags { |
[email protected] | b2f2308d | 2011-05-23 22:00:04 | [diff] [blame] | 35 | 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] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 51 | PLATFORM_FILE_WRITE_ATTRIBUTES = 8192, // Used on Windows only |
| 52 | PLATFORM_FILE_ENUMERATE = 16384, // May enumerate directory |
[email protected] | ad0f81dd | 2011-06-22 19:29:23 | [diff] [blame] | 53 | |
| 54 | PLATFORM_FILE_SHARE_DELETE = 32768, // Used on Windows only |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 55 | }; |
| 56 | |
[email protected] | 5746640 | 2010-09-22 03:29:14 | [diff] [blame] | 57 | // 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] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 60 | enum PlatformFileError { |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 61 | PLATFORM_FILE_OK = 0, |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 62 | 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] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 69 | PLATFORM_FILE_ERROR_NO_SPACE = -8, |
| 70 | PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, |
[email protected] | 5746640 | 2010-09-22 03:29:14 | [diff] [blame] | 71 | PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, |
| 72 | PLATFORM_FILE_ERROR_SECURITY = -11, |
[email protected] | 81b18ca | 2010-10-07 08:35:09 | [diff] [blame] | 73 | PLATFORM_FILE_ERROR_ABORT = -12, |
| 74 | PLATFORM_FILE_ERROR_NOT_A_FILE = -13, |
| 75 | PLATFORM_FILE_ERROR_NOT_EMPTY = -14, |
[email protected] | 24dceaf | 2011-04-20 09:05:52 | [diff] [blame] | 76 | PLATFORM_FILE_ERROR_INVALID_URL = -15, |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 77 | }; |
| 78 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 79 | // 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 84 | struct BASE_API PlatformFileInfo { |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 85 | PlatformFileInfo(); |
| 86 | ~PlatformFileInfo(); |
| 87 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 88 | // 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] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 94 | // True if the file corresponds to a symbolic link. |
| 95 | bool is_symbolic_link; |
| 96 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 97 | // 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] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 107 | // 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 111 | BASE_API PlatformFile CreatePlatformFile(const FilePath& name, |
| 112 | int flags, |
| 113 | bool* created, |
| 114 | PlatformFileError* error_code); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 115 | |
[email protected] | 5018448 | 2010-09-27 18:51:20 | [diff] [blame] | 116 | // Closes a file handle. Returns |true| on success and |false| otherwise. |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 117 | BASE_API bool ClosePlatformFile(PlatformFile file); |
[email protected] | ee8d4c8 | 2009-08-28 21:58:28 | [diff] [blame] | 118 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 119 | // 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 121 | BASE_API int ReadPlatformFile(PlatformFile file, int64 offset, |
| 122 | char* data, int size); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 123 | |
| 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 127 | BASE_API int WritePlatformFile(PlatformFile file, int64 offset, |
| 128 | const char* data, int size); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 129 | |
| 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 133 | BASE_API bool TruncatePlatformFile(PlatformFile file, int64 length); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 134 | |
| 135 | // Flushes the buffers of the given file. |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 136 | BASE_API bool FlushPlatformFile(PlatformFile file); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 137 | |
| 138 | // Touches the given file. |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 139 | BASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time, |
| 140 | const Time& last_modified_time); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 141 | |
| 142 | // Returns some information for the given file. |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 143 | BASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 144 | |
[email protected] | 21ae1f9 | 2010-07-30 23:05:15 | [diff] [blame] | 145 | // 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] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 165 | class BASE_API PassPlatformFile { |
[email protected] | 21ae1f9 | 2010-07-30 23:05:15 | [diff] [blame] | 166 | 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] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 183 | } // namespace base |
| 184 | |
| 185 | #endif // BASE_PLATFORM_FILE_H_ |