| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "bin/file.h" | 5 #include "bin/file.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 #include <libgen.h> | 11 #include <libgen.h> |
| 12 #include <limits.h> | 12 #include <limits.h> |
| 13 | 13 |
| 14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
| 15 #include "bin/log.h" |
| 15 | 16 |
| 16 class FileHandle { | 17 class FileHandle { |
| 17 public: | 18 public: |
| 18 explicit FileHandle(int fd) : fd_(fd) { } | 19 explicit FileHandle(int fd) : fd_(fd) { } |
| 19 ~FileHandle() { } | 20 ~FileHandle() { } |
| 20 int fd() const { return fd_; } | 21 int fd() const { return fd_; } |
| 21 void set_fd(int fd) { fd_ = fd; } | 22 void set_fd(int fd) { fd_ = fd; } |
| 22 | 23 |
| 23 private: | 24 private: |
| 24 int fd_; | 25 int fd_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 } | 37 } |
| 37 | 38 |
| 38 | 39 |
| 39 void File::Close() { | 40 void File::Close() { |
| 40 ASSERT(handle_->fd() >= 0); | 41 ASSERT(handle_->fd() >= 0); |
| 41 int err = TEMP_FAILURE_RETRY(close(handle_->fd())); | 42 int err = TEMP_FAILURE_RETRY(close(handle_->fd())); |
| 42 if (err != 0) { | 43 if (err != 0) { |
| 43 const int kBufferSize = 1024; | 44 const int kBufferSize = 1024; |
| 44 char error_message[kBufferSize]; | 45 char error_message[kBufferSize]; |
| 45 strerror_r(errno, error_message, kBufferSize); | 46 strerror_r(errno, error_message, kBufferSize); |
| 46 fprintf(stderr, "%s\n", error_message); | 47 Log::PrintErr("%s\n", error_message); |
| 47 } | 48 } |
| 48 handle_->set_fd(kClosedFd); | 49 handle_->set_fd(kClosedFd); |
| 49 } | 50 } |
| 50 | 51 |
| 51 | 52 |
| 52 bool File::IsClosed() { | 53 bool File::IsClosed() { |
| 53 return handle_->fd() == kClosedFd; | 54 return handle_->fd() == kClosedFd; |
| 54 } | 55 } |
| 55 | 56 |
| 56 | 57 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 int result = fstat(fd, &buf); | 241 int result = fstat(fd, &buf); |
| 241 if (result == -1) { | 242 if (result == -1) { |
| 242 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 243 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 243 } | 244 } |
| 244 if (S_ISCHR(buf.st_mode)) return kTerminal; | 245 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 245 if (S_ISFIFO(buf.st_mode)) return kPipe; | 246 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 246 if (S_ISSOCK(buf.st_mode)) return kSocket; | 247 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 247 if (S_ISREG(buf.st_mode)) return kFile; | 248 if (S_ISREG(buf.st_mode)) return kFile; |
| 248 return kOther; | 249 return kOther; |
| 249 } | 250 } |
| OLD | NEW |