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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Where an extraction writes.
//!
//! An in-place run is told a directory by path and opens it itself. A staged
//! run is handed the staging directory it created: on Unix the descriptor it
//! verified, so the extraction never resolves the staging name again, and the
//! ledger of what the run creates, which the rollback reads after the
//! extraction ended.
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use super::run_record::RecordBudget;
use super::run_record::RunRecord;
use crate::mark::MarkRun;
/// A staging directory an extraction writes into, with the record of what it
/// creates there.
///
/// Made only by the staging layer, from the directory it created and verified.
#[derive(Debug)]
pub(crate) struct StagedOutput {
path: PathBuf,
destination: PathBuf,
#[cfg(unix)]
root: std::fs::File,
ledger: Rc<RunRecord>,
}
impl StagedOutput {
/// The staging directory behind `root`, logically at `path`, built for the
/// extraction to `destination`.
#[cfg(unix)]
pub(crate) const fn new(
path: PathBuf,
destination: PathBuf,
root: std::fs::File,
ledger: Rc<RunRecord>,
) -> Self {
Self {
path,
destination,
root,
ledger,
}
}
/// The staging directory at `path`, which has no descriptor to pin, built
/// for the extraction to `destination`.
#[cfg(not(unix))]
pub(crate) const fn new(path: PathBuf, destination: PathBuf, ledger: Rc<RunRecord>) -> Self {
Self {
path,
destination,
ledger,
}
}
/// The staging directory's logical path.
pub(crate) fn path(&self) -> &Path {
&self.path
}
/// The destination the caller asked to extract to, which a failure of the
/// run names instead of the internal staging path.
pub(crate) fn destination(&self) -> &Path {
&self.destination
}
/// The descriptor of the staging directory itself.
#[cfg(unix)]
pub(crate) const fn root(&self) -> &std::fs::File {
&self.root
}
/// What the run creates in the directory is recorded here.
pub(crate) fn ledger(&self) -> Rc<RunRecord> {
Rc::clone(&self.ledger)
}
}
/// Where an extraction writes: a directory named by path or a staging
/// directory.
#[derive(Debug, Clone, Copy)]
pub(super) enum Place<'a> {
/// A directory named by path, created if absent and opened by the run.
Path(&'a Path),
/// A staging directory pinned by the staging layer.
Staged(&'a StagedOutput),
}
/// The directory an extraction writes into, with the mark the files it
/// creates are to carry.
#[derive(Debug, Clone, Copy)]
pub(crate) struct OutputRoot<'a> {
place: Place<'a>,
mark: Option<&'a Rc<MarkRun>>,
budget: RecordBudget,
}
impl<'a> OutputRoot<'a> {
const fn at(place: Place<'a>) -> Self {
Self {
place,
mark: None,
budget: RecordBudget::Full,
}
}
/// The same directory, whose run record keeps `budget`. A staged run keeps
/// the ledger the staging layer made, whatever the budget.
pub(crate) const fn with_record_budget(self, budget: RecordBudget) -> Self {
Self { budget, ..self }
}
pub(super) const fn record_budget(&self) -> RecordBudget {
self.budget
}
/// The same directory, whose files carry the mark of `mark`; `None` marks
/// nothing.
pub(crate) const fn with_mark(self, mark: Option<&'a Rc<MarkRun>>) -> Self {
Self { mark, ..self }
}
pub(super) const fn place(&self) -> Place<'a> {
self.place
}
pub(super) fn mark(&self) -> Option<Rc<MarkRun>> {
self.mark.map(Rc::clone)
}
}
impl<'a> From<&'a Path> for OutputRoot<'a> {
fn from(path: &'a Path) -> Self {
Self::at(Place::Path(path))
}
}
impl<'a> From<&'a PathBuf> for OutputRoot<'a> {
fn from(path: &'a PathBuf) -> Self {
Self::at(Place::Path(path))
}
}
impl<'a> From<&'a StagedOutput> for OutputRoot<'a> {
fn from(staged: &'a StagedOutput) -> Self {
Self::at(Place::Staged(staged))
}
}