-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathutils.rs
More file actions
352 lines (307 loc) · 11.5 KB
/
Copy pathutils.rs
File metadata and controls
352 lines (307 loc) · 11.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::future::Future;
use std::io::Write;
use std::os::fd::{AsFd, BorrowedFd};
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::time::Duration;
use anyhow::{Context, Result};
use bootc_utils::CommandRunExt;
use camino::Utf8Path;
use cap_std_ext::cap_std::fs::Dir;
use cap_std_ext::dirext::CapStdExtDirExt;
use cap_std_ext::prelude::CapStdExtCommandExt;
use fn_error_context::context;
use indicatif::HumanDuration;
use libsystemd::logging::journal_print;
use ostree::glib;
use ostree_ext::container::SignatureSource;
use ostree_ext::ostree;
use rustix::fs::StatVfsMountFlags;
/// Try to look for keys injected by e.g. rpm-ostree requesting machine-local
/// changes; if any are present, return `true`.
pub(crate) fn origin_has_rpmostree_stuff(kf: &glib::KeyFile) -> bool {
// These are groups set in https://github.com/coreos/rpm-ostree/blob/27f72dce4f9b5c176ad030911c12354e2498c07d/rust/src/origin.rs#L23
// TODO: Add some notion of "owner" into origin files
for group in ["rpmostree", "packages", "overrides", "modules"] {
if kf.has_group(group) {
return true;
}
}
false
}
// Access the file descriptor for a sysroot
#[allow(unsafe_code)]
pub(crate) fn sysroot_fd(sysroot: &ostree::Sysroot) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(sysroot.fd()) }
}
// Return a cap-std `Dir` type for a sysroot
pub(crate) fn sysroot_dir(sysroot: &ostree::Sysroot) -> Result<Dir> {
Dir::reopen_dir(&sysroot_fd(sysroot)).map_err(Into::into)
}
// Return a cap-std `Dir` type for a deployment.
// TODO: in the future this should perhaps actually mount via composefs
pub(crate) fn deployment_fd(
sysroot: &ostree::Sysroot,
deployment: &ostree::Deployment,
) -> Result<Dir> {
let sysroot_dir = &Dir::reopen_dir(&sysroot_fd(sysroot))?;
let dirpath = sysroot.deployment_dirpath(deployment);
sysroot_dir.open_dir(&dirpath).map_err(Into::into)
}
/// Given an mount option string list like foo,bar=baz,something=else,ro parse it and find
/// the first entry like $optname=
/// This will not match a bare `optname` without an equals.
pub(crate) fn find_mount_option<'a>(
option_string_list: &'a str,
optname: &'_ str,
) -> Option<&'a str> {
option_string_list
.split(',')
.filter_map(|k| k.split_once('='))
.filter_map(|(k, v)| (k == optname).then_some(v))
.next()
}
pub fn have_executable(name: &str) -> Result<bool> {
let Some(path) = std::env::var_os("PATH") else {
return Ok(false);
};
for mut elt in std::env::split_paths(&path) {
elt.push(name);
if elt.try_exists()? {
return Ok(true);
}
}
Ok(false)
}
/// Given a target directory, if it's a read-only mount, then remount it writable
#[context("Opening {target} with writable mount")]
pub(crate) fn open_dir_remount_rw(root: &Dir, target: &Utf8Path) -> Result<Dir> {
let target_dir = root
.open_dir(target)
.with_context(|| format!("Opening {target}"))?;
if matches!(target_dir.is_mountpoint("."), Ok(Some(true))) {
let st = rustix::fs::fstatvfs(target_dir.as_fd())
.with_context(|| format!("Getting filesystem info for {target}"))?;
if !st.f_flag.contains(StatVfsMountFlags::RDONLY) {
return Ok(target_dir);
};
tracing::debug!("Target {target} is a mountpoint, remounting rw");
let st = Command::new("mount")
.args(["-o", "remount,rw", target.as_str()])
.cwd_dir(root.try_clone()?)
.status()?;
anyhow::ensure!(st.success(), "Failed to remount: {st:?}");
}
Ok(target_dir)
}
/// Given a target path, remove its immutability if present
#[context("Removing immutable flag from {target}")]
pub(crate) fn remove_immutability(root: &Dir, target: &Utf8Path) -> Result<()> {
use anyhow::ensure;
tracing::debug!("Target {target} is a mountpoint, remounting rw");
let st = Command::new("chattr")
.args(["-i", target.as_str()])
.cwd_dir(root.try_clone()?)
.status()?;
ensure!(st.success(), "Failed to remove immutability: {st:?}");
Ok(())
}
pub(crate) fn spawn_editor(tmpf: &tempfile::NamedTempFile) -> Result<()> {
let editor_variables = ["EDITOR"];
// These roughly match https://github.com/systemd/systemd/blob/769ca9ab557b19ee9fb5c5106995506cace4c68f/src/shared/edit-util.c#L275
let backup_editors = ["nano", "vim", "vi"];
let editor = editor_variables.into_iter().find_map(std::env::var_os);
let editor = if let Some(e) = editor.as_ref() {
e.to_str()
} else {
backup_editors
.into_iter()
.find(|v| std::path::Path::new("/usr/bin").join(v).exists())
};
let editor =
editor.ok_or_else(|| anyhow::anyhow!("$EDITOR is unset, and no backup editor found"))?;
let mut editor_args = editor.split_ascii_whitespace();
let argv0 = editor_args
.next()
.ok_or_else(|| anyhow::anyhow!("Invalid editor: {editor}"))?;
Command::new(argv0)
.args(editor_args)
.arg(tmpf.path())
.lifecycle_bind()
.run_inherited()
.with_context(|| format!("Invoking editor {editor} failed"))
}
/// Convert a combination of values (likely from CLI parsing) into a signature source
pub(crate) fn sigpolicy_from_opt(enforce_container_verification: bool) -> SignatureSource {
match enforce_container_verification {
true => SignatureSource::ContainerPolicy,
false => SignatureSource::ContainerPolicyAllowInsecure,
}
}
/// Output a warning message that we want to be quite visible.
/// The process (thread) execution will be delayed for a short time.
pub(crate) fn medium_visibility_warning(s: &str) {
anstream::eprintln!(
"{}{s}{}",
anstyle::AnsiColor::Red.render_fg(),
anstyle::Reset.render()
);
// When warning, add a sleep to ensure it's seen
std::thread::sleep(std::time::Duration::from_secs(1));
}
/// Call an async task function, and write a message to stderr
/// with an automatic spinner to show that we're not blocked.
/// Note that generally the called function should not output
/// anything to stderr as this will interfere with the spinner.
pub(crate) async fn async_task_with_spinner<F, T>(msg: &str, f: F) -> T
where
F: Future<Output = T>,
{
let start_time = std::time::Instant::now();
let pb = indicatif::ProgressBar::new_spinner();
let style = indicatif::ProgressStyle::default_bar();
pb.set_style(style.template("{spinner} {msg}").unwrap());
pb.set_message(msg.to_string());
pb.enable_steady_tick(Duration::from_millis(150));
// We need to handle the case where we aren't connected to
// a tty, so indicatif would show nothing by default.
if pb.is_hidden() {
eprint!("{msg}...");
std::io::stderr().flush().unwrap();
}
let r = f.await;
let elapsed = HumanDuration(start_time.elapsed());
let _ = journal_print(
libsystemd::logging::Priority::Info,
&format!("completed task in {elapsed}: {msg}"),
);
if pb.is_hidden() {
eprintln!("done ({elapsed})");
} else {
pb.finish_with_message(format!("{msg}: done ({elapsed})"));
}
r
}
/// Given a possibly tagged image like quay.io/foo/bar:latest and a digest 0ab32..., return
/// the digested form quay.io/foo/bar:latest@sha256:0ab32...
/// If the image already has a digest, it will be replaced.
#[allow(dead_code)]
pub(crate) fn digested_pullspec(image: &str, digest: &str) -> String {
let image = image.rsplit_once('@').map(|v| v.0).unwrap_or(image);
format!("{image}@{digest}")
}
#[derive(Debug)]
pub enum EfiError {
SystemNotUEFI,
MissingVar,
#[allow(dead_code)]
InvalidData(&'static str),
#[allow(dead_code)]
Io(std::io::Error),
}
impl From<std::io::Error> for EfiError {
fn from(e: std::io::Error) -> Self {
EfiError::Io(e)
}
}
pub fn read_uefi_var(var_name: &str) -> Result<String, EfiError> {
use crate::install::EFIVARFS;
use cap_std_ext::cap_std::ambient_authority;
let efivarfs = match Dir::open_ambient_dir(EFIVARFS, ambient_authority()) {
Ok(dir) => dir,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Err(EfiError::SystemNotUEFI),
Err(e) => Err(e)?,
};
match efivarfs.read(var_name) {
Ok(loader_bytes) => {
if loader_bytes.len() % 2 != 0 {
return Err(EfiError::InvalidData(
"EFI var length is not valid UTF-16 LE",
));
}
// EFI vars are UTF-16 LE
let loader_u16_bytes: Vec<u16> = loader_bytes
.chunks_exact(2)
.map(|x| u16::from_le_bytes([x[0], x[1]]))
.collect();
let loader = String::from_utf16(&loader_u16_bytes)
.map_err(|_| EfiError::InvalidData("EFI var is not UTF-16"))?;
return Ok(loader);
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Err(EfiError::MissingVar);
}
Err(e) => Err(e)?,
}
}
/// Computes a relative path from `from` to `to`.
///
/// Both `from` and `to` must be absolute paths.
pub(crate) fn path_relative_to(from: &Path, to: &Path) -> Result<PathBuf> {
if !from.is_absolute() || !to.is_absolute() {
anyhow::bail!("Paths must be absolute");
}
let from = from.components().collect::<Vec<_>>();
let to = to.components().collect::<Vec<_>>();
let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
let up = std::iter::repeat(Component::ParentDir).take(from.len() - common);
let mut final_path = PathBuf::new();
final_path.extend(up);
final_path.extend(&to[common..]);
return Ok(final_path);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_digested_pullspec() {
let digest = "ebe3bdccc041864e5a485f1e755e242535c3b83d110c0357fe57f110b73b143e";
assert_eq!(
digested_pullspec("quay.io/example/foo:bar", digest),
format!("quay.io/example/foo:bar@{digest}")
);
assert_eq!(
digested_pullspec("quay.io/example/foo@sha256:otherdigest", digest),
format!("quay.io/example/foo@{digest}")
);
assert_eq!(
digested_pullspec("quay.io/example/foo", digest),
format!("quay.io/example/foo@{digest}")
);
}
#[test]
fn test_find_mount_option() {
const V1: &str = "rw,relatime,compress=foo,subvol=blah,fast";
assert_eq!(find_mount_option(V1, "subvol").unwrap(), "blah");
assert_eq!(find_mount_option(V1, "rw"), None);
assert_eq!(find_mount_option(V1, "somethingelse"), None);
}
#[test]
fn test_sigpolicy_from_opts() {
assert_eq!(sigpolicy_from_opt(true), SignatureSource::ContainerPolicy);
assert_eq!(
sigpolicy_from_opt(false),
SignatureSource::ContainerPolicyAllowInsecure
);
}
#[test]
fn test_relative_path() {
let from = Path::new("/sysroot/state/deploy/image_id");
let to = Path::new("/sysroot/state/os/default/var");
assert_eq!(
path_relative_to(from, to).unwrap(),
PathBuf::from("../../os/default/var")
);
assert_eq!(
path_relative_to(&Path::new("state/deploy"), to)
.unwrap_err()
.to_string(),
"Paths must be absolute"
);
}
#[test]
fn test_have_executable() {
assert!(have_executable("true").unwrap());
assert!(!have_executable("someexethatdoesnotexist").unwrap());
}
}