Skip to content

Commit

Permalink
correctly return extra hashes even if they're cached
Browse files Browse the repository at this point in the history
  • Loading branch information
chadaustin committed Dec 22, 2024
1 parent d096ef7 commit 2b77f50
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
105 changes: 104 additions & 1 deletion src/cmd/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,119 @@ async fn process_file(
.add_image_metadata(&content_metadata.blake3, image_metadata.as_ref().unwrap())?;
}

let has_extra_hashes = current_hashes.extra_hashes.md5.is_some()
&& current_hashes.extra_hashes.sha1.is_some()
&& current_hashes.extra_hashes.sha256.is_some();

Ok(ProcessFileResult {
path,
blake3_computed,
content_metadata,
image_metadata_computed,
image_metadata,
extra_hashes: if extra_hashes_computed {
extra_hashes: if has_extra_hashes {
Some(current_hashes.extra_hashes)
} else {
None
},
})
}

#[cfg(test)]
mod tests {
use super::*;
use std::time::SystemTime;

#[tokio::test(flavor = "multi_thread")]
async fn process_file_computes_blake3() -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(Database::open_memory()?));
let pixel_semaphore = Arc::new(Semaphore::new(1));
let file_info = FileInfo {
inode: 1,
size: 2,
mtime: SystemTime::now(),
};

let pfr = process_file(
db,
pixel_semaphore,
String::from("tests/images/Moonlight.heic"),
file_info,
false,
)
.await?;
assert_eq!(
"d8828886771faa4da22c36c352acdbf0988f780b457dd8525499a3f2153a25d5",
hex::encode(pfr.content_metadata.blake3)
);
assert_eq!(None, pfr.extra_hashes);

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn process_file_extra_hashes_twice() -> anyhow::Result<()> {
let db = Arc::new(Mutex::new(Database::open_memory()?));
let pixel_semaphore = Arc::new(Semaphore::new(1));
let file_info = FileInfo {
inode: 1,
size: 2,
mtime: SystemTime::now(),
};

let pfr = process_file(
db.clone(),
pixel_semaphore.clone(),
"tests/images/Moonlight.heic".to_owned(),
file_info.clone(),
true,
)
.await?;
assert_eq!(
"d8828886771faa4da22c36c352acdbf0988f780b457dd8525499a3f2153a25d5",
hex::encode(pfr.content_metadata.blake3)
);
let extra_hashes = pfr.extra_hashes.as_ref();
assert_eq!(
Some("e5dae7611472d7102fc3a05a16152247".to_owned()),
extra_hashes.and_then(|eh| eh.md5).map(hex::encode)
);
assert_eq!(
Some("3260706646db71bb48cc4165e46410fde3e98a44".to_owned()),
extra_hashes.and_then(|eh| eh.sha1).map(hex::encode)
);
assert_eq!(
Some("d5b1055e3a5f5fc68d5a1ae706639f3cd1bd34349e6db7003e48cb11f755d3e8".to_owned()),
extra_hashes.and_then(|eh| eh.sha256).map(hex::encode)
);

// We should have saved this information in the database. Look it up again.
let pfr = process_file(
db,
pixel_semaphore,
"tests/images/Moonlight.heic".to_owned(),
file_info,
true,
)
.await?;
assert_eq!(
"d8828886771faa4da22c36c352acdbf0988f780b457dd8525499a3f2153a25d5",
hex::encode(pfr.content_metadata.blake3)
);
let extra_hashes = pfr.extra_hashes.as_ref();
assert_eq!(
Some("e5dae7611472d7102fc3a05a16152247".to_owned()),
extra_hashes.and_then(|eh| eh.md5).map(hex::encode)
);
assert_eq!(
Some("3260706646db71bb48cc4165e46410fde3e98a44".to_owned()),
extra_hashes.and_then(|eh| eh.sha1).map(hex::encode)
);
assert_eq!(
Some("d5b1055e3a5f5fc68d5a1ae706639f3cd1bd34349e6db7003e48cb11f755d3e8".to_owned()),
extra_hashes.and_then(|eh| eh.sha256).map(hex::encode)
);

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type IMPath = String;
/// TODO: Restic makes a good case that ctime should also be included
/// in this set. We should backfill.
/// https://restic.readthedocs.io/en/latest/040_backup.html#file-change-detection
#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct FileInfo {
/// 0 on Windows for now. May contain file_index() when the API is
/// stabilized.
Expand Down
2 changes: 1 addition & 1 deletion tests/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn saturn_v() -> anyhow::Result<()> {

#[tokio::test(flavor = "multi_thread")]
async fn blake3() -> anyhow::Result<()> {
let b3 = compute_blake3("tests//images/Moonlight.heic".into()).await?;
let b3 = compute_blake3("tests/images/Moonlight.heic".into()).await?;
assert_eq!(
"d8828886771faa4da22c36c352acdbf0988f780b457dd8525499a3f2153a25d5",
hex::encode(b3)
Expand Down

0 comments on commit 2b77f50

Please sign in to comment.