| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // The file is not capable of seeking. Return an error. | 99 // The file is not capable of seeking. Return an error. |
| 100 return -1; | 100 return -1; |
| 101 } | 101 } |
| 102 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); | 102 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); |
| 103 TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET)); | 103 TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET)); |
| 104 return result; | 104 return result; |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 File* File::Open(const char* name, FileOpenMode mode) { | 108 File* File::Open(const char* name, FileOpenMode mode) { |
| 109 struct stat st; |
| 110 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 111 if (!S_ISREG(st.st_mode)) { |
| 112 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 113 return false; |
| 114 } |
| 115 } |
| 109 int flags = O_RDONLY; | 116 int flags = O_RDONLY; |
| 110 if ((mode & kWrite) != 0) { | 117 if ((mode & kWrite) != 0) { |
| 111 flags = (O_RDWR | O_CREAT); | 118 flags = (O_RDWR | O_CREAT); |
| 112 } | 119 } |
| 113 if ((mode & kTruncate) != 0) { | 120 if ((mode & kTruncate) != 0) { |
| 114 flags = flags | O_TRUNC; | 121 flags = flags | O_TRUNC; |
| 115 } | 122 } |
| 116 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 123 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 117 if (fd < 0) { | 124 if (fd < 0) { |
| 118 return NULL; | 125 return NULL; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 do { | 185 do { |
| 179 abs_path = realpath(pathname, NULL); | 186 abs_path = realpath(pathname, NULL); |
| 180 } while (abs_path == NULL && errno == EINTR); | 187 } while (abs_path == NULL && errno == EINTR); |
| 181 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); | 188 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); |
| 182 } | 189 } |
| 183 return abs_path; | 190 return abs_path; |
| 184 } | 191 } |
| 185 | 192 |
| 186 | 193 |
| 187 char* File::GetContainingDirectory(char* pathname) { | 194 char* File::GetContainingDirectory(char* pathname) { |
| 195 struct stat st; |
| 196 if (TEMP_FAILURE_RETRY(stat(pathname, &st)) == 0) { |
| 197 if (!S_ISREG(st.st_mode)) { |
| 198 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 199 return NULL; |
| 200 } |
| 201 } else { |
| 202 return NULL; |
| 203 } |
| 188 char* path = NULL; | 204 char* path = NULL; |
| 189 do { | 205 do { |
| 190 path = dirname(pathname); | 206 path = dirname(pathname); |
| 191 } while (path == NULL && errno == EINTR); | 207 } while (path == NULL && errno == EINTR); |
| 192 return GetCanonicalPath(path); | 208 return GetCanonicalPath(path); |
| 193 } | 209 } |
| 194 | 210 |
| 195 | 211 |
| 196 const char* File::PathSeparator() { | 212 const char* File::PathSeparator() { |
| 197 return "/"; | 213 return "/"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 209 int result = fstat(fd, &buf); | 225 int result = fstat(fd, &buf); |
| 210 if (result == -1) { | 226 if (result == -1) { |
| 211 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 227 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 212 } | 228 } |
| 213 if (S_ISCHR(buf.st_mode)) return kTerminal; | 229 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 214 if (S_ISFIFO(buf.st_mode)) return kPipe; | 230 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 215 if (S_ISSOCK(buf.st_mode)) return kSocket; | 231 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 216 if (S_ISREG(buf.st_mode)) return kFile; | 232 if (S_ISREG(buf.st_mode)) return kFile; |
| 217 return kOther; | 233 return kOther; |
| 218 } | 234 } |
| OLD | NEW |