Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(944)

Unified Diff: runtime/bin/file_fuchsia.cc

Issue 3001963002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Fuchsia fix Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/file_fuchsia.cc
diff --git a/runtime/bin/file_fuchsia.cc b/runtime/bin/file_fuchsia.cc
index cc36ea9f7dab661e5b2d28ccb64578d9f2f6d3d8..9c4a22757f9d5df859a20458e742b8ed612945e2 100644
--- a/runtime/bin/file_fuchsia.cc
+++ b/runtime/bin/file_fuchsia.cc
@@ -7,18 +7,20 @@
#include "bin/file.h"
-#include <errno.h> // NOLINT
-#include <fcntl.h> // NOLINT
-#include <libgen.h> // NOLINT
-#include <sys/mman.h> // NOLINT
-#include <sys/stat.h> // NOLINT
-#include <sys/types.h> // NOLINT
-#include <unistd.h> // NOLINT
-#include <utime.h> // NOLINT
+#include <errno.h> // NOLINT
+#include <fcntl.h> // NOLINT
+#include <libgen.h> // NOLINT
+#include <mxio/namespace.h> // NOLINT
+#include <sys/mman.h> // NOLINT
+#include <sys/stat.h> // NOLINT
+#include <sys/types.h> // NOLINT
+#include <unistd.h> // NOLINT
+#include <utime.h> // NOLINT
#include "bin/builtin.h"
#include "bin/fdutils.h"
#include "bin/log.h"
+#include "bin/namespace.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
@@ -39,7 +41,8 @@ class FileHandle {
};
File::~File() {
- if (!IsClosed()) {
+ if (!IsClosed() && (handle_->fd() != STDOUT_FILENO) &&
+ (handle_->fd() != STDERR_FILENO)) {
Close();
}
delete handle_;
@@ -175,10 +178,11 @@ File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) {
return NULL;
}
-File* File::Open(const char* name, FileOpenMode mode) {
+File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
+ NamespaceScope ns(namespc, name);
// Report errors for non-regular files.
- struct stat st;
- if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) {
+ struct stat64 st;
+ if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return NULL;
@@ -197,7 +201,7 @@ File* File::Open(const char* name, FileOpenMode mode) {
flags = flags | O_TRUNC;
}
flags |= O_CLOEXEC;
- int fd = NO_RETRY_EXPECTED(open(name, flags, 0666));
+ int fd = NO_RETRY_EXPECTED(openat64(ns.fd(), name, flags, 0666));
if (fd < 0) {
return NULL;
}
@@ -215,9 +219,10 @@ File* File::OpenStdio(int fd) {
return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd));
}
-bool File::Exists(const char* name) {
+bool File::Exists(Namespace* namespc, const char* name) {
+ NamespaceScope ns(namespc, name);
struct stat64 st;
- if (NO_RETRY_EXPECTED(stat64(name, &st)) == 0) {
+ if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
// Everything but a directory and a link is a file to Dart.
return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
} else {
@@ -225,9 +230,10 @@ bool File::Exists(const char* name) {
}
}
-bool File::Create(const char* name) {
- int fd =
- NO_RETRY_EXPECTED(open64(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
+bool File::Create(Namespace* namespc, const char* name) {
+ NamespaceScope ns(namespc, name);
+ const int fd = NO_RETRY_EXPECTED(openat64(
+ ns.fd(), ns.path(), O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
if (fd < 0) {
return false;
}
@@ -249,17 +255,22 @@ bool File::Create(const char* name) {
return is_file;
}
-bool File::CreateLink(const char* name, const char* target) {
- return NO_RETRY_EXPECTED(symlink(target, name)) == 0;
+bool File::CreateLink(Namespace* namespc, const char* name, const char* target) {
+ NamespaceScope ns(namespc, name);
+ return NO_RETRY_EXPECTED(symlinkat(target, ns.fd(), ns.path())) == 0;
}
-File::Type File::GetType(const char* pathname, bool follow_links) {
- struct stat entry_info;
+File::Type File::GetType(
+ Namespace* namespc, const char* name, bool follow_links) {
+ NamespaceScope ns(namespc, name);
+ struct stat64 entry_info;
int stat_success;
if (follow_links) {
- stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info));
+ stat_success = TEMP_FAILURE_RETRY(fstatat64(
+ ns.fd(), ns.path(), &entry_info, 0));
} else {
- stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info));
+ stat_success = TEMP_FAILURE_RETRY(fstatat64(
+ ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW));
}
if (stat_success == -1) {
return File::kDoesNotExist;
@@ -276,10 +287,11 @@ File::Type File::GetType(const char* pathname, bool follow_links) {
return File::kDoesNotExist;
}
-static bool CheckTypeAndSetErrno(const char* name,
+static bool CheckTypeAndSetErrno(Namespace* namespc,
+ const char* name,
File::Type expected,
bool follow_links) {
- File::Type actual = File::GetType(name, follow_links);
+ File::Type actual = File::GetType(namespc, name, follow_links);
if (actual == expected) {
return true;
}
@@ -297,40 +309,53 @@ static bool CheckTypeAndSetErrno(const char* name,
return false;
}
-bool File::Delete(const char* name) {
- return CheckTypeAndSetErrno(name, kIsFile, true) &&
- (NO_RETRY_EXPECTED(unlink(name)) == 0);
+bool File::Delete(Namespace* namespc, const char* name) {
+ NamespaceScope ns(namespc, name);
+ return CheckTypeAndSetErrno(namespc, name, kIsFile, true) &&
+ (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
}
-bool File::DeleteLink(const char* name) {
- return CheckTypeAndSetErrno(name, kIsLink, false) &&
- (NO_RETRY_EXPECTED(unlink(name)) == 0);
+bool File::DeleteLink(Namespace* namespc, const char* name) {
+ NamespaceScope ns(namespc, name);
+ return CheckTypeAndSetErrno(namespc, name, kIsLink, false) &&
+ (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
}
-bool File::Rename(const char* old_path, const char* new_path) {
- return CheckTypeAndSetErrno(old_path, kIsFile, true) &&
- (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0);
+bool File::Rename(
+ Namespace* namespc, const char* old_path, const char* new_path) {
+ NamespaceScope oldns(namespc, old_path);
+ NamespaceScope newns(namespc, new_path);
+ return CheckTypeAndSetErrno(namespc, old_path, kIsFile, true) &&
+ (NO_RETRY_EXPECTED(renameat(
+ oldns.fd(), oldns.path(), newns.fd(), newns.path())) == 0);
}
-bool File::RenameLink(const char* old_path, const char* new_path) {
- return CheckTypeAndSetErrno(old_path, kIsLink, false) &&
- (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0);
+bool File::RenameLink(
+ Namespace* namespc, const char* old_path, const char* new_path) {
+ NamespaceScope oldns(namespc, old_path);
+ NamespaceScope newns(namespc, new_path);
+ return CheckTypeAndSetErrno(namespc, old_path, kIsLink, false) &&
+ (NO_RETRY_EXPECTED(renameat(
+ oldns.fd(), oldns.path(), newns.fd(), newns.path())) == 0);
}
-bool File::Copy(const char* old_path, const char* new_path) {
- if (!CheckTypeAndSetErrno(old_path, kIsFile, true)) {
+bool File::Copy(Namespace* namespc, const char* old_path, const char* new_path) {
+ if (!CheckTypeAndSetErrno(namespc, old_path, kIsFile, true)) {
return false;
}
+ NamespaceScope oldns(namespc, old_path);
struct stat64 st;
- if (NO_RETRY_EXPECTED(stat64(old_path, &st)) != 0) {
+ if (NO_RETRY_EXPECTED(fstatat64(oldns.fd(), oldns.path(), &st, 0)) != 0) {
return false;
}
- int old_fd = NO_RETRY_EXPECTED(open64(old_path, O_RDONLY | O_CLOEXEC));
+ const int old_fd =
+ NO_RETRY_EXPECTED(openat64(oldns.fd(), oldns.path(), O_RDONLY | O_CLOEXEC));
if (old_fd < 0) {
return false;
}
- int new_fd = NO_RETRY_EXPECTED(
- open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode));
+ NamespaceScope newns(namespc, new_path);
+ const int new_fd = NO_RETRY_EXPECTED(openat64(
+ newns.fd(), newns.path(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode));
if (new_fd < 0) {
VOID_TEMP_FAILURE_RETRY(close(old_fd));
return false;
@@ -350,15 +375,16 @@ bool File::Copy(const char* old_path, const char* new_path) {
FDUtils::SaveErrorAndClose(new_fd);
if (result < 0) {
int e = errno;
- VOID_NO_RETRY_EXPECTED(unlink(new_path));
+ VOID_NO_RETRY_EXPECTED(unlinkat(newns.fd(), newns.path(), 0));
errno = e;
return false;
}
return true;
}
-static bool StatHelper(const char* name, struct stat64* st) {
- if (NO_RETRY_EXPECTED(stat64(name, st)) != 0) {
+static bool StatHelper(Namespace* namespc, const char* name, struct stat64* st) {
+ NamespaceScope ns(namespc, name);
+ if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), st, 0)) != 0) {
return false;
}
// Signal an error if it's a directory.
@@ -370,17 +396,29 @@ static bool StatHelper(const char* name, struct stat64* st) {
return true;
}
-int64_t File::LengthFromPath(const char* name) {
+int64_t File::LengthFromPath(Namespace* namespc, const char* name) {
struct stat64 st;
- if (!StatHelper(name, &st)) {
+ if (!StatHelper(namespc, name, &st)) {
return -1;
}
return st.st_size;
}
-void File::Stat(const char* name, int64_t* data) {
- struct stat st;
- if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) {
+static int64_t TimespecToMilliseconds(const struct timespec& t) {
+ return static_cast<int64_t>(t.tv_sec) * 1000L +
+ static_cast<int64_t>(t.tv_nsec) / 1000000L;
+}
+
+static void MillisecondsToTimespec(int64_t millis, struct timespec* t) {
+ ASSERT(t != NULL);
+ t->tv_sec = millis / kMillisecondsPerSecond;
+ t->tv_nsec = (millis - (t->tv_nsec * kMillisecondsPerSecond)) * 1000L;
+}
+
+void File::Stat(Namespace* namespc, const char* name, int64_t* data) {
+ NamespaceScope ns(namespc, name);
+ struct stat64 st;
+ if (TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
if (S_ISREG(st.st_mode)) {
data[kType] = kIsFile;
} else if (S_ISDIR(st.st_mode)) {
@@ -390,9 +428,9 @@ void File::Stat(const char* name, int64_t* data) {
} else {
data[kType] = kDoesNotExist;
}
- data[kCreatedTime] = static_cast<int64_t>(st.st_ctime) * 1000;
- data[kModifiedTime] = static_cast<int64_t>(st.st_mtime) * 1000;
- data[kAccessedTime] = static_cast<int64_t>(st.st_atime) * 1000;
+ data[kCreatedTime] = TimespecToMilliseconds(st.st_ctim);
+ data[kModifiedTime] = TimespecToMilliseconds(st.st_mtim);
+ data[kAccessedTime] = TimespecToMilliseconds(st.st_atim);
data[kMode] = st.st_mode;
data[kSize] = st.st_size;
} else {
@@ -400,66 +438,79 @@ void File::Stat(const char* name, int64_t* data) {
}
}
-time_t File::LastModified(const char* name) {
- struct stat st;
- if (!StatHelper(name, &st)) {
+time_t File::LastModified(Namespace* namespc, const char* name) {
+ struct stat64 st;
+ if (!StatHelper(namespc, name, &st)) {
return -1;
}
return st.st_mtime;
}
-time_t File::LastAccessed(const char* name) {
- struct stat st;
- if (!StatHelper(name, &st)) {
+time_t File::LastAccessed(Namespace* namespc, const char* name) {
+ struct stat64 st;
+ if (!StatHelper(namespc, name, &st)) {
return -1;
}
return st.st_atime;
}
-bool File::SetLastAccessed(const char* name, int64_t millis) {
+bool File::SetLastAccessed(
+ Namespace* namespc, const char* name, int64_t millis) {
// First get the current times.
- struct stat st;
- if (!StatHelper(name, &st)) {
+ struct stat64 st;
+ if (!StatHelper(namespc, name, &st)) {
return false;
}
// Set the new time:
- struct utimbuf times;
- times.actime = millis / kMillisecondsPerSecond;
- times.modtime = st.st_mtime;
- return utime(name, &times) == 0;
+ NamespaceScope ns(namespc, name);
+ struct timespec times[2];
+ MillisecondsToTimespec(millis, &times[0]);
+ times[1] = st.st_mtim;
+ return utimensat(ns.fd(), ns.path(), times, 0) == 0;
}
-bool File::SetLastModified(const char* name, int64_t millis) {
+bool File::SetLastModified(
+ Namespace* namespc, const char* name, int64_t millis) {
// First get the current times.
- struct stat st;
- if (!StatHelper(name, &st)) {
+ struct stat64 st;
+ if (!StatHelper(namespc, name, &st)) {
return false;
}
// Set the new time:
- struct utimbuf times;
- times.actime = st.st_atime;
- times.modtime = millis / kMillisecondsPerSecond;
- return utime(name, &times) == 0;
-}
-
-const char* File::LinkTarget(const char* pathname) {
- struct stat link_stats;
- if (lstat(pathname, &link_stats) != 0) {
+ NamespaceScope ns(namespc, name);
+ struct timespec times[2];
+ times[0] = st.st_atim;
+ MillisecondsToTimespec(millis, &times[1]);
+ return utimensat(ns.fd(), ns.path(), times, 0) == 0;
+}
+
+const char* File::LinkTarget(Namespace* namespc, const char* name) {
+ NamespaceScope ns(namespc, name);
+ struct stat64 link_stats;
+ const int status = TEMP_FAILURE_RETRY(fstatat64(
+ ns.fd(), ns.path(), &link_stats, AT_SYMLINK_NOFOLLOW));
+ if (status != 0) {
return NULL;
}
if (!S_ISLNK(link_stats.st_mode)) {
errno = ENOENT;
return NULL;
}
- size_t target_size = link_stats.st_size;
- char* target_name = DartUtils::ScopedCString(target_size + 1);
- ASSERT(target_name != NULL);
- size_t read_size = readlink(pathname, target_name, target_size + 1);
- if (read_size != target_size) {
+ // Don't rely on the link_stats.st_size for the size of the link
+ // target. For some filesystems, e.g. procfs, this value is always
+ // 0. Also the link might have changed before the readlink call.
+ const int kBufferSize = PATH_MAX + 1;
+ char target[kBufferSize];
+ const int target_size =
+ TEMP_FAILURE_RETRY(readlinkat(ns.fd(), ns.path(), target, kBufferSize));
+ if (target_size <= 0) {
return NULL;
}
+ char* target_name = DartUtils::ScopedCString(target_size + 1);
+ ASSERT(target_name != NULL);
+ memmove(target_name, target, target_size);
target_name[target_size] = '\0';
return target_name;
}
@@ -468,15 +519,24 @@ bool File::IsAbsolutePath(const char* pathname) {
return ((pathname != NULL) && (pathname[0] == '/'));
}
-const char* File::GetCanonicalPath(const char* pathname) {
- char* abs_path = NULL;
- if (pathname != NULL) {
- char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1);
- ASSERT(resolved_path != NULL);
- abs_path = realpath(pathname, resolved_path);
- ASSERT((abs_path == NULL) || IsAbsolutePath(abs_path));
- ASSERT((abs_path == NULL) || (abs_path == resolved_path));
+const char* File::GetCanonicalPath(Namespace* namespc, const char* name) {
+ if (name == NULL) {
+ return NULL;
}
+ if (!Namespace::IsDefault(namespc)) {
+ // TODO(zra): There is no realpathat(). Also chasing a symlink might result
+ // in a path to something outside of the namespace, so canonicalizing paths
+ // would have to be done carefully. For now, don't do anything.
+ return name;
+ }
+ char* abs_path;
+ char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1);
+ ASSERT(resolved_path != NULL);
+ do {
+ abs_path = realpath(name, resolved_path);
+ } while ((abs_path == NULL) && (errno == EINTR));
+ ASSERT(abs_path == NULL || IsAbsolutePath(abs_path));
+ ASSERT(abs_path == NULL || (abs_path == resolved_path));
return abs_path;
}
@@ -490,8 +550,8 @@ const char* File::StringEscapedPathSeparator() {
File::StdioHandleType File::GetStdioHandleType(int fd) {
ASSERT((0 <= fd) && (fd <= 2));
- struct stat buf;
- int result = fstat(fd, &buf);
+ struct stat64 buf;
+ int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
if (result == -1) {
return kOther;
}
@@ -510,11 +570,20 @@ File::StdioHandleType File::GetStdioHandleType(int fd) {
return kOther;
}
-File::Identical File::AreIdentical(const char* file_1, const char* file_2) {
- struct stat file_1_info;
- struct stat file_2_info;
- if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) ||
- (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) {
+File::Identical File::AreIdentical(
+ Namespace* namespc, const char* file_1, const char* file_2) {
+ NamespaceScope ns1(namespc, file_1);
+ NamespaceScope ns2(namespc, file_2);
+ struct stat64 file_1_info;
+ struct stat64 file_2_info;
+ int status = TEMP_FAILURE_RETRY(fstatat64(
+ ns1.fd(), ns1.path(), &file_1_info, AT_SYMLINK_NOFOLLOW));
+ if (status == -1) {
+ return File::kError;
+ }
+ status = TEMP_FAILURE_RETRY(fstatat64(
+ ns2.fd(), ns2.path(), &file_2_info, AT_SYMLINK_NOFOLLOW));
+ if (status == -1) {
return File::kError;
}
return ((file_1_info.st_ino == file_2_info.st_ino) &&
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698