starnix_core/vfs/pseudo/
stub_empty_file.rs

1// Copyright 2024 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::vfs::pseudo::simple_file::SimpleFileNode;
6use crate::vfs::{
7    FileOps, FsNodeOps, fileops_impl_dataless, fileops_impl_nonseekable, fileops_impl_noop_sync,
8};
9use bstr::ByteSlice;
10use starnix_logging::BugRef;
11use starnix_sync::{FileOpsCore, Locked};
12use std::panic::Location;
13
14#[derive(Clone, Debug)]
15pub struct StubEmptyFile {
16    bug: BugRef,
17    location: &'static Location<'static>,
18}
19
20impl StubEmptyFile {
21    #[track_caller]
22    pub fn new_node(bug: BugRef) -> impl FsNodeOps {
23        SimpleFileNode::new(move |_, _| Ok(StubEmptyFile::new(bug)))
24    }
25
26    #[track_caller]
27    pub fn new(bug: BugRef) -> Self {
28        // This ensures the caller of this fn is recorded instead of the location of the closure.
29        let location = Location::caller();
30        Self { bug, location }
31    }
32}
33
34impl FileOps for StubEmptyFile {
35    fileops_impl_dataless!();
36    fileops_impl_nonseekable!();
37    fileops_impl_noop_sync!();
38
39    fn open(
40        &self,
41        _locked: &mut Locked<FileOpsCore>,
42        file: &crate::vfs::FileObject,
43        current_task: &crate::task::CurrentTask,
44    ) -> Result<(), starnix_uapi::errors::Errno> {
45        let path = file.name.path(current_task);
46        starnix_logging::__track_stub_inner(
47            self.bug,
48            path.to_str_lossy().as_ref(),
49            None,
50            &self.location,
51        );
52        Ok(())
53    }
54}