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_sync::{FileOpsCore, LockEqualOrBefore, Locked};
8use starnix_uapi::errors::Errno;
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/// Create a FileHandle from a zx::NullableHandle.
29pub fn create_file_from_handle<L>(
30    locked: &mut Locked<L>,
31    current_task: &CurrentTask,
32    handle: zx::NullableHandle,
33) -> Result<FileHandle, Errno>
34where
35    L: LockEqualOrBefore<FileOpsCore>,
36{
37    new_remote_file(locked, current_task, handle, OpenFlags::RDWR)
38}
39
40#[cfg(test)]
41mod test {
42    use super::*;
43    use crate::testing::*;
44    use zx::HandleBased;
45
46    #[fuchsia::test]
47    async fn test_create_from_invalid_handle() {
48        spawn_kernel_and_run(async |locked, current_task| {
49            assert!(
50                create_file_from_handle(locked, current_task, zx::NullableHandle::invalid())
51                    .is_err()
52            );
53        })
54        .await;
55    }
56
57    #[fuchsia::test]
58    async fn test_create_pipe_from_handle() {
59        spawn_kernel_and_run(async |locked, current_task| {
60            let (left_handle, right_handle) = zx::Socket::create_stream();
61            create_file_from_handle(locked, current_task, left_handle.into_handle())
62                .expect("failed to create left FileHandle");
63            create_file_from_handle(locked, current_task, right_handle.into_handle())
64                .expect("failed to create right FileHandle");
65        })
66        .await;
67    }
68}