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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(HOST_OS_ANDROID)
7
8#include "bin/file.h"
9
10#include <errno.h> // NOLINT
11#include <fcntl.h> // NOLINT
12#include <libgen.h> // NOLINT
13#include <sys/mman.h> // NOLINT
14#include <sys/sendfile.h> // NOLINT
15#include <sys/stat.h> // NOLINT
16#include <sys/syscall.h> // NOLINT
17#include <sys/types.h> // NOLINT
18#include <unistd.h> // NOLINT
19#include <utime.h> // NOLINT
20
21#include "bin/builtin.h"
22#include "bin/fdutils.h"
23#include "bin/namespace.h"
24#include "platform/signal_blocker.h"
25#include "platform/syslog.h"
26#include "platform/utils.h"
27
28namespace dart {
29namespace bin {
30
31class FileHandle {
32 public:
33 explicit FileHandle(int fd) : fd_(fd) {}
34 ~FileHandle() {}
35 int fd() const { return fd_; }
36 void set_fd(int fd) { fd_ = fd; }
37
38 private:
39 int fd_;
40
41 DISALLOW_COPY_AND_ASSIGN(FileHandle);
42};
43
44File::~File() {
45 if (!IsClosed() && (handle_->fd() != STDOUT_FILENO) &&
46 (handle_->fd() != STDERR_FILENO)) {
47 Close();
48 }
49 delete handle_;
50}
51
52void File::Close() {
53 ASSERT(handle_->fd() >= 0);
54 if (handle_->fd() == STDOUT_FILENO) {
55 // If stdout, redirect fd to /dev/null.
56 int null_fd = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY));
57 ASSERT(null_fd >= 0);
58 VOID_TEMP_FAILURE_RETRY(dup2(null_fd, handle_->fd()));
59 close(null_fd);
60 } else {
61 int err = close(handle_->fd());
62 if (err != 0) {
63 const int kBufferSize = 1024;
64 char error_buf[kBufferSize];
65 Syslog::PrintErr("%s\n", Utils::StrError(errno, error_buf, kBufferSize));
66 }
67 }
68 handle_->set_fd(kClosedFd);
69}
70
71intptr_t File::GetFD() {
72 return handle_->fd();
73}
74
75bool File::IsClosed() {
76 return handle_->fd() == kClosedFd;
77}
78
79MappedMemory* File::Map(MapType type,
80 int64_t position,
81 int64_t length,
82 void* start) {
83 ASSERT(handle_->fd() >= 0);
84 ASSERT(length > 0);
85 int prot = PROT_NONE;
86 switch (type) {
87 case kReadOnly:
88 prot = PROT_READ;
89 break;
90 case kReadExecute:
91 prot = PROT_READ | PROT_EXEC;
92 break;
93 case kReadWrite:
94 prot = PROT_READ | PROT_WRITE;
95 break;
96 }
97 const int flags = MAP_PRIVATE | (start != nullptr ? MAP_FIXED : 0);
98 void* addr = mmap(start, length, prot, flags, handle_->fd(), position);
99 if (addr == MAP_FAILED) {
100 return NULL;
101 }
102 return new MappedMemory(addr, length, /*should_unmap=*/start == nullptr);
103}
104
105void MappedMemory::Unmap() {
106 int result = munmap(address_, size_);
107 ASSERT(result == 0);
108 address_ = 0;
109 size_ = 0;
110}
111
112int64_t File::Read(void* buffer, int64_t num_bytes) {
113 ASSERT(handle_->fd() >= 0);
114 return TEMP_FAILURE_RETRY(read(handle_->fd(), buffer, num_bytes));
115}
116
117int64_t File::Write(const void* buffer, int64_t num_bytes) {
118 ASSERT(handle_->fd() >= 0);
119 return TEMP_FAILURE_RETRY(write(handle_->fd(), buffer, num_bytes));
120}
121
122bool File::VPrint(const char* format, va_list args) {
123 // Measure.
124 va_list measure_args;
125 va_copy(measure_args, args);
126 intptr_t len = vsnprintf(NULL, 0, format, measure_args);
127 va_end(measure_args);
128
129 char* buffer = reinterpret_cast<char*>(malloc(len + 1));
130
131 // Print.
132 va_list print_args;
133 va_copy(print_args, args);
134 vsnprintf(buffer, len + 1, format, print_args);
135 va_end(print_args);
136
137 bool result = WriteFully(buffer, len);
138 free(buffer);
139 return result;
140}
141
142int64_t File::Position() {
143 ASSERT(handle_->fd() >= 0);
144 return NO_RETRY_EXPECTED(lseek64(handle_->fd(), 0, SEEK_CUR));
145}
146
147bool File::SetPosition(int64_t position) {
148 ASSERT(handle_->fd() >= 0);
149 return NO_RETRY_EXPECTED(lseek64(handle_->fd(), position, SEEK_SET)) >= 0;
150}
151
152bool File::Truncate(int64_t length) {
153 ASSERT(handle_->fd() >= 0);
154 return TEMP_FAILURE_RETRY(ftruncate(handle_->fd(), length) != -1);
155}
156
157bool File::Flush() {
158 ASSERT(handle_->fd() >= 0);
159 return NO_RETRY_EXPECTED(fsync(handle_->fd()) != -1);
160}
161
162bool File::Lock(File::LockType lock, int64_t start, int64_t end) {
163 ASSERT(handle_->fd() >= 0);
164 ASSERT((end == -1) || (end > start));
165 struct flock fl;
166 switch (lock) {
167 case File::kLockUnlock:
168 fl.l_type = F_UNLCK;
169 break;
170 case File::kLockShared:
171 case File::kLockBlockingShared:
172 fl.l_type = F_RDLCK;
173 break;
174 case File::kLockExclusive:
175 case File::kLockBlockingExclusive:
176 fl.l_type = F_WRLCK;
177 break;
178 default:
179 return false;
180 }
181 fl.l_whence = SEEK_SET;
182 fl.l_start = start;
183 fl.l_len = end == -1 ? 0 : end - start;
184 int cmd = F_SETLK;
185 if ((lock == File::kLockBlockingShared) ||
186 (lock == File::kLockBlockingExclusive)) {
187 cmd = F_SETLKW;
188 }
189 return TEMP_FAILURE_RETRY(fcntl(handle_->fd(), cmd, &fl)) != -1;
190}
191
192int64_t File::Length() {
193 ASSERT(handle_->fd() >= 0);
194 struct stat st;
195 if (NO_RETRY_EXPECTED(fstat(handle_->fd(), &st)) == 0) {
196 return st.st_size;
197 }
198 return -1;
199}
200
201File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) {
202 UNREACHABLE();
203 return NULL;
204}
205
206File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
207 NamespaceScope ns(namespc, name);
208 // Report errors for non-regular files.
209 struct stat st;
210 if (TEMP_FAILURE_RETRY(fstatat(ns.fd(), ns.path(), &st, 0)) == 0) {
211 // Only accept regular files, character devices, and pipes.
212 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) {
213 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT;
214 return NULL;
215 }
216 }
217 int flags = O_RDONLY;
218 if ((mode & kWrite) != 0) {
219 ASSERT((mode & kWriteOnly) == 0);
220 flags = (O_RDWR | O_CREAT);
221 }
222 if ((mode & kWriteOnly) != 0) {
223 ASSERT((mode & kWrite) == 0);
224 flags = (O_WRONLY | O_CREAT);
225 }
226 if ((mode & kTruncate) != 0) {
227 flags = flags | O_TRUNC;
228 }
229 flags |= O_CLOEXEC;
230 const int fd = TEMP_FAILURE_RETRY(openat(ns.fd(), ns.path(), flags, 0666));
231 if (fd < 0) {
232 return NULL;
233 }
234 if ((((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) ||
235 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) {
236 int64_t position = NO_RETRY_EXPECTED(lseek(fd, 0, SEEK_END));
237 if (position < 0) {
238 return NULL;
239 }
240 }
241 return new File(new FileHandle(fd));
242}
243
244Utils::CStringUniquePtr File::UriToPath(const char* uri) {
245 const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0)
246 ? uri + 7 : uri;
247 UriDecoder uri_decoder(path);
248 if (uri_decoder.decoded() == nullptr) {
249 errno = EINVAL;
250 return Utils::CreateCStringUniquePtr(nullptr);
251 }
252 return Utils::CreateCStringUniquePtr(strdup(uri_decoder.decoded()));
253}
254
255File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
256 auto path = UriToPath(uri);
257 if (path == nullptr) {
258 return nullptr;
259 }
260 return File::Open(namespc, path.get(), mode);
261}
262
263File* File::OpenStdio(int fd) {
264 return new File(new FileHandle(fd));
265}
266
267bool File::Exists(Namespace* namespc, const char* name) {
268 NamespaceScope ns(namespc, name);
269 struct stat st;
270 if (TEMP_FAILURE_RETRY(fstatat(ns.fd(), ns.path(), &st, 0)) == 0) {
271 // Everything but a directory and a link is a file to Dart.
272 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
273 } else {
274 return false;
275 }
276}
277
278bool File::ExistsUri(Namespace* namespc, const char* uri) {
279 auto path = UriToPath(uri);
280 if (path == nullptr) {
281 return false;
282 }
283 return File::Exists(namespc, path.get());
284}
285
286bool File::Create(Namespace* namespc, const char* name) {
287 NamespaceScope ns(namespc, name);
288 const int fd = TEMP_FAILURE_RETRY(
289 openat(ns.fd(), ns.path(), O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
290 if (fd < 0) {
291 return false;
292 }
293 // File.create returns a File, so we shouldn't be giving the illusion that the
294 // call has created a file or that a file already exists if there is already
295 // an entity at the same path that is a directory or a link.
296 bool is_file = true;
297 struct stat st;
298 if (TEMP_FAILURE_RETRY(fstat(fd, &st)) == 0) {
299 if (S_ISDIR(st.st_mode)) {
300 errno = EISDIR;
301 is_file = false;
302 } else if (S_ISLNK(st.st_mode)) {
303 errno = ENOENT;
304 is_file = false;
305 }
306 }
307 FDUtils::SaveErrorAndClose(fd);
308 return is_file;
309}
310
311// symlinkat is added to Android libc in android-21.
312static int SymlinkAt(const char* oldpath, int newdirfd, const char* newpath) {
313 return syscall(__NR_symlinkat, oldpath, newdirfd, newpath);
314}
315
316bool File::CreateLink(Namespace* namespc,
317 const char* name,
318 const char* target) {
319 NamespaceScope ns(namespc, name);
320 return NO_RETRY_EXPECTED(SymlinkAt(target, ns.fd(), ns.path())) == 0;
321}
322
323File::Type File::GetType(Namespace* namespc,
324 const char* name,
325 bool follow_links) {
326 NamespaceScope ns(namespc, name);
327 struct stat entry_info;
328 int stat_success;
329 if (follow_links) {
330 stat_success =
331 TEMP_FAILURE_RETRY(fstatat(ns.fd(), ns.path(), &entry_info, 0));
332 } else {
333 stat_success = TEMP_FAILURE_RETRY(
334 fstatat(ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW));
335 }
336 if (stat_success == -1) {
337 return File::kDoesNotExist;
338 }
339 if (S_ISDIR(entry_info.st_mode)) {
340 return File::kIsDirectory;
341 }
342 if (S_ISREG(entry_info.st_mode)) {
343 return File::kIsFile;
344 }
345 if (S_ISLNK(entry_info.st_mode)) {
346 return File::kIsLink;
347 }
348 return File::kDoesNotExist;
349}
350
351static bool CheckTypeAndSetErrno(Namespace* namespc,
352 const char* name,
353 File::Type expected,
354 bool follow_links) {
355 File::Type actual = File::GetType(namespc, name, follow_links);
356 if (actual == expected) {
357 return true;
358 }
359 switch (actual) {
360 case File::kIsDirectory:
361 errno = EISDIR;
362 break;
363 case File::kDoesNotExist:
364 errno = ENOENT;
365 break;
366 default:
367 errno = EINVAL;
368 break;
369 }
370 return false;
371}
372
373bool File::Delete(Namespace* namespc, const char* name) {
374 NamespaceScope ns(namespc, name);
375 return CheckTypeAndSetErrno(namespc, name, kIsFile, true) &&
376 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
377}
378
379bool File::DeleteLink(Namespace* namespc, const char* name) {
380 NamespaceScope ns(namespc, name);
381 return CheckTypeAndSetErrno(namespc, name, kIsLink, false) &&
382 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
383}
384
385bool File::Rename(Namespace* namespc,
386 const char* old_path,
387 const char* new_path) {
388 NamespaceScope oldns(namespc, old_path);
389 NamespaceScope newns(namespc, new_path);
390 return CheckTypeAndSetErrno(namespc, old_path, kIsFile, true) &&
391 (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(),
392 newns.path())) == 0);
393}
394
395bool File::RenameLink(Namespace* namespc,
396 const char* old_path,
397 const char* new_path) {
398 NamespaceScope oldns(namespc, old_path);
399 NamespaceScope newns(namespc, new_path);
400 return CheckTypeAndSetErrno(namespc, old_path, kIsLink, false) &&
401 (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(),
402 newns.path())) == 0);
403}
404
405bool File::Copy(Namespace* namespc,
406 const char* old_path,
407 const char* new_path) {
408 if (!CheckTypeAndSetErrno(namespc, old_path, kIsFile, true)) {
409 return false;
410 }
411 NamespaceScope oldns(namespc, old_path);
412 struct stat st;
413 if (TEMP_FAILURE_RETRY(fstatat(oldns.fd(), oldns.path(), &st, 0)) != 0) {
414 return false;
415 }
416 const int old_fd = TEMP_FAILURE_RETRY(
417 openat(oldns.fd(), oldns.path(), O_RDONLY | O_CLOEXEC));
418 if (old_fd < 0) {
419 return false;
420 }
421 NamespaceScope newns(namespc, new_path);
422 const int new_fd = TEMP_FAILURE_RETRY(
423 openat(newns.fd(), newns.path(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC,
424 st.st_mode));
425 if (new_fd < 0) {
426 close(old_fd);
427 return false;
428 }
429 off_t offset = 0;
430 intptr_t result = 1;
431 while (result > 0) {
432 // Loop to ensure we copy everything, and not only up to 2GB.
433 result = NO_RETRY_EXPECTED(sendfile(new_fd, old_fd, &offset, kMaxUint32));
434 }
435 // From sendfile man pages:
436 // Applications may wish to fall back to read(2)/write(2) in the case
437 // where sendfile() fails with EINVAL or ENOSYS.
438 if ((result < 0) && ((errno == EINVAL) || (errno == ENOSYS))) {
439 const intptr_t kBufferSize = 8 * KB;
440 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(kBufferSize));
441 while ((result = TEMP_FAILURE_RETRY(read(old_fd, buffer, kBufferSize))) >
442 0) {
443 int wrote = TEMP_FAILURE_RETRY(write(new_fd, buffer, result));
444 if (wrote != result) {
445 result = -1;
446 break;
447 }
448 }
449 free(buffer);
450 }
451 int e = errno;
452 close(old_fd);
453 close(new_fd);
454 if (result < 0) {
455 VOID_NO_RETRY_EXPECTED(unlinkat(newns.fd(), newns.path(), 0));
456 errno = e;
457 return false;
458 }
459 return true;
460}
461
462static bool StatHelper(Namespace* namespc, const char* name, struct stat* st) {
463 NamespaceScope ns(namespc, name);
464 if (TEMP_FAILURE_RETRY(fstatat(ns.fd(), ns.path(), st, 0)) != 0) {
465 return false;
466 }
467 // Signal an error if it's a directory.
468 if (S_ISDIR(st->st_mode)) {
469 errno = EISDIR;
470 return false;
471 }
472 // Otherwise assume the caller knows what it's doing.
473 return true;
474}
475
476int64_t File::LengthFromPath(Namespace* namespc, const char* name) {
477 struct stat st;
478 if (!StatHelper(namespc, name, &st)) {
479 return -1;
480 }
481 return st.st_size;
482}
483
484static void MillisecondsToTimespec(int64_t millis, struct timespec* t) {
485 ASSERT(t != NULL);
486 t->tv_sec = millis / kMillisecondsPerSecond;
487 t->tv_nsec = (millis % kMillisecondsPerSecond) * 1000L;
488}
489
490void File::Stat(Namespace* namespc, const char* name, int64_t* data) {
491 NamespaceScope ns(namespc, name);
492 struct stat st;
493 if (TEMP_FAILURE_RETRY(fstatat(ns.fd(), ns.path(), &st, 0)) == 0) {
494 if (S_ISREG(st.st_mode)) {
495 data[kType] = kIsFile;
496 } else if (S_ISDIR(st.st_mode)) {
497 data[kType] = kIsDirectory;
498 } else if (S_ISLNK(st.st_mode)) {
499 data[kType] = kIsLink;
500 } else {
501 data[kType] = kDoesNotExist;
502 }
503 data[kCreatedTime] = static_cast<int64_t>(st.st_ctime) * 1000;
504 data[kModifiedTime] = static_cast<int64_t>(st.st_mtime) * 1000;
505 data[kAccessedTime] = static_cast<int64_t>(st.st_atime) * 1000;
506 data[kMode] = st.st_mode;
507 data[kSize] = st.st_size;
508 } else {
509 data[kType] = kDoesNotExist;
510 }
511}
512
513time_t File::LastModified(Namespace* namespc, const char* name) {
514 struct stat st;
515 if (!StatHelper(namespc, name, &st)) {
516 return -1;
517 }
518 return st.st_mtime;
519}
520
521time_t File::LastAccessed(Namespace* namespc, const char* name) {
522 struct stat st;
523 if (!StatHelper(namespc, name, &st)) {
524 return -1;
525 }
526 return st.st_atime;
527}
528
529bool File::SetLastAccessed(Namespace* namespc,
530 const char* name,
531 int64_t millis) {
532 // First get the current times.
533 struct stat st;
534 if (!StatHelper(namespc, name, &st)) {
535 return false;
536 }
537
538 // Set the new time:
539 NamespaceScope ns(namespc, name);
540 struct timespec times[2];
541 MillisecondsToTimespec(millis, &times[0]);
542 MillisecondsToTimespec(static_cast<int64_t>(st.st_mtime) * 1000, &times[1]);
543 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
544}
545
546bool File::SetLastModified(Namespace* namespc,
547 const char* name,
548 int64_t millis) {
549 // First get the current times.
550 struct stat st;
551 if (!StatHelper(namespc, name, &st)) {
552 return false;
553 }
554
555 // Set the new time:
556 NamespaceScope ns(namespc, name);
557 struct timespec times[2];
558 MillisecondsToTimespec(static_cast<int64_t>(st.st_atime) * 1000, &times[0]);
559 MillisecondsToTimespec(millis, &times[1]);
560 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
561}
562
563// readlinkat is added to Android libc in android-21.
564static int ReadLinkAt(int dirfd,
565 const char* pathname,
566 char* buf,
567 size_t bufsize) {
568 return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsize);
569}
570
571const char* File::LinkTarget(Namespace* namespc,
572 const char* name,
573 char* dest,
574 int dest_size) {
575 NamespaceScope ns(namespc, name);
576 struct stat link_stats;
577 const int status = TEMP_FAILURE_RETRY(
578 fstatat(ns.fd(), ns.path(), &link_stats, AT_SYMLINK_NOFOLLOW));
579 if (status != 0) {
580 return NULL;
581 }
582 if (!S_ISLNK(link_stats.st_mode)) {
583 errno = ENOENT;
584 return NULL;
585 }
586 // Don't rely on the link_stats.st_size for the size of the link
587 // target. For some filesystems, e.g. procfs, this value is always
588 // 0. Also the link might have changed before the readlink call.
589 const int kBufferSize = PATH_MAX + 1;
590 char target[kBufferSize];
591 const int target_size =
592 TEMP_FAILURE_RETRY(ReadLinkAt(ns.fd(), ns.path(), target, kBufferSize));
593 if (target_size <= 0) {
594 return NULL;
595 }
596 if (dest == NULL) {
597 dest = DartUtils::ScopedCString(target_size + 1);
598 } else {
599 ASSERT(dest_size > 0);
600 if (dest_size <= target_size) {
601 return NULL;
602 }
603 }
604 memmove(dest, target, target_size);
605 dest[target_size] = '\0';
606 return dest;
607}
608
609bool File::IsAbsolutePath(const char* pathname) {
610 return ((pathname != NULL) && (pathname[0] == '/'));
611}
612
613intptr_t File::ReadLinkInto(const char* pathname,
614 char* result,
615 size_t result_size) {
616 ASSERT(pathname != NULL);
617 ASSERT(IsAbsolutePath(pathname));
618 struct stat link_stats;
619 if (TEMP_FAILURE_RETRY(lstat(pathname, &link_stats)) != 0) {
620 return -1;
621 }
622 if (!S_ISLNK(link_stats.st_mode)) {
623 errno = ENOENT;
624 return -1;
625 }
626 size_t target_size =
627 TEMP_FAILURE_RETRY(readlink(pathname, result, result_size));
628 if (target_size <= 0) {
629 return -1;
630 }
631 // readlink returns non-zero terminated strings. Append.
632 if (target_size < result_size) {
633 result[target_size] = '\0';
634 target_size++;
635 }
636 return target_size;
637}
638
639const char* File::ReadLink(const char* pathname) {
640 // Don't rely on the link_stats.st_size for the size of the link
641 // target. For some filesystems, e.g. procfs, this value is always
642 // 0. Also the link might have changed before the readlink call.
643 const int kBufferSize = PATH_MAX + 1;
644 char target[kBufferSize];
645 size_t target_size = ReadLinkInto(pathname, target, kBufferSize);
646 if (target_size <= 0) {
647 return NULL;
648 }
649 char* target_name = DartUtils::ScopedCString(target_size);
650 ASSERT(target_name != NULL);
651 memmove(target_name, target, target_size);
652 return target_name;
653}
654
655const char* File::GetCanonicalPath(Namespace* namespc,
656 const char* name,
657 char* dest,
658 int dest_size) {
659 if (name == NULL) {
660 return NULL;
661 }
662 if (!Namespace::IsDefault(namespc)) {
663 // TODO(zra): There is no realpathat(). Also chasing a symlink might result
664 // in a path to something outside of the namespace, so canonicalizing paths
665 // would have to be done carefully. For now, don't do anything.
666 return name;
667 }
668 char* abs_path;
669 if (dest == NULL) {
670 dest = DartUtils::ScopedCString(PATH_MAX + 1);
671 } else {
672 ASSERT(dest_size >= PATH_MAX);
673 }
674 ASSERT(dest != NULL);
675 do {
676 abs_path = realpath(name, dest);
677 } while ((abs_path == NULL) && (errno == EINTR));
678 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path));
679 ASSERT(abs_path == NULL || (abs_path == dest));
680 return abs_path;
681}
682
683const char* File::PathSeparator() {
684 return "/";
685}
686
687const char* File::StringEscapedPathSeparator() {
688 return "/";
689}
690
691File::StdioHandleType File::GetStdioHandleType(int fd) {
692 struct stat buf;
693 int result = fstat(fd, &buf);
694 if (result == -1) {
695 return kTypeError;
696 }
697 if (S_ISCHR(buf.st_mode)) {
698 return kTerminal;
699 }
700 if (S_ISFIFO(buf.st_mode)) {
701 return kPipe;
702 }
703 if (S_ISSOCK(buf.st_mode)) {
704 return kSocket;
705 }
706 if (S_ISREG(buf.st_mode)) {
707 return kFile;
708 }
709 return kOther;
710}
711
712File::Identical File::AreIdentical(Namespace* namespc_1,
713 const char* file_1,
714 Namespace* namespc_2,
715 const char* file_2) {
716 struct stat file_1_info;
717 struct stat file_2_info;
718 int status;
719 {
720 NamespaceScope ns1(namespc_1, file_1);
721 status = TEMP_FAILURE_RETRY(
722 fstatat(ns1.fd(), ns1.path(), &file_1_info, AT_SYMLINK_NOFOLLOW));
723 if (status == -1) {
724 return File::kError;
725 }
726 }
727 {
728 NamespaceScope ns2(namespc_2, file_2);
729 status = TEMP_FAILURE_RETRY(
730 fstatat(ns2.fd(), ns2.path(), &file_2_info, AT_SYMLINK_NOFOLLOW));
731 if (status == -1) {
732 return File::kError;
733 }
734 }
735 return ((file_1_info.st_ino == file_2_info.st_ino) &&
736 (file_1_info.st_dev == file_2_info.st_dev))
737 ? File::kIdentical
738 : File::kDifferent;
739}
740
741} // namespace bin
742} // namespace dart
743
744#endif // defined(HOST_OS_ANDROID)
745