| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); | 101 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); |
| 102 TEMP_FAILURE_RETRY(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 // Report errors for non-regular files. |
| 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 NULL; |
| 114 } |
| 115 } |
| 108 int flags = O_RDONLY; | 116 int flags = O_RDONLY; |
| 109 if ((mode & kWrite) != 0) { | 117 if ((mode & kWrite) != 0) { |
| 110 flags = (O_RDWR | O_CREAT); | 118 flags = (O_RDWR | O_CREAT); |
| 111 } | 119 } |
| 112 if ((mode & kTruncate) != 0) { | 120 if ((mode & kTruncate) != 0) { |
| 113 flags = flags | O_TRUNC; | 121 flags = flags | O_TRUNC; |
| 114 } | 122 } |
| 115 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 123 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 116 if (fd < 0) { | 124 if (fd < 0) { |
| 117 return NULL; | 125 return NULL; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 do { | 179 do { |
| 172 abs_path = realpath(pathname, NULL); | 180 abs_path = realpath(pathname, NULL); |
| 173 } while (abs_path == NULL && errno == EINTR); | 181 } while (abs_path == NULL && errno == EINTR); |
| 174 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); | 182 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); |
| 175 } | 183 } |
| 176 return abs_path; | 184 return abs_path; |
| 177 } | 185 } |
| 178 | 186 |
| 179 | 187 |
| 180 char* File::GetContainingDirectory(char* pathname) { | 188 char* File::GetContainingDirectory(char* pathname) { |
| 189 // Report errors for non-regular files. |
| 190 struct stat st; |
| 191 if (TEMP_FAILURE_RETRY(stat(pathname, &st)) == 0) { |
| 192 if (!S_ISREG(st.st_mode)) { |
| 193 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 194 return NULL; |
| 195 } |
| 196 } else { |
| 197 return NULL; |
| 198 } |
| 181 char* path = NULL; | 199 char* path = NULL; |
| 182 do { | 200 do { |
| 183 path = dirname(pathname); | 201 path = dirname(pathname); |
| 184 } while (path == NULL && errno == EINTR); | 202 } while (path == NULL && errno == EINTR); |
| 185 return GetCanonicalPath(path); | 203 return GetCanonicalPath(path); |
| 186 } | 204 } |
| 187 | 205 |
| 188 | 206 |
| 189 const char* File::PathSeparator() { | 207 const char* File::PathSeparator() { |
| 190 return "/"; | 208 return "/"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 202 int result = fstat(fd, &buf); | 220 int result = fstat(fd, &buf); |
| 203 if (result == -1) { | 221 if (result == -1) { |
| 204 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 222 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 205 } | 223 } |
| 206 if (S_ISCHR(buf.st_mode)) return kTerminal; | 224 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 207 if (S_ISFIFO(buf.st_mode)) return kPipe; | 225 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 208 if (S_ISSOCK(buf.st_mode)) return kSocket; | 226 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 209 if (S_ISREG(buf.st_mode)) return kFile; | 227 if (S_ISREG(buf.st_mode)) return kFile; |
| 210 return kOther; | 228 return kOther; |
| 211 } | 229 } |
| OLD | NEW |