vfs/
directory.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Module holding different kinds of pseudo directories and their building blocks.
6
7use crate::directory::entry_container::Directory;
8use crate::path::Path;
9use fidl_fuchsia_io as fio;
10use std::sync::Arc;
11
12#[macro_use]
13pub mod test_utils;
14
15pub mod common;
16
17pub mod immutable;
18pub mod mutable;
19
20mod connection;
21pub mod dirents_sink;
22pub mod entry;
23pub mod entry_container;
24pub mod helper;
25pub mod read_dirents;
26pub mod simple;
27pub mod traversal_position;
28pub mod watchers;
29
30/// A directory can be open either as a directory or a node.
31#[derive(Clone)]
32pub struct DirectoryOptions {
33    pub(crate) rights: fio::Operations,
34}
35
36impl DirectoryOptions {
37    pub(crate) fn to_io1(&self) -> fio::OpenFlags {
38        // Note that rights in io1 correspond to several different rights in io2. The *_STAR_DIR
39        // constants defined in the protocol indicate which rights these flags map to. Note that
40        // this is more strict than the checks in FileOptions::to_io1, as OpenFlags map to several
41        // different io2 directory rights.
42        let mut flags = fio::OpenFlags::empty();
43        if self.rights.contains(fio::R_STAR_DIR) {
44            flags |= fio::OpenFlags::RIGHT_READABLE;
45        }
46        if self.rights.contains(fio::W_STAR_DIR) {
47            flags |= fio::OpenFlags::RIGHT_WRITABLE;
48        }
49        if self.rights.contains(fio::X_STAR_DIR) {
50            flags |= fio::OpenFlags::RIGHT_EXECUTABLE;
51        }
52        flags
53    }
54}
55
56impl From<&DirectoryOptions> for fio::Flags {
57    fn from(options: &DirectoryOptions) -> Self {
58        // There is 1:1 mapping between `fio::Operations` and `fio::Flags`.
59        fio::Flags::PROTOCOL_DIRECTORY | fio::Flags::from_bits_truncate(options.rights.bits())
60    }
61}
62
63impl Default for DirectoryOptions {
64    fn default() -> Self {
65        DirectoryOptions { rights: fio::R_STAR_DIR }
66    }
67}
68
69/// Helper function to serve a new connection to `directory` with `flags`. Errors will be
70/// communicated via epitaph on the returned proxy. A new [`crate::execution_scope::ExecutionScope`]
71/// will be created for the request.
72pub fn serve<D: Directory + ?Sized>(directory: Arc<D>, flags: fio::Flags) -> fio::DirectoryProxy {
73    crate::serve_directory(directory, Path::dot(), flags)
74}
75
76/// Helper function to serve a new connection to `directory` as read-only (i.e. with
77/// [`fio::PERM_READABLE`]). Errors will be communicated via epitaph on the returned proxy. A new
78/// [`crate::execution_scope::ExecutionScope`] will be created for the request.
79pub fn serve_read_only<D: Directory + ?Sized>(directory: Arc<D>) -> fio::DirectoryProxy {
80    crate::serve_directory(directory, Path::dot(), fio::PERM_READABLE)
81}