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::open_flags::OpenFlags;
9
10mod remote;
11mod remote_bundle;
12mod remote_unix_domain_socket;
13mod remote_volume;
14mod syslog;
15mod timer;
16
17pub mod sync_file;
18pub mod zxio;
19
20pub use remote::*;
21pub use remote_bundle::RemoteBundle;
22pub use remote_unix_domain_socket::*;
23pub use remote_volume::*;
24pub use syslog::*;
25pub use timer::*;
26
27/// Create a FileHandle from a zx::NullableHandle.
28pub fn create_file_from_handle(
29    current_task: &CurrentTask,
30    handle: zx::NullableHandle,
31) -> Result<FileHandle, Errno> {
32    new_remote_file(current_task, handle, OpenFlags::RDWR)
33}
34
35#[cfg(test)]
36mod test {
37    use super::*;
38    use crate::testing::*;
39
40    #[fuchsia::test]
41    async fn test_create_from_invalid_handle() {
42        spawn_kernel_and_run(async |current_task| {
43            assert!(create_file_from_handle(current_task, zx::NullableHandle::invalid()).is_err());
44        })
45        .await;
46    }
47
48    #[fuchsia::test]
49    async fn test_create_pipe_from_handle() {
50        spawn_kernel_and_run(async |current_task| {
51            let (left_handle, right_handle) = zx::Socket::create_stream();
52            create_file_from_handle(current_task, left_handle.into_handle())
53                .expect("failed to create left FileHandle");
54            create_file_from_handle(current_task, right_handle.into_handle())
55                .expect("failed to create right FileHandle");
56        })
57        .await;
58    }
59}