File size: 5,117 Bytes
f737a91 e163843 f737a91 63c2fe4 f737a91 e163843 f737a91 c178359 f737a91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Retrieving and verifying files
The catalog holds no file content. To use a file, download it from the address in its pointer and check it against the size and hashes the pointer gives. The fields are described in [fields.md](fields.md).
## Steps
1. Choose an address: `uri`, or one from `alt_uris`, which serve the same bytes. `s3://` addresses usually have a public https address in `alt_uris`. `hf://datasets/<repo>@<revision>/<path>` stands for `https://huggingface.co/datasets/<repo>/resolve/<revision>/<path>`.
2. If `access.needs_auth` says that a login, a guestbook or a form is needed, follow the source's procedure first.
3. If the pointer has `range`, request only those bytes with the HTTP header `Range: bytes=<offset>-<offset + length - 1>`. The file is that byte range of `uri`, for example one record inside a WARC file, and `size_bytes` equals `length`.
4. Compare the number of bytes with `size_bytes`, unless it is `null` (the source does not publish the size) or `hash_scope` is `registry_manifest` (below).
5. Compute the hashes named in `hashes` and compare every value; `hash_scope` says what they cover (below). Treat a mismatch as a failure; do not substitute another file.
6. If `hashes` is empty (`hash_source` is `computed_on_fetch`), the source publishes no hash: compute your own after download and keep it with the file.
[fetch_selected.py](fetch_selected.py), next to this page, follows these steps for every pointer of a selection whose hashes cover the downloaded bytes (`hash_scope` of `file`), byte ranges included; how to run it is in [selecting.md](selecting.md).
## What the hashes cover (`hash_scope`)
- `file` (the default when the field is absent): the bytes you downloaded, that is the whole file, or the byte range when the pointer has `range`.
- `http_payload`: the range is one gzip-compressed WARC record. Decompress it, remove the WARC and HTTP headers, undo chunked transfer encoding, and hash the HTTP response body. `size_bytes` stays the compressed length of the record.
- `api_body`: `uri` returns JSON from an API. Hash the UTF-8 bytes of the field named in `source_meta.api_body_field` (a dotted path in which numbers index lists; `items.0.body` when absent), not the whole response. Non-string values are written with `json.dumps(value, ensure_ascii=False)` before hashing. The path `$` names the whole response body as returned (after gzip decoding), hashed without parsing and reserializing it. When `source_meta.api_body_decode` is `base64`, the field holds the file content as base64 text: decode it and compare size and hashes on the decoded bytes, which are the file.
- `html_region`: `uri` is an HTML page with parts that can differ between requests (comments, ratings, text shown only to some readers). Hash only the stable region named in `source_meta.html_region`, not the whole page; `size_bytes` is the size of that region. For `ctftime_page`, working on the page bytes: take the span from the first `<div class="page-header">` up to (not including) the first `<h3>Comments</h3>` after it, split it on newline bytes, and append the lines after that point that contain `MDConvert.render(`; drop lines that contain `ctftimecanary` (any letter case) but not `MDConvert.render(`, and lines that are empty or only ASCII whitespace; strip trailing ASCII whitespace from each line; join with a newline byte and add one final newline. A page without both markers fails.
- `registry_manifest`: `uri` is a container image manifest in a container registry, addressed by its digest. Get a pull token first (the batch notes say how), request `uri` with that token and an `Accept` header listing the manifest types, and hash the response bytes; the sha256 equals the digest. `size_bytes` is not the length of the manifest: it is the compressed size of the image layers, which equals the sum of `layers[].size` inside the manifest.
- `zip_member_deflate`: the range is the compressed data of one member of a ZIP archive (the bytes after its local header). Inflate it as raw DEFLATE data (no zlib or gzip header). The result is the file: its size is `payload_size_bytes`, its CRC-32 is `source_meta.crc32` (8 lowercase hex digits), and `hashes` cover it. `size_bytes` stays the compressed length.
## Files inside archives
A pointer with `parent_pointer_id` and `member_path` is a file inside an archive that has its own pointer: retrieve and verify the archive, then extract `member_path`.
When it also has `zip_entry`, `uri` is the zip archive and the file can be retrieved alone: request the 30-byte local file header at `local_header_offset`, read the file name length (bytes 26-27) and extra field length (bytes 28-29), request `compressed_size` bytes right after them, decompress (`compression_method` 0 is stored, 8 is raw deflate), and compare the length with `size_bytes` and the CRC32 with `hashes.crc32`.
## Source notes and the manifests themselves
Each `collection.json` adds notes for its source under `fetch` and `notes`. Its `shards` list gives the `sha256` of every part file of the batch, so the downloaded manifests can be checked too.
|