| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/platform_file.h" | 5 #include "base/platform_file.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 | 17 |
| 18 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
| 19 #include "base/os_compat_android.h" | 19 #include "base/os_compat_android.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 // Make sure our Whence mappings match the system headers. |
| 25 COMPILE_ASSERT(PLATFORM_FILE_FROM_BEGIN == SEEK_SET && |
| 26 PLATFORM_FILE_FROM_CURRENT == SEEK_CUR && |
| 27 PLATFORM_FILE_FROM_END == SEEK_END, whence_matches_system); |
| 28 |
| 24 #if defined(OS_BSD) || defined(OS_MACOSX) | 29 #if defined(OS_BSD) || defined(OS_MACOSX) |
| 25 typedef struct stat stat_wrapper_t; | 30 typedef struct stat stat_wrapper_t; |
| 26 static int CallFstat(int fd, stat_wrapper_t *sb) { | 31 static int CallFstat(int fd, stat_wrapper_t *sb) { |
| 27 base::ThreadRestrictions::AssertIOAllowed(); | 32 base::ThreadRestrictions::AssertIOAllowed(); |
| 28 return fstat(fd, sb); | 33 return fstat(fd, sb); |
| 29 } | 34 } |
| 30 #else | 35 #else |
| 31 typedef struct stat64 stat_wrapper_t; | 36 typedef struct stat64 stat_wrapper_t; |
| 32 static int CallFstat(int fd, stat_wrapper_t *sb) { | 37 static int CallFstat(int fd, stat_wrapper_t *sb) { |
| 33 base::ThreadRestrictions::AssertIOAllowed(); | 38 base::ThreadRestrictions::AssertIOAllowed(); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 156 } |
| 152 | 157 |
| 153 return descriptor; | 158 return descriptor; |
| 154 } | 159 } |
| 155 | 160 |
| 156 bool ClosePlatformFile(PlatformFile file) { | 161 bool ClosePlatformFile(PlatformFile file) { |
| 157 base::ThreadRestrictions::AssertIOAllowed(); | 162 base::ThreadRestrictions::AssertIOAllowed(); |
| 158 return !HANDLE_EINTR(close(file)); | 163 return !HANDLE_EINTR(close(file)); |
| 159 } | 164 } |
| 160 | 165 |
| 166 int64 SeekPlatformFile(PlatformFile file, |
| 167 PlatformFileWhence whence, |
| 168 int64 offset) { |
| 169 base::ThreadRestrictions::AssertIOAllowed(); |
| 170 if (file < 0 || offset < 0) |
| 171 return -1; |
| 172 |
| 173 return lseek(file, static_cast<off_t>(offset), static_cast<int>(whence)); |
| 174 } |
| 175 |
| 161 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { | 176 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { |
| 162 base::ThreadRestrictions::AssertIOAllowed(); | 177 base::ThreadRestrictions::AssertIOAllowed(); |
| 163 if (file < 0 || size < 0) | 178 if (file < 0 || size < 0) |
| 164 return -1; | 179 return -1; |
| 165 | 180 |
| 166 int bytes_read = 0; | 181 int bytes_read = 0; |
| 167 int rv; | 182 int rv; |
| 168 do { | 183 do { |
| 169 rv = HANDLE_EINTR(pread(file, data + bytes_read, | 184 rv = HANDLE_EINTR(pread(file, data + bytes_read, |
| 170 size - bytes_read, offset + bytes_read)); | 185 size - bytes_read, offset + bytes_read)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 197 | 212 |
| 198 int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, | 213 int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, |
| 199 char* data, int size) { | 214 char* data, int size) { |
| 200 base::ThreadRestrictions::AssertIOAllowed(); | 215 base::ThreadRestrictions::AssertIOAllowed(); |
| 201 if (file < 0) | 216 if (file < 0) |
| 202 return -1; | 217 return -1; |
| 203 | 218 |
| 204 return HANDLE_EINTR(pread(file, data, size, offset)); | 219 return HANDLE_EINTR(pread(file, data, size, offset)); |
| 205 } | 220 } |
| 206 | 221 |
| 222 int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, |
| 223 char* data, int size) { |
| 224 base::ThreadRestrictions::AssertIOAllowed(); |
| 225 if (file < 0 || size < 0) |
| 226 return -1; |
| 227 |
| 228 return HANDLE_EINTR(read(file, data, size)); |
| 229 } |
| 230 |
| 207 int WritePlatformFile(PlatformFile file, int64 offset, | 231 int WritePlatformFile(PlatformFile file, int64 offset, |
| 208 const char* data, int size) { | 232 const char* data, int size) { |
| 209 base::ThreadRestrictions::AssertIOAllowed(); | 233 base::ThreadRestrictions::AssertIOAllowed(); |
| 210 if (file < 0 || size < 0) | 234 if (file < 0 || size < 0) |
| 211 return -1; | 235 return -1; |
| 212 | 236 |
| 213 int bytes_written = 0; | 237 int bytes_written = 0; |
| 214 int rv; | 238 int rv; |
| 215 do { | 239 do { |
| 216 rv = HANDLE_EINTR(pwrite(file, data + bytes_written, | 240 rv = HANDLE_EINTR(pwrite(file, data + bytes_written, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 236 rv = HANDLE_EINTR(write(file, data, size)); | 260 rv = HANDLE_EINTR(write(file, data, size)); |
| 237 if (rv <= 0) | 261 if (rv <= 0) |
| 238 break; | 262 break; |
| 239 | 263 |
| 240 bytes_written += rv; | 264 bytes_written += rv; |
| 241 } while (bytes_written < size); | 265 } while (bytes_written < size); |
| 242 | 266 |
| 243 return bytes_written ? bytes_written : rv; | 267 return bytes_written ? bytes_written : rv; |
| 244 } | 268 } |
| 245 | 269 |
| 270 int WritePlatformFileCurPosNoBestEffort(PlatformFile file, |
| 271 const char* data, int size) { |
| 272 base::ThreadRestrictions::AssertIOAllowed(); |
| 273 if (file < 0 || size < 0) |
| 274 return -1; |
| 275 |
| 276 return HANDLE_EINTR(write(file, data, size)); |
| 277 } |
| 278 |
| 246 bool TruncatePlatformFile(PlatformFile file, int64 length) { | 279 bool TruncatePlatformFile(PlatformFile file, int64 length) { |
| 247 base::ThreadRestrictions::AssertIOAllowed(); | 280 base::ThreadRestrictions::AssertIOAllowed(); |
| 248 return ((file >= 0) && !HANDLE_EINTR(ftruncate(file, length))); | 281 return ((file >= 0) && !HANDLE_EINTR(ftruncate(file, length))); |
| 249 } | 282 } |
| 250 | 283 |
| 251 bool FlushPlatformFile(PlatformFile file) { | 284 bool FlushPlatformFile(PlatformFile file) { |
| 252 base::ThreadRestrictions::AssertIOAllowed(); | 285 base::ThreadRestrictions::AssertIOAllowed(); |
| 253 return !HANDLE_EINTR(fsync(file)); | 286 return !HANDLE_EINTR(fsync(file)); |
| 254 } | 287 } |
| 255 | 288 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 276 info->is_directory = S_ISDIR(file_info.st_mode); | 309 info->is_directory = S_ISDIR(file_info.st_mode); |
| 277 info->is_symbolic_link = S_ISLNK(file_info.st_mode); | 310 info->is_symbolic_link = S_ISLNK(file_info.st_mode); |
| 278 info->size = file_info.st_size; | 311 info->size = file_info.st_size; |
| 279 info->last_modified = base::Time::FromTimeT(file_info.st_mtime); | 312 info->last_modified = base::Time::FromTimeT(file_info.st_mtime); |
| 280 info->last_accessed = base::Time::FromTimeT(file_info.st_atime); | 313 info->last_accessed = base::Time::FromTimeT(file_info.st_atime); |
| 281 info->creation_time = base::Time::FromTimeT(file_info.st_ctime); | 314 info->creation_time = base::Time::FromTimeT(file_info.st_ctime); |
| 282 return true; | 315 return true; |
| 283 } | 316 } |
| 284 | 317 |
| 285 } // namespace base | 318 } // namespace base |
| OLD | NEW |