Improve logic for store path construction.
This commit is contained in:
parent
bf4c9042f0
commit
4f2b808a29
1 changed files with 21 additions and 7 deletions
30
src/lib.rs
30
src/lib.rs
|
|
@ -43,15 +43,29 @@ impl TryFrom<PathBuf> for StorePath {
|
|||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(path: PathBuf) -> Result<Self> {
|
||||
let canon = path.canonicalize().unwrap_or(path.clone());
|
||||
if !canon.starts_with(PathBuf::from("/").join("nix").join("store")) {
|
||||
anyhow::bail!(
|
||||
"Error constructing store path, not in store: {} (canonicalised: {})",
|
||||
path.display(),
|
||||
canon.display()
|
||||
);
|
||||
let nix_store = PathBuf::from("/").join("nix").join("store");
|
||||
|
||||
if path.starts_with(&nix_store) {
|
||||
Ok(Self { store_path: path })
|
||||
} else if path.is_symlink() {
|
||||
if let Ok(target) = path.read_link() {
|
||||
if target.starts_with(&nix_store) {
|
||||
Ok(Self { store_path: target })
|
||||
} else {
|
||||
Self::try_from(target)
|
||||
}
|
||||
} else {
|
||||
anyhow::bail!(
|
||||
"Error constructing store path: cannot read symlink: {}",
|
||||
path.display()
|
||||
)
|
||||
}
|
||||
} else {
|
||||
anyhow::bail!(
|
||||
"Error constructing store path: not in nix store: {}",
|
||||
path.display()
|
||||
)
|
||||
}
|
||||
Ok(Self { store_path: canon })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue