1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct FileMetadata(u16);
const DIR_BIT: u16 = 0x200;
use byteorder::ByteOrder;
impl FileMetadata {
pub fn from_contents(p: &[u8]) -> Self {
debug_assert!(p.len() == 2);
FileMetadata(BigEndian::read_u16(p))
}
pub fn new(perm: usize, is_dir: bool) -> Self {
let mut m = FileMetadata(0);
m.set_permissions(perm as u16);
if is_dir {
m.set_dir()
} else {
m.unset_dir()
}
m
}
pub fn permissions(&self) -> u16 {
u16::from_le(self.0) & 0x1ff
}
pub fn set_permissions(&mut self, perm: u16) {
let bits = u16::from_le(self.0);
let perm = (bits & !0x1ff) | perm;
self.0 = perm.to_le()
}
pub fn is_dir(&self) -> bool {
u16::from_le(self.0) & DIR_BIT != 0
}
pub fn set_dir(&mut self) {
let bits = u16::from_le(self.0);
self.0 = (bits | DIR_BIT).to_le()
}
pub fn unset_dir(&mut self) {
let bits = u16::from_le(self.0);
self.0 = (bits & !DIR_BIT).to_le()
}
}
use byteorder::{BigEndian, WriteBytesExt};
pub trait WriteMetadata: std::io::Write {
fn write_metadata(&mut self, m: FileMetadata) -> std::io::Result<()> {
self.write_u16::<BigEndian>(m.0)
}
}
impl<W: std::io::Write> WriteMetadata for W {}
use sanakirja::{Representable, Alignment};
use std;
use super::key::*;
use super::patch_id::*;
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FileStatus {
Ok = 0,
Moved = 1,
Deleted = 2,
}
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[repr(packed)]
pub struct FileHeader {
pub metadata: FileMetadata,
pub status: FileStatus,
pub key: Key<PatchId>,
}
impl FileHeader {
pub fn to_unsafe(&self) -> UnsafeFileHeader {
UnsafeFileHeader(self)
}
pub unsafe fn from_unsafe<'a>(p: UnsafeFileHeader) -> &'a Self {
&*p.0
}
}
#[derive(Clone, Copy, Debug)]
pub struct UnsafeFileHeader(*const FileHeader);
impl Representable for UnsafeFileHeader {
fn alignment() -> Alignment {
Alignment::B1
}
fn onpage_size(&self) -> u16 {
std::mem::size_of::<FileHeader>() as u16
}
unsafe fn write_value(&self, p: *mut u8) {
trace!("write_value {:?}", p);
std::ptr::copy(self.0, p as *mut FileHeader, 1)
}
unsafe fn read_value(p: *const u8) -> Self {
trace!("read_value {:?}", p);
UnsafeFileHeader(p as *const FileHeader)
}
unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
let a: &FileHeader = &*self.0;
let b: &FileHeader = &*x.0;
a.cmp(b)
}
type PageOffsets = std::iter::Empty<u64>;
fn page_offsets(&self) -> Self::PageOffsets { std::iter::empty() }
}