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
125
126
127
128
#[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 super::key::*;
use super::patch_id::*;
use sanakirja::{Alignment, Representable};
use std;
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum FileStatus {
Ok = 0,
Moved = 1,
Deleted = 2,
Zombie = 3,
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct FileHeader {
pub metadata: FileMetadata,
pub status: FileStatus,
pub key: Key<PatchId>,
}
#[test]
fn test_fileheader_alignment() {
assert_eq!(std::mem::size_of::<FileHeader>(), 2 + 1 + 16)
}
impl Representable for FileHeader {
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) {
let meta = self.metadata.0;
*p = (meta & 255) as u8;
*(p.offset(1)) = (meta >> 8) as u8;
*(p.offset(2)) = self.status as u8;
self.key.write_value(p.offset(3))
}
unsafe fn read_value(p: *const u8) -> Self {
let metadata = {
let x0 = (*p) as u16;
let x1 = (*(p.offset(1))) as u16;
std::mem::transmute((x1 << 8) | x0)
};
let status = std::mem::transmute(*(p.offset(2)));
let key: Key<PatchId> = Representable::read_value(p.offset(3));
FileHeader {
metadata,
status,
key,
}
}
unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
self.cmp(&x)
}
type PageOffsets = std::iter::Empty<u64>;
fn page_offsets(&self) -> Self::PageOffsets {
std::iter::empty()
}
}