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
38 changes: 34 additions & 4 deletions pkgs/id/crates/id-sync/src/fileops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,21 @@ async fn free_archive_name(store: &Store, name: &str, ts: u64) -> Result<String>
}
}

/// The archive timestamp of `name` when it has the archive tag shape used by
/// FileOps: `{stem}.archive.{ts}` with a non-empty stem and an unsigned
/// decimal `ts` that fits in u64. Readers that classify archives must use this
/// same rule so writer and reader never disagree on a name.
pub fn archive_tag_timestamp(name: &str) -> Option<u64> {
let (file_name, suffix) = name.rsplit_once(".archive.")?;
if file_name.is_empty() || suffix.is_empty() || !suffix.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
suffix.parse::<u64>().ok()
}

/// Whether `name` has the archive tag shape used by FileOps.
fn is_archive_tag_name(name: &str) -> bool {
let Some((file_name, suffix)) = name.rsplit_once(".archive.") else {
return false;
};
!file_name.is_empty() && !suffix.is_empty() && suffix.bytes().all(|b| b.is_ascii_digit())
archive_tag_timestamp(name).is_some()
Comment on lines 196 to +197

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align archive deletion before admitting overflowing suffixes

With this change, users can create an ordinary file named report.archive.18446744073709551616, but deleting report also deletes that independent file and its metadata. FileOps::delete still collects every tag with the matching prefix and an all-digit suffix (fileops.rs:711–715), regardless of whether the suffix fits in u64 or the tag has archive ownership metadata. Update the deletion scan and ownership predicate to use the shared archive rule before relaxing the writer's reservation.

Useful? React with 👍 / 👎.

}

/// Resolve a free archive name for `name` at `ts` and claim it for
Expand Down Expand Up @@ -797,6 +806,27 @@ fn is_owned_archive_tag_name(name: &str, archive_name: &str) -> bool {
.is_some_and(|suffix| !suffix.is_empty() && suffix.bytes().all(|b| b.is_ascii_digit()))
}

#[cfg(test)]
mod archive_tag_shape_tests {
use super::archive_tag_timestamp;

#[test]
fn archive_tag_timestamp_matches_writer_and_reader_rule() {
assert_eq!(archive_tag_timestamp("x.archive.5"), Some(5));
assert_eq!(
archive_tag_timestamp("x.archive.18446744073709551615"),
Some(u64::MAX)
);
assert_eq!(archive_tag_timestamp(".archive.5"), None);
assert_eq!(
archive_tag_timestamp("x.archive.18446744073709551616"),
None
);
assert_eq!(archive_tag_timestamp("x.archive.+5"), None);
assert_eq!(archive_tag_timestamp("x.archive.notes"), None);
}
}

#[cfg(test)]
mod archive_reference_tests {
use super::is_owned_archive_tag_name;
Expand Down
35 changes: 19 additions & 16 deletions pkgs/id/crates/id-web/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,11 @@ fn classify_tag(name: &str) -> (FileKind, Option<u64>) {
}
}

// Check for archive: "{name}.archive.{unix_timestamp}"
// Only an archive when the suffix actually parses as a unix
// timestamp (#2793) — the same rule #809 applied to the "auto-"
// prefix above. An ordinary file that merely contains ".archive."
// (notes.archive.txt) is Primary. No producer mints a non-numeric
// suffix: the archive claim path always formats a u64.
if let Some(pos) = name.rfind(".archive.") {
let ts_str = &name[pos + ".archive.".len()..];
// The producer's Display form is unsigned decimal digits only:
// parse::<u64>() would also accept a leading '+', a spelling no
// producer mints (#4329).
if !ts_str.is_empty() && ts_str.bytes().all(|b| b.is_ascii_digit()) {
if let Ok(ts) = ts_str.parse::<u64>() {
return (FileKind::Archive, Some(ts));
}
}
// Check for archive: "{name}.archive.{unix_timestamp}". The shared
// FileOps rule decides it, so the writer's reservation and this reader
// classify every name the same way (#2793, #4329, #11790).
if let Some(ts) = id_sync::fileops::archive_tag_timestamp(name) {
return (FileKind::Archive, Some(ts));
}

(FileKind::Primary, None)
Expand Down Expand Up @@ -5457,6 +5446,20 @@ mod tests {
}
}

#[test]
fn test_classify_tag_archive_matches_writer_rule() {
// Regression (#11790): the reader must agree with the FileOps writer
// on empty-stem and u64-overflow archive suffixes.
for name in [".archive.5", "x.archive.18446744073709551616"] {
let (kind, ts) = classify_tag(name);
assert_eq!(kind, FileKind::Primary, "{name}");
assert!(ts.is_none(), "{name}");
}
let (kind, ts) = classify_tag("x.archive.18446744073709551615");
assert_eq!(kind, FileKind::Archive);
assert_eq!(ts, Some(u64::MAX));
}

#[test]
fn test_parse_iso_timestamp_rejects_out_of_range_components() {
// Each of these used to roll over into a different, plausible
Expand Down