| 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> |
| 11 #include <libgen.h> | 11 #include <libgen.h> |
| 12 #include <limits.h> | 12 #include <limits.h> |
| 13 | 13 |
| 14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
| 15 #include "bin/fdutils.h" |
| 15 | 16 |
| 16 class FileHandle { | 17 class FileHandle { |
| 17 public: | 18 public: |
| 18 explicit FileHandle(int fd) : fd_(fd) { } | 19 explicit FileHandle(int fd) : fd_(fd) { } |
| 19 ~FileHandle() { } | 20 ~FileHandle() { } |
| 20 int fd() const { return fd_; } | 21 int fd() const { return fd_; } |
| 21 void set_fd(int fd) { fd_ = fd; } | 22 void set_fd(int fd) { fd_ = fd; } |
| 22 | 23 |
| 23 private: | 24 private: |
| 24 int fd_; | 25 int fd_; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 return NULL; | 110 return NULL; |
| 110 } | 111 } |
| 111 } | 112 } |
| 112 int flags = O_RDONLY; | 113 int flags = O_RDONLY; |
| 113 if ((mode & kWrite) != 0) { | 114 if ((mode & kWrite) != 0) { |
| 114 flags = (O_RDWR | O_CREAT); | 115 flags = (O_RDWR | O_CREAT); |
| 115 } | 116 } |
| 116 if ((mode & kTruncate) != 0) { | 117 if ((mode & kTruncate) != 0) { |
| 117 flags = flags | O_TRUNC; | 118 flags = flags | O_TRUNC; |
| 118 } | 119 } |
| 119 flags |= O_CLOEXEC; | |
| 120 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 120 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
| 121 if (fd < 0) { | 121 if (fd < 0) { |
| 122 return NULL; | 122 return NULL; |
| 123 } | 123 } |
| 124 FDUtils::SetCloseOnExec(fd); |
| 124 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { | 125 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { |
| 125 int position = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_END)); | 126 int position = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_END)); |
| 126 if (position < 0) { | 127 if (position < 0) { |
| 127 return NULL; | 128 return NULL; |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 return new File(new FileHandle(fd)); | 131 return new File(new FileHandle(fd)); |
| 131 } | 132 } |
| 132 | 133 |
| 133 | 134 |
| 134 File* File::OpenStdio(int fd) { | 135 File* File::OpenStdio(int fd) { |
| 135 if (fd < 0 || 2 < fd) return NULL; | 136 if (fd < 0 || 2 < fd) return NULL; |
| 136 return new File(new FileHandle(fd)); | 137 return new File(new FileHandle(fd)); |
| 137 } | 138 } |
| 138 | 139 |
| 139 | 140 |
| 140 bool File::Exists(const char* name) { | 141 bool File::Exists(const char* name) { |
| 141 struct stat st; | 142 struct stat st; |
| 142 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | 143 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { |
| 143 return S_ISREG(st.st_mode); | 144 return S_ISREG(st.st_mode); |
| 144 } else { | 145 } else { |
| 145 return false; | 146 return false; |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 | 150 |
| 150 bool File::Create(const char* name) { | 151 bool File::Create(const char* name) { |
| 151 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); | 152 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); |
| 152 if (fd < 0) { | 153 if (fd < 0) { |
| 153 return false; | 154 return false; |
| 154 } | 155 } |
| 155 return (close(fd) == 0); | 156 return (close(fd) == 0); |
| 156 } | 157 } |
| 157 | 158 |
| 158 | 159 |
| 159 bool File::Delete(const char* name) { | 160 bool File::Delete(const char* name) { |
| 160 int status = TEMP_FAILURE_RETRY(remove(name)); | 161 int status = TEMP_FAILURE_RETRY(remove(name)); |
| 161 if (status == -1) { | 162 if (status == -1) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 int result = fstat(fd, &buf); | 242 int result = fstat(fd, &buf); |
| 242 if (result == -1) { | 243 if (result == -1) { |
| 243 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 244 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
| 244 } | 245 } |
| 245 if (S_ISCHR(buf.st_mode)) return kTerminal; | 246 if (S_ISCHR(buf.st_mode)) return kTerminal; |
| 246 if (S_ISFIFO(buf.st_mode)) return kPipe; | 247 if (S_ISFIFO(buf.st_mode)) return kPipe; |
| 247 if (S_ISSOCK(buf.st_mode)) return kSocket; | 248 if (S_ISSOCK(buf.st_mode)) return kSocket; |
| 248 if (S_ISREG(buf.st_mode)) return kFile; | 249 if (S_ISREG(buf.st_mode)) return kFile; |
| 249 return kOther; | 250 return kOther; |
| 250 } | 251 } |
| OLD | NEW |