| OLD | NEW |
| 1 // Copyright (c) 2011, 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 <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/builtin.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 31 // Close the file (unless it's a standard stream). | 31 // Close the file (unless it's a standard stream). |
| 32 if (handle_->fd() > STDERR_FILENO) { | 32 if (handle_->fd() > STDERR_FILENO) { |
| 33 Close(); | 33 Close(); |
| 34 } | 34 } |
| 35 delete handle_; | 35 delete handle_; |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 void File::Close() { | 39 void File::Close() { |
| 40 ASSERT(handle_->fd() >= 0); | 40 ASSERT(handle_->fd() >= 0); |
| 41 int err = close(handle_->fd()); | 41 int err = TEMP_FAILURE_RETRY(close(handle_->fd())); |
| 42 if (err != 0) { | 42 if (err != 0) { |
| 43 const int kBufferSize = 1024; | 43 const int kBufferSize = 1024; |
| 44 char error_message[kBufferSize]; | 44 char error_message[kBufferSize]; |
| 45 strerror_r(errno, error_message, kBufferSize); | 45 strerror_r(errno, error_message, kBufferSize); |
| 46 fprintf(stderr, "%s\n", error_message); | 46 fprintf(stderr, "%s\n", error_message); |
| 47 } | 47 } |
| 48 handle_->set_fd(kClosedFd); | 48 handle_->set_fd(kClosedFd); |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 bool File::IsClosed() { | 52 bool File::IsClosed() { |
| 53 return handle_->fd() == kClosedFd; | 53 return handle_->fd() == kClosedFd; |
| 54 } | 54 } |
| 55 | 55 |
| 56 | 56 |
| 57 int64_t File::Read(void* buffer, int64_t num_bytes) { | 57 int64_t File::Read(void* buffer, int64_t num_bytes) { |
| 58 ASSERT(handle_->fd() >= 0); | 58 ASSERT(handle_->fd() >= 0); |
| 59 return read(handle_->fd(), buffer, num_bytes); | 59 return TEMP_FAILURE_RETRY(read(handle_->fd(), buffer, num_bytes)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 | 62 |
| 63 int64_t File::Write(const void* buffer, int64_t num_bytes) { | 63 int64_t File::Write(const void* buffer, int64_t num_bytes) { |
| 64 ASSERT(handle_->fd() >= 0); | 64 ASSERT(handle_->fd() >= 0); |
| 65 return write(handle_->fd(), buffer, num_bytes); | 65 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes)); |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 off_t File::Position() { | 69 off_t File::Position() { |
| 70 ASSERT(handle_->fd() >= 0); | 70 ASSERT(handle_->fd() >= 0); |
| 71 return lseek(handle_->fd(), 0, SEEK_CUR); | 71 return TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_CUR)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 | 74 |
| 75 bool File::SetPosition(int64_t position) { | 75 bool File::SetPosition(int64_t position) { |
| 76 ASSERT(handle_->fd() >= 0); | 76 ASSERT(handle_->fd() >= 0); |
| 77 return (lseek(handle_->fd(), position, SEEK_SET) != -1); | 77 return TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET) != -1); |
| 78 } | 78 } |
| 79 | 79 |
| 80 | 80 |
| 81 bool File::Truncate(int64_t length) { | 81 bool File::Truncate(int64_t length) { |
| 82 ASSERT(handle_->fd() >= 0); | 82 ASSERT(handle_->fd() >= 0); |
| 83 return (ftruncate(handle_->fd(), length) != -1); | 83 return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length) != -1); |
| 84 } | 84 } |
| 85 | 85 |
| 86 | 86 |
| 87 void File::Flush() { | 87 void File::Flush() { |
| 88 ASSERT(handle_->fd() >= 0); | 88 ASSERT(handle_->fd() >= 0); |
| 89 fsync(handle_->fd()); | 89 TEMP_FAILURE_RETRY(fsync(handle_->fd())); |
| 90 } | 90 } |
| 91 | 91 |
| 92 | 92 |
| 93 off_t File::Length() { | 93 off_t File::Length() { |
| 94 ASSERT(handle_->fd() >= 0); | 94 ASSERT(handle_->fd() >= 0); |
| 95 off_t position = lseek(handle_->fd(), 0, SEEK_CUR); | 95 off_t position = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_CUR)); |
| 96 if (position < 0) { | 96 if (position < 0) { |
| 97 // The file is not capable of seeking. Return an error. | 97 // The file is not capable of seeking. Return an error. |
| 98 return -1; | 98 return -1; |
| 99 } | 99 } |
| 100 off_t result = lseek(handle_->fd(), 0, SEEK_END); | 100 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); |
| 101 lseek(handle_->fd(), position, SEEK_SET); | 101 TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET)); |
| 102 return result; | 102 return result; |
| 103 } | 103 } |
| 104 | 104 |
| 105 | 105 |
| 106 File* File::Open(const char* name, FileOpenMode mode) { | 106 File* File::Open(const char* name, FileOpenMode mode) { |
| 107 int flags = O_RDONLY; | 107 int flags = O_RDONLY; |
| 108 if ((mode & kWrite) != 0) { | 108 if ((mode & kWrite) != 0) { |
| 109 flags = (O_RDWR | O_CREAT); | 109 flags = (O_RDWR | O_CREAT); |
| 110 } | 110 } |
| 111 if ((mode & kTruncate) != 0) { | 111 if ((mode & kTruncate) != 0) { |
| 112 flags = flags | O_TRUNC; | 112 flags = flags | O_TRUNC; |
| 113 } | 113 } |
| 114 int fd = open(name, flags, 0666); | 114 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 115 if (fd < 0) { | 115 if (fd < 0) { |
| 116 return NULL; | 116 return NULL; |
| 117 } | 117 } |
| 118 return new File(name, new FileHandle(fd)); | 118 return new File(name, new FileHandle(fd)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 | 121 |
| 122 bool File::Exists(const char* name) { | 122 bool File::Exists(const char* name) { |
| 123 struct stat st; | 123 struct stat st; |
| 124 if (stat(name, &st) == 0) { | 124 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 125 return S_ISREG(st.st_mode); // Deal with symlinks? | 125 return S_ISREG(st.st_mode); // Deal with symlinks? |
| 126 } else { | 126 } else { |
| 127 return false; | 127 return false; |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 | 131 |
| 132 bool File::Create(const char* name) { | 132 bool File::Create(const char* name) { |
| 133 int fd = open(name, O_RDONLY | O_CREAT, 0666); | 133 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); |
| 134 if (fd < 0) { | 134 if (fd < 0) { |
| 135 return false; | 135 return false; |
| 136 } | 136 } |
| 137 return (close(fd) == 0); | 137 return (close(fd) == 0); |
| 138 } | 138 } |
| 139 | 139 |
| 140 | 140 |
| 141 bool File::Delete(const char* name) { | 141 bool File::Delete(const char* name) { |
| 142 int status = remove(name); | 142 int status = TEMP_FAILURE_RETRY(remove(name)); |
| 143 if (status == -1) { | 143 if (status == -1) { |
| 144 return false; | 144 return false; |
| 145 } | 145 } |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 | 148 |
| 149 | 149 |
| 150 bool File::IsAbsolutePath(const char* pathname) { | 150 bool File::IsAbsolutePath(const char* pathname) { |
| 151 return (pathname != NULL && pathname[0] == '/'); | 151 return (pathname != NULL && pathname[0] == '/'); |
| 152 } | 152 } |
| 153 | 153 |
| 154 | 154 |
| 155 char* File::GetCanonicalPath(const char* pathname) { | 155 char* File::GetCanonicalPath(const char* pathname) { |
| 156 char* abs_path = NULL; | 156 char* abs_path = NULL; |
| 157 if (pathname != NULL) { | 157 if (pathname != NULL) { |
| 158 abs_path = realpath(pathname, NULL); | 158 do { |
| 159 abs_path = realpath(pathname, NULL); |
| 160 } while (abs_path == NULL && errno == EINTR); |
| 159 assert(abs_path == NULL || IsAbsolutePath(abs_path)); | 161 assert(abs_path == NULL || IsAbsolutePath(abs_path)); |
| 160 } | 162 } |
| 161 return abs_path; | 163 return abs_path; |
| 162 } | 164 } |
| 163 | 165 |
| 164 | 166 |
| 165 const char* File::PathSeparator() { | 167 const char* File::PathSeparator() { |
| 166 return "/"; | 168 return "/"; |
| 167 } | 169 } |
| 168 | 170 |
| 169 | 171 |
| 170 const char* File::StringEscapedPathSeparator() { | 172 const char* File::StringEscapedPathSeparator() { |
| 171 return "/"; | 173 return "/"; |
| 172 } | 174 } |
| OLD | NEW |