starnix_core/vfs/
memory_directory.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::{
7    DirectoryEntryType, DirentSink, FileObject, FileOps, FsString, SeekTarget, default_seek,
8    fileops_impl_directory, fileops_impl_noop_sync,
9};
10use starnix_sync::{FileOpsCore, Locked, Mutex};
11use starnix_uapi::errors::Errno;
12use starnix_uapi::{error, off_t};
13use std::ops::Bound;
14
15pub struct MemoryDirectoryFile {
16    /// The current position for readdir.
17    ///
18    /// When readdir is called multiple times, we need to return subsequent
19    /// directory entries. This field records where the previous readdir
20    /// stopped.
21    ///
22    /// The state is actually recorded twice: once in the offset for this
23    /// FileObject and again here. Recovering the state from the offset is slow
24    /// because we would need to iterate through the keys of the BTree. Having
25    /// the FsString cached lets us search the keys of the BTree faster.
26    ///
27    /// The initial "." and ".." entries are not recorded here. They are
28    /// represented only in the offset field in the FileObject.
29    readdir_position: Mutex<Bound<FsString>>,
30}
31
32impl MemoryDirectoryFile {
33    pub fn new() -> MemoryDirectoryFile {
34        MemoryDirectoryFile { readdir_position: Mutex::new(Bound::Unbounded) }
35    }
36}
37
38/// If the offset is less than 2, emits . and .. entries for the specified file.
39///
40/// The offset will always be at least 2 after this function returns successfully. It's often
41/// necessary to subtract 2 from the offset in subsequent logic.
42pub fn emit_dotdot(file: &FileObject, sink: &mut dyn DirentSink) -> Result<(), Errno> {
43    if sink.offset() == 0 {
44        sink.add(file.node().ino, 1, DirectoryEntryType::DIR, ".".into())?;
45    }
46    if sink.offset() == 1 {
47        sink.add(
48            file.name.entry.parent_or_self().node.ino,
49            2,
50            DirectoryEntryType::DIR,
51            "..".into(),
52        )?;
53    }
54    Ok(())
55}
56
57impl FileOps for MemoryDirectoryFile {
58    fileops_impl_directory!();
59    fileops_impl_noop_sync!();
60
61    fn seek(
62        &self,
63        _locked: &mut Locked<FileOpsCore>,
64        file: &FileObject,
65        _current_task: &CurrentTask,
66        current_offset: off_t,
67        target: SeekTarget,
68    ) -> Result<off_t, Errno> {
69        let new_offset = default_seek(current_offset, target, || error!(EINVAL))?;
70        // Nothing to do.
71        if current_offset == new_offset {
72            return Ok(new_offset);
73        }
74
75        let mut readdir_position = self.readdir_position.lock();
76
77        // We use 0 and 1 for "." and ".."
78        if new_offset <= 2 {
79            *readdir_position = Bound::Unbounded;
80        } else {
81            file.name.entry.get_children(|children| {
82                let count = (new_offset - 2) as usize;
83                *readdir_position = children
84                    .iter()
85                    .take(count)
86                    .next_back()
87                    .map_or(Bound::Unbounded, |(name, _)| Bound::Excluded(name.clone()));
88            });
89        }
90
91        Ok(new_offset)
92    }
93
94    fn readdir(
95        &self,
96        _locked: &mut Locked<FileOpsCore>,
97        file: &FileObject,
98        _current_task: &CurrentTask,
99        sink: &mut dyn DirentSink,
100    ) -> Result<(), Errno> {
101        emit_dotdot(file, sink)?;
102
103        let mut readdir_position = self.readdir_position.lock();
104        file.name.entry.get_children(|children| {
105            for (name, maybe_entry) in children.range((readdir_position.clone(), Bound::Unbounded))
106            {
107                if let Some(entry) = maybe_entry.upgrade() {
108                    sink.add(
109                        entry.node.ino,
110                        sink.offset() + 1,
111                        DirectoryEntryType::from_mode(entry.node.info().mode),
112                        name.as_ref(),
113                    )?;
114                    *readdir_position = Bound::Excluded(name.clone());
115                }
116            }
117            Ok(())
118        })
119    }
120}