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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
(...skipping 20 matching lines...) Expand all Loading... |
31 void set_fd(int fd) { fd_ = fd; } | 31 void set_fd(int fd) { fd_ = fd; } |
32 | 32 |
33 private: | 33 private: |
34 int fd_; | 34 int fd_; |
35 | 35 |
36 DISALLOW_COPY_AND_ASSIGN(FileHandle); | 36 DISALLOW_COPY_AND_ASSIGN(FileHandle); |
37 }; | 37 }; |
38 | 38 |
39 | 39 |
40 File::~File() { | 40 File::~File() { |
41 // Close the file (unless it's a standard stream). | 41 Close(); |
42 if (handle_->fd() > STDERR_FILENO) { | |
43 Close(); | |
44 } | |
45 delete handle_; | 42 delete handle_; |
46 } | 43 } |
47 | 44 |
48 | 45 |
49 void File::Close() { | 46 void File::Close() { |
50 ASSERT(handle_->fd() >= 0); | 47 ASSERT(handle_->fd() >= 0); |
51 int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(handle_->fd())); | 48 if (handle_->fd() == STDOUT_FILENO) { |
52 if (err != 0) { | 49 // If stdout, redirect fd to /dev/null. |
53 const int kBufferSize = 1024; | 50 int null_fd = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY)); |
54 char error_message[kBufferSize]; | 51 ASSERT(null_fd >= 0); |
55 strerror_r(errno, error_message, kBufferSize); | 52 VOID_TEMP_FAILURE_RETRY(dup2(null_fd, handle_->fd())); |
56 Log::PrintErr("%s\n", error_message); | 53 VOID_TEMP_FAILURE_RETRY(close(null_fd)); |
| 54 } else { |
| 55 int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(handle_->fd())); |
| 56 if (err != 0) { |
| 57 const int kBufferSize = 1024; |
| 58 char error_message[kBufferSize]; |
| 59 strerror_r(errno, error_message, kBufferSize); |
| 60 Log::PrintErr("%s\n", error_message); |
| 61 } |
57 } | 62 } |
58 handle_->set_fd(kClosedFd); | 63 handle_->set_fd(kClosedFd); |
59 } | 64 } |
60 | 65 |
61 | 66 |
62 bool File::IsClosed() { | 67 bool File::IsClosed() { |
63 return handle_->fd() == kClosedFd; | 68 return handle_->fd() == kClosedFd; |
64 } | 69 } |
65 | 70 |
66 | 71 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 return (file_1_info.st_ino == file_2_info.st_ino && | 383 return (file_1_info.st_ino == file_2_info.st_ino && |
379 file_1_info.st_dev == file_2_info.st_dev) ? | 384 file_1_info.st_dev == file_2_info.st_dev) ? |
380 File::kIdentical : | 385 File::kIdentical : |
381 File::kDifferent; | 386 File::kDifferent; |
382 } | 387 } |
383 | 388 |
384 } // namespace bin | 389 } // namespace bin |
385 } // namespace dart | 390 } // namespace dart |
386 | 391 |
387 #endif // defined(TARGET_OS_MACOS) | 392 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |