| 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> |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 | 88 |
| 89 bool File::Flush() { | 89 bool File::Flush() { |
| 90 ASSERT(handle_->fd() >= 0); | 90 ASSERT(handle_->fd() >= 0); |
| 91 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1); | 91 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1); |
| 92 } | 92 } |
| 93 | 93 |
| 94 | 94 |
| 95 off_t File::Length() { | 95 off_t File::Length() { |
| 96 ASSERT(handle_->fd() >= 0); | 96 ASSERT(handle_->fd() >= 0); |
| 97 off_t position = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_CUR)); | 97 struct stat st; |
| 98 if (position < 0) { | 98 if (TEMP_FAILURE_RETRY(fstat(handle_->fd(), &st)) == 0) { |
| 99 // The file is not capable of seeking. Return an error. | 99 return st.st_size; |
| 100 return -1; | |
| 101 } | 100 } |
| 102 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); | 101 return -1; |
| 103 TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET)); | |
| 104 return result; | |
| 105 } | 102 } |
| 106 | 103 |
| 107 | 104 |
| 108 File* File::Open(const char* name, FileOpenMode mode) { | 105 File* File::Open(const char* name, FileOpenMode mode) { |
| 109 // Report errors for non-regular files. | 106 // Report errors for non-regular files. |
| 110 struct stat st; | 107 struct stat st; |
| 111 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | 108 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 112 if (!S_ISREG(st.st_mode)) { | 109 if (!S_ISREG(st.st_mode)) { |
| 113 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | 110 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 114 return NULL; | 111 return NULL; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 159 |
| 163 bool File::Delete(const char* name) { | 160 bool File::Delete(const char* name) { |
| 164 int status = TEMP_FAILURE_RETRY(remove(name)); | 161 int status = TEMP_FAILURE_RETRY(remove(name)); |
| 165 if (status == -1) { | 162 if (status == -1) { |
| 166 return false; | 163 return false; |
| 167 } | 164 } |
| 168 return true; | 165 return true; |
| 169 } | 166 } |
| 170 | 167 |
| 171 | 168 |
| 169 off_t File::LengthFromName(const char* name) { |
| 170 struct stat st; |
| 171 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 172 return st.st_size; |
| 173 } |
| 174 return -1; |
| 175 } |
| 176 |
| 177 |
| 172 bool File::IsAbsolutePath(const char* pathname) { | 178 bool File::IsAbsolutePath(const char* pathname) { |
| 173 return (pathname != NULL && pathname[0] == '/'); | 179 return (pathname != NULL && pathname[0] == '/'); |
| 174 } | 180 } |
| 175 | 181 |
| 176 | 182 |
| 177 char* File::GetCanonicalPath(const char* pathname) { | 183 char* File::GetCanonicalPath(const char* pathname) { |
| 178 char* abs_path = NULL; | 184 char* abs_path = NULL; |
| 179 if (pathname != NULL) { | 185 if (pathname != NULL) { |
| 180 // On some older MacOs versions the default behaviour of realpath allocating | 186 // On some older MacOs versions the default behaviour of realpath allocating |
| 181 // space for the resolved_path when a NULL is passed in does not seem to | 187 // space for the resolved_path when a NULL is passed in does not seem to |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 int result = fstat(fd, &buf); | 233 int result = fstat(fd, &buf); |
| 228 if (result == -1) { | 234 if (result == -1) { |
| 229 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 235 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 230 } | 236 } |
| 231 if (S_ISCHR(buf.st_mode)) return kTerminal; | 237 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 232 if (S_ISFIFO(buf.st_mode)) return kPipe; | 238 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 233 if (S_ISSOCK(buf.st_mode)) return kSocket; | 239 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 234 if (S_ISREG(buf.st_mode)) return kFile; | 240 if (S_ISREG(buf.st_mode)) return kFile; |
| 235 return kOther; | 241 return kOther; |
| 236 } | 242 } |
| OLD | NEW |