Skip to contents

Reads path as bytes and returns them as chunk strings suitable for merkle_root() and friends. The default 1 MiB chunk is a compromise: smaller chunks localise a change more precisely but make the tree and its proofs larger.

Usage

chunk_file(path, chunk_bytes = 1048576L)

Arguments

path

Path to an existing file.

chunk_bytes

Chunk size in bytes (default 1048576, i.e. 1 MiB).

Value

A list of raw vectors, in file order. A zero-length file gives an empty list. Chunks are returned as bytes rather than strings because a file is bytes: an R string cannot hold a zero byte, so a character chunk could not represent an arbitrary binary file at all.

Examples

p <- tempfile()
writeLines(rep("some capsule content", 50), p)

# One small chunk size to show the splitting.
ch <- chunk_file(p, chunk_bytes = 128)
length(ch)
#> [1] 9

# The chunks reconstruct the file and pin it as a Merkle root.
merkle_root(ch)
#> [1] "c4cb691d88a0dbf1dad9767c19095d54ac6958e6411e06d81dced0dba895c382"

# They are the file's bytes, so they concatenate back to it exactly.
identical(unlist(ch), readBin(p, "raw", file.size(p)))
#> [1] TRUE

# Editing the file changes exactly one leaf.
unlink(p)