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

Handle symlinks in nodes_path or classes_path as separate entities #26

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use anyhow::{anyhow, Result};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use std::collections::HashMap;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
use walkdir::WalkDir;

use inventory::Inventory;
Expand All @@ -44,6 +44,30 @@ pub struct Reclass {
nodes: HashMap<String, PathBuf>,
}

/// Converts `p` to an absolute path, but doesn't resolve symlinks. The function does normalize the
/// path by resolving any `.` and `..` components which are present.
///
/// Copied from https://internals.rust-lang.org/t/path-to-lexical-absolute/14940.
fn to_lexical_absolute(p: &Path) -> Result<PathBuf> {
let mut absolute = if p.is_absolute() {
PathBuf::new()
} else {
std::env::current_dir()?
};
for component in p.components() {
match component {
Component::CurDir => { /* do nothing for `.` components */ }
Component::ParentDir => {
// pop the last element that we added for `..` components
absolute.pop();
}
// just push the component for any other component
component => absolute.push(component.as_os_str()),
}
}
Ok(absolute)
}

fn err_duplicate_entity(root: &str, relpath: &Path, cls: &str, prev: &Path) -> Result<()> {
fn stringify(p: &Path) -> Result<&str> {
p.to_str()
Expand Down Expand Up @@ -74,7 +98,7 @@ fn walk_entity_dir(
entity_map: &mut HashMap<String, PathBuf>,
max_depth: usize,
) -> Result<()> {
let entity_root = PathBuf::from(root).canonicalize()?;
let entity_root = to_lexical_absolute(&PathBuf::from(root))?;

for entry in WalkDir::new(root).max_depth(max_depth) {
let entry = entry?;
Expand All @@ -85,13 +109,13 @@ fn walk_entity_dir(
};
if ext.is_some() && SUPPORTED_YAML_EXTS.contains(&ext.unwrap()) {
// it's an entity (class or node), process it
let abspath = entry.path().canonicalize()?;
let abspath = to_lexical_absolute(entry.path())?;
let relpath = abspath.strip_prefix(&entity_root)?;
let cls = relpath
.with_extension("")
.to_str()
.ok_or(anyhow!(
"Failed to canonicalize entity {}",
"Failed to normalize entity {}",
entry.path().display()
))?
.replace(MAIN_SEPARATOR, ".");
Expand Down
4 changes: 2 additions & 2 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use yaml_merge_keys::merge_keys_serde;
use crate::list::{List, RemovableList, UniqueList};
use crate::refs::Token;
use crate::types::{Mapping, Value};
use crate::Reclass;
use crate::{to_lexical_absolute, Reclass};

mod nodeinfo;

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Node {
invpath.push(npath);
let ncontents = std::fs::read_to_string(invpath.canonicalize()?)?;

meta.uri = format!("yaml_fs://{}", invpath.canonicalize()?.display());
meta.uri = format!("yaml_fs://{}", to_lexical_absolute(&invpath)?.display());

Node::from_str(meta, None, &ncontents)
}
Expand Down
1 change: 1 addition & 0 deletions tests/inventory/classes/config_symlink.yml
17 changes: 10 additions & 7 deletions tests/test_nodeinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import reclass_rs

import platform
from pathlib import Path


Expand All @@ -10,12 +9,7 @@ def test_nodeinfo_n1():
)
n = r.nodeinfo("n1")
npath = Path("./tests/inventory/nodes/n1.yml").resolve()
unc_infix = ""
if platform.system() == "Windows":
# On Windows, Rust's std::fs::canonicalize uses UNC paths, so the path is prefixed with
# `\\?\`. Python's pathlib doesn't do that when resolving paths.
unc_infix = "\\\\?\\"
assert n.__reclass__.uri == f"yaml_fs://{unc_infix}{npath}"
assert n.__reclass__.uri == f"yaml_fs://{npath}"
assert n.applications == ["app1", "app2"]
assert n.classes == ["cls1", "cls2"]
assert n.parameters == {
Expand All @@ -33,6 +27,15 @@ def test_nodeinfo_n1():
}


def test_nodeinfo_n1_meta_symlink():
r = reclass_rs.Reclass(
nodes_path="./tests/inventory/targets", classes_path="./tests/inventory/classes"
)
n = r.nodeinfo("n1")
npath = Path("./tests/inventory/targets/n1.yml").absolute()
assert n.__reclass__.uri == f"yaml_fs://{npath}"


def test_nodeinfo_n2():
r = reclass_rs.Reclass(
nodes_path="./tests/inventory/nodes", classes_path="./tests/inventory/classes"
Expand Down