namespace/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! `namespace` defines namespace types and transformations between their common representations.

use cm_types::{IterablePath, NamespacePath};
use fidl::endpoints::ClientEnd;
use thiserror::Error;
use {
    fidl_fuchsia_component as fcomponent, fidl_fuchsia_component_runner as fcrunner,
    fidl_fuchsia_io as fio, fidl_fuchsia_process as fprocess,
};

#[cfg(target_os = "fuchsia")]
use std::sync::Arc;

mod tree;
pub use tree::Tree;

/// The namespace of a component instance.
///
/// Namespaces may be represented as a collection of directory client endpoints and their
/// corresponding unique paths, so called "flat representation". In this case each path
/// must be a valid [`cm_types::NamespacePath`], and no path can be a parent of another path.
///
/// See https://fuchsia.dev/fuchsia-src/concepts/process/namespaces for the definition
/// of namespaces of a process. The namespace of a component largely follows except that
/// some more characters are disallowed (c.f. [`cm_types::NamespacePath`] documentation).
#[derive(Debug)]
pub struct Namespace {
    tree: Tree<ClientEnd<fio::DirectoryMarker>>,
}

#[derive(Error, Debug, Clone)]
pub enum NamespaceError {
    #[error(
        "path `{0}` is the parent or child of another namespace entry. \
        This is not supported."
    )]
    Shadow(NamespacePath),

    #[error("duplicate namespace path `{0}`")]
    Duplicate(NamespacePath),

    #[error("invalid namespace entry `{0}`")]
    EntryError(#[from] EntryError),
}

impl Namespace {
    pub fn new() -> Self {
        Self { tree: Default::default() }
    }

    pub fn add(
        &mut self,
        path: &NamespacePath,
        directory: ClientEnd<fio::DirectoryMarker>,
    ) -> Result<(), NamespaceError> {
        self.tree.add(path, directory)?;
        Ok(())
    }

    pub fn get(&self, path: &NamespacePath) -> Option<&ClientEnd<fio::DirectoryMarker>> {
        self.tree.get(path)
    }

    pub fn remove(&mut self, path: &NamespacePath) -> Option<ClientEnd<fio::DirectoryMarker>> {
        self.tree.remove(path)
    }

    pub fn flatten(self) -> Vec<Entry> {
        self.tree.flatten().into_iter().map(|(path, directory)| Entry { path, directory }).collect()
    }

    /// Get a copy of the paths in the namespace.
    pub fn paths(&self) -> Vec<NamespacePath> {
        self.tree.map_ref(|_| ()).flatten().into_iter().map(|(path, ())| path).collect()
    }
}

impl Default for Namespace {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(target_os = "fuchsia")]
impl Clone for Namespace {
    fn clone(&self) -> Self {
        use fidl::AsHandleRef;

        // TODO(https://fxbug.dev/42083023): The unsafe block can go away if Rust FIDL bindings exposed the
        // feature of calling FIDL methods (e.g. Clone) on a borrowed client endpoint.
        let tree = self.tree.map_ref(|dir| {
            let raw_handle = dir.channel().as_handle_ref().raw_handle();
            // SAFETY: the channel is forgotten at the end of scope so it is not double closed.
            unsafe {
                let borrowed: zx::Channel = zx::Handle::from_raw(raw_handle).into();
                let borrowed = fio::DirectorySynchronousProxy::new(borrowed);
                let (client_end, server_end) =
                    fidl::endpoints::create_endpoints::<fio::DirectoryMarker>();
                let _ = borrowed.clone2(server_end.into_channel().into());
                std::mem::forget(borrowed.into_channel());
                client_end
            }
        });
        Self { tree }
    }
}

impl From<Namespace> for Vec<Entry> {
    fn from(namespace: Namespace) -> Self {
        namespace.flatten()
    }
}

impl From<Namespace> for Vec<fcrunner::ComponentNamespaceEntry> {
    fn from(namespace: Namespace) -> Self {
        namespace.flatten().into_iter().map(Into::into).collect()
    }
}

impl From<Namespace> for Vec<fprocess::NameInfo> {
    fn from(namespace: Namespace) -> Self {
        namespace.flatten().into_iter().map(Into::into).collect()
    }
}

#[cfg(target_os = "fuchsia")]
impl From<Namespace> for Vec<process_builder::NamespaceEntry> {
    fn from(namespace: Namespace) -> Self {
        namespace.flatten().into_iter().map(Into::into).collect()
    }
}

/// Converts the [Namespace] to a vfs [TreeBuilder] with tree nodes for each entry.
///
/// The TreeBuilder can then be used to build a vfs directory for this Namespace.
#[cfg(target_os = "fuchsia")]
impl TryFrom<Namespace> for vfs::tree_builder::TreeBuilder {
    type Error = vfs::tree_builder::Error;

    fn try_from(namespace: Namespace) -> Result<Self, Self::Error> {
        let mut builder = vfs::tree_builder::TreeBuilder::empty_dir();
        for Entry { path, directory } in namespace.flatten().into_iter() {
            let path: Vec<&str> = path.iter_segments().map(|s| s.as_str()).collect();
            builder.add_entry(path, vfs::remote::remote_dir(directory.into_proxy()))?;
        }
        Ok(builder)
    }
}

/// Converts the [Namespace] into a vfs directory.
#[cfg(target_os = "fuchsia")]
impl TryFrom<Namespace> for Arc<vfs::directory::immutable::simple::Simple> {
    type Error = vfs::tree_builder::Error;

    fn try_from(namespace: Namespace) -> Result<Self, Self::Error> {
        let builder: vfs::tree_builder::TreeBuilder = namespace.try_into()?;
        Ok(builder.build())
    }
}

impl From<Tree<ClientEnd<fio::DirectoryMarker>>> for Namespace {
    fn from(tree: Tree<ClientEnd<fio::DirectoryMarker>>) -> Self {
        Self { tree }
    }
}

impl TryFrom<Vec<Entry>> for Namespace {
    type Error = NamespaceError;

    fn try_from(value: Vec<Entry>) -> Result<Self, Self::Error> {
        let mut ns = Namespace::new();
        for entry in value {
            ns.add(&entry.path, entry.directory)?;
        }
        Ok(ns)
    }
}

impl TryFrom<Vec<fcrunner::ComponentNamespaceEntry>> for Namespace {
    type Error = NamespaceError;

    fn try_from(entries: Vec<fcrunner::ComponentNamespaceEntry>) -> Result<Self, Self::Error> {
        let entries: Vec<Entry> =
            entries.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, EntryError>>()?;
        entries.try_into()
    }
}

impl TryFrom<Vec<fcomponent::NamespaceEntry>> for Namespace {
    type Error = NamespaceError;

    fn try_from(entries: Vec<fcomponent::NamespaceEntry>) -> Result<Self, Self::Error> {
        let entries: Vec<Entry> =
            entries.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, EntryError>>()?;
        entries.try_into()
    }
}

impl TryFrom<Vec<fprocess::NameInfo>> for Namespace {
    type Error = NamespaceError;

    fn try_from(entries: Vec<fprocess::NameInfo>) -> Result<Self, Self::Error> {
        let entries: Vec<Entry> =
            entries.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, EntryError>>()?;
        entries.try_into()
    }
}

#[cfg(target_os = "fuchsia")]
impl TryFrom<Vec<process_builder::NamespaceEntry>> for Namespace {
    type Error = NamespaceError;

    fn try_from(entries: Vec<process_builder::NamespaceEntry>) -> Result<Self, Self::Error> {
        let entries: Vec<Entry> =
            entries.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, EntryError>>()?;
        entries.try_into()
    }
}

/// A container for a single namespace entry, containing a path and a directory handle.
#[derive(Eq, Ord, PartialOrd, PartialEq, Debug)]
pub struct Entry {
    /// Namespace path.
    pub path: NamespacePath,

    /// Namespace directory handle.
    pub directory: ClientEnd<fio::DirectoryMarker>,
}

impl From<Entry> for fcrunner::ComponentNamespaceEntry {
    fn from(entry: Entry) -> Self {
        Self {
            path: Some(entry.path.into()),
            directory: Some(entry.directory),
            ..Default::default()
        }
    }
}

impl From<Entry> for fcomponent::NamespaceEntry {
    fn from(entry: Entry) -> Self {
        Self {
            path: Some(entry.path.into()),
            directory: Some(entry.directory),
            ..Default::default()
        }
    }
}

impl From<Entry> for fprocess::NameInfo {
    fn from(entry: Entry) -> Self {
        Self { path: entry.path.into(), directory: entry.directory }
    }
}

#[cfg(target_os = "fuchsia")]
impl From<Entry> for process_builder::NamespaceEntry {
    fn from(entry: Entry) -> Self {
        Self { path: entry.path.into(), directory: entry.directory }
    }
}

#[derive(Debug, Clone, Error)]
pub enum EntryError {
    #[error("path is not set")]
    MissingPath,

    #[error("directory is not set")]
    MissingDirectory,

    #[error("entry type is not supported (must be directory or dictionary")]
    UnsupportedType,

    #[error("path is invalid for a namespace entry: `{0}`")]
    InvalidPath(#[from] cm_types::ParseError),
}

impl TryFrom<fcrunner::ComponentNamespaceEntry> for Entry {
    type Error = EntryError;

    fn try_from(entry: fcrunner::ComponentNamespaceEntry) -> Result<Self, Self::Error> {
        Ok(Self {
            path: entry.path.ok_or_else(|| EntryError::MissingPath)?.parse()?,
            directory: entry.directory.ok_or_else(|| EntryError::MissingDirectory)?,
        })
    }
}

impl TryFrom<fcomponent::NamespaceEntry> for Entry {
    type Error = EntryError;

    fn try_from(entry: fcomponent::NamespaceEntry) -> Result<Self, Self::Error> {
        Ok(Self {
            path: entry.path.ok_or_else(|| EntryError::MissingPath)?.parse()?,
            directory: entry.directory.ok_or_else(|| EntryError::MissingDirectory)?,
        })
    }
}

impl TryFrom<fprocess::NameInfo> for Entry {
    type Error = EntryError;

    fn try_from(entry: fprocess::NameInfo) -> Result<Self, Self::Error> {
        Ok(Self { path: entry.path.parse()?, directory: entry.directory })
    }
}

#[cfg(target_os = "fuchsia")]
impl TryFrom<process_builder::NamespaceEntry> for Entry {
    type Error = EntryError;

    fn try_from(entry: process_builder::NamespaceEntry) -> Result<Self, Self::Error> {
        Ok(Self { path: entry.path.try_into()?, directory: entry.directory })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use fuchsia_async as fasync;
    use zx::{AsHandleRef, Peered};

    fn ns_path(str: &str) -> NamespacePath {
        str.parse().unwrap()
    }

    #[test]
    fn test_try_from_namespace() {
        {
            let (client_end_1, _) = fidl::endpoints::create_endpoints();
            let (client_end_2, _) = fidl::endpoints::create_endpoints();
            let entries = vec![
                Entry { path: ns_path("/foo"), directory: client_end_1 },
                Entry { path: ns_path("/foo/bar"), directory: client_end_2 },
            ];
            assert_matches!(
                Namespace::try_from(entries),
                Err(NamespaceError::Shadow(path)) if path.to_string() == "/foo/bar"
            );
        }
        {
            let (client_end_1, _) = fidl::endpoints::create_endpoints();
            let (client_end_2, _) = fidl::endpoints::create_endpoints();
            let entries = vec![
                Entry { path: ns_path("/foo"), directory: client_end_1 },
                Entry { path: ns_path("/foo"), directory: client_end_2 },
            ];
            assert_matches!(
                Namespace::try_from(entries),
                Err(NamespaceError::Duplicate(path)) if path.to_string() == "/foo"
            );
        }
    }

    #[cfg(target_os = "fuchsia")]
    #[fasync::run_singlethreaded(test)]
    async fn test_clone() {
        use vfs::directory::entry_container::Directory;
        use vfs::execution_scope::ExecutionScope;
        use vfs::file::vmo::read_only;

        // Set up a directory server.
        let dir = vfs::pseudo_directory! {
            "foo" => vfs::pseudo_directory! {
                "bar" => read_only(b"Fuchsia"),
            },
        };
        let (client_end, server_end) = fidl::endpoints::create_endpoints::<fio::DirectoryMarker>();
        let scope = ExecutionScope::new();
        dir.open(
            scope,
            fio::OpenFlags::RIGHT_READABLE | fio::OpenFlags::DIRECTORY,
            vfs::path::Path::dot(),
            fidl::endpoints::ServerEnd::new(server_end.into()),
        );

        // Make a namespace pointing to that server.
        let mut namespace = Namespace::new();
        namespace.add(&ns_path("/data"), client_end).unwrap();

        // Both this namespace and the clone should point to the same server.
        let namespace_clone = namespace.clone();

        let mut entries = namespace.flatten();
        let mut entries_clone = namespace_clone.flatten();

        async fn verify(entry: Entry) {
            assert_eq!(entry.path.to_string(), "/data");
            let dir = entry.directory.into_proxy();
            let file = fuchsia_fs::directory::open_file(&dir, "foo/bar", fio::PERM_READABLE)
                .await
                .unwrap();
            let content = fuchsia_fs::file::read(&file).await.unwrap();
            assert_eq!(content, b"Fuchsia");
        }

        verify(entries.remove(0)).await;
        verify(entries_clone.remove(0)).await;
    }

    #[test]
    fn test_flatten() {
        let mut namespace = Namespace::new();
        let (client_end, server_end) = fidl::endpoints::create_endpoints();
        namespace.add(&ns_path("/svc"), client_end).unwrap();
        let mut entries = namespace.flatten();
        assert_eq!(entries[0].path.to_string(), "/svc");
        entries
            .remove(0)
            .directory
            .into_channel()
            .signal_peer(zx::Signals::empty(), zx::Signals::USER_0)
            .unwrap();
        server_end.wait_handle(zx::Signals::USER_0, zx::MonotonicInstant::INFINITE).unwrap();
    }

    #[test]
    fn test_remove() {
        let mut namespace = Namespace::new();
        let (client_end, server_end) = fidl::endpoints::create_endpoints();
        namespace.add(&ns_path("/svc"), client_end).unwrap();
        let client_end = namespace.remove(&ns_path("/svc")).unwrap();
        let entries = namespace.flatten();
        assert!(entries.is_empty());
        client_end.into_channel().signal_peer(zx::Signals::empty(), zx::Signals::USER_0).unwrap();
        server_end.wait_handle(zx::Signals::USER_0, zx::MonotonicInstant::INFINITE).unwrap();
    }
}