Skip to content

Commit

Permalink
Fix sprite name generation so symlinks work
Browse files Browse the repository at this point in the history
This addresses an issue creating valid sprite names for symlinks, as
detailed in issue #81. Symlinked files were being canonicalised, which
caused a problem if the target was outside the base path. This change
replaces the canonicalisation step with `std::path::absolute()`, which
doesn't resolve symlinks and so they remain within the base path.

The change has two side-effects:

- It allows sprite names to be generated for non-existent files (a good
  thing, since there's no need for names to be linked to the
  filesystem)
- It modifies the `spreet::sprite_name()` function to return a
  `PathError` instead of an `IoError` when the `abs_path` argument is
  not an ancestor of the `path` argument.

Use of `std::path::absolute()` means the minimum supported Rust version
(MSRV) is now 1.74.
  • Loading branch information
flother committed Aug 23, 2024
1 parent 9ecbaa2 commit 014ce64
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

## Development version

- Fix issue creating valid sprite names for symlinks (see [#81](https://github.com/flother/spreet/issues/81)). This has two side effects:
1. sprite names can now be generated for files that don't exist
2. `spreet::sprite_name()` now returns a `PathError` instead of an `IoError` when the `abs_path` argument is not an ancestor of the `path` argument.
- Update [clap](https://crates.io/crates/clap) dependency to v4.5
- Update [multimap](https://crates.io/crates/multimap) dependency to v0.10
- Update [oxipng](https://crates.io/crates/multimap) dependency to v9.1
- Update [resvg](https://crates.io/crates/resvg) dependency to v0.43
- Update [assert_fs](https://crates.io/crates/assert_fs) dev dependency to v1.1
- Update [softprops/action-gh-release](https://github.com/softprops/action-gh-release) to v2

The minimum supported version of Rust is now 1.74.0 (released November 2023).
The minimum supported version of Rust is now 1.79.0 (released June 2024).

## v0.11.0 (2023-12-05)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "spreet"
version = "0.12.0-dev"
edition = "2021"
rust-version = "1.74"
rust-version = "1.79"
description = "Create a spritesheet from a set of SVG images"
readme = "README.md"
repository = "https://github.com/flother/spreet"
Expand Down
10 changes: 5 additions & 5 deletions src/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ impl Spritesheet {
///
/// This function will return an error if:
///
/// - `path` does not exist
/// - `abs_path` does not exist
/// - `abs_path` is not a ancestor of `path`
/// - `abs_path` is not an ancestor of `path`
/// - `path` is empty
/// - getting the current directory fails
pub fn sprite_name<P1: AsRef<Path>, P2: AsRef<Path>>(
path: P1,
base_path: P2,
) -> SpreetResult<String> {
let abs_path = path.as_ref().canonicalize()?;
let abs_base_path = base_path.as_ref().canonicalize()?;
let abs_path = std::path::absolute(path.as_ref())?;
let abs_base_path = std::path::absolute(base_path)?;
let Ok(rel_path) = abs_path.strip_prefix(abs_base_path) else {
return Err(SpreetError::PathError(path.as_ref().to_path_buf()));
};
Expand Down
10 changes: 5 additions & 5 deletions tests/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ fn sprite_name_works_with_deeply_nested_files() {
}

#[test]
fn sprite_name_returns_error_for_non_existent_path() {
assert_matches!(
sprite_name(Path::new("./does_not_exist.svg"), Path::new("./")),
Err(SpreetError::IoError(_))
fn sprite_name_returns_ok_for_non_existent_path() {
assert_eq!(
sprite_name(Path::new("./does_not_exist.svg"), Path::new("./")).unwrap(),
"does_not_exist"
);
}

Expand All @@ -63,7 +63,7 @@ fn sprite_name_returns_error_for_non_existent_base_path() {
Path::new("./tests/fixtures/svgs/bicycle.svg"),
Path::new("./tests/fixtures/foo"),
),
Err(SpreetError::IoError(_))
Err(SpreetError::PathError(_))
);
}

Expand Down

0 comments on commit 014ce64

Please sign in to comment.