| 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 | 12 |
| 13 #include "bin/builtin.h" | 13 #include "bin/builtin.h" |
| 14 #include "bin/log.h" |
| 14 | 15 |
| 15 class FileHandle { | 16 class FileHandle { |
| 16 public: | 17 public: |
| 17 explicit FileHandle(int fd) : fd_(fd) { } | 18 explicit FileHandle(int fd) : fd_(fd) { } |
| 18 ~FileHandle() { } | 19 ~FileHandle() { } |
| 19 int fd() const { return fd_; } | 20 int fd() const { return fd_; } |
| 20 void set_fd(int fd) { fd_ = fd; } | 21 void set_fd(int fd) { fd_ = fd; } |
| 21 | 22 |
| 22 private: | 23 private: |
| 23 int fd_; | 24 int fd_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 = TEMP_FAILURE_RETRY(close(handle_->fd())); | 41 int err = TEMP_FAILURE_RETRY(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 Log::PrintErr("%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 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 int result = fstat(fd, &buf); | 234 int result = fstat(fd, &buf); |
| 234 if (result == -1) { | 235 if (result == -1) { |
| 235 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 236 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 236 } | 237 } |
| 237 if (S_ISCHR(buf.st_mode)) return kTerminal; | 238 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 238 if (S_ISFIFO(buf.st_mode)) return kPipe; | 239 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 239 if (S_ISSOCK(buf.st_mode)) return kSocket; | 240 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 240 if (S_ISREG(buf.st_mode)) return kFile; | 241 if (S_ISREG(buf.st_mode)) return kFile; |
| 241 return kOther; | 242 return kOther; |
| 242 } | 243 } |
| OLD | NEW |