Skip to main content

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