| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // Report errors for non-regular files. | 109 // Report errors for non-regular files. |
| 110 struct stat st; | 110 struct stat st; |
| 111 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | 111 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 112 if (!S_ISREG(st.st_mode)) { | 112 if (!S_ISREG(st.st_mode)) { |
| 113 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | 113 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| 114 return NULL | 114 return NULL; |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 int flags = O_RDONLY; | 117 int flags = O_RDONLY; |
| 118 if ((mode & kWrite) != 0) { | 118 if ((mode & kWrite) != 0) { |
| 119 flags = (O_RDWR | O_CREAT); | 119 flags = (O_RDWR | O_CREAT); |
| 120 } | 120 } |
| 121 if ((mode & kTruncate) != 0) { | 121 if ((mode & kTruncate) != 0) { |
| 122 flags = flags | O_TRUNC; | 122 flags = flags | O_TRUNC; |
| 123 } | 123 } |
| 124 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 124 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 int result = fstat(fd, &buf); | 227 int result = fstat(fd, &buf); |
| 228 if (result == -1) { | 228 if (result == -1) { |
| 229 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 229 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 230 } | 230 } |
| 231 if (S_ISCHR(buf.st_mode)) return kTerminal; | 231 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 232 if (S_ISFIFO(buf.st_mode)) return kPipe; | 232 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 233 if (S_ISSOCK(buf.st_mode)) return kSocket; | 233 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 234 if (S_ISREG(buf.st_mode)) return kFile; | 234 if (S_ISREG(buf.st_mode)) return kFile; |
| 235 return kOther; | 235 return kOther; |
| 236 } | 236 } |
| OLD | NEW |