| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/base_api.h" | 16 #include "base/base_export.h" |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/file_path.h" | 18 #include "base/file_path.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 | 22 |
| 23 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 24 typedef HANDLE PlatformFile; | 24 typedef HANDLE PlatformFile; |
| 25 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; | 25 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 26 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, | 74 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, |
| 75 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, | 75 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, |
| 76 PLATFORM_FILE_ERROR_INVALID_URL = -15, | 76 PLATFORM_FILE_ERROR_INVALID_URL = -15, |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 // Used to hold information about a given file. | 79 // Used to hold information about a given file. |
| 80 // If you add more fields to this structure (platform-specific fields are OK), | 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 | 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 | 82 // too, and the ParamTraits<base::PlatformFileInfo> implementation in |
| 83 // chrome/common/common_param_traits.cc. | 83 // chrome/common/common_param_traits.cc. |
| 84 struct BASE_API PlatformFileInfo { | 84 struct BASE_EXPORT PlatformFileInfo { |
| 85 PlatformFileInfo(); | 85 PlatformFileInfo(); |
| 86 ~PlatformFileInfo(); | 86 ~PlatformFileInfo(); |
| 87 | 87 |
| 88 // The size of the file in bytes. Undefined when is_directory is true. | 88 // The size of the file in bytes. Undefined when is_directory is true. |
| 89 int64 size; | 89 int64 size; |
| 90 | 90 |
| 91 // True if the file corresponds to a directory. | 91 // True if the file corresponds to a directory. |
| 92 bool is_directory; | 92 bool is_directory; |
| 93 | 93 |
| 94 // True if the file corresponds to a symbolic link. | 94 // True if the file corresponds to a symbolic link. |
| 95 bool is_symbolic_link; | 95 bool is_symbolic_link; |
| 96 | 96 |
| 97 // The last modified time of a file. | 97 // The last modified time of a file. |
| 98 base::Time last_modified; | 98 base::Time last_modified; |
| 99 | 99 |
| 100 // The last accessed time of a file. | 100 // The last accessed time of a file. |
| 101 base::Time last_accessed; | 101 base::Time last_accessed; |
| 102 | 102 |
| 103 // The creation time of a file. | 103 // The creation time of a file. |
| 104 base::Time creation_time; | 104 base::Time creation_time; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 // Creates or opens the given file. If |created| is provided, it will be set to | 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 | 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 | 109 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and |
| 110 // false otherwise. |error_code| can be NULL. | 110 // false otherwise. |error_code| can be NULL. |
| 111 BASE_API PlatformFile CreatePlatformFile(const FilePath& name, | 111 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name, |
| 112 int flags, | 112 int flags, |
| 113 bool* created, | 113 bool* created, |
| 114 PlatformFileError* error_code); | 114 PlatformFileError* error_code); |
| 115 | 115 |
| 116 // Closes a file handle. Returns |true| on success and |false| otherwise. | 116 // Closes a file handle. Returns |true| on success and |false| otherwise. |
| 117 BASE_API bool ClosePlatformFile(PlatformFile file); | 117 BASE_EXPORT bool ClosePlatformFile(PlatformFile file); |
| 118 | 118 |
| 119 // Reads the given number of bytes (or until EOF is reached) starting with the | 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. | 120 // given offset. Returns the number of bytes read, or -1 on error. |
| 121 BASE_API int ReadPlatformFile(PlatformFile file, int64 offset, | 121 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, |
| 122 char* data, int size); | 122 char* data, int size); |
| 123 | 123 |
| 124 // Writes the given buffer into the file at the given offset, overwritting any | 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 | 125 // data that was previously there. Returns the number of bytes written, or -1 |
| 126 // on error. | 126 // on error. |
| 127 BASE_API int WritePlatformFile(PlatformFile file, int64 offset, | 127 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, |
| 128 const char* data, int size); | 128 const char* data, int size); |
| 129 | 129 |
| 130 // Truncates the given file to the given length. If |length| is greater than | 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 | 131 // the current size of the file, the file is extended with zeros. If the file |
| 132 // doesn't exist, |false| is returned. | 132 // doesn't exist, |false| is returned. |
| 133 BASE_API bool TruncatePlatformFile(PlatformFile file, int64 length); | 133 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); |
| 134 | 134 |
| 135 // Flushes the buffers of the given file. | 135 // Flushes the buffers of the given file. |
| 136 BASE_API bool FlushPlatformFile(PlatformFile file); | 136 BASE_EXPORT bool FlushPlatformFile(PlatformFile file); |
| 137 | 137 |
| 138 // Touches the given file. | 138 // Touches the given file. |
| 139 BASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time, | 139 BASE_EXPORT bool TouchPlatformFile(PlatformFile file, |
| 140 const Time& last_modified_time); | 140 const Time& last_access_time, |
| 141 const Time& last_modified_time); |
| 141 | 142 |
| 142 // Returns some information for the given file. | 143 // Returns some information for the given file. |
| 143 BASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); | 144 BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); |
| 144 | 145 |
| 145 // Use this class to pass ownership of a PlatformFile to a receiver that may or | 146 // 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 // may not want to accept it. This class does not own the storage for the |
| 147 // PlatformFile. | 148 // PlatformFile. |
| 148 // | 149 // |
| 149 // EXAMPLE: | 150 // EXAMPLE: |
| 150 // | 151 // |
| 151 // void MaybeProcessFile(PassPlatformFile pass_file) { | 152 // void MaybeProcessFile(PassPlatformFile pass_file) { |
| 152 // if (...) { | 153 // if (...) { |
| 153 // PlatformFile file = pass_file.ReleaseValue(); | 154 // PlatformFile file = pass_file.ReleaseValue(); |
| 154 // // Now, we are responsible for closing |file|. | 155 // // Now, we are responsible for closing |file|. |
| 155 // } | 156 // } |
| 156 // } | 157 // } |
| 157 // | 158 // |
| 158 // void OpenAndMaybeProcessFile(const FilePath& path) { | 159 // void OpenAndMaybeProcessFile(const FilePath& path) { |
| 159 // PlatformFile file = CreatePlatformFile(path, ...); | 160 // PlatformFile file = CreatePlatformFile(path, ...); |
| 160 // MaybeProcessFile(PassPlatformFile(&file)); | 161 // MaybeProcessFile(PassPlatformFile(&file)); |
| 161 // if (file != kInvalidPlatformFileValue) | 162 // if (file != kInvalidPlatformFileValue) |
| 162 // ClosePlatformFile(file); | 163 // ClosePlatformFile(file); |
| 163 // } | 164 // } |
| 164 // | 165 // |
| 165 class BASE_API PassPlatformFile { | 166 class BASE_EXPORT PassPlatformFile { |
| 166 public: | 167 public: |
| 167 explicit PassPlatformFile(PlatformFile* value) : value_(value) { | 168 explicit PassPlatformFile(PlatformFile* value) : value_(value) { |
| 168 } | 169 } |
| 169 | 170 |
| 170 // Called to retrieve the PlatformFile stored in this object. The caller | 171 // 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 // 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 // Any subsequent calls to this method will return an invalid PlatformFile. |
| 173 PlatformFile ReleaseValue() { | 174 PlatformFile ReleaseValue() { |
| 174 PlatformFile temp = *value_; | 175 PlatformFile temp = *value_; |
| 175 *value_ = kInvalidPlatformFileValue; | 176 *value_ = kInvalidPlatformFileValue; |
| 176 return temp; | 177 return temp; |
| 177 } | 178 } |
| 178 | 179 |
| 179 private: | 180 private: |
| 180 PlatformFile* value_; | 181 PlatformFile* value_; |
| 181 }; | 182 }; |
| 182 | 183 |
| 183 } // namespace base | 184 } // namespace base |
| 184 | 185 |
| 185 #endif // BASE_PLATFORM_FILE_H_ | 186 #endif // BASE_PLATFORM_FILE_H_ |
| OLD | NEW |