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

Unified Diff: runtime/bin/directory_linux.cc

Issue 3007703002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Remove namespace_unsupported.cc 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/directory_fuchsia.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/directory_linux.cc
diff --git a/runtime/bin/directory_linux.cc b/runtime/bin/directory_linux.cc
index 6e50322f564bbeb0b125c8f7aab84a80cb7c36c1..357bbb3141abd740f100cf406a33831da1ced5da 100644
--- a/runtime/bin/directory_linux.cc
+++ b/runtime/bin/directory_linux.cc
@@ -9,14 +9,18 @@
#include <dirent.h> // NOLINT
#include <errno.h> // NOLINT
+#include <fcntl.h> // NOLINT
#include <stdlib.h> // NOLINT
#include <string.h> // NOLINT
#include <sys/param.h> // NOLINT
#include <sys/stat.h> // NOLINT
#include <unistd.h> // NOLINT
+#include "bin/crypto.h"
#include "bin/dartutils.h"
+#include "bin/fdutils.h"
#include "bin/file.h"
+#include "bin/namespace.h"
#include "bin/platform.h"
#include "platform/signal_blocker.h"
@@ -81,12 +85,22 @@ ListType DirectoryListingEntry::Next(DirectoryListing* listing) {
return kListDone;
}
+ if (fd_ == -1) {
+ ASSERT(lister_ == 0);
+ NamespaceScope ns(listing->namespc(), listing->path_buffer().AsString());
+ const int listingfd =
+ TEMP_FAILURE_RETRY(openat64(ns.fd(), ns.path(), O_DIRECTORY));
+ if (listingfd < 0) {
+ done_ = true;
+ return kListError;
+ }
+ fd_ = listingfd;
+ }
+
if (lister_ == 0) {
do {
- lister_ = reinterpret_cast<intptr_t>(
- opendir(listing->path_buffer().AsString()));
+ lister_ = reinterpret_cast<intptr_t>(fdopendir(fd_));
} while ((lister_ == 0) && (errno == EINTR));
-
if (lister_ == 0) {
done_ = true;
return kListError;
@@ -135,10 +149,12 @@ ListType DirectoryListingEntry::Next(DirectoryListing* listing) {
// readdir. For those and for links we use stat to determine
// the actual entry type. Notice that stat returns the type of
// the file pointed to.
+ NamespaceScope ns(listing->namespc(),
+ listing->path_buffer().AsString());
struct stat64 entry_info;
int stat_success;
stat_success = TEMP_FAILURE_RETRY(
- lstat64(listing->path_buffer().AsString(), &entry_info));
+ fstatat64(ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW));
if (stat_success == -1) {
return kListError;
}
@@ -154,8 +170,8 @@ ListType DirectoryListingEntry::Next(DirectoryListing* listing) {
}
previous = previous->next;
}
- stat_success = TEMP_FAILURE_RETRY(
- stat64(listing->path_buffer().AsString(), &entry_info));
+ stat_success =
+ TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0));
if (stat_success == -1) {
// Report a broken link as a link, even if follow_links is true.
return kListLink;
@@ -208,6 +224,7 @@ ListType DirectoryListingEntry::Next(DirectoryListing* listing) {
DirectoryListingEntry::~DirectoryListingEntry() {
ResetLink();
if (lister_ != 0) {
+ // This also closes fd_.
VOID_NO_RETRY_EXPECTED(closedir(reinterpret_cast<DIR*>(lister_)));
}
}
@@ -222,28 +239,29 @@ void DirectoryListingEntry::ResetLink() {
}
}
-static bool DeleteRecursively(PathBuffer* path);
+static bool DeleteRecursively(int dirfd, PathBuffer* path);
-static bool DeleteFile(char* file_name, PathBuffer* path) {
+static bool DeleteFile(int dirfd, char* file_name, PathBuffer* path) {
return path->Add(file_name) &&
- (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0);
+ (NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), 0)) == 0);
}
-static bool DeleteDir(char* dir_name, PathBuffer* path) {
+static bool DeleteDir(int dirfd, char* dir_name, PathBuffer* path) {
if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) {
return true;
}
- return path->Add(dir_name) && DeleteRecursively(path);
+ return path->Add(dir_name) && DeleteRecursively(dirfd, path);
}
-static bool DeleteRecursively(PathBuffer* path) {
+static bool DeleteRecursively(int dirfd, PathBuffer* path) {
// Do not recurse into links for deletion. Instead delete the link.
// If it's a file, delete it.
struct stat64 st;
- if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &st)) == -1) {
+ if (TEMP_FAILURE_RETRY(
+ fstatat64(dirfd, path->AsString(), &st, AT_SYMLINK_NOFOLLOW)) == -1) {
return false;
} else if (!S_ISDIR(st.st_mode)) {
- return (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0);
+ return (NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), 0)) == 0);
}
if (!path->Add(File::PathSeparator())) {
@@ -252,11 +270,17 @@ static bool DeleteRecursively(PathBuffer* path) {
// Not a link. Attempt to open as a directory and recurse into the
// directory.
+ const int fd =
+ TEMP_FAILURE_RETRY(openat64(dirfd, path->AsString(), O_DIRECTORY));
+ if (fd < 0) {
+ return false;
+ }
DIR* dir_pointer;
do {
- dir_pointer = opendir(path->AsString());
+ dir_pointer = fdopendir(fd);
} while ((dir_pointer == NULL) && (errno == EINTR));
if (dir_pointer == NULL) {
+ FDUtils::SaveErrorAndClose(fd);
return false;
}
@@ -278,13 +302,19 @@ static bool DeleteRecursively(PathBuffer* path) {
break;
}
// End of directory.
- return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) &&
- (NO_RETRY_EXPECTED(remove(path->AsString())) == 0);
+ int status = NO_RETRY_EXPECTED(closedir(dir_pointer));
+ FDUtils::SaveErrorAndClose(fd);
+ if (status != 0) {
+ return false;
+ }
+ status =
+ NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), AT_REMOVEDIR));
+ return status == 0;
}
bool ok = false;
switch (entry->d_type) {
case DT_DIR:
- ok = DeleteDir(entry->d_name, path);
+ ok = DeleteDir(dirfd, entry->d_name, path);
break;
case DT_BLK:
case DT_CHR:
@@ -295,7 +325,7 @@ static bool DeleteRecursively(PathBuffer* path) {
// Treat all links as files. This will delete the link which
// is what we want no matter if the link target is a file or a
// directory.
- ok = DeleteFile(entry->d_name, path);
+ ok = DeleteFile(dirfd, entry->d_name, path);
break;
case DT_UNKNOWN: {
if (!path->Add(entry->d_name)) {
@@ -305,17 +335,18 @@ static bool DeleteRecursively(PathBuffer* path) {
// readdir. For those we use lstat to determine the entry
// type.
struct stat64 entry_info;
- if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &entry_info)) == -1) {
+ if (TEMP_FAILURE_RETRY(fstatat64(dirfd, path->AsString(), &entry_info,
+ AT_SYMLINK_NOFOLLOW)) == -1) {
break;
}
path->Reset(path_length);
if (S_ISDIR(entry_info.st_mode)) {
- ok = DeleteDir(entry->d_name, path);
+ ok = DeleteDir(dirfd, entry->d_name, path);
} else {
// Treat links as files. This will delete the link which is
// what we want no matter if the link target is a file or a
// directory.
- ok = DeleteFile(entry->d_name, path);
+ ok = DeleteFile(dirfd, entry->d_name, path);
}
break;
}
@@ -333,13 +364,17 @@ static bool DeleteRecursively(PathBuffer* path) {
ASSERT(errno != 0);
int err = errno;
VOID_NO_RETRY_EXPECTED(closedir(dir_pointer));
+ FDUtils::SaveErrorAndClose(fd);
errno = err;
return false;
}
-Directory::ExistsResult Directory::Exists(const char* dir_name) {
+Directory::ExistsResult Directory::Exists(Namespace* namespc,
+ const char* dir_name) {
+ NamespaceScope ns(namespc, dir_name);
struct stat64 entry_info;
- int success = TEMP_FAILURE_RETRY(stat64(dir_name, &entry_info));
+ int success =
+ TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0));
if (success == 0) {
if (S_ISDIR(entry_info.st_mode)) {
return EXISTS;
@@ -367,30 +402,19 @@ char* Directory::CurrentNoScope() {
return getcwd(NULL, 0);
}
-const char* Directory::Current() {
- char buffer[PATH_MAX];
- if (getcwd(buffer, PATH_MAX) == NULL) {
- return NULL;
- }
- return DartUtils::ScopedCopyCString(buffer);
-}
-
-bool Directory::SetCurrent(const char* path) {
- return (NO_RETRY_EXPECTED(chdir(path)) == 0);
-}
-
-bool Directory::Create(const char* dir_name) {
+bool Directory::Create(Namespace* namespc, const char* dir_name) {
+ NamespaceScope ns(namespc, dir_name);
// Create the directory with the permissions specified by the
// process umask.
- int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777));
+ const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777));
// If the directory already exists, treat it as a success.
if ((result == -1) && (errno == EEXIST)) {
- return (Exists(dir_name) == EXISTS);
+ return (Exists(namespc, dir_name) == EXISTS);
}
return (result == 0);
}
-const char* Directory::SystemTemp() {
+const char* Directory::SystemTemp(Namespace* namespc) {
PathBuffer path;
const char* temp_dir = getenv("TMPDIR");
if (temp_dir == NULL) {
@@ -399,7 +423,8 @@ const char* Directory::SystemTemp() {
if (temp_dir == NULL) {
temp_dir = "/tmp";
}
- if (!path.Add(temp_dir)) {
+ NamespaceScope ns(namespc, temp_dir);
+ if (!path.Add(ns.path())) {
return NULL;
}
@@ -412,51 +437,72 @@ const char* Directory::SystemTemp() {
return path.AsScopedString();
}
-const char* Directory::CreateTemp(const char* prefix) {
- // Returns a new, unused directory name, adding characters to the end
- // of prefix. Creates the directory with the permissions specified
- // by the process umask.
- // The return value is Dart_ScopeAllocated.
+// Returns a new, unused directory name, adding characters to the end
+// of prefix. Creates the directory with the permissions specified
+// by the process umask.
+// The return value is Dart_ScopeAllocated.
+const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) {
PathBuffer path;
+ const int firstchar = 'A';
+ const int numchars = 'Z' - 'A' + 1;
+ uint8_t random_bytes[7];
+
+ // mkdtemp doesn't have an "at" variant, so we have to simulate it.
if (!path.Add(prefix)) {
return NULL;
}
- if (!path.Add("XXXXXX")) {
- // Pattern has overflowed.
- return NULL;
- }
- char* result;
- do {
- result = mkdtemp(path.AsString());
- } while ((result == NULL) && (errno == EINTR));
- if (result == NULL) {
- return NULL;
+ intptr_t prefix_length = path.length();
+ while (true) {
+ Crypto::GetRandomBytes(6, random_bytes);
+ for (intptr_t i = 0; i < 6; i++) {
+ random_bytes[i] = (random_bytes[i] % numchars) + firstchar;
+ }
+ random_bytes[6] = '\0';
+ if (!path.Add(reinterpret_cast<char*>(random_bytes))) {
+ return NULL;
+ }
+ NamespaceScope ns(namespc, path.AsString());
+ const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777));
+ if (result == 0) {
+ return path.AsScopedString();
+ } else if (errno == EEXIST) {
+ path.Reset(prefix_length);
+ } else {
+ return NULL;
+ }
}
- return path.AsScopedString();
}
-bool Directory::Delete(const char* dir_name, bool recursive) {
+bool Directory::Delete(Namespace* namespc,
+ const char* dir_name,
+ bool recursive) {
+ NamespaceScope ns(namespc, dir_name);
if (!recursive) {
- if ((File::GetType(dir_name, false) == File::kIsLink) &&
- (File::GetType(dir_name, true) == File::kIsDirectory)) {
- return NO_RETRY_EXPECTED(unlink(dir_name)) == 0;
+ if ((File::GetType(namespc, dir_name, false) == File::kIsLink) &&
+ (File::GetType(namespc, dir_name, true) == File::kIsDirectory)) {
+ return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0;
}
- return NO_RETRY_EXPECTED(rmdir(dir_name)) == 0;
+ return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), AT_REMOVEDIR)) == 0;
} else {
PathBuffer path;
- if (!path.Add(dir_name)) {
+ if (!path.Add(ns.path())) {
return false;
}
- return DeleteRecursively(&path);
+ return DeleteRecursively(ns.fd(), &path);
}
}
-bool Directory::Rename(const char* path, const char* new_path) {
- ExistsResult exists = Exists(path);
+bool Directory::Rename(Namespace* namespc,
+ const char* old_path,
+ const char* new_path) {
+ ExistsResult exists = Exists(namespc, old_path);
if (exists != EXISTS) {
return false;
}
- return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0);
+ NamespaceScope oldns(namespc, old_path);
+ NamespaceScope newns(namespc, new_path);
+ return (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(),
+ newns.path())) == 0);
}
} // namespace bin
« no previous file with comments | « runtime/bin/directory_fuchsia.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698