| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 // For 64-bit file access (off_t = off64_t, lseek64, etc). | 5 // For 64-bit file access (off_t = off64_t, lseek64, etc). |
| 6 #define _FILE_OFFSET_BITS 64 | 6 #define _FILE_OFFSET_BITS 64 |
| 7 | 7 |
| 8 #include "net/base/file_stream.h" | 8 #include "net/base/file_stream.h" |
| 9 | 9 |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <fcntl.h> | 12 #include <fcntl.h> |
| 13 #include <unistd.h> | 13 #include <unistd.h> |
| 14 #include <errno.h> | 14 #include <errno.h> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/eintr_wrappers.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/message_loop.h" | 19 #include "base/message_loop.h" |
| 19 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 20 #include "base/waitable_event.h" | 21 #include "base/waitable_event.h" |
| 21 #include "base/worker_pool.h" | 22 #include "base/worker_pool.h" |
| 22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 23 | 24 |
| 24 // We cast back and forth, so make sure it's the size we're expecting. | 25 // We cast back and forth, so make sure it's the size we're expecting. |
| 25 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 26 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
| 26 | 27 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 44 return ERR_FAILED; | 45 return ERR_FAILED; |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 | 48 |
| 48 // ReadFile() is a simple wrapper around read() that handles EINTR signals and | 49 // ReadFile() is a simple wrapper around read() that handles EINTR signals and |
| 49 // calls MapErrorCode() to map errno to net error codes. | 50 // calls MapErrorCode() to map errno to net error codes. |
| 50 int ReadFile(base::PlatformFile file, char* buf, int buf_len) { | 51 int ReadFile(base::PlatformFile file, char* buf, int buf_len) { |
| 51 // read(..., 0) returns 0 to indicate end-of-file. | 52 // read(..., 0) returns 0 to indicate end-of-file. |
| 52 | 53 |
| 53 // Loop in the case of getting interrupted by a signal. | 54 // Loop in the case of getting interrupted by a signal. |
| 54 for (;;) { | 55 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); |
| 55 ssize_t res = read(file, buf, static_cast<size_t>(buf_len)); | 56 if (res == static_cast<ssize_t>(-1)) |
| 56 if (res == static_cast<ssize_t>(-1)) { | 57 return MapErrorCode(errno); |
| 57 if (errno == EINTR) | 58 return static_cast<int>(res); |
| 58 continue; | |
| 59 return MapErrorCode(errno); | |
| 60 } | |
| 61 return static_cast<int>(res); | |
| 62 } | |
| 63 } | 59 } |
| 64 | 60 |
| 65 // WriteFile() is a simple wrapper around write() that handles EINTR signals and | 61 // WriteFile() is a simple wrapper around write() that handles EINTR signals and |
| 66 // calls MapErrorCode() to map errno to net error codes. It tries to write to | 62 // calls MapErrorCode() to map errno to net error codes. It tries to write to |
| 67 // completion. | 63 // completion. |
| 68 int WriteFile(base::PlatformFile file, const char* buf, int buf_len) { | 64 int WriteFile(base::PlatformFile file, const char* buf, int buf_len) { |
| 69 while (true) { | 65 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); |
| 70 ssize_t res = write(file, buf, buf_len); | 66 if (res == -1) |
| 71 if (res == -1) { | 67 return MapErrorCode(errno); |
| 72 if (errno == EINTR) | 68 return res; |
| 73 continue; | |
| 74 return MapErrorCode(errno); | |
| 75 } | |
| 76 return res; | |
| 77 } | |
| 78 } | 69 } |
| 79 | 70 |
| 80 // BackgroundReadTask is a simple task that reads a file and then runs | 71 // BackgroundReadTask is a simple task that reads a file and then runs |
| 81 // |callback|. AsyncContext will post this task to the WorkerPool. | 72 // |callback|. AsyncContext will post this task to the WorkerPool. |
| 82 class BackgroundReadTask : public Task { | 73 class BackgroundReadTask : public Task { |
| 83 public: | 74 public: |
| 84 BackgroundReadTask(base::PlatformFile file, char* buf, int buf_len, | 75 BackgroundReadTask(base::PlatformFile file, char* buf, int buf_len, |
| 85 CompletionCallback* callback); | 76 CompletionCallback* callback); |
| 86 ~BackgroundReadTask(); | 77 ~BackgroundReadTask(); |
| 87 | 78 |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 int64 seek_position = Seek(FROM_BEGIN, bytes); | 437 int64 seek_position = Seek(FROM_BEGIN, bytes); |
| 447 if (seek_position != bytes) | 438 if (seek_position != bytes) |
| 448 return ERR_UNEXPECTED; | 439 return ERR_UNEXPECTED; |
| 449 | 440 |
| 450 // And truncate the file. | 441 // And truncate the file. |
| 451 int result = ftruncate(file_, bytes); | 442 int result = ftruncate(file_, bytes); |
| 452 return result == 0 ? seek_position : MapErrorCode(errno); | 443 return result == 0 ? seek_position : MapErrorCode(errno); |
| 453 } | 444 } |
| 454 | 445 |
| 455 } // namespace net | 446 } // namespace net |
| OLD | NEW |