2023-06-30 16:28:22 +02:00
|
|
|
//! This module contains all the data structures used to track information
|
|
|
|
//! about inodes, which present tvix-store nodes in a filesystem.
|
2023-09-21 22:32:44 +03:00
|
|
|
use tvix_castore::proto as castorepb;
|
|
|
|
use tvix_castore::B3Digest;
|
2023-05-29 15:11:31 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum InodeData {
|
|
|
|
Regular(B3Digest, u32, bool), // digest, size, executable
|
2023-07-19 18:52:50 +03:00
|
|
|
Symlink(bytes::Bytes), // target
|
2023-05-29 15:11:31 +03:00
|
|
|
Directory(DirectoryInodeData), // either [DirectoryInodeData:Sparse] or [DirectoryInodeData:Populated]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This encodes the two different states of [InodeData::Directory].
|
2023-09-21 22:32:44 +03:00
|
|
|
/// Either the data still is sparse (we only saw a [castorepb::DirectoryNode],
|
|
|
|
/// but didn't fetch the [castorepb::Directory] struct yet, or we processed a
|
|
|
|
/// lookup and did fetch the data.
|
2023-05-29 15:11:31 +03:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum DirectoryInodeData {
|
2023-09-21 22:32:44 +03:00
|
|
|
Sparse(B3Digest, u32), // digest, size
|
|
|
|
Populated(B3Digest, Vec<(u64, castorepb::node::Node)>), // [(child_inode, node)]
|
2023-05-29 15:11:31 +03:00
|
|
|
}
|
|
|
|
|
2023-09-21 22:32:44 +03:00
|
|
|
impl From<&castorepb::node::Node> for InodeData {
|
|
|
|
fn from(value: &castorepb::node::Node) -> Self {
|
2023-05-29 15:11:31 +03:00
|
|
|
match value {
|
2023-09-21 22:32:44 +03:00
|
|
|
castorepb::node::Node::Directory(directory_node) => directory_node.into(),
|
|
|
|
castorepb::node::Node::File(file_node) => file_node.into(),
|
|
|
|
castorepb::node::Node::Symlink(symlink_node) => symlink_node.into(),
|
2023-05-29 15:11:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 22:32:44 +03:00
|
|
|
impl From<&castorepb::SymlinkNode> for InodeData {
|
|
|
|
fn from(value: &castorepb::SymlinkNode) -> Self {
|
2023-05-29 15:11:31 +03:00
|
|
|
InodeData::Symlink(value.target.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 22:32:44 +03:00
|
|
|
impl From<&castorepb::FileNode> for InodeData {
|
|
|
|
fn from(value: &castorepb::FileNode) -> Self {
|
2023-05-29 15:11:31 +03:00
|
|
|
InodeData::Regular(
|
2023-07-20 13:37:29 +03:00
|
|
|
value.digest.clone().try_into().unwrap(),
|
2023-05-29 15:11:31 +03:00
|
|
|
value.size,
|
|
|
|
value.executable,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a DirectoryNode to a sparsely populated InodeData::Directory.
|
2023-09-21 22:32:44 +03:00
|
|
|
impl From<&castorepb::DirectoryNode> for InodeData {
|
|
|
|
fn from(value: &castorepb::DirectoryNode) -> Self {
|
2023-05-29 15:11:31 +03:00
|
|
|
InodeData::Directory(DirectoryInodeData::Sparse(
|
2023-07-20 13:37:29 +03:00
|
|
|
value.digest.clone().try_into().unwrap(),
|
2023-05-29 15:11:31 +03:00
|
|
|
value.size,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// converts a proto::Directory to a InodeData::Directory(DirectoryInodeData::Populated(..)).
|
|
|
|
/// The inodes for each child are 0, because it's up to the InodeTracker to allocate them.
|
2023-09-21 22:32:44 +03:00
|
|
|
impl From<castorepb::Directory> for InodeData {
|
|
|
|
fn from(value: castorepb::Directory) -> Self {
|
2023-05-29 15:11:31 +03:00
|
|
|
let digest = value.digest();
|
|
|
|
|
2023-09-21 22:32:44 +03:00
|
|
|
let children: Vec<(u64, castorepb::node::Node)> =
|
|
|
|
value.nodes().map(|node| (0, node)).collect();
|
2023-05-29 15:11:31 +03:00
|
|
|
|
|
|
|
InodeData::Directory(DirectoryInodeData::Populated(digest, children))
|
|
|
|
}
|
|
|
|
}
|