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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(HOST_OS_MACOS) | 6 #if defined(HOST_OS_MACOS) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include <copyfile.h> // NOLINT | 10 #include <copyfile.h> // NOLINT |
(...skipping 22 matching lines...) Expand all Loading... |
33 ~FileHandle() {} | 33 ~FileHandle() {} |
34 int fd() const { return fd_; } | 34 int fd() const { return fd_; } |
35 void set_fd(int fd) { fd_ = fd; } | 35 void set_fd(int fd) { fd_ = fd; } |
36 | 36 |
37 private: | 37 private: |
38 int fd_; | 38 int fd_; |
39 | 39 |
40 DISALLOW_COPY_AND_ASSIGN(FileHandle); | 40 DISALLOW_COPY_AND_ASSIGN(FileHandle); |
41 }; | 41 }; |
42 | 42 |
43 | |
44 File::~File() { | 43 File::~File() { |
45 if (!IsClosed() && handle_->fd() != STDOUT_FILENO && | 44 if (!IsClosed() && handle_->fd() != STDOUT_FILENO && |
46 handle_->fd() != STDERR_FILENO) { | 45 handle_->fd() != STDERR_FILENO) { |
47 Close(); | 46 Close(); |
48 } | 47 } |
49 delete handle_; | 48 delete handle_; |
50 } | 49 } |
51 | 50 |
52 | |
53 void File::Close() { | 51 void File::Close() { |
54 ASSERT(handle_->fd() >= 0); | 52 ASSERT(handle_->fd() >= 0); |
55 if (handle_->fd() == STDOUT_FILENO) { | 53 if (handle_->fd() == STDOUT_FILENO) { |
56 // If stdout, redirect fd to /dev/null. | 54 // If stdout, redirect fd to /dev/null. |
57 intptr_t null_fd = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY)); | 55 intptr_t null_fd = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY)); |
58 ASSERT(null_fd >= 0); | 56 ASSERT(null_fd >= 0); |
59 VOID_TEMP_FAILURE_RETRY(dup2(null_fd, handle_->fd())); | 57 VOID_TEMP_FAILURE_RETRY(dup2(null_fd, handle_->fd())); |
60 VOID_TEMP_FAILURE_RETRY(close(null_fd)); | 58 VOID_TEMP_FAILURE_RETRY(close(null_fd)); |
61 } else { | 59 } else { |
62 intptr_t err = TEMP_FAILURE_RETRY(close(handle_->fd())); | 60 intptr_t err = TEMP_FAILURE_RETRY(close(handle_->fd())); |
63 if (err != 0) { | 61 if (err != 0) { |
64 const int kBufferSize = 1024; | 62 const int kBufferSize = 1024; |
65 char error_message[kBufferSize]; | 63 char error_message[kBufferSize]; |
66 Utils::StrError(errno, error_message, kBufferSize); | 64 Utils::StrError(errno, error_message, kBufferSize); |
67 Log::PrintErr("%s\n", error_message); | 65 Log::PrintErr("%s\n", error_message); |
68 } | 66 } |
69 } | 67 } |
70 handle_->set_fd(kClosedFd); | 68 handle_->set_fd(kClosedFd); |
71 } | 69 } |
72 | 70 |
73 | |
74 intptr_t File::GetFD() { | 71 intptr_t File::GetFD() { |
75 return handle_->fd(); | 72 return handle_->fd(); |
76 } | 73 } |
77 | 74 |
78 | |
79 bool File::IsClosed() { | 75 bool File::IsClosed() { |
80 return handle_->fd() == kClosedFd; | 76 return handle_->fd() == kClosedFd; |
81 } | 77 } |
82 | 78 |
83 | |
84 MappedMemory* File::Map(MapType type, int64_t position, int64_t length) { | 79 MappedMemory* File::Map(MapType type, int64_t position, int64_t length) { |
85 ASSERT(handle_->fd() >= 0); | 80 ASSERT(handle_->fd() >= 0); |
86 ASSERT(length > 0); | 81 ASSERT(length > 0); |
87 int prot = PROT_NONE; | 82 int prot = PROT_NONE; |
88 switch (type) { | 83 switch (type) { |
89 case kReadOnly: | 84 case kReadOnly: |
90 prot = PROT_READ; | 85 prot = PROT_READ; |
91 break; | 86 break; |
92 case kReadExecute: | 87 case kReadExecute: |
93 prot = PROT_READ | PROT_EXEC; | 88 prot = PROT_READ | PROT_EXEC; |
94 break; | 89 break; |
95 default: | 90 default: |
96 return NULL; | 91 return NULL; |
97 } | 92 } |
98 void* addr = mmap(NULL, length, prot, MAP_PRIVATE, handle_->fd(), position); | 93 void* addr = mmap(NULL, length, prot, MAP_PRIVATE, handle_->fd(), position); |
99 if (addr == MAP_FAILED) { | 94 if (addr == MAP_FAILED) { |
100 return NULL; | 95 return NULL; |
101 } | 96 } |
102 return new MappedMemory(addr, length); | 97 return new MappedMemory(addr, length); |
103 } | 98 } |
104 | 99 |
105 | |
106 void MappedMemory::Unmap() { | 100 void MappedMemory::Unmap() { |
107 int result = munmap(address_, size_); | 101 int result = munmap(address_, size_); |
108 ASSERT(result == 0); | 102 ASSERT(result == 0); |
109 address_ = 0; | 103 address_ = 0; |
110 size_ = 0; | 104 size_ = 0; |
111 } | 105 } |
112 | 106 |
113 | |
114 int64_t File::Read(void* buffer, int64_t num_bytes) { | 107 int64_t File::Read(void* buffer, int64_t num_bytes) { |
115 ASSERT(handle_->fd() >= 0); | 108 ASSERT(handle_->fd() >= 0); |
116 return TEMP_FAILURE_RETRY(read(handle_->fd(), buffer, num_bytes)); | 109 return TEMP_FAILURE_RETRY(read(handle_->fd(), buffer, num_bytes)); |
117 } | 110 } |
118 | 111 |
119 | |
120 int64_t File::Write(const void* buffer, int64_t num_bytes) { | 112 int64_t File::Write(const void* buffer, int64_t num_bytes) { |
121 ASSERT(handle_->fd() >= 0); | 113 ASSERT(handle_->fd() >= 0); |
122 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes)); | 114 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes)); |
123 } | 115 } |
124 | 116 |
125 | |
126 bool File::VPrint(const char* format, va_list args) { | 117 bool File::VPrint(const char* format, va_list args) { |
127 // Measure. | 118 // Measure. |
128 va_list measure_args; | 119 va_list measure_args; |
129 va_copy(measure_args, args); | 120 va_copy(measure_args, args); |
130 intptr_t len = vsnprintf(NULL, 0, format, measure_args); | 121 intptr_t len = vsnprintf(NULL, 0, format, measure_args); |
131 va_end(measure_args); | 122 va_end(measure_args); |
132 | 123 |
133 char* buffer = reinterpret_cast<char*>(malloc(len + 1)); | 124 char* buffer = reinterpret_cast<char*>(malloc(len + 1)); |
134 | 125 |
135 // Print. | 126 // Print. |
136 va_list print_args; | 127 va_list print_args; |
137 va_copy(print_args, args); | 128 va_copy(print_args, args); |
138 vsnprintf(buffer, len + 1, format, print_args); | 129 vsnprintf(buffer, len + 1, format, print_args); |
139 va_end(print_args); | 130 va_end(print_args); |
140 | 131 |
141 bool result = WriteFully(buffer, len); | 132 bool result = WriteFully(buffer, len); |
142 free(buffer); | 133 free(buffer); |
143 return result; | 134 return result; |
144 } | 135 } |
145 | 136 |
146 int64_t File::Position() { | 137 int64_t File::Position() { |
147 ASSERT(handle_->fd() >= 0); | 138 ASSERT(handle_->fd() >= 0); |
148 return lseek(handle_->fd(), 0, SEEK_CUR); | 139 return lseek(handle_->fd(), 0, SEEK_CUR); |
149 } | 140 } |
150 | 141 |
151 | |
152 bool File::SetPosition(int64_t position) { | 142 bool File::SetPosition(int64_t position) { |
153 ASSERT(handle_->fd() >= 0); | 143 ASSERT(handle_->fd() >= 0); |
154 return lseek(handle_->fd(), position, SEEK_SET) >= 0; | 144 return lseek(handle_->fd(), position, SEEK_SET) >= 0; |
155 } | 145 } |
156 | 146 |
157 | |
158 bool File::Truncate(int64_t length) { | 147 bool File::Truncate(int64_t length) { |
159 ASSERT(handle_->fd() >= 0); | 148 ASSERT(handle_->fd() >= 0); |
160 return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length)) != -1; | 149 return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length)) != -1; |
161 } | 150 } |
162 | 151 |
163 | |
164 bool File::Flush() { | 152 bool File::Flush() { |
165 ASSERT(handle_->fd() >= 0); | 153 ASSERT(handle_->fd() >= 0); |
166 return NO_RETRY_EXPECTED(fsync(handle_->fd())) != -1; | 154 return NO_RETRY_EXPECTED(fsync(handle_->fd())) != -1; |
167 } | 155 } |
168 | 156 |
169 | |
170 bool File::Lock(File::LockType lock, int64_t start, int64_t end) { | 157 bool File::Lock(File::LockType lock, int64_t start, int64_t end) { |
171 ASSERT(handle_->fd() >= 0); | 158 ASSERT(handle_->fd() >= 0); |
172 ASSERT((end == -1) || (end > start)); | 159 ASSERT((end == -1) || (end > start)); |
173 struct flock fl; | 160 struct flock fl; |
174 switch (lock) { | 161 switch (lock) { |
175 case File::kLockUnlock: | 162 case File::kLockUnlock: |
176 fl.l_type = F_UNLCK; | 163 fl.l_type = F_UNLCK; |
177 break; | 164 break; |
178 case File::kLockShared: | 165 case File::kLockShared: |
179 case File::kLockBlockingShared: | 166 case File::kLockBlockingShared: |
(...skipping 10 matching lines...) Expand all Loading... |
190 fl.l_start = start; | 177 fl.l_start = start; |
191 fl.l_len = end == -1 ? 0 : end - start; | 178 fl.l_len = end == -1 ? 0 : end - start; |
192 int cmd = F_SETLK; | 179 int cmd = F_SETLK; |
193 if ((lock == File::kLockBlockingShared) || | 180 if ((lock == File::kLockBlockingShared) || |
194 (lock == File::kLockBlockingExclusive)) { | 181 (lock == File::kLockBlockingExclusive)) { |
195 cmd = F_SETLKW; | 182 cmd = F_SETLKW; |
196 } | 183 } |
197 return TEMP_FAILURE_RETRY(fcntl(handle_->fd(), cmd, &fl)) != -1; | 184 return TEMP_FAILURE_RETRY(fcntl(handle_->fd(), cmd, &fl)) != -1; |
198 } | 185 } |
199 | 186 |
200 | |
201 int64_t File::Length() { | 187 int64_t File::Length() { |
202 ASSERT(handle_->fd() >= 0); | 188 ASSERT(handle_->fd() >= 0); |
203 struct stat st; | 189 struct stat st; |
204 if (NO_RETRY_EXPECTED(fstat(handle_->fd(), &st)) == 0) { | 190 if (NO_RETRY_EXPECTED(fstat(handle_->fd(), &st)) == 0) { |
205 return st.st_size; | 191 return st.st_size; |
206 } | 192 } |
207 return -1; | 193 return -1; |
208 } | 194 } |
209 | 195 |
210 | |
211 File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) { | 196 File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) { |
212 UNREACHABLE(); | 197 UNREACHABLE(); |
213 return NULL; | 198 return NULL; |
214 } | 199 } |
215 | 200 |
216 | |
217 File* File::Open(const char* name, FileOpenMode mode) { | 201 File* File::Open(const char* name, FileOpenMode mode) { |
218 // Report errors for non-regular files. | 202 // Report errors for non-regular files. |
219 struct stat st; | 203 struct stat st; |
220 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 204 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
221 // Only accept regular files, character devices, and pipes. | 205 // Only accept regular files, character devices, and pipes. |
222 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) { | 206 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) { |
223 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; | 207 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
224 return NULL; | 208 return NULL; |
225 } | 209 } |
226 } | 210 } |
(...skipping 17 matching lines...) Expand all Loading... |
244 if ((((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) || | 228 if ((((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) || |
245 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) { | 229 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) { |
246 int64_t position = lseek(fd, 0, SEEK_END); | 230 int64_t position = lseek(fd, 0, SEEK_END); |
247 if (position < 0) { | 231 if (position < 0) { |
248 return NULL; | 232 return NULL; |
249 } | 233 } |
250 } | 234 } |
251 return new File(new FileHandle(fd)); | 235 return new File(new FileHandle(fd)); |
252 } | 236 } |
253 | 237 |
254 | |
255 File* File::OpenStdio(int fd) { | 238 File* File::OpenStdio(int fd) { |
256 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); | 239 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); |
257 } | 240 } |
258 | 241 |
259 | |
260 bool File::Exists(const char* name) { | 242 bool File::Exists(const char* name) { |
261 struct stat st; | 243 struct stat st; |
262 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 244 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
263 // Everything but a directory and a link is a file to Dart. | 245 // Everything but a directory and a link is a file to Dart. |
264 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode); | 246 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode); |
265 } else { | 247 } else { |
266 return false; | 248 return false; |
267 } | 249 } |
268 } | 250 } |
269 | 251 |
270 | |
271 bool File::Create(const char* name) { | 252 bool File::Create(const char* name) { |
272 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); | 253 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); |
273 if (fd < 0) { | 254 if (fd < 0) { |
274 return false; | 255 return false; |
275 } | 256 } |
276 // File.create returns a File, so we shouldn't be giving the illusion that the | 257 // File.create returns a File, so we shouldn't be giving the illusion that the |
277 // call has created a file or that a file already exists if there is already | 258 // call has created a file or that a file already exists if there is already |
278 // an entity at the same path that is a directory or a link. | 259 // an entity at the same path that is a directory or a link. |
279 bool is_file = true; | 260 bool is_file = true; |
280 struct stat st; | 261 struct stat st; |
281 if (NO_RETRY_EXPECTED(fstat(fd, &st)) == 0) { | 262 if (NO_RETRY_EXPECTED(fstat(fd, &st)) == 0) { |
282 if (S_ISDIR(st.st_mode)) { | 263 if (S_ISDIR(st.st_mode)) { |
283 errno = EISDIR; | 264 errno = EISDIR; |
284 is_file = false; | 265 is_file = false; |
285 } else if (S_ISLNK(st.st_mode)) { | 266 } else if (S_ISLNK(st.st_mode)) { |
286 errno = ENOENT; | 267 errno = ENOENT; |
287 is_file = false; | 268 is_file = false; |
288 } | 269 } |
289 } | 270 } |
290 FDUtils::SaveErrorAndClose(fd); | 271 FDUtils::SaveErrorAndClose(fd); |
291 return is_file; | 272 return is_file; |
292 } | 273 } |
293 | 274 |
294 | |
295 bool File::CreateLink(const char* name, const char* target) { | 275 bool File::CreateLink(const char* name, const char* target) { |
296 int status = NO_RETRY_EXPECTED(symlink(target, name)); | 276 int status = NO_RETRY_EXPECTED(symlink(target, name)); |
297 return (status == 0); | 277 return (status == 0); |
298 } | 278 } |
299 | 279 |
300 | |
301 File::Type File::GetType(const char* pathname, bool follow_links) { | 280 File::Type File::GetType(const char* pathname, bool follow_links) { |
302 struct stat entry_info; | 281 struct stat entry_info; |
303 int stat_success; | 282 int stat_success; |
304 if (follow_links) { | 283 if (follow_links) { |
305 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); | 284 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); |
306 } else { | 285 } else { |
307 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); | 286 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); |
308 } | 287 } |
309 if (stat_success == -1) { | 288 if (stat_success == -1) { |
310 return File::kDoesNotExist; | 289 return File::kDoesNotExist; |
311 } | 290 } |
312 if (S_ISDIR(entry_info.st_mode)) { | 291 if (S_ISDIR(entry_info.st_mode)) { |
313 return File::kIsDirectory; | 292 return File::kIsDirectory; |
314 } | 293 } |
315 if (S_ISREG(entry_info.st_mode)) { | 294 if (S_ISREG(entry_info.st_mode)) { |
316 return File::kIsFile; | 295 return File::kIsFile; |
317 } | 296 } |
318 if (S_ISLNK(entry_info.st_mode)) { | 297 if (S_ISLNK(entry_info.st_mode)) { |
319 return File::kIsLink; | 298 return File::kIsLink; |
320 } | 299 } |
321 return File::kDoesNotExist; | 300 return File::kDoesNotExist; |
322 } | 301 } |
323 | 302 |
324 | |
325 static bool CheckTypeAndSetErrno(const char* name, | 303 static bool CheckTypeAndSetErrno(const char* name, |
326 File::Type expected, | 304 File::Type expected, |
327 bool follow_links) { | 305 bool follow_links) { |
328 File::Type actual = File::GetType(name, follow_links); | 306 File::Type actual = File::GetType(name, follow_links); |
329 if (actual == expected) { | 307 if (actual == expected) { |
330 return true; | 308 return true; |
331 } | 309 } |
332 switch (actual) { | 310 switch (actual) { |
333 case File::kIsDirectory: | 311 case File::kIsDirectory: |
334 errno = EISDIR; | 312 errno = EISDIR; |
335 break; | 313 break; |
336 case File::kDoesNotExist: | 314 case File::kDoesNotExist: |
337 errno = ENOENT; | 315 errno = ENOENT; |
338 break; | 316 break; |
339 default: | 317 default: |
340 errno = EINVAL; | 318 errno = EINVAL; |
341 break; | 319 break; |
342 } | 320 } |
343 return false; | 321 return false; |
344 } | 322 } |
345 | 323 |
346 | |
347 bool File::Delete(const char* name) { | 324 bool File::Delete(const char* name) { |
348 return CheckTypeAndSetErrno(name, kIsFile, true) && | 325 return CheckTypeAndSetErrno(name, kIsFile, true) && |
349 (NO_RETRY_EXPECTED(unlink(name)) == 0); | 326 (NO_RETRY_EXPECTED(unlink(name)) == 0); |
350 } | 327 } |
351 | 328 |
352 | |
353 bool File::DeleteLink(const char* name) { | 329 bool File::DeleteLink(const char* name) { |
354 return CheckTypeAndSetErrno(name, kIsLink, false) && | 330 return CheckTypeAndSetErrno(name, kIsLink, false) && |
355 (NO_RETRY_EXPECTED(unlink(name)) == 0); | 331 (NO_RETRY_EXPECTED(unlink(name)) == 0); |
356 } | 332 } |
357 | 333 |
358 | |
359 bool File::Rename(const char* old_path, const char* new_path) { | 334 bool File::Rename(const char* old_path, const char* new_path) { |
360 return CheckTypeAndSetErrno(old_path, kIsFile, true) && | 335 return CheckTypeAndSetErrno(old_path, kIsFile, true) && |
361 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); | 336 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); |
362 } | 337 } |
363 | 338 |
364 | |
365 bool File::RenameLink(const char* old_path, const char* new_path) { | 339 bool File::RenameLink(const char* old_path, const char* new_path) { |
366 return CheckTypeAndSetErrno(old_path, kIsLink, false) && | 340 return CheckTypeAndSetErrno(old_path, kIsLink, false) && |
367 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); | 341 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); |
368 } | 342 } |
369 | 343 |
370 | |
371 bool File::Copy(const char* old_path, const char* new_path) { | 344 bool File::Copy(const char* old_path, const char* new_path) { |
372 return CheckTypeAndSetErrno(old_path, kIsFile, true) && | 345 return CheckTypeAndSetErrno(old_path, kIsFile, true) && |
373 (copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0); | 346 (copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0); |
374 } | 347 } |
375 | 348 |
376 | |
377 static bool StatHelper(const char* name, struct stat* st) { | 349 static bool StatHelper(const char* name, struct stat* st) { |
378 if (NO_RETRY_EXPECTED(stat(name, st)) != 0) { | 350 if (NO_RETRY_EXPECTED(stat(name, st)) != 0) { |
379 return false; | 351 return false; |
380 } | 352 } |
381 // Signal an error if it's a directory. | 353 // Signal an error if it's a directory. |
382 if (S_ISDIR(st->st_mode)) { | 354 if (S_ISDIR(st->st_mode)) { |
383 errno = EISDIR; | 355 errno = EISDIR; |
384 return false; | 356 return false; |
385 } | 357 } |
386 // Otherwise assume the caller knows what it's doing. | 358 // Otherwise assume the caller knows what it's doing. |
387 return true; | 359 return true; |
388 } | 360 } |
389 | 361 |
390 | |
391 int64_t File::LengthFromPath(const char* name) { | 362 int64_t File::LengthFromPath(const char* name) { |
392 struct stat st; | 363 struct stat st; |
393 if (!StatHelper(name, &st)) { | 364 if (!StatHelper(name, &st)) { |
394 return -1; | 365 return -1; |
395 } | 366 } |
396 return st.st_size; | 367 return st.st_size; |
397 } | 368 } |
398 | 369 |
399 | |
400 static int64_t TimespecToMilliseconds(const struct timespec& t) { | 370 static int64_t TimespecToMilliseconds(const struct timespec& t) { |
401 return static_cast<int64_t>(t.tv_sec) * 1000L + | 371 return static_cast<int64_t>(t.tv_sec) * 1000L + |
402 static_cast<int64_t>(t.tv_nsec) / 1000000L; | 372 static_cast<int64_t>(t.tv_nsec) / 1000000L; |
403 } | 373 } |
404 | 374 |
405 | |
406 void File::Stat(const char* name, int64_t* data) { | 375 void File::Stat(const char* name, int64_t* data) { |
407 struct stat st; | 376 struct stat st; |
408 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { | 377 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { |
409 if (S_ISREG(st.st_mode)) { | 378 if (S_ISREG(st.st_mode)) { |
410 data[kType] = kIsFile; | 379 data[kType] = kIsFile; |
411 } else if (S_ISDIR(st.st_mode)) { | 380 } else if (S_ISDIR(st.st_mode)) { |
412 data[kType] = kIsDirectory; | 381 data[kType] = kIsDirectory; |
413 } else if (S_ISLNK(st.st_mode)) { | 382 } else if (S_ISLNK(st.st_mode)) { |
414 data[kType] = kIsLink; | 383 data[kType] = kIsLink; |
415 } else { | 384 } else { |
416 data[kType] = kDoesNotExist; | 385 data[kType] = kDoesNotExist; |
417 } | 386 } |
418 data[kCreatedTime] = st.st_ctime; | 387 data[kCreatedTime] = st.st_ctime; |
419 data[kModifiedTime] = st.st_mtime; | 388 data[kModifiedTime] = st.st_mtime; |
420 data[kAccessedTime] = st.st_atime; | 389 data[kAccessedTime] = st.st_atime; |
421 data[kCreatedTime] = TimespecToMilliseconds(st.st_ctimespec); | 390 data[kCreatedTime] = TimespecToMilliseconds(st.st_ctimespec); |
422 data[kModifiedTime] = TimespecToMilliseconds(st.st_mtimespec); | 391 data[kModifiedTime] = TimespecToMilliseconds(st.st_mtimespec); |
423 data[kAccessedTime] = TimespecToMilliseconds(st.st_atimespec); | 392 data[kAccessedTime] = TimespecToMilliseconds(st.st_atimespec); |
424 data[kMode] = st.st_mode; | 393 data[kMode] = st.st_mode; |
425 data[kSize] = st.st_size; | 394 data[kSize] = st.st_size; |
426 } else { | 395 } else { |
427 data[kType] = kDoesNotExist; | 396 data[kType] = kDoesNotExist; |
428 } | 397 } |
429 } | 398 } |
430 | 399 |
431 | |
432 time_t File::LastModified(const char* name) { | 400 time_t File::LastModified(const char* name) { |
433 struct stat st; | 401 struct stat st; |
434 if (!StatHelper(name, &st)) { | 402 if (!StatHelper(name, &st)) { |
435 return -1; | 403 return -1; |
436 } | 404 } |
437 return st.st_mtime; | 405 return st.st_mtime; |
438 } | 406 } |
439 | 407 |
440 | |
441 time_t File::LastAccessed(const char* name) { | 408 time_t File::LastAccessed(const char* name) { |
442 struct stat st; | 409 struct stat st; |
443 if (!StatHelper(name, &st)) { | 410 if (!StatHelper(name, &st)) { |
444 return -1; | 411 return -1; |
445 } | 412 } |
446 return st.st_atime; | 413 return st.st_atime; |
447 } | 414 } |
448 | 415 |
449 | |
450 bool File::SetLastAccessed(const char* name, int64_t millis) { | 416 bool File::SetLastAccessed(const char* name, int64_t millis) { |
451 // First get the current times. | 417 // First get the current times. |
452 struct stat st; | 418 struct stat st; |
453 if (!StatHelper(name, &st)) { | 419 if (!StatHelper(name, &st)) { |
454 return false; | 420 return false; |
455 } | 421 } |
456 | 422 |
457 // Set the new time: | 423 // Set the new time: |
458 struct utimbuf times; | 424 struct utimbuf times; |
459 times.actime = millis / kMillisecondsPerSecond; | 425 times.actime = millis / kMillisecondsPerSecond; |
460 times.modtime = st.st_mtime; | 426 times.modtime = st.st_mtime; |
461 return utime(name, ×) == 0; | 427 return utime(name, ×) == 0; |
462 } | 428 } |
463 | 429 |
464 | |
465 bool File::SetLastModified(const char* name, int64_t millis) { | 430 bool File::SetLastModified(const char* name, int64_t millis) { |
466 // First get the current times. | 431 // First get the current times. |
467 struct stat st; | 432 struct stat st; |
468 if (!StatHelper(name, &st)) { | 433 if (!StatHelper(name, &st)) { |
469 return false; | 434 return false; |
470 } | 435 } |
471 | 436 |
472 // Set the new time: | 437 // Set the new time: |
473 struct utimbuf times; | 438 struct utimbuf times; |
474 times.actime = st.st_atime; | 439 times.actime = st.st_atime; |
475 times.modtime = millis / kMillisecondsPerSecond; | 440 times.modtime = millis / kMillisecondsPerSecond; |
476 return utime(name, ×) == 0; | 441 return utime(name, ×) == 0; |
477 } | 442 } |
478 | 443 |
479 | |
480 const char* File::LinkTarget(const char* pathname) { | 444 const char* File::LinkTarget(const char* pathname) { |
481 struct stat link_stats; | 445 struct stat link_stats; |
482 if (lstat(pathname, &link_stats) != 0) { | 446 if (lstat(pathname, &link_stats) != 0) { |
483 return NULL; | 447 return NULL; |
484 } | 448 } |
485 if (!S_ISLNK(link_stats.st_mode)) { | 449 if (!S_ISLNK(link_stats.st_mode)) { |
486 errno = ENOENT; | 450 errno = ENOENT; |
487 return NULL; | 451 return NULL; |
488 } | 452 } |
489 // Don't rely on the link_stats.st_size for the size of the link | 453 // Don't rely on the link_stats.st_size for the size of the link |
490 // target. The link might have changed before the readlink call. | 454 // target. The link might have changed before the readlink call. |
491 const int kBufferSize = 1024; | 455 const int kBufferSize = 1024; |
492 char target[kBufferSize]; | 456 char target[kBufferSize]; |
493 size_t target_size = | 457 size_t target_size = |
494 TEMP_FAILURE_RETRY(readlink(pathname, target, kBufferSize)); | 458 TEMP_FAILURE_RETRY(readlink(pathname, target, kBufferSize)); |
495 if (target_size <= 0) { | 459 if (target_size <= 0) { |
496 return NULL; | 460 return NULL; |
497 } | 461 } |
498 char* target_name = DartUtils::ScopedCString(target_size + 1); | 462 char* target_name = DartUtils::ScopedCString(target_size + 1); |
499 ASSERT(target_name != NULL); | 463 ASSERT(target_name != NULL); |
500 memmove(target_name, target, target_size); | 464 memmove(target_name, target, target_size); |
501 target_name[target_size] = '\0'; | 465 target_name[target_size] = '\0'; |
502 return target_name; | 466 return target_name; |
503 } | 467 } |
504 | 468 |
505 | |
506 bool File::IsAbsolutePath(const char* pathname) { | 469 bool File::IsAbsolutePath(const char* pathname) { |
507 return (pathname != NULL && pathname[0] == '/'); | 470 return (pathname != NULL && pathname[0] == '/'); |
508 } | 471 } |
509 | 472 |
510 | |
511 const char* File::GetCanonicalPath(const char* pathname) { | 473 const char* File::GetCanonicalPath(const char* pathname) { |
512 char* abs_path = NULL; | 474 char* abs_path = NULL; |
513 if (pathname != NULL) { | 475 if (pathname != NULL) { |
514 // On some older MacOs versions the default behaviour of realpath allocating | 476 // On some older MacOs versions the default behaviour of realpath allocating |
515 // space for the resolved_path when a NULL is passed in does not seem to | 477 // space for the resolved_path when a NULL is passed in does not seem to |
516 // work, so we explicitly allocate space. | 478 // work, so we explicitly allocate space. |
517 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1); | 479 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1); |
518 ASSERT(resolved_path != NULL); | 480 ASSERT(resolved_path != NULL); |
519 do { | 481 do { |
520 abs_path = realpath(pathname, resolved_path); | 482 abs_path = realpath(pathname, resolved_path); |
521 } while ((abs_path == NULL) && (errno == EINTR)); | 483 } while ((abs_path == NULL) && (errno == EINTR)); |
522 ASSERT((abs_path == NULL) || IsAbsolutePath(abs_path)); | 484 ASSERT((abs_path == NULL) || IsAbsolutePath(abs_path)); |
523 ASSERT((abs_path == NULL) || (abs_path == resolved_path)); | 485 ASSERT((abs_path == NULL) || (abs_path == resolved_path)); |
524 } | 486 } |
525 return abs_path; | 487 return abs_path; |
526 } | 488 } |
527 | 489 |
528 | |
529 const char* File::PathSeparator() { | 490 const char* File::PathSeparator() { |
530 return "/"; | 491 return "/"; |
531 } | 492 } |
532 | 493 |
533 | |
534 const char* File::StringEscapedPathSeparator() { | 494 const char* File::StringEscapedPathSeparator() { |
535 return "/"; | 495 return "/"; |
536 } | 496 } |
537 | 497 |
538 | |
539 File::StdioHandleType File::GetStdioHandleType(int fd) { | 498 File::StdioHandleType File::GetStdioHandleType(int fd) { |
540 ASSERT((0 <= fd) && (fd <= 2)); | 499 ASSERT((0 <= fd) && (fd <= 2)); |
541 struct stat buf; | 500 struct stat buf; |
542 int result = fstat(fd, &buf); | 501 int result = fstat(fd, &buf); |
543 if (result == -1) { | 502 if (result == -1) { |
544 return kOther; | 503 return kOther; |
545 } | 504 } |
546 if (S_ISCHR(buf.st_mode)) { | 505 if (S_ISCHR(buf.st_mode)) { |
547 return kTerminal; | 506 return kTerminal; |
548 } | 507 } |
549 if (S_ISFIFO(buf.st_mode)) { | 508 if (S_ISFIFO(buf.st_mode)) { |
550 return kPipe; | 509 return kPipe; |
551 } | 510 } |
552 if (S_ISSOCK(buf.st_mode)) { | 511 if (S_ISSOCK(buf.st_mode)) { |
553 return kSocket; | 512 return kSocket; |
554 } | 513 } |
555 if (S_ISREG(buf.st_mode)) { | 514 if (S_ISREG(buf.st_mode)) { |
556 return kFile; | 515 return kFile; |
557 } | 516 } |
558 return kOther; | 517 return kOther; |
559 } | 518 } |
560 | 519 |
561 | |
562 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { | 520 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { |
563 struct stat file_1_info; | 521 struct stat file_1_info; |
564 struct stat file_2_info; | 522 struct stat file_2_info; |
565 if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) || | 523 if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) || |
566 (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) { | 524 (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) { |
567 return File::kError; | 525 return File::kError; |
568 } | 526 } |
569 return ((file_1_info.st_ino == file_2_info.st_ino) && | 527 return ((file_1_info.st_ino == file_2_info.st_ino) && |
570 (file_1_info.st_dev == file_2_info.st_dev)) | 528 (file_1_info.st_dev == file_2_info.st_dev)) |
571 ? File::kIdentical | 529 ? File::kIdentical |
572 : File::kDifferent; | 530 : File::kDifferent; |
573 } | 531 } |
574 | 532 |
575 } // namespace bin | 533 } // namespace bin |
576 } // namespace dart | 534 } // namespace dart |
577 | 535 |
578 #endif // defined(HOST_OS_MACOS) | 536 #endif // defined(HOST_OS_MACOS) |
OLD | NEW |