libpijul_compat/backend/
inode.rs

1use hex;
2use sanakirja::{Alignment, Representable};
3use std;
4pub const INODE_SIZE: usize = 8;
5/// A unique identifier for files or directories in the actual
6/// file system, to map "files from the graph" to real files.
7#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
8pub struct Inode([u8; INODE_SIZE]);
9/// The `Inode` representing the root of the repository (on the
10/// actual file system).
11pub const ROOT_INODE: Inode = Inode([0; INODE_SIZE]);
12impl std::ops::Deref for Inode {
13    type Target = [u8];
14    fn deref(&self) -> &[u8] {
15        &self.0
16    }
17}
18impl std::ops::DerefMut for Inode {
19    fn deref_mut(&mut self) -> &mut [u8] {
20        &mut self.0
21    }
22}
23
24impl std::fmt::Debug for Inode {
25    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
26        write!(fmt, "Inode({})", hex::encode(&self.0))
27    }
28}
29impl Inode {
30    pub fn to_hex(&self) -> String {
31        use hex::ToHex;
32        let mut s = String::new();
33        self.0.write_hex(&mut s).unwrap();
34        s
35    }
36
37    pub fn is_root(&self) -> bool {
38        *self == ROOT_INODE
39    }
40
41    /// Decode an inode from its hexadecimal representation.
42    pub fn from_hex(hex: &str) -> Option<Inode> {
43        let mut i = Inode([0; INODE_SIZE]);
44        if super::from_hex(hex, &mut i) {
45            Some(i)
46        } else {
47            None
48        }
49    }
50}
51
52impl Representable for Inode {
53    fn alignment() -> Alignment {
54        Alignment::B1
55    }
56    fn onpage_size(&self) -> u16 {
57        std::mem::size_of::<Inode>() as u16
58    }
59    unsafe fn write_value(&self, p: *mut u8) {
60        trace!("write_value {:?}", p);
61        std::ptr::copy(self.0.as_ptr(), p, INODE_SIZE)
62    }
63    unsafe fn read_value(p: *const u8) -> Self {
64        trace!("read_value {:?}", p);
65        let mut i: Inode = std::mem::uninitialized();
66        std::ptr::copy(p, i.0.as_mut_ptr(), INODE_SIZE);
67        i
68    }
69    unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
70        self.cmp(&x)
71    }
72    type PageOffsets = std::iter::Empty<u64>;
73    fn page_offsets(&self) -> Self::PageOffsets {
74        std::iter::empty()
75    }
76}