mnix: Nix binary-cache client PoC in MoonBit (install hello from cache.nixos.org) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
4e7ce11973
commit
fa70164336
48 files changed
+3112
-2
No files matched your search
+246
@@ -0,0 +1,246 @@
|
||||
///|
|
||||
/// In-memory tree of a NAR (Nix ARchive). Directory entries are kept in the
|
||||
/// strictly ascending byte order that the NAR format mandates.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) enum Node {
|
||||
File(executable~ : Bool, contents~ : Bytes)
|
||||
Symlink(target~ : String)
|
||||
Directory(entries~ : Array[(String, Node)])
|
||||
}
|
||||
|
||||
///|
|
||||
const MAGIC : String = "nix-archive-1"
|
||||
|
||||
///|
|
||||
// ---------------------------------------------------------------- reader
|
||||
|
||||
///|
|
||||
priv struct Reader {
|
||||
data : Bytes
|
||||
mut pos : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
fn[T] Reader::fail(self : Reader, msg : String) -> T raise @store.NixError {
|
||||
raise Parse("NAR: \{msg} (at offset \{self.pos})")
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::read_u64(self : Reader) -> Int32 raise @store.NixError {
|
||||
if self.pos + 8 > self.data.length() {
|
||||
self.fail("unexpected end of data reading length")
|
||||
}
|
||||
let mut v = 0L
|
||||
for i in 0..<8 {
|
||||
v = v | (self.data[self.pos + i].to_int64() << (8 * i))
|
||||
}
|
||||
if v < 0L || v > 0x7fffffffL {
|
||||
self.fail("length \{v} too large for this implementation (max 2 GiB)")
|
||||
}
|
||||
self.pos += 8
|
||||
v.to_int()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Read a length-prefixed, 8-byte-padded byte string.
|
||||
fn Reader::read_bytes(self : Reader) -> Bytes raise @store.NixError {
|
||||
let len = self.read_u64()
|
||||
let padded = (len + 7) / 8 * 8
|
||||
if self.pos + padded > self.data.length() {
|
||||
self.fail("string of length \{len} runs past end of data")
|
||||
}
|
||||
let out = self.data[self.pos:self.pos + len].to_owned()
|
||||
for i in len..<padded {
|
||||
if self.data[self.pos + i] != 0 {
|
||||
self.fail("non-zero padding")
|
||||
}
|
||||
}
|
||||
self.pos += padded
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::read_str(self : Reader) -> String raise @store.NixError {
|
||||
let b = self.read_bytes()
|
||||
@utf8.decode(b) catch {
|
||||
_ => self.fail("invalid UTF-8 in name/target")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::expect(self : Reader, s : String) -> Unit raise @store.NixError {
|
||||
let got = self.read_str()
|
||||
if got != s {
|
||||
self.fail("expected '\{s}', got '\{got}'")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Byte-wise lexicographic comparison (the order Nix sorts directory entries by).
|
||||
fn bytes_compare(a : Bytes, b : Bytes) -> Int32 {
|
||||
let n = if a.length() < b.length() { a.length() } else { b.length() }
|
||||
for i in 0..<n {
|
||||
if a[i] != b[i] {
|
||||
return if a[i] < b[i] { -1 } else { 1 }
|
||||
}
|
||||
}
|
||||
a.length() - b.length()
|
||||
}
|
||||
|
||||
///|
|
||||
fn valid_entry_name(n : String) -> Bool {
|
||||
n.length() > 0 &&
|
||||
n != "." &&
|
||||
n != ".." &&
|
||||
!n.contains("/") &&
|
||||
!n.contains("\u{0}")
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::read_node(
|
||||
self : Reader,
|
||||
depth : Int32,
|
||||
) -> Node raise @store.NixError {
|
||||
if depth > 512 {
|
||||
self.fail("directory nesting too deep")
|
||||
}
|
||||
self.expect("(")
|
||||
self.expect("type")
|
||||
let ty = self.read_str()
|
||||
match ty {
|
||||
"regular" => {
|
||||
let mut executable = false
|
||||
let mut tag = self.read_str()
|
||||
if tag == "executable" {
|
||||
executable = true
|
||||
self.expect("")
|
||||
tag = self.read_str()
|
||||
}
|
||||
if tag != "contents" {
|
||||
self.fail("expected 'contents', got '\{tag}'")
|
||||
}
|
||||
let contents = self.read_bytes()
|
||||
self.expect(")")
|
||||
File(executable~, contents~)
|
||||
}
|
||||
"symlink" => {
|
||||
self.expect("target")
|
||||
let target = self.read_str()
|
||||
self.expect(")")
|
||||
Symlink(target~)
|
||||
}
|
||||
"directory" => {
|
||||
let entries : Array[(String, Node)] = []
|
||||
let mut prev : String? = None
|
||||
while true {
|
||||
let tag = self.read_str()
|
||||
if tag == ")" {
|
||||
break
|
||||
}
|
||||
if tag != "entry" {
|
||||
self.fail("expected 'entry' or ')', got '\{tag}'")
|
||||
}
|
||||
self.expect("(")
|
||||
self.expect("name")
|
||||
let name = self.read_str()
|
||||
if !valid_entry_name(name) {
|
||||
self.fail("invalid directory entry name '\{name}'")
|
||||
}
|
||||
if prev is Some(p) &&
|
||||
bytes_compare(@utf8.encode(p), @utf8.encode(name)) >= 0 {
|
||||
self.fail("entries not strictly sorted: '\{p}' before '\{name}'")
|
||||
}
|
||||
prev = Some(name)
|
||||
self.expect("node")
|
||||
let node = self.read_node(depth + 1)
|
||||
self.expect(")")
|
||||
entries.push((name, node))
|
||||
}
|
||||
Directory(entries~)
|
||||
}
|
||||
_ => self.fail("unknown node type '\{ty}'")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Parse a complete NAR. Fails on any malformation, including trailing bytes.
|
||||
pub fn parse(data : Bytes) -> Node raise @store.NixError {
|
||||
let r = { data, pos: 0, }
|
||||
r.expect(MAGIC)
|
||||
let node = r.read_node(0)
|
||||
if r.pos != data.length() {
|
||||
r.fail("trailing garbage after archive")
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
///|
|
||||
// ---------------------------------------------------------------- writer
|
||||
|
||||
///|
|
||||
fn put_u64(buf : @buffer.Buffer, n : Int32) -> Unit {
|
||||
let v = n.to_int64()
|
||||
for i in 0..<8 {
|
||||
buf.write_byte(((v >> (8 * i)) & 0xffL).to_int().to_byte())
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn put_bytes(buf : @buffer.Buffer, b : BytesView) -> Unit {
|
||||
put_u64(buf, b.length())
|
||||
buf.write_bytesview(b)
|
||||
let pad = (8 - b.length() % 8) % 8
|
||||
for _ in 0..<pad {
|
||||
buf.write_byte(b'\x00')
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn put_str(buf : @buffer.Buffer, s : String) -> Unit {
|
||||
put_bytes(buf, @utf8.encode(s))
|
||||
}
|
||||
|
||||
///|
|
||||
fn put_node(buf : @buffer.Buffer, n : Node) -> Unit {
|
||||
put_str(buf, "(")
|
||||
put_str(buf, "type")
|
||||
match n {
|
||||
File(executable~, contents~) => {
|
||||
put_str(buf, "regular")
|
||||
if executable {
|
||||
put_str(buf, "executable")
|
||||
put_str(buf, "")
|
||||
}
|
||||
put_str(buf, "contents")
|
||||
put_bytes(buf, contents)
|
||||
}
|
||||
Symlink(target~) => {
|
||||
put_str(buf, "symlink")
|
||||
put_str(buf, "target")
|
||||
put_str(buf, target)
|
||||
}
|
||||
Directory(entries~) => {
|
||||
put_str(buf, "directory")
|
||||
for e in entries {
|
||||
let (name, child) = e
|
||||
put_str(buf, "entry")
|
||||
put_str(buf, "(")
|
||||
put_str(buf, "name")
|
||||
put_str(buf, name)
|
||||
put_str(buf, "node")
|
||||
put_node(buf, child)
|
||||
put_str(buf, ")")
|
||||
}
|
||||
}
|
||||
}
|
||||
put_str(buf, ")")
|
||||
}
|
||||
|
||||
///|
|
||||
/// Serialize a node tree into canonical NAR bytes.
|
||||
pub fn serialize(n : Node) -> Bytes {
|
||||
let buf = @buffer.Buffer()
|
||||
put_str(buf, MAGIC)
|
||||
put_node(buf, n)
|
||||
buf.to_bytes()
|
||||
}
|
||||
Reference in new issue
Block a user