Flutter Engine
The Flutter Engine
namespace_fuchsia.cc
Go to the documentation of this file.
1// Copyright (c) 2017, 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(DART_HOST_OS_FUCHSIA)
7
8#include "bin/namespace.h"
10
11#include <errno.h>
12#include <fcntl.h>
13#include <lib/fdio/namespace.h>
14#include <zircon/status.h>
15
16#include "bin/file.h"
19
20namespace dart {
21namespace bin {
22
23NamespaceImpl::NamespaceImpl(fdio_ns_t* fdio_ns)
24 : fdio_ns_(fdio_ns), cwd_(strdup("/")) {
25 rootfd_ = fdio_ns_opendir(fdio_ns);
26 if (rootfd_ < 0) {
27 FATAL("Failed to open file descriptor for namespace: errno=%d: %s", errno,
28 strerror(errno));
29 }
30 cwdfd_ = dup(rootfd_);
31 if (cwdfd_ < 0) {
32 FATAL("Failed to dup() namespace file descriptor: errno=%d: %s", errno,
33 strerror(errno));
34 }
35}
36
37NamespaceImpl::NamespaceImpl(const char* path)
38 : fdio_ns_(nullptr), cwd_(strdup("/")) {
39 rootfd_ = TEMP_FAILURE_RETRY(open(path, O_DIRECTORY));
40 if (rootfd_ < 0) {
41 FATAL("Failed to open file descriptor for namespace: errno=%d: %s", errno,
42 strerror(errno));
43 }
44 cwdfd_ = dup(rootfd_);
45 if (cwdfd_ < 0) {
46 FATAL("Failed to dup() namespace file descriptor: errno=%d: %s", errno,
47 strerror(errno));
48 }
49}
50
51NamespaceImpl::~NamespaceImpl() {
52 NO_RETRY_EXPECTED(close(rootfd_));
53 free(cwd_);
54 NO_RETRY_EXPECTED(close(cwdfd_));
55 if (fdio_ns_ != nullptr) {
56 zx_status_t status = fdio_ns_destroy(fdio_ns_);
57 if (status != ZX_OK) {
58 Syslog::PrintErr("fdio_ns_destroy: %s\n", zx_status_get_string(status));
59 }
60 }
61}
62
63bool NamespaceImpl::SetCwd(Namespace* namespc, const char* new_path) {
64 NamespaceScope ns(namespc, new_path);
65 const intptr_t new_cwdfd =
66 TEMP_FAILURE_RETRY(openat(ns.fd(), ns.path(), O_DIRECTORY));
67 if (new_cwdfd < 0) {
68 return false;
69 }
70
71 // Build the new cwd.
72 TextBuffer tbuf(PATH_MAX);
73 if (!File::IsAbsolutePath(new_path)) {
74 tbuf.AddString(cwd_);
75 }
76 tbuf.AddString(File::PathSeparator());
77 tbuf.AddString(ns.path());
78
79 // Normalize it.
80 char result[PATH_MAX];
81 const intptr_t result_len =
82 File::CleanUnixPath(tbuf.buffer(), result, PATH_MAX);
83 if (result_len < 0) {
84 errno = ENAMETOOLONG;
85 return false;
86 }
87
88 free(cwd_);
89 cwd_ = strdup(result);
90 close(cwdfd_);
91 cwdfd_ = new_cwdfd;
92 return true;
93}
94
95Namespace* Namespace::Create(intptr_t namespc) {
96 NamespaceImpl* namespc_impl = nullptr;
97 if (namespc != kNone) {
98 namespc_impl = new NamespaceImpl(reinterpret_cast<fdio_ns_t*>(namespc));
99 }
100 return new Namespace(namespc_impl);
101}
102
103Namespace* Namespace::Create(const char* path) {
104 return new Namespace(new NamespaceImpl(path));
105}
106
107Namespace::~Namespace() {
108 delete namespc_;
109}
110
111intptr_t Namespace::Default() {
112 return kNone;
113}
114
115const char* Namespace::GetCurrent(Namespace* namespc) {
116 if (Namespace::IsDefault(namespc)) {
117 char buffer[PATH_MAX];
118 if (getcwd(buffer, PATH_MAX) == nullptr) {
119 return nullptr;
120 }
121 return DartUtils::ScopedCopyCString(buffer);
122 }
123 return namespc->namespc()->cwd();
124}
125
126bool Namespace::SetCurrent(Namespace* namespc, const char* path) {
127 if (Namespace::IsDefault(namespc)) {
128 return (NO_RETRY_EXPECTED(chdir(path)) == 0);
129 }
130 return namespc->namespc()->SetCwd(namespc, path);
131}
132
133void Namespace::ResolvePath(Namespace* namespc,
134 const char* path,
135 intptr_t* dirfd,
136 const char** resolved_path) {
137 ASSERT(dirfd != nullptr);
138 ASSERT(resolved_path != nullptr);
139 if (Namespace::IsDefault(namespc)) {
140 *dirfd = AT_FDCWD;
141 *resolved_path = path;
142 return;
143 }
145 *dirfd = namespc->namespc()->rootfd();
146 if (strcmp(path, File::PathSeparator()) == 0) {
147 // Change "/" to ".".
148 *resolved_path = ".";
149 } else {
150 // Otherwise strip off the leading "/".
151 *resolved_path = &path[1];
152 }
153 } else {
154 *dirfd = namespc->namespc()->cwdfd();
155 *resolved_path = path;
156 }
157}
158
159NamespaceScope::NamespaceScope(Namespace* namespc, const char* path) {
160 Namespace::ResolvePath(namespc, path, &fd_, &path_);
161}
162
163NamespaceScope::~NamespaceScope() {}
164
165} // namespace bin
166} // namespace dart
167
168#endif // defined(DART_HOST_OS_FUCHSIA)
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
NamespaceImpl(fdio_ns_t *fdio_ns)
#define ASSERT(E)
#define FATAL(error)
GAsyncResult * result
Definition: dart_vm.cc:33
@ kNone
Definition: layer.h:53
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
static bool IsAbsolutePath(const char *path)
Definition: file_win.cc:37
char * strdup(const char *str1)
#define PATH_MAX
Definition: globals.h:708
#define NO_RETRY_EXPECTED(expression)
#define TEMP_FAILURE_RETRY(expression)