-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate.rs
More file actions
192 lines (153 loc) · 4.97 KB
/
create.rs
File metadata and controls
192 lines (153 loc) · 4.97 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use super::*;
#[derive(Parser)]
pub(crate) struct Create {
#[arg(help = "Deny <LINT_GROUP>", long, value_name = "LINT_GROUP")]
deny: Option<LintGroup>,
#[arg(help = "Overwrite manifest if it already exists", long)]
force: bool,
#[arg(
help = "Write manifest to <MANIFEST>, defaults to `<ROOT>/filepack.json`",
long
)]
manifest: Option<Utf8PathBuf>,
#[arg(help = "Include metadata from YAML document <METADATA>`", long)]
metadata: Option<Utf8PathBuf>,
#[arg(help = "Create manifest for files in <ROOT> directory, defaults to current directory")]
root: Option<Utf8PathBuf>,
#[arg(help = "Sign manifest with master key", long)]
sign: bool,
}
impl Create {
pub(crate) fn run(self, options: Options) -> Result {
let current_dir = current_dir()?;
let root = self.root.unwrap_or_else(|| current_dir.clone());
let manifest_path = if let Some(path) = self.manifest {
path
} else {
root.join(Manifest::FILENAME)
};
if let Some(path) = &self.metadata {
let yaml = filesystem::read_to_string(path)?;
let template = serde_yaml::from_str::<Template>(&yaml)
.context(error::DeserializeMetadataTemplate { path })?;
let path = root.join(Metadata::FILENAME);
ensure! {
self.force || !filesystem::exists(&path)?,
error::MetadataAlreadyExists { path: &path },
}
Metadata::from(template).save(&path)?;
}
let cleaned_manifest = current_dir.join(&manifest_path).lexiclean();
let cleaned_metadata = self.metadata.map(|path| current_dir.join(path).lexiclean());
let mut paths = HashMap::new();
let mut case_conflicts = HashMap::<RelativePath, Vec<RelativePath>>::new();
let mut lint_errors = 0u64;
let mut dirs = Vec::new();
for entry in WalkDir::new(&root) {
let entry = entry?;
let path = entry.path();
let path = decode_path(path)?;
let cleaned_path = current_dir.join(path).lexiclean();
if cleaned_path == cleaned_manifest {
continue;
}
if cleaned_metadata
.as_ref()
.is_some_and(|path| cleaned_path == *path)
{
return Err(error::MetadataTemplateIncluded { path }.build());
}
while let Some(dir) = dirs.last() {
if path.starts_with(dir) {
dirs.pop();
} else {
break;
}
}
if entry.file_type().is_dir() {
if path != root {
dirs.push(path.to_owned());
}
continue;
}
ensure! {
!entry.file_type().is_symlink(),
error::Symlink { path },
}
let relative = path.strip_prefix(&root).unwrap();
let relative = RelativePath::try_from(relative).context(error::Path { path: relative })?;
match self.deny {
None => {}
Some(LintGroup::All) => {
if let Some(lint) = relative.lint() {
eprintln!("error: path failed lint: `{relative}`");
eprintln!(" └─ {lint}");
lint_errors += 1;
}
case_conflicts
.entry(relative.to_lowercase())
.or_default()
.push(relative.clone());
}
}
let metadata = filesystem::metadata(path)?;
paths.insert(relative, metadata.len());
}
for mut originals in case_conflicts.into_values() {
if originals.len() > 1 {
originals.sort();
eprintln!("error: paths would conflict on case-insensitive filesystem:");
for (i, original) in originals.iter().enumerate() {
eprintln!(
" {}─ `{original}`",
if i < originals.len() - 1 {
'├'
} else {
'└'
}
);
}
lint_errors += 1;
}
}
if lint_errors > 0 {
return Err(error::Lint { count: lint_errors }.build());
}
if !dirs.is_empty() {
dirs.sort();
return Err(Error::EmptyDirectory {
paths: dirs
.into_iter()
.map(|dir| dir.strip_prefix(&root).unwrap().to_owned().into())
.collect(),
});
}
ensure! {
self.force || !manifest_path.try_exists().context(error::FilesystemIo { path: &manifest_path })?,
error::ManifestAlreadyExists {
path: manifest_path,
},
}
let mut files = BTreeMap::new();
let bar = progress_bar::new(&options, paths.values().sum());
for (path, _size) in paths {
let entry = options
.hash_file(&root.join(&path))
.context(error::FilesystemIo { path: &path })?;
files.insert(path, entry);
bar.inc(entry.size);
}
let mut manifest = Manifest {
files,
signatures: BTreeMap::new(),
};
if self.sign {
let private_key_path = options.key_dir()?.join(MASTER_PRIVATE_KEY);
let (public_key, signature) =
PrivateKey::load_and_sign(&private_key_path, manifest.fingerprint().as_bytes())?;
manifest.signatures.insert(public_key, signature);
}
manifest.save(&manifest_path)?;
Ok(())
}
}