Skip to content
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
74 changes: 73 additions & 1 deletion pkgs/id/src/fileops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use futures_lite::StreamExt;
use iroh_blobs::{Hash, api::Store};
use thiserror::Error;

use crate::tags::{TagStore, now_unix};
use crate::tags::{TagStore, TagValue, now_unix};

const ARCHIVE_IDENTITY_TAG_KEYS: [&[u8]; 5] = [
b"name",
Expand Down Expand Up @@ -182,6 +182,36 @@ pub(crate) async fn claim_archive_name(
Ok(candidate)
}

/// Provenance metadata keys recorded at a file's first write (#4973):
/// the origin node and the upload (`created`) timestamp. Listings read
/// a file's origin and upload time from these tags on its own name.
pub(crate) const PROVENANCE_TAG_KEYS: [&[u8]; 2] = [b"origin", b"created"];

/// Snapshot `from`'s recorded provenance tags onto `to` (#5383).
///
/// An archive alias points at the same blob as its source but lives
/// under a new name, so without this copy a listing reports the
/// archive's origin as unknown and origin filtering misses it. Tags
/// are copied by content hash so the recorded values are preserved
/// exactly; a source with no recorded provenance leaves `to` with
/// none — unknown stays unknown, provenance is never invented.
pub(crate) async fn snapshot_provenance_tags(tags: &TagStore, from: &str, to: &str) -> Result<()> {
for key in PROVENANCE_TAG_KEYS {
for tag in tags.get_by_key(&tags.global, from.as_bytes(), key).await? {
tags.set_tag_hash(
&tags.global,
to.as_bytes(),
&tag.key,
tag.value.as_ref().map(TagValue::as_bytes),
tag.hash,
tag.len,
)
.await?;
}
}
Ok(())
}

impl<'a> FileOps<'a> {
/// Create file operations over the given stores.
pub const fn new(store: &'a Store, tags: &'a TagStore) -> Self {
Expand Down Expand Up @@ -445,6 +475,15 @@ impl<'a> FileOps<'a> {
None
};

// The archive alias keeps the source's recorded provenance
// (#5383): snapshot it now, before the transfer below moves
// (and deletes) the source name's tags onto `to`.
if let Some(archive_name) = &archived_original
&& let Err(e) = snapshot_provenance_tags(self.tags, from, archive_name).await
{
tracing::warn!("failed to snapshot provenance {from} -> {archive_name}: {e:#}");
}

self.store
.tags()
.delete(from)
Expand Down Expand Up @@ -893,6 +932,39 @@ mod replacement_metadata_tests {
}

#[tokio::test]
async fn rename_with_archive_preserves_source_provenance_on_archive_alias() {
let node = LocalNode::open(true).await.unwrap();
put(&node, "before.txt", b"original").await;
set_singleton(&node, "before.txt", "origin", "node-alpha").await;
set_singleton(&node, "before.txt", "created", "1700000000").await;

// The source's recorded provenance, exactly as stored.
let source_tags = tags_of(&node, "before.txt").await;
assert_single_value(&source_tags, "origin", "node-alpha");
let source_created = values(&source_tags, "created");
assert!(!source_created.is_empty(), "{source_tags:?}");

let rename = node
.ops()
.rename("before.txt", "after.txt", true)
.await
.unwrap();
let archive = rename.archived_original.unwrap();

// #5383: the archive alias carries the source's recorded
// provenance, not an unknown origin.
let archive_tags = tags_of(&node, &archive).await;
assert_single_value(&archive_tags, "origin", "node-alpha");
assert_eq!(values(&archive_tags, "created"), source_created);

// The live name keeps its provenance too (tags move with it).
let live_tags = tags_of(&node, "after.txt").await;
assert_single_value(&live_tags, "origin", "node-alpha");
assert_eq!(values(&live_tags, "created"), source_created);

node.shutdown().await.unwrap();
}

async fn rename_onto_existing_moves_replaced_metadata_and_refreshes_archive_identity() {
let node = LocalNode::open(true).await.unwrap();
seed_replacement_pair(&node).await;
Expand Down
67 changes: 65 additions & 2 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use super::templates::{
render_file_list_content, render_main_page_wrapper, render_media_viewer, render_page,
render_peers, render_settings,
};
use crate::fileops::{FileOpError, FileOps, claim_archive_name};
use crate::fileops::{FileOpError, FileOps, claim_archive_name, snapshot_provenance_tags};
use crate::tags::TagStore;

/// Default number of files per page.
const DEFAULT_PER_PAGE: usize = 50;
Expand Down Expand Up @@ -1633,7 +1634,8 @@ async fn save_handler(State(state): State<AppState>, Json(req): Json<SaveRequest
let document_id = published.id.to_hex();

// Archive the original blob under a timestamped tag
let archive_name = archive_original_tag(&state.store, &req.name, &req.doc_id).await;
let archive_name =
archive_original_tag(&state.store, &state.tag_store, &req.name, &req.doc_id).await;

// Set the tag to point to the new blob
let tag = iroh_blobs::api::Tag::from(req.name.clone());
Expand Down Expand Up @@ -2726,6 +2728,7 @@ fn extract_raw_text(doc: &serde_json::Value) -> String {
/// creates a new tag `{name}.archive.{timestamp}` pointing to the same hash.
async fn archive_original_tag(
store: &iroh_blobs::api::Store,
tag_store: &TagStore,
name: &str,
expected_hash: &str,
) -> Option<String> {
Expand Down Expand Up @@ -2760,6 +2763,14 @@ async fn archive_original_tag(
// concurrent same-second archives could collide (#2462).
match claim_archive_name(store, name, timestamp, expected).await {
Ok(archive_name) => {
// The archive alias keeps the source's recorded provenance
// (#5383), so listings report its real origin and origin
// filtering finds it.
if let Err(err) = snapshot_provenance_tags(tag_store, name, &archive_name).await {
tracing::warn!(
"[routes] Failed to snapshot provenance {name} -> {archive_name}: {err:#}"
);
}
tracing::info!(
"[routes] Archived original: {} -> {}",
archive_name,
Expand Down Expand Up @@ -2894,6 +2905,58 @@ mod save_publication_tests {
assert!(state.local_events.lock().await.history(second_id).is_ok());
}

#[tokio::test]
async fn archive_original_tag_preserves_recorded_provenance() {
let (state, _endpoint, _docs) = state().await;
let outcome = state
.store
.blobs()
.add_bytes(b"original bytes".to_vec())
.await
.unwrap();
let hash = outcome.hash;
state
.store
.tags()
.set(iroh_blobs::api::Tag::from("doc.md".to_owned()), hash)
.await
.unwrap();
for (key, value) in [("origin", "node-alpha"), ("created", "1700000000")] {
state
.tag_store
.set_singleton(
&state.tag_store.global,
b"doc.md",
key.as_bytes(),
Some(value.as_bytes()),
b"",
)
.await
.unwrap();
}

let archive =
archive_original_tag(&state.store, &state.tag_store, "doc.md", &hash.to_string())
.await
.unwrap();
assert!(archive.starts_with("doc.md.archive."), "{archive}");

// #5383: the archive alias carries the source's recorded
// provenance, so listings report its real origin.
for (key, expected) in [
(b"origin".as_slice(), "node-alpha"),
(b"created".as_slice(), "1700000000"),
] {
let tags = state
.tag_store
.get_by_key(&state.tag_store.global, archive.as_bytes(), key)
.await
.unwrap();
assert_eq!(tags.len(), 1, "{key:?}: {tags:?}");
assert_eq!(tags[0].value.as_ref().unwrap().as_str(), Some(expected));
}
}

#[tokio::test]
async fn refused_filename_tag_does_not_return_a_success_response() {
let (state, _endpoint, _docs) = state().await;
Expand Down
Loading