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        // This ensures the caller of this fn is recorded instead of the location of the closure.
24        let location = Location::caller();
25        SimpleFileNode::new(move || Ok(StubEmptyFile { bug, location }))
26    }
27}
28
29impl FileOps for StubEmptyFile {
30    fileops_impl_dataless!();
31    fileops_impl_nonseekable!();
32    fileops_impl_noop_sync!();
33
34    fn open(
35        &self,
36        _locked: &mut Locked<FileOpsCore>,
37        file: &crate::vfs::FileObject,
38        current_task: &crate::task::CurrentTask,
39    ) -> Result<(), starnix_uapi::errors::Errno> {
40        let path = file.name.path(current_task);
41        starnix_logging::__track_stub_inner(
42            self.bug,
43            path.to_str_lossy().as_ref(),
44            None,
45            &self.location,
46        );
47        Ok(())
48    }
49}