| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 #include <libgen.h> | 9 #include <libgen.h> |
| 10 | 10 |
| 11 #include "bin/builtin.h" |
| 11 #include "bin/file.h" | 12 #include "bin/file.h" |
| 12 | 13 |
| 13 class FileHandle { | 14 class FileHandle { |
| 14 public: | 15 public: |
| 15 explicit FileHandle(int fd) : fd_(fd) { } | 16 explicit FileHandle(int fd) : fd_(fd) { } |
| 16 ~FileHandle() { } | 17 ~FileHandle() { } |
| 17 int fd() const { return fd_; } | 18 int fd() const { return fd_; } |
| 18 void set_fd(int fd) { fd_ = fd; } | 19 void set_fd(int fd) { fd_ = fd; } |
| 19 | 20 |
| 20 private: | 21 private: |
| 21 int fd_; | 22 int fd_; |
| 22 | 23 |
| 23 // DISALLOW_COPY_AND_ASSIGN(FileHandle). | 24 // DISALLOW_COPY_AND_ASSIGN(FileHandle). |
| 24 FileHandle(const FileHandle&); | 25 FileHandle(const FileHandle&); |
| 25 void operator=(const FileHandle&); | 26 void operator=(const FileHandle&); |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 | 29 |
| 29 File::~File() { | 30 File::~File() { |
| 30 // Close the file (unless it's a standard stream). | 31 // Close the file (unless it's a standard stream). |
| 31 if (handle_->fd() > STDERR_FILENO) { | 32 if (handle_->fd() > STDERR_FILENO) { |
| 32 Close(); | 33 Close(); |
| 33 } | 34 } |
| 34 delete handle_; | 35 delete handle_; |
| 35 } | 36 } |
| 36 | 37 |
| 37 | 38 |
| 38 void File::Close() { | 39 void File::Close() { |
| 39 assert(handle_->fd() >= 0); | 40 ASSERT(handle_->fd() >= 0); |
| 40 int err = close(handle_->fd()); | 41 int err = close(handle_->fd()); |
| 41 if (err != 0) { | 42 if (err != 0) { |
| 42 const int kBufferSize = 1024; | 43 const int kBufferSize = 1024; |
| 43 char error_message[kBufferSize]; | 44 char error_message[kBufferSize]; |
| 44 strerror_r(errno, error_message, kBufferSize); | 45 strerror_r(errno, error_message, kBufferSize); |
| 45 fprintf(stderr, "%s\n", error_message); | 46 fprintf(stderr, "%s\n", error_message); |
| 46 } | 47 } |
| 47 handle_->set_fd(kClosedFd); | 48 handle_->set_fd(kClosedFd); |
| 48 } | 49 } |
| 49 | 50 |
| 50 | 51 |
| 51 bool File::IsClosed() { | 52 bool File::IsClosed() { |
| 52 return handle_->fd() == kClosedFd; | 53 return handle_->fd() == kClosedFd; |
| 53 } | 54 } |
| 54 | 55 |
| 55 | 56 |
| 56 int64_t File::Read(void* buffer, int64_t num_bytes) { | 57 int64_t File::Read(void* buffer, int64_t num_bytes) { |
| 57 assert(handle_->fd() >= 0); | 58 ASSERT(handle_->fd() >= 0); |
| 58 return read(handle_->fd(), buffer, num_bytes); | 59 return read(handle_->fd(), buffer, num_bytes); |
| 59 } | 60 } |
| 60 | 61 |
| 61 | 62 |
| 62 int64_t File::Write(const void* buffer, int64_t num_bytes) { | 63 int64_t File::Write(const void* buffer, int64_t num_bytes) { |
| 63 assert(handle_->fd() >= 0); | 64 ASSERT(handle_->fd() >= 0); |
| 64 return write(handle_->fd(), buffer, num_bytes); | 65 return write(handle_->fd(), buffer, num_bytes); |
| 65 } | 66 } |
| 66 | 67 |
| 67 | 68 |
| 68 off_t File::Position() { | 69 off_t File::Position() { |
| 69 assert(handle_->fd() >= 0); | 70 ASSERT(handle_->fd() >= 0); |
| 70 return lseek(handle_->fd(), 0, SEEK_CUR); | 71 return lseek(handle_->fd(), 0, SEEK_CUR); |
| 71 } | 72 } |
| 72 | 73 |
| 73 | 74 |
| 74 void File::Flush() { | 75 void File::Flush() { |
| 75 assert(handle_->fd() >= 0); | 76 ASSERT(handle_->fd() >= 0); |
| 76 fsync(handle_->fd()); | 77 fsync(handle_->fd()); |
| 77 } | 78 } |
| 78 | 79 |
| 79 | 80 |
| 80 off_t File::Length() { | 81 off_t File::Length() { |
| 81 assert(handle_->fd() >= 0); | 82 ASSERT(handle_->fd() >= 0); |
| 82 off_t position = lseek(handle_->fd(), 0, SEEK_CUR); | 83 off_t position = lseek(handle_->fd(), 0, SEEK_CUR); |
| 83 if (position < 0) { | 84 if (position < 0) { |
| 84 // The file is not capable of seeking. Return an error. | 85 // The file is not capable of seeking. Return an error. |
| 85 return -1; | 86 return -1; |
| 86 } | 87 } |
| 87 off_t result = lseek(handle_->fd(), 0, SEEK_END); | 88 off_t result = lseek(handle_->fd(), 0, SEEK_END); |
| 88 lseek(handle_->fd(), position, SEEK_SET); | 89 lseek(handle_->fd(), position, SEEK_SET); |
| 89 return result; | 90 return result; |
| 90 } | 91 } |
| 91 | 92 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 119 | 120 |
| 120 | 121 |
| 121 const char* File::PathSeparator() { | 122 const char* File::PathSeparator() { |
| 122 return "/"; | 123 return "/"; |
| 123 } | 124 } |
| 124 | 125 |
| 125 | 126 |
| 126 const char* File::StringEscapedPathSeparator() { | 127 const char* File::StringEscapedPathSeparator() { |
| 127 return "/"; | 128 return "/"; |
| 128 } | 129 } |
| OLD | NEW |