Chromium Code Reviews| Index: runtime/bin/file_linux.cc |
| diff --git a/runtime/bin/file_linux.cc b/runtime/bin/file_linux.cc |
| index 7e966ce2640587e9dc620e1ba797df0cbccb8f92..70831d3c3b8cf09f963c33b9cfb790d2d526300e 100644 |
| --- a/runtime/bin/file_linux.cc |
| +++ b/runtime/bin/file_linux.cc |
| @@ -20,6 +20,7 @@ |
| #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" |
| @@ -40,8 +41,8 @@ class FileHandle { |
| }; |
| File::~File() { |
| - if (!IsClosed() && handle_->fd() != STDOUT_FILENO && |
| - handle_->fd() != STDERR_FILENO) { |
| + if (!IsClosed() && (handle_->fd() != STDOUT_FILENO) && |
| + (handle_->fd() != STDERR_FILENO)) { |
| Close(); |
| } |
| delete handle_; |
| @@ -196,10 +197,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 stat64 st; |
| - if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { |
| + if (TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) { |
| // Only accept regular files, character devices, and pipes. |
| if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) { |
| errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; |
| @@ -219,7 +221,7 @@ File* File::Open(const char* name, FileOpenMode mode) { |
| flags = flags | O_TRUNC; |
| } |
| flags |= O_CLOEXEC; |
| - int fd = TEMP_FAILURE_RETRY(open64(name, flags, 0666)); |
| + const int fd = TEMP_FAILURE_RETRY(openat64(ns.fd(), ns.path(), flags, 0666)); |
| if (fd < 0) { |
| return NULL; |
| } |
| @@ -237,9 +239,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 (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { |
| + if (TEMP_FAILURE_RETRY(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 { |
| @@ -247,9 +250,10 @@ bool File::Exists(const char* name) { |
| } |
| } |
| -bool File::Create(const char* name) { |
| - int fd = |
| - TEMP_FAILURE_RETRY(open64(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); |
| +bool File::Create(Namespace* namespc, const char* name) { |
| + NamespaceScope ns(namespc, name); |
| + const int fd = TEMP_FAILURE_RETRY( |
| + openat64(ns.fd(), ns.path(), O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); |
| if (fd < 0) { |
| return false; |
| } |
| @@ -271,17 +275,25 @@ 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) { |
| +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 = TEMP_FAILURE_RETRY(stat64(pathname, &entry_info)); |
| + stat_success = |
| + TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0)); |
| } else { |
| - stat_success = TEMP_FAILURE_RETRY(lstat64(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; |
| @@ -298,10 +310,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; |
| } |
| @@ -319,40 +332,58 @@ 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 (TEMP_FAILURE_RETRY(stat64(old_path, &st)) != 0) { |
| + if (TEMP_FAILURE_RETRY(fstatat64(oldns.fd(), oldns.path(), &st, 0)) != 0) { |
| return false; |
| } |
| - int old_fd = TEMP_FAILURE_RETRY(open64(old_path, O_RDONLY | O_CLOEXEC)); |
| + const int old_fd = TEMP_FAILURE_RETRY( |
| + openat64(oldns.fd(), oldns.path(), O_RDONLY | O_CLOEXEC)); |
| if (old_fd < 0) { |
| return false; |
| } |
| - int new_fd = TEMP_FAILURE_RETRY( |
| - open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode)); |
| + NamespaceScope newns(namespc, new_path); |
| + const int new_fd = TEMP_FAILURE_RETRY( |
| + 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; |
| @@ -382,15 +413,18 @@ bool File::Copy(const char* old_path, const char* new_path) { |
| VOID_TEMP_FAILURE_RETRY(close(old_fd)); |
| VOID_TEMP_FAILURE_RETRY(close(new_fd)); |
| if (result < 0) { |
| - 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 (TEMP_FAILURE_RETRY(stat64(name, st)) != 0) { |
| +static bool StatHelper(Namespace* namespc, |
| + const char* name, |
| + struct stat64* st) { |
| + NamespaceScope ns(namespc, name); |
| + if (TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), st, 0)) != 0) { |
| return false; |
| } |
| // Signal an error if it's a directory. |
| @@ -402,9 +436,9 @@ 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; |
| @@ -415,9 +449,16 @@ static int64_t TimespecToMilliseconds(const struct timespec& t) { |
| static_cast<int64_t>(t.tv_nsec) / 1000000L; |
| } |
| -void File::Stat(const char* name, int64_t* data) { |
| +static void MillisecondsToTimespec(int64_t millis, struct timespec* t) { |
| + ASSERT(t != NULL); |
| + t->tv_sec = millis / kMillisecondsPerSecond; |
| + t->tv_nsec = (millis - (t->tv_sec * kMillisecondsPerSecond)) * 1000L; |
|
zra
2017/08/29 20:22:52
Test failure was caused by a typo here. I had t->t
|
| +} |
| + |
| +void File::Stat(Namespace* namespc, const char* name, int64_t* data) { |
| + NamespaceScope ns(namespc, name); |
| struct stat64 st; |
| - if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { |
| + 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)) { |
| @@ -437,53 +478,62 @@ void File::Stat(const char* name, int64_t* data) { |
| } |
| } |
| -time_t File::LastModified(const char* name) { |
| +time_t File::LastModified(Namespace* namespc, const char* name) { |
| struct stat64 st; |
| - if (!StatHelper(name, &st)) { |
| + if (!StatHelper(namespc, name, &st)) { |
| return -1; |
| } |
| return st.st_mtime; |
| } |
| -time_t File::LastAccessed(const char* name) { |
| +time_t File::LastAccessed(Namespace* namespc, const char* name) { |
| struct stat64 st; |
| - if (!StatHelper(name, &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 stat64 st; |
| - if (!StatHelper(name, &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, ×) == 0; |
| + NamespaceScope ns(namespc, name); |
| + struct timespec times[2]; |
| + MillisecondsToTimespec(millis, ×[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 stat64 st; |
| - if (!StatHelper(name, &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, ×) == 0; |
| + NamespaceScope ns(namespc, name); |
| + struct timespec times[2]; |
| + times[0] = st.st_atim; |
| + MillisecondsToTimespec(millis, ×[1]); |
| + return utimensat(ns.fd(), ns.path(), times, 0) == 0; |
| } |
| -const char* File::LinkTarget(const char* pathname) { |
| +const char* File::LinkTarget(Namespace* namespc, const char* name) { |
| + NamespaceScope ns(namespc, name); |
| struct stat64 link_stats; |
| - if (TEMP_FAILURE_RETRY(lstat64(pathname, &link_stats)) != 0) { |
| + 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)) { |
| @@ -495,8 +545,8 @@ const char* File::LinkTarget(const char* pathname) { |
| // 0. Also the link might have changed before the readlink call. |
| const int kBufferSize = PATH_MAX + 1; |
| char target[kBufferSize]; |
| - size_t target_size = |
| - TEMP_FAILURE_RETRY(readlink(pathname, target, kBufferSize)); |
| + const int target_size = |
| + TEMP_FAILURE_RETRY(readlinkat(ns.fd(), ns.path(), target, kBufferSize)); |
| if (target_size <= 0) { |
| return NULL; |
| } |
| @@ -508,20 +558,55 @@ const char* File::LinkTarget(const char* pathname) { |
| } |
| bool File::IsAbsolutePath(const char* pathname) { |
| - return (pathname != NULL && pathname[0] == '/'); |
| + 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); |
| - do { |
| - abs_path = realpath(pathname, resolved_path); |
| - } while (abs_path == NULL && errno == EINTR); |
| - ASSERT(abs_path == NULL || IsAbsolutePath(abs_path)); |
| - ASSERT(abs_path == NULL || (abs_path == resolved_path)); |
| +const char* File::ReadLink(const char* pathname) { |
| + ASSERT(pathname != NULL); |
| + ASSERT(IsAbsolutePath(pathname)); |
| + struct stat64 link_stats; |
| + if (TEMP_FAILURE_RETRY(lstat64(pathname, &link_stats)) != 0) { |
| + return NULL; |
| } |
| + if (!S_ISLNK(link_stats.st_mode)) { |
| + errno = ENOENT; |
| + return NULL; |
| + } |
| + // 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]; |
| + size_t target_size = |
| + TEMP_FAILURE_RETRY(readlink(pathname, 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; |
| +} |
| + |
| +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; |
| } |
| @@ -555,11 +640,21 @@ File::StdioHandleType File::GetStdioHandleType(int fd) { |
| return kOther; |
| } |
| -File::Identical File::AreIdentical(const char* file_1, const char* file_2) { |
| +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; |
| - if ((TEMP_FAILURE_RETRY(lstat64(file_1, &file_1_info)) == -1) || |
| - (TEMP_FAILURE_RETRY(lstat64(file_2, &file_2_info)) == -1)) { |
| + 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) && |