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