| 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 return NULL; | 108 return NULL; |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 int flags = O_RDONLY; | 111 int flags = O_RDONLY; |
| 112 if ((mode & kWrite) != 0) { | 112 if ((mode & kWrite) != 0) { |
| 113 flags = (O_RDWR | O_CREAT); | 113 flags = (O_RDWR | O_CREAT); |
| 114 } | 114 } |
| 115 if ((mode & kTruncate) != 0) { | 115 if ((mode & kTruncate) != 0) { |
| 116 flags = flags | O_TRUNC; | 116 flags = flags | O_TRUNC; |
| 117 } | 117 } |
| 118 flags |= O_CLOEXEC; |
| 118 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 119 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 119 if (fd < 0) { | 120 if (fd < 0) { |
| 120 return NULL; | 121 return NULL; |
| 121 } | 122 } |
| 122 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { | 123 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { |
| 123 int position = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_END)); | 124 int position = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_END)); |
| 124 if (position < 0) { | 125 if (position < 0) { |
| 125 return NULL; | 126 return NULL; |
| 126 } | 127 } |
| 127 } | 128 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 139 struct stat st; | 140 struct stat st; |
| 140 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | 141 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 141 return S_ISREG(st.st_mode); | 142 return S_ISREG(st.st_mode); |
| 142 } else { | 143 } else { |
| 143 return false; | 144 return false; |
| 144 } | 145 } |
| 145 } | 146 } |
| 146 | 147 |
| 147 | 148 |
| 148 bool File::Create(const char* name) { | 149 bool File::Create(const char* name) { |
| 149 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); | 150 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); |
| 150 if (fd < 0) { | 151 if (fd < 0) { |
| 151 return false; | 152 return false; |
| 152 } | 153 } |
| 153 return (close(fd) == 0); | 154 return (close(fd) == 0); |
| 154 } | 155 } |
| 155 | 156 |
| 156 | 157 |
| 157 bool File::Delete(const char* name) { | 158 bool File::Delete(const char* name) { |
| 158 int status = TEMP_FAILURE_RETRY(remove(name)); | 159 int status = TEMP_FAILURE_RETRY(remove(name)); |
| 159 if (status == -1) { | 160 if (status == -1) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 int result = fstat(fd, &buf); | 234 int result = fstat(fd, &buf); |
| 234 if (result == -1) { | 235 if (result == -1) { |
| 235 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 236 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 236 } | 237 } |
| 237 if (S_ISCHR(buf.st_mode)) return kTerminal; | 238 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 238 if (S_ISFIFO(buf.st_mode)) return kPipe; | 239 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 239 if (S_ISSOCK(buf.st_mode)) return kSocket; | 240 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 240 if (S_ISREG(buf.st_mode)) return kFile; | 241 if (S_ISREG(buf.st_mode)) return kFile; |
| 241 return kOther; | 242 return kOther; |
| 242 } | 243 } |
| OLD | NEW |