| 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> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 typedef struct stat64 stat_wrapper_t; | 39 typedef struct stat64 stat_wrapper_t; |
| 40 static int CallFstat(int fd, stat_wrapper_t *sb) { | 40 static int CallFstat(int fd, stat_wrapper_t *sb) { |
| 41 base::ThreadRestrictions::AssertIOAllowed(); | 41 base::ThreadRestrictions::AssertIOAllowed(); |
| 42 return fstat64(fd, sb); | 42 return fstat64(fd, sb); |
| 43 } | 43 } |
| 44 #endif | 44 #endif |
| 45 | 45 |
| 46 // NaCl doesn't provide the following system calls, so either simulate them or | 46 // NaCl doesn't provide the following system calls, so either simulate them or |
| 47 // wrap them in order to minimize the number of #ifdef's in this file. | 47 // wrap them in order to minimize the number of #ifdef's in this file. |
| 48 #if !defined(OS_NACL) | 48 #if !defined(OS_NACL) |
| 49 static int DoPread(PlatformFile file, char* data, int size, int64 offset) { | |
| 50 return HANDLE_EINTR(pread(file, data, size, offset)); | |
| 51 } | |
| 52 | |
| 53 static int DoPwrite(PlatformFile file, const char* data, int size, | |
| 54 int64 offset) { | |
| 55 return HANDLE_EINTR(pwrite(file, data, size, offset)); | |
| 56 } | |
| 57 | |
| 58 static bool IsOpenAppend(PlatformFile file) { | 49 static bool IsOpenAppend(PlatformFile file) { |
| 59 return (fcntl(file, F_GETFL) & O_APPEND) != 0; | 50 return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
| 60 } | 51 } |
| 61 | 52 |
| 62 static int CallFtruncate(PlatformFile file, int64 length) { | 53 static int CallFtruncate(PlatformFile file, int64 length) { |
| 63 return HANDLE_EINTR(ftruncate(file, length)); | 54 return HANDLE_EINTR(ftruncate(file, length)); |
| 64 } | 55 } |
| 65 | 56 |
| 66 static int CallFsync(PlatformFile file) { | 57 static int CallFsync(PlatformFile file) { |
| 67 return HANDLE_EINTR(fsync(file)); | 58 return HANDLE_EINTR(fsync(file)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 88 struct flock lock; | 79 struct flock lock; |
| 89 lock.l_type = F_WRLCK; | 80 lock.l_type = F_WRLCK; |
| 90 lock.l_whence = SEEK_SET; | 81 lock.l_whence = SEEK_SET; |
| 91 lock.l_start = 0; | 82 lock.l_start = 0; |
| 92 lock.l_len = 0; // Lock entire file. | 83 lock.l_len = 0; // Lock entire file. |
| 93 if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) | 84 if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) |
| 94 return ErrnoToPlatformFileError(errno); | 85 return ErrnoToPlatformFileError(errno); |
| 95 return PLATFORM_FILE_OK; | 86 return PLATFORM_FILE_OK; |
| 96 } | 87 } |
| 97 #else // defined(OS_NACL) | 88 #else // defined(OS_NACL) |
| 98 // TODO(bbudge) Remove DoPread, DoPwrite when NaCl implements pread, pwrite. | |
| 99 static int DoPread(PlatformFile file, char* data, int size, int64 offset) { | |
| 100 lseek(file, static_cast<off_t>(offset), SEEK_SET); | |
| 101 return HANDLE_EINTR(read(file, data, size)); | |
| 102 } | |
| 103 | |
| 104 static int DoPwrite(PlatformFile file, const char* data, int size, | |
| 105 int64 offset) { | |
| 106 lseek(file, static_cast<off_t>(offset), SEEK_SET); | |
| 107 return HANDLE_EINTR(write(file, data, size)); | |
| 108 } | |
| 109 | 89 |
| 110 static bool IsOpenAppend(PlatformFile file) { | 90 static bool IsOpenAppend(PlatformFile file) { |
| 111 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX | 91 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX |
| 112 // standard and always appends if the file is opened with O_APPEND, just | 92 // standard and always appends if the file is opened with O_APPEND, just |
| 113 // return false here. | 93 // return false here. |
| 114 return false; | 94 return false; |
| 115 } | 95 } |
| 116 | 96 |
| 117 static int CallFtruncate(PlatformFile file, int64 length) { | 97 static int CallFtruncate(PlatformFile file, int64 length) { |
| 118 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. | 98 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 235 } |
| 256 | 236 |
| 257 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { | 237 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { |
| 258 base::ThreadRestrictions::AssertIOAllowed(); | 238 base::ThreadRestrictions::AssertIOAllowed(); |
| 259 if (file < 0 || size < 0) | 239 if (file < 0 || size < 0) |
| 260 return -1; | 240 return -1; |
| 261 | 241 |
| 262 int bytes_read = 0; | 242 int bytes_read = 0; |
| 263 int rv; | 243 int rv; |
| 264 do { | 244 do { |
| 265 rv = DoPread(file, data + bytes_read, | 245 rv = HANDLE_EINTR(pread(file, data + bytes_read, |
| 266 size - bytes_read, offset + bytes_read); | 246 size - bytes_read, offset + bytes_read)); |
| 267 if (rv <= 0) | 247 if (rv <= 0) |
| 268 break; | 248 break; |
| 269 | 249 |
| 270 bytes_read += rv; | 250 bytes_read += rv; |
| 271 } while (bytes_read < size); | 251 } while (bytes_read < size); |
| 272 | 252 |
| 273 return bytes_read ? bytes_read : rv; | 253 return bytes_read ? bytes_read : rv; |
| 274 } | 254 } |
| 275 | 255 |
| 276 int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) { | 256 int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 290 | 270 |
| 291 return bytes_read ? bytes_read : rv; | 271 return bytes_read ? bytes_read : rv; |
| 292 } | 272 } |
| 293 | 273 |
| 294 int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, | 274 int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, |
| 295 char* data, int size) { | 275 char* data, int size) { |
| 296 base::ThreadRestrictions::AssertIOAllowed(); | 276 base::ThreadRestrictions::AssertIOAllowed(); |
| 297 if (file < 0) | 277 if (file < 0) |
| 298 return -1; | 278 return -1; |
| 299 | 279 |
| 300 return DoPread(file, data, size, offset); | 280 return HANDLE_EINTR(pread(file, data, size, offset)); |
| 301 } | 281 } |
| 302 | 282 |
| 303 int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, | 283 int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, |
| 304 char* data, int size) { | 284 char* data, int size) { |
| 305 base::ThreadRestrictions::AssertIOAllowed(); | 285 base::ThreadRestrictions::AssertIOAllowed(); |
| 306 if (file < 0 || size < 0) | 286 if (file < 0 || size < 0) |
| 307 return -1; | 287 return -1; |
| 308 | 288 |
| 309 return HANDLE_EINTR(read(file, data, size)); | 289 return HANDLE_EINTR(read(file, data, size)); |
| 310 } | 290 } |
| 311 | 291 |
| 312 int WritePlatformFile(PlatformFile file, int64 offset, | 292 int WritePlatformFile(PlatformFile file, int64 offset, |
| 313 const char* data, int size) { | 293 const char* data, int size) { |
| 314 base::ThreadRestrictions::AssertIOAllowed(); | 294 base::ThreadRestrictions::AssertIOAllowed(); |
| 315 | 295 |
| 316 if (IsOpenAppend(file)) | 296 if (IsOpenAppend(file)) |
| 317 return WritePlatformFileAtCurrentPos(file, data, size); | 297 return WritePlatformFileAtCurrentPos(file, data, size); |
| 318 | 298 |
| 319 if (file < 0 || size < 0) | 299 if (file < 0 || size < 0) |
| 320 return -1; | 300 return -1; |
| 321 | 301 |
| 322 int bytes_written = 0; | 302 int bytes_written = 0; |
| 323 int rv; | 303 int rv; |
| 324 do { | 304 do { |
| 325 rv = DoPwrite(file, data + bytes_written, | 305 rv = HANDLE_EINTR(pwrite(file, data + bytes_written, |
| 326 size - bytes_written, offset + bytes_written); | 306 size - bytes_written, offset + bytes_written)); |
| 327 if (rv <= 0) | 307 if (rv <= 0) |
| 328 break; | 308 break; |
| 329 | 309 |
| 330 bytes_written += rv; | 310 bytes_written += rv; |
| 331 } while (bytes_written < size); | 311 } while (bytes_written < size); |
| 332 | 312 |
| 333 return bytes_written ? bytes_written : rv; | 313 return bytes_written ? bytes_written : rv; |
| 334 } | 314 } |
| 335 | 315 |
| 336 int WritePlatformFileAtCurrentPos(PlatformFile file, | 316 int WritePlatformFileAtCurrentPos(PlatformFile file, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 default: | 456 default: |
| 477 #if !defined(OS_NACL) // NaCl build has no metrics code. | 457 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 478 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", | 458 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
| 479 saved_errno); | 459 saved_errno); |
| 480 #endif | 460 #endif |
| 481 return PLATFORM_FILE_ERROR_FAILED; | 461 return PLATFORM_FILE_ERROR_FAILED; |
| 482 } | 462 } |
| 483 } | 463 } |
| 484 | 464 |
| 485 } // namespace base | 465 } // namespace base |
| OLD | NEW |