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 struct stat st; | |
109 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { | |
Mads Ager (google)
2012/03/13 10:56:17
Why do you need this? Isn't the error code from op
Søren Gjesse
2012/03/13 12:39:49
This is to avoid opening non-regular files. Before
| |
110 if (!S_ISREG(st.st_mode)) { | |
111 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | |
112 return false; | |
Mads Ager (google)
2012/03/13 10:56:17
false -> NULL
Søren Gjesse
2012/03/13 12:39:49
Good catch, done.
| |
113 } | |
114 } | |
108 int flags = O_RDONLY; | 115 int flags = O_RDONLY; |
109 if ((mode & kWrite) != 0) { | 116 if ((mode & kWrite) != 0) { |
110 flags = (O_RDWR | O_CREAT); | 117 flags = (O_RDWR | O_CREAT); |
111 } | 118 } |
112 if ((mode & kTruncate) != 0) { | 119 if ((mode & kTruncate) != 0) { |
113 flags = flags | O_TRUNC; | 120 flags = flags | O_TRUNC; |
114 } | 121 } |
115 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); | 122 int fd = TEMP_FAILURE_RETRY(open(name, flags, 0666)); |
116 if (fd < 0) { | 123 if (fd < 0) { |
117 return NULL; | 124 return NULL; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 do { | 178 do { |
172 abs_path = realpath(pathname, NULL); | 179 abs_path = realpath(pathname, NULL); |
173 } while (abs_path == NULL && errno == EINTR); | 180 } while (abs_path == NULL && errno == EINTR); |
174 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); | 181 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); |
175 } | 182 } |
176 return abs_path; | 183 return abs_path; |
177 } | 184 } |
178 | 185 |
179 | 186 |
180 char* File::GetContainingDirectory(char* pathname) { | 187 char* File::GetContainingDirectory(char* pathname) { |
188 struct stat st; | |
189 if (TEMP_FAILURE_RETRY(stat(pathname, &st)) == 0) { | |
Mads Ager (google)
2012/03/13 10:56:17
Similarly here, don't we get a good enough error m
Søren Gjesse
2012/03/13 12:39:49
As for open above we used to have an exists check
| |
190 if (!S_ISREG(st.st_mode)) { | |
191 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | |
192 return NULL; | |
193 } | |
194 } else { | |
195 return NULL; | |
196 } | |
181 char* path = NULL; | 197 char* path = NULL; |
182 do { | 198 do { |
183 path = dirname(pathname); | 199 path = dirname(pathname); |
184 } while (path == NULL && errno == EINTR); | 200 } while (path == NULL && errno == EINTR); |
185 return GetCanonicalPath(path); | 201 return GetCanonicalPath(path); |
186 } | 202 } |
187 | 203 |
188 | 204 |
189 const char* File::PathSeparator() { | 205 const char* File::PathSeparator() { |
190 return "/"; | 206 return "/"; |
(...skipping 11 matching lines...) Expand all Loading... | |
202 int result = fstat(fd, &buf); | 218 int result = fstat(fd, &buf); |
203 if (result == -1) { | 219 if (result == -1) { |
204 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); | 220 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); |
205 } | 221 } |
206 if (S_ISCHR(buf.st_mode)) return kTerminal; | 222 if (S_ISCHR(buf.st_mode)) return kTerminal; |
207 if (S_ISFIFO(buf.st_mode)) return kPipe; | 223 if (S_ISFIFO(buf.st_mode)) return kPipe; |
208 if (S_ISSOCK(buf.st_mode)) return kSocket; | 224 if (S_ISSOCK(buf.st_mode)) return kSocket; |
209 if (S_ISREG(buf.st_mode)) return kFile; | 225 if (S_ISREG(buf.st_mode)) return kFile; |
210 return kOther; | 226 return kOther; |
211 } | 227 } |
OLD | NEW |