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

cli: Add workaround for Windows path canonicalization failures #104

Merged
merged 1 commit into from
Dec 24, 2024
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
4 changes: 2 additions & 2 deletions xdvdfs-cli/src/cmd_compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use xdvdfs::{
write::{self, img::ProgressInfo},
};

use crate::img::{open_image_raw, with_extension};
use crate::img::{absolute_path, open_image_raw, with_extension};

#[derive(Args)]
#[command(about = "Pack and compress an image from a given directory or source ISO image")]
Expand Down Expand Up @@ -65,7 +65,7 @@ pub async fn cmd_compress(args: &CompressArgs) -> Result<(), anyhow::Error> {

// This is unlikely to happen, since compressed input is unsupported
// and this will fail anyway, but we check to avoid truncating the input accidentally
if image_path.exists() && image_path.canonicalize()? == source_path {
if image_path.exists() && absolute_path(&image_path)? == source_path {
return Err(anyhow::anyhow!("Source and destination paths are the same"));
}

Expand Down
11 changes: 6 additions & 5 deletions xdvdfs-cli/src/cmd_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Args;
use maybe_async::maybe_async;
use xdvdfs::write::{self, img::ProgressInfo};

use crate::img::with_extension;
use crate::img::{absolute_path, with_extension};

#[derive(Args)]
#[command(about = "Pack an image from a given directory or source ISO image")]
Expand All @@ -22,7 +22,7 @@ fn get_default_image_path(source_path: &Path, is_dir: bool) -> Result<PathBuf, a
.ok_or(anyhow::anyhow!("Failed to get file name from source path"))?;
let output = with_extension(Path::new(source_file_name), "iso", is_dir);

if output.exists() && output.canonicalize()? == source_path {
if output.exists() && absolute_path(&output)? == source_path {
return Ok(with_extension(
Path::new(source_file_name),
"xiso.iso",
Expand All @@ -35,7 +35,8 @@ fn get_default_image_path(source_path: &Path, is_dir: bool) -> Result<PathBuf, a

#[maybe_async]
pub async fn cmd_pack(args: &PackArgs) -> Result<(), anyhow::Error> {
let source_path = PathBuf::from(&args.source_path).canonicalize()?;
let source_path = Path::new(&args.source_path);
let source_path = absolute_path(source_path)?;
let meta = std::fs::metadata(&source_path)?;
let is_dir = meta.is_dir();

Expand All @@ -46,7 +47,7 @@ pub async fn cmd_pack(args: &PackArgs) -> Result<(), anyhow::Error> {
.map(Ok)
.unwrap_or_else(|| get_default_image_path(&source_path, is_dir))?;

if image_path.exists() && image_path.canonicalize()? == source_path {
if image_path.exists() && absolute_path(&image_path)? == source_path {
return Err(anyhow::anyhow!("Source and destination paths are the same"));
}

Expand All @@ -57,7 +58,7 @@ pub async fn cmd_pack(args: &PackArgs) -> Result<(), anyhow::Error> {
.open(&image_path)?;
let mut image = std::io::BufWriter::with_capacity(1024 * 1024, image);

if image_path.canonicalize()?.starts_with(&source_path) {
if absolute_path(&image_path)?.starts_with(&source_path) {
return Err(anyhow::anyhow!(
"Destination path is contained by source path"
));
Expand Down
11 changes: 11 additions & 0 deletions xdvdfs-cli/src/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ pub async fn open_image(
}
}

/// Returns the result of std::fs::canonicalize, if it returned without error,
/// otherwise returns the result of std::path::absolute
/// std::fs::canonicalize will follow symlinks, but seems to error on some filesystems
/// on Windows. Following symlinks is desirable, so it is attempted first
pub fn absolute_path(path: &Path) -> std::io::Result<PathBuf> {
match std::fs::canonicalize(path) {
Err(_) => std::path::absolute(path),
ok => ok,
}
}

/// Similar to Path::with_extension, but will not overwrite the extension for
/// directories
// TODO: Replace with `Path::with_added_extension` after it stabilizes
Expand Down
Loading