Skip to main content

starnix_core/fs/fuchsia/
mod.rs

1// Copyright 2021 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
5use crate::task::CurrentTask;
6use crate::vfs::FileHandle;
7use starnix_uapi::errors::Errno;
8use starnix_uapi::from_status_like_fdio;
9use starnix_uapi::open_flags::OpenFlags;
10
11mod remote;
12mod remote_bundle;
13mod remote_unix_domain_socket;
14mod remote_volume;
15mod syslog;
16mod timer;
17
18pub mod sync_file;
19pub mod zxio;
20
21pub use remote::*;
22pub use remote_bundle::RemoteBundle;
23pub use remote_unix_domain_socket::*;
24pub use remote_volume::*;
25pub use syslog::*;
26pub use timer::*;
27
28/// Creates a [`FileHandle`] from a [`zx::NullableHandle`].
29///
30/// Wraps [`zx::ObjectType::SOCKET`] handles (such as component stdio handles) in `pipefs`-backed
31/// FIFO (`S_IFIFO`) nodes via [`create_fuchsia_pipe`], and uses [`new_remote_file`] for all other
32/// handle types.
33pub fn create_file_from_handle(
34    current_task: &CurrentTask,
35    handle: zx::NullableHandle,
36) -> Result<FileHandle, Errno> {
37    let info = handle.basic_info().map_err(|status| from_status_like_fdio!(status))?;
38    if info.object_type == zx::ObjectType::SOCKET {
39        create_fuchsia_pipe(current_task, zx::Socket::from(handle), OpenFlags::RDWR)
40    } else {
41        new_remote_file(current_task, handle, OpenFlags::RDWR)
42    }
43}
44
45#[cfg(test)]
46mod test {
47    use super::*;
48    use crate::testing::*;
49
50    #[fuchsia::test]
51    async fn test_create_from_invalid_handle() {
52        spawn_kernel_and_run(async |current_task| {
53            assert!(create_file_from_handle(current_task, zx::NullableHandle::invalid()).is_err());
54        })
55        .await;
56    }
57
58    #[fuchsia::test]
59    async fn test_create_pipe_from_handle() {
60        spawn_kernel_and_run(async |current_task| {
61            let (left_handle, right_handle) = zx::Socket::create_stream();
62            create_file_from_handle(current_task, left_handle.into_handle())
63                .expect("failed to create left FileHandle");
64            create_file_from_handle(current_task, right_handle.into_handle())
65                .expect("failed to create right FileHandle");
66        })
67        .await;
68    }
69}