Skip to content

Commit db29e2f

Browse files
committed
ostree-ext/tar/export: add unmap_path and rework map_path to share inner fn
In preparation for (un)mapping tar stream data back to ostree data so we can look up the correct metadata for a given tar entry
1 parent 19616ef commit db29e2f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

ostree-ext/src/tar/export.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,35 @@ mode=bare-split-xattrs
5656
/// System calls are expensive.
5757
const BUF_CAPACITY: usize = 131072;
5858

59-
/// Convert /usr/etc back to /etc
60-
fn map_path(p: &Utf8Path) -> std::borrow::Cow<Utf8Path> {
61-
match p.strip_prefix("./usr/etc") {
62-
Ok(r) => Cow::Owned(Utf8Path::new("./etc").join(r)),
59+
/// Convert `from` to `to`
60+
fn map_path_inner<'p>(
61+
p: &'p Utf8Path,
62+
from: &'_ str,
63+
to: &'_ str,
64+
) -> std::borrow::Cow<'p, Utf8Path> {
65+
match p.strip_prefix(from) {
66+
Ok(r) => {
67+
if r.components().count() > 0 {
68+
Cow::Owned(Utf8Path::new(to).join(r))
69+
} else {
70+
Cow::Owned(Utf8PathBuf::from(to))
71+
}
72+
}
6373
_ => Cow::Borrowed(p),
6474
}
6575
}
6676

77+
/// Convert /usr/etc back to /etc
78+
fn map_path(p: &Utf8Path) -> std::borrow::Cow<Utf8Path> {
79+
map_path_inner(p, "./usr/etc", "./etc")
80+
}
81+
82+
/// Convert etc to usr/etc
83+
/// Note: no leading '/' or './'
84+
fn unmap_path(p: &Utf8Path) -> std::borrow::Cow<Utf8Path> {
85+
map_path_inner(p, "etc", "usr/etc")
86+
}
87+
6788
/// Convert usr/etc back to etc for the tar stream.
6889
fn map_path_v1(p: &Utf8Path) -> &Utf8Path {
6990
debug_assert!(!p.starts_with("/") && !p.starts_with("."));

0 commit comments

Comments
 (0)