Skip to content

Commit

Permalink
Add 'unwind_symlinks' flag to multiple FS functions
Browse files Browse the repository at this point in the history
This flag allows the user to disable the symlink unwinding
  • Loading branch information
maxkofler committed Dec 8, 2023
1 parent 38d1ab3 commit 3e535c7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/package/built.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ pub struct BuiltPackage {

impl BuiltPackage {
/// Constructs a built package from a formula package, the built architecture and the index of its file contents
///
/// This will **not** unwind symlinks to prevent double treatment of files
/// # Arguments
/// * `src` - The source `FormulaPackage` to construct this package from
/// * `arch` - The architecture the package has been built for
/// * `path` - The path to the package, containing the `data/` directory
pub fn from_formula(src: FormulaPackage, arch: String, path: &Path) -> Result<Self, Error> {
let index = Directory::index(&path.join("data"), true)?;
pub fn from_formula(src: FormulaFile, arch: String, path: &Path) -> Result<Self, Error> {
let index = Directory::index(&path.join("data"), true, false)?;

Ok(Self {
name: src.name,
Expand Down
5 changes: 4 additions & 1 deletion src/package/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub struct InstalledPackage {

impl InstalledPackage {
/// Creates a new `InstalledPackage` from a `IndexPackage` by parsing the package metadata file and indexing the `root` directory
///
/// An installed package will unwind symlinks, so symlinks to ELF files get treated as ELF files to ensure
/// discoverability by validators
/// # Arguments
/// * `index_pkg` - The IndexPackage to use for information on where to find the package
/// * `acacia_dir` - The path to the `/acacia` directory to search for packages
Expand All @@ -45,7 +48,7 @@ impl InstalledPackage {
let pkg_meta_path = pkg_path.join("package.toml");
let pkg_meta: PackageFile = parse_toml(&pkg_meta_path).e_context(context)?;

let dir = Directory::index(&pkg_path.join("root"), true).e_context(context)?;
let dir = Directory::index(&pkg_path.join("root"), true, true).e_context(context)?;

Ok(Self {
name: pkg_meta.package.name,
Expand Down
9 changes: 7 additions & 2 deletions src/util/fs/fsentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ impl FSEntry {
/// Tries to infer the type of filesystem entry by using the `infer` crate
/// # Arguments
/// * `path` - The path to infer
pub fn infer(path: &Path) -> Result<Self, Error> {
/// * `do_unwind_symlinks` - If this function should trace symlinks to their destination
pub fn infer(path: &Path, do_unwind_symlinks: bool) -> Result<Self, Error> {
// Store the filename prior to unwinding symlinks, so symlinked files keep their name
let name = path.file_name().expect("Filename").to_owned();
let path = unwind_symlinks(path);
let path = if do_unwind_symlinks {
unwind_symlinks(path)
} else {
path.to_owned()
};

if path.is_symlink() {
trace!("[infer] SLNK: {}", path.to_string_lossy());
Expand Down
7 changes: 4 additions & 3 deletions src/util/fs/fsentry/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ impl Directory {
/// # Arguments
/// * `path` - The path to walk
/// * `recursive` - If this function should operate recursively
/// * `do_unwind_symlinks` - If this function should unwind symlinks to their real destination
/// # Errors
/// Uses the std::fs::read_dir() function which will error on:
/// - The `path` does not exist
/// - Permission is denied
/// - The `path` is not a directory
pub fn index(path: &Path, recursive: bool) -> Result<Self, Error> {
pub fn index(path: &Path, recursive: bool, do_unwind_symlinks: bool) -> Result<Self, Error> {
let mut index = Self {
name: path.file_name().unwrap_or_default().to_owned(),
children: Vec::new(),
Expand All @@ -45,12 +46,12 @@ impl Directory {
// Do only walk a subdirectory if it is not a symlink
if !path.is_symlink() && path.is_dir() && recursive {
index.children.push(FSEntry::Directory(
Directory::index(&path, recursive)
Directory::index(&path, recursive, do_unwind_symlinks)
.e_context(|| format!("Indexing {}", &path.to_string_lossy()))?,
));
} else {
index.children.push(
FSEntry::infer(&path)
FSEntry::infer(&path, do_unwind_symlinks)
.e_context(|| format!("Inferring type of {}", &path.to_string_lossy()))?,
);
}
Expand Down

0 comments on commit 3e535c7

Please sign in to comment.