diff --git a/src/gallery_entry.rs b/src/gallery_entry.rs
@@ -2,7 +2,7 @@ use serde::{
de::{Deserializer, Error, Unexpected},
Deserialize,
};
-use std::{fmt::{self, Display}, path::PathBuf};
+use std::{fmt::{self, Display}, path::PathBuf, fs::{self, Metadata}};
const LICENSES: &[&str] = &[
"proprietary",
@@ -49,7 +49,7 @@ const LICENSES: &[&str] = &[
const FILE_FORMATS: &[&str] = &["tikz", "eps", "png", "jpeg", "jpg"];
/// Info on a individual entry on the gallery
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone)]
pub struct GalleryEntry {
pub path: PathBuf,
pub file_name: String,
@@ -60,6 +60,7 @@ pub struct GalleryEntry {
pub source: Option<String>,
pub author: String,
pub author_url: Option<String>,
+ pub metadata: Option<Metadata>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -254,6 +255,7 @@ impl<'de> Deserialize<'de> for GalleryEntry {
caption,
author: author.trim().to_string(),
author_url: author_url.map(|s| s.trim().to_string()),
+ metadata: fs::symlink_metadata(&path).ok(),
license,
})
} else {
diff --git a/src/main.rs b/src/main.rs
@@ -6,7 +6,7 @@ use std::{
fmt::{self, Display},
fs::{self, File},
io::{self, Write},
- path::{PathBuf, Path},
+ path::PathBuf,
process::{ExitCode, Command},
sync::mpsc,
os::unix,
@@ -255,8 +255,12 @@ fn render_pic_page(pic: &GalleryEntry, full_build: bool) -> RenderResult {
// Only try to re-render HTML page in case the page is older than the
// image file
- if !full_build && !needs_update(&path, &pic.path) {
- return RenderResult::Skipped;
+ if !full_build {
+ if let (Ok(path_m), Some(pic_m)) = (fs::metadata(&path), &pic.metadata) {
+ if path_m.modified().unwrap() > pic_m.modified().unwrap() {
+ return RenderResult::Skipped;
+ }
+ }
}
let mut f = match File::create(&path) {
@@ -433,14 +437,20 @@ fn write_license(f: &mut File) -> io::Result<()> {
fn render_thumbnail(pic: GalleryEntry, full_build: bool) -> RenderResult {
let thumb_path = thumb_path(&pic);
- // Only try to render thumbnail in case the thumbnail file in the machine
- // is older than the source file
- if !full_build && !needs_update(&thumb_path, &pic.path) {
- warnln!(
- "Skipped rendering the thumbnail for {name:?} (use {FULL_BUILD_OPT} to overwrite)",
- name = pic.file_name
- );
- return RenderResult::Skipped;
+ // Here we do not want to call fs::symlink_metada: we want to know when was
+ // the symlink last updated
+ let thumb_meta = fs::metadata(&thumb_path);
+
+ if !full_build {
+ if let (Ok(thumb_m), Some(pic_m)) = (&thumb_meta, &pic.metadata) {
+ if thumb_m.modified().unwrap() > pic_m.modified().unwrap() {
+ warnln!(
+ "Skipped rendering the thumbnail for {name:?} (use {FULL_BUILD_OPT} to overwrite)",
+ name = pic.file_name
+ );
+ return RenderResult::Skipped;
+ }
+ }
}
match pic.file_format {
@@ -506,10 +516,8 @@ fn render_thumbnail(pic: GalleryEntry, full_build: bool) -> RenderResult {
// Delete the thumbnail file if it exists already: fs::symlink does
// not override files
- if let Ok(meta) = fs::symlink_metadata(&thumb_path) {
- if meta.is_file() || meta.is_symlink() {
- let _ = fs::remove_file(&thumb_path);
- }
+ if let Ok(true) = thumb_meta.map(|m| m.is_file() || m.is_symlink()) {
+ let _ = fs::remove_file(&thumb_path);
}
if let Err(err) = unix::fs::symlink(&src_path, &thumb_path) {
@@ -598,18 +606,6 @@ fn thumb_path(pic: &GalleryEntry) -> PathBuf {
result
}
-/// Returns `false` if both `p1` and `p2` exist and and `p1` is newer than
-/// `f2`. Returns `true` otherwise
-fn needs_update<P1: AsRef<Path>, P2: AsRef<Path>>(p1: P1, p2: P2) -> bool {
- if let (Ok(m1), Ok(m2)) = (fs::metadata(&p1), fs::metadata(&p2)) {
- if m1.modified().unwrap() > m2.modified().unwrap() {
- return false;
- }
- }
-
- true
-}
-
impl<'a> Display for ThumbPath<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "/{THUMBS_PATH}/{name}", name = Escaped(&self.0.file_name))?;