Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importer: Canonicalize symlink targets #3266

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions rust/src/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use crate::cxxrsutil::{CxxResult, FFIGObjectWrapper};
use crate::utils;
use anyhow::{bail, format_err, Result};
use camino::{Utf8Path, Utf8PathBuf};
use fn_error_context::context;
use gio::{FileInfo, FileType};
use ostree::RepoCommitFilterResult;
Expand All @@ -17,6 +18,23 @@ use std::borrow::Cow;
use std::fmt::Write;
use std::pin::Pin;

/// Canonicalize a path, e.g. replace `//` with `/` and `././` with `./`.
// For some background behind this, see https://github.com/alexcrichton/tar-rs/pull/274
// The specific problem case was:
// # rpm -qf /usr/lib/systemd/systemd-sysv-install
// chkconfig-1.13-2.el8.x86_64
// # ll /usr/lib/systemd/systemd-sysv-install
// lrwxrwxrwx. 2 root root 24 Nov 29 18:08 /usr/lib/systemd/systemd-sysv-install -> ../../..//sbin/chkconfig
// #
fn canonicalize_path(p: &str) -> String {
let p = Utf8Path::new(p);
let mut r = Utf8PathBuf::new();
for part in p.components() {
r.push(part);
}
r.into_string()
}

/// Adjust mode for specific file entries.
pub fn tweak_imported_file_info(
mut file_info: Pin<&mut crate::FFIGFileInfo>,
Expand Down Expand Up @@ -44,6 +62,18 @@ pub fn tweak_imported_file_info(
file_info.set_attribute_uint32("unix::mode", mode);
}
}

if filetype == FileType::SymbolicLink {
if let Some(target) = file_info.symlink_target() {
// See above, this is a special case hack until
// https://github.com/fedora-sysv/chkconfig/pull/67 propagates everywhere
// and/or https://github.com/ostreedev/ostree-rs-ext/pull/182 merges.
if target.ends_with("//sbin/chkconfig") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I had in mind was a filter matching on the exact source path (i.e. not the target content), but it should be an irrelevant difference. This works for me too.

let canonicalized = &canonicalize_path(&target);
file_info.set_symlink_target(canonicalized);
}
}
}
}

/// Apply filtering and manipulation logic to an RPM file before importing.
Expand Down Expand Up @@ -186,6 +216,21 @@ fn fix_tmpfiles_path(abs_path: Cow<str>) -> Cow<str> {
mod tests {
use super::*;

#[test]
fn test_canonicalize_path() {
let canonical = &["/", "/usr", "../usr/share", "../../usr/lib/systemd/system"];
for &k in canonical {
assert_eq!(k, canonicalize_path(k));
}
let noncanonical = &[
("./././foo", "./foo"),
("../../..//sbin/chkconfig", "../../../sbin/chkconfig"),
];
for k in noncanonical {
assert_eq!(canonicalize_path(k.0), k.1);
}
}

#[test]
fn test_path_is_compliant() {
let ostree_cases = &["/", "/usr", "/usr/share", "/bin/foo", "/usr/lib/opt/bar"];
Expand Down