Track owners and hashes¶
One tape can hold tracks from several sources, and one track can lie in several files. A reader then has two questions: which source a track came from, and whether two recordings hold the same data for it. A track's owner answers the first and its track hash the second. Both build on Pieces and chains.
Tracks of several owners¶
Each track definition names an owner, TrackDef::owner: 32 bytes that ww_tape does not interpret. Zeros, the
default, mean the owner of the piece.
The owner is not hashed into the track key or the track id. Two owners can therefore have tracks with the same id, and a reader tells such tracks apart by id and owner together.
Two definitions with the same key and different owners are two tracks. Each has its own item numbers, its own rows in the piece head and the close, and its own hash.
For example, an application records two devices of the same type into one tape. It defines each device's tracks with the same keys, and with the device's id as the owner.
A track's hash¶
A TrackHash holds each track's running hash, the track hash. It starts as 32 zero bytes and takes in each
chunk of the track and each of its records as the writer writes them.
The writer leaves out puts and copies, which repeat what is already on the tape. The piece head states each track's hash before the piece, and the close record states it after the piece.
Closed::tracks reports the hash when a piece ends.
The hash depends only on the track's own data, not on where the data lies. A track written alone or among other tracks, in one piece or many, in one file or many, ends with the same hash.
The hash takes in each chunk whole, with its head and how its columns are coded. Two recordings with the same hash for a track hold the same chunks, and so the same data. The same items cut into other chunks, or coded with another codec, hash differently, so a different hash alone does not prove different data. A subset of a file that keeps a track's chunks and records unchanged ends with the original's hash.
The file format lists the bytes that the hash takes in.
The hash of a range¶
The hash of a part of a track works the same way. Folded from 32 zero bytes over a run of whole chunks, it depends only on those chunks.
TapeReader::range_hash returns it, as a RangeHash, for the chunks that hold any item of an item range. A
track's records are not chunks, so a range hash leaves them out.
For example, a program that processed items 1 000 to 50 000 of a track can record the range hash of its input. Anyone can later check that a file still holds exactly that input.
Folding from the index¶
The writer stores each chunk's share of the hash, its unit hash, in the chunk's index entry. The fold therefore reads only the index, never a payload.
A fold from the index gives the hash that the writer stated, checked by the index's CRC codes. It is not a check of
the payloads. RangeHash::stated counts the chunks folded that way, and RangeHash::read those hashed from their
bytes.
The strict check, TapeReader::verify, checks the stated hashes: the content hash, each entry against its block, and
each close row against its track's fold. Every read of a chunk checks them too.
Without an index¶
Where the index is damaged or missing, the reader reads the chunks and computes the same hash. A damaged chunk of the track is an error there, as it is through the index.
Bytes that no block explains are counted in RangeHash::skipped, and a chunk written again folds once. The reader
refuses index entries that name one block twice or overlap (IndexError::BadEntries), from a final index,
checkpoints or a scan alike. It then reads the chunks instead.
Guards against a wrong index¶
The fold also guards against a wrong index. An index entry of a kind that the reader does not fold is counted in
RangeHash::skipped, never dropped silently. A chunk that the fold reads must be the one its entry names, with the
range the entry states.
A piece below Integrity::Full holds no unit hashes: each of its index entries is marked ENTRY_UNHASHED. The fold
then reads each such chunk and hashes its bytes, so the hash is real; RangeHash::read counts those chunks.
RangeHash::unprotected counts the chunks of a piece of Integrity::None, whose payload no CRC covered.
pub owner: [u8; 32],
Whose track this is, opaque to ww_tape (a run, a capture): with the id, it tells apart tracks of
one identity written into one file by several owners.
pub struct TrackHash(pub [u8; 32]);
A track's running content hash, see the module docs.
pub tracks: Vec<TrackStart>,
Every track of the chain so far (carried over by the piece head, or with data in the piece), ascending id then owner: its hash after this piece and where its items go on.
pub fn range_hash(
&self,
track_id: TrackId,
owner: OwnerId,
items: Range<ItemNum>,
from: TrackHash,
) -> R<RangeHash, S>
The hash of the whole chunks of track track_id owned by owner that hold an item in items, folded from
from (TrackHash::then) over the pieces in order.
pub struct RangeHash
What TapeReader::range_hash folded.
pub fn stated(&self) -> u64 {
self.chunks.saturating_sub(self.read)
}
Chunks folded by the unit hash their index entry states, not read: the writer's statement, which TapeReader::verify proves.
pub fn verify(&self, i: usize) -> R<Verification, S>
Whether closed piece i holds up as a whole: its bytes before its close record hash to the close's content
hash (each block that reads whole as it is, a chunk's payload by its hash, other bytes as they are,
piece docs); its final index and the blocks walked are the same set: each block walked that
its writer lists has exactly the entries its writer made of it, field for field (offset, length, kind, track,
items, ticks, a chunk's unit hash of its bytes and its range relation's range from its column), but a
byte-identical copy of the listed block before it; each entry names a block walked, or a block that doesn't
read whole in bytes no block explains, ending before the close and before the next block walked; and each
close row states the hash its track's blocks fold to from the head row's, or says it incomplete exactly where
a damaged block kept or the head leaves units out.
BadEntries,
A track's entries don't match their CRC or its list, or an entry names no block (a length outside a block's).