Nix Archive (NAR) is the archive format of the Nix package manager. A NAR contains a single file system object: a regular file, a symbolic link, or a directory with everything in it. Besides names, file contents and symlink targets, it only stores which regular files are executable. There are no timestamps and no other permissions.
Unlike TAR or ZIP, the format is canonical: a given file system object always has the same NAR. For example, directory entries are always sorted by name. Nix identifies the contents of each store path by the SHA-256 hash of its NAR, and binary caches serve store paths as NARs, usually compressed. GNU Guix uses the same format.
This Kaitai Struct implementation rejects the same NARs as Nix 2.36.0
with the default settings on Linux, except that it accepts entry names
that contain /.
Older versions of Nix may accept more NARs. For example, Nix checks the order of directory entries only since version 2.25.0, and it limits the length of names and symlink targets and the nesting depth only since 2.35.0. Some of these checks were also backported to patch releases of older versions.
This page hosts a formal specification of Nix Archive (NAR) using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#![allow(unused_imports)]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(irrefutable_let_patterns)]
#![allow(unused_comparisons)]
extern crate kaitai;
use kaitai::*;
use std::convert::{TryFrom, TryInto};
use std::cell::{Ref, Cell, RefCell};
use std::rc::{Rc, Weak};
/**
* Nix Archive (NAR) is the archive format of the [Nix](https://nixos.org/)
* package manager. A NAR contains a single file system object: a regular
* file, a symbolic link, or a directory with everything in it. Besides
* names, file contents and symlink targets, it only stores which regular
* files are executable. There are
* [no timestamps and no other permissions](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L15-L19).
*
* Unlike TAR or ZIP, the format is
* [canonical](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L21-L27):
* a given file system object always has the same NAR. For example,
* directory entries are always sorted by name. Nix identifies the contents
* of each store path by the SHA-256 hash of its NAR, and
* [binary caches](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/binary-cache/index.md?plain=1#L8-L10)
* serve store paths as NARs, usually compressed.
* [GNU Guix](https://codeberg.org/guix/guix/src/commit/ab6a9f84e783710335a29f6591f570e5e2df143c/guix/serialization.scm#L441-L446)
* uses the same format.
*
* This Kaitai Struct implementation rejects the same NARs as Nix 2.36.0
* with the default settings on Linux, except that it accepts entry names
* that contain `/`.
*
* Older versions of Nix may accept more NARs. For example, Nix checks the
* order of directory entries only since version 2.25.0, and it limits the
* length of names and symlink targets and the nesting depth only since
* 2.35.0. Some of these checks were also backported to patch releases of
* older versions.
* \sa https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md Git tag "2.36.0"
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar>,
pub _self: SharedType<Self>,
magic: RefCell<OptRc<NixNar_PaddedStr>>,
root_node: RefCell<OptRc<NixNar_Node>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar {
type Root = NixNar;
type Parent = NixNar;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.magic.borrow_mut() = t;
let _tmpa = *self_rc.magic();
if !(*_tmpa.body().as_ref().unwrap() == "nix-archive-1".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/seq/0".to_string() }));
}
let f = |t : &mut NixNar_Node| Ok(t.set_params((0).try_into().map_err(|_| KError::CastError)?));
let t = Self::read_into_with_init::<_, NixNar_Node>(&*_io, Some(self_rc._root.clone()), None, &f)?.into();
*self_rc.root_node.borrow_mut() = t;
Ok(())
}
}
impl NixNar {
}
/**
* Magic string, must be `nix-archive-1`.
*/
impl NixNar {
pub fn magic(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.magic.borrow()
}
}
/**
* The root of the archive, which is always a single node.
*/
impl NixNar {
pub fn root_node(&self) -> Ref<'_, OptRc<NixNar_Node>> {
self.root_node.borrow()
}
}
impl NixNar {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A single file system node (regular file, directory, or symlink).
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_Node {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<KStructUnit>,
pub _self: SharedType<Self>,
depth: RefCell<i32>,
unnamed0: RefCell<Vec<u8>>,
open_paren: RefCell<OptRc<NixNar_PaddedStr>>,
type_key: RefCell<OptRc<NixNar_PaddedStr>>,
type_val: RefCell<OptRc<NixNar_PaddedStr>>,
body: RefCell<Option<NixNar_Node_Body>>,
close_paren: RefCell<OptRc<NixNar_PaddedStr>>,
_io: RefCell<BytesReader>,
}
#[derive(Debug, Clone)]
pub enum NixNar_Node_Body {
NixNar_TypeDirectory(OptRc<NixNar_TypeDirectory>),
NixNar_TypeRegular(OptRc<NixNar_TypeRegular>),
NixNar_TypeSymlink(OptRc<NixNar_TypeSymlink>),
}
impl From<&NixNar_Node_Body> for OptRc<NixNar_TypeDirectory> {
fn from(v: &NixNar_Node_Body) -> Self {
if let NixNar_Node_Body::NixNar_TypeDirectory(x) = v {
return x.clone();
}
panic!("expected NixNar_Node_Body::NixNar_TypeDirectory, got {:?}", v)
}
}
impl From<OptRc<NixNar_TypeDirectory>> for NixNar_Node_Body {
fn from(v: OptRc<NixNar_TypeDirectory>) -> Self {
Self::NixNar_TypeDirectory(v)
}
}
impl From<&NixNar_Node_Body> for OptRc<NixNar_TypeRegular> {
fn from(v: &NixNar_Node_Body) -> Self {
if let NixNar_Node_Body::NixNar_TypeRegular(x) = v {
return x.clone();
}
panic!("expected NixNar_Node_Body::NixNar_TypeRegular, got {:?}", v)
}
}
impl From<OptRc<NixNar_TypeRegular>> for NixNar_Node_Body {
fn from(v: OptRc<NixNar_TypeRegular>) -> Self {
Self::NixNar_TypeRegular(v)
}
}
impl From<&NixNar_Node_Body> for OptRc<NixNar_TypeSymlink> {
fn from(v: &NixNar_Node_Body) -> Self {
if let NixNar_Node_Body::NixNar_TypeSymlink(x) = v {
return x.clone();
}
panic!("expected NixNar_Node_Body::NixNar_TypeSymlink, got {:?}", v)
}
}
impl From<OptRc<NixNar_TypeSymlink>> for NixNar_Node_Body {
fn from(v: OptRc<NixNar_TypeSymlink>) -> Self {
Self::NixNar_TypeSymlink(v)
}
}
impl KStruct for NixNar_Node {
type Root = NixNar;
type Parent = KStructUnit;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
*self_rc.unnamed0.borrow_mut() = _io.read_bytes(0 as usize)?.into();
let _tmpa = *self_rc.unnamed0();
if !(((*self_rc.depth() as i32) < (64 as i32))) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/node/seq/0".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.open_paren.borrow_mut() = t;
let _tmpa = *self_rc.open_paren();
if !(*_tmpa.body().as_ref().unwrap() == "(".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/node/seq/1".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.type_key.borrow_mut() = t;
let _tmpa = *self_rc.type_key();
if !(*_tmpa.body().as_ref().unwrap() == "type".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/node/seq/2".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.type_val.borrow_mut() = t;
let _tmpa = *self_rc.type_val();
if !( ((*_tmpa.body().as_ref().unwrap() == "regular".to_string()) || (*_tmpa.body().as_ref().unwrap() == "directory".to_string()) || (*_tmpa.body().as_ref().unwrap() == "symlink".to_string())) ) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/node/seq/3".to_string() }));
}
{
let on = self_rc.type_val().body();
if *on == "directory" {
let t = Self::read_into::<_, NixNar_TypeDirectory>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
*self_rc.body.borrow_mut() = t;
}
else if *on == "regular" {
let t = Self::read_into::<_, NixNar_TypeRegular>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
*self_rc.body.borrow_mut() = t;
}
else if *on == "symlink" {
let t = Self::read_into::<_, NixNar_TypeSymlink>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
*self_rc.body.borrow_mut() = t;
}
}
if *self_rc.type_val().body() != "directory".to_string() {
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.close_paren.borrow_mut() = t;
let _tmpa = *self_rc.close_paren();
if !(*_tmpa.body().as_ref().unwrap() == ")".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/node/seq/5".to_string() }));
}
}
Ok(())
}
}
impl NixNar_Node {
pub fn depth(&self) -> Ref<'_, i32> {
self.depth.borrow()
}
}
impl NixNar_Node {
pub fn set_params(&mut self, depth: i32) {
*self.depth.borrow_mut() = depth;
}
}
impl NixNar_Node {
}
impl NixNar_Node {
pub fn unnamed0(&self) -> Ref<'_, Vec<u8>> {
self.unnamed0.borrow()
}
}
/**
* Must be `(`, a token starting the node definition.
*/
impl NixNar_Node {
pub fn open_paren(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.open_paren.borrow()
}
}
/**
* Must be `type`.
*/
impl NixNar_Node {
pub fn type_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.type_key.borrow()
}
}
/**
* Type of the node (`regular`, `directory`, or `symlink`).
*/
impl NixNar_Node {
pub fn type_val(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.type_val.borrow()
}
}
impl NixNar_Node {
pub fn body(&self) -> Ref<'_, Option<NixNar_Node_Body>> {
self.body.borrow()
}
}
/**
* Must be `)`, a token ending the node definition. A directory node
* has no `close_paren`: its `)` is read as the terminator at the end
* of `body.entries`.
*/
impl NixNar_Node {
pub fn close_paren(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.close_paren.borrow()
}
}
impl NixNar_Node {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A byte sequence, prefixed with its length (u8le) and
* padded with null bytes to the next 8-byte boundary.
*
* Used for entry names and symlink targets, which Nix handles as raw
* bytes, without decoding them as text.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_PaddedBytes {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<KStructUnit>,
pub _self: SharedType<Self>,
len_body: RefCell<u64>,
body: RefCell<Vec<u8>>,
padding: RefCell<Vec<u8>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar_PaddedBytes {
type Root = NixNar;
type Parent = KStructUnit;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
*self_rc.len_body.borrow_mut() = _io.read_u8le()?.into();
*self_rc.body.borrow_mut() = _io.read_bytes(*self_rc.len_body() as usize)?.into();
*self_rc.padding.borrow_mut() = _io.read_bytes(modulo(((8 as i32) - (((*self_rc.len_body() as u64) % (8 as u64)) as i32)) as i64, 8 as i64) as usize)?.into();
let _tmpa = *self_rc.padding();
if !(if ((_tmpa.len() as i32) != (0 as i32)) { ((_tmpa.iter().max().ok_or(KError::EmptyIterator)? as u8) == (0 as u8)) } else { true }) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/padded_bytes/seq/2".to_string() }));
}
Ok(())
}
}
impl NixNar_PaddedBytes {
}
impl NixNar_PaddedBytes {
pub fn len_body(&self) -> Ref<'_, u64> {
self.len_body.borrow()
}
}
impl NixNar_PaddedBytes {
pub fn body(&self) -> Ref<'_, Vec<u8>> {
self.body.borrow()
}
}
impl NixNar_PaddedBytes {
pub fn padding(&self) -> Ref<'_, Vec<u8>> {
self.padding.borrow()
}
}
impl NixNar_PaddedBytes {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A string, prefixed with its length (u8le) and
* padded with null bytes to the next 8-byte boundary.
*
* Used for the tokens of the format, such as `nix-archive-1`, `(` and
* `type`. They are all ASCII.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_PaddedStr {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<KStructUnit>,
pub _self: SharedType<Self>,
len_body: RefCell<u64>,
body: RefCell<String>,
padding: RefCell<Vec<u8>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar_PaddedStr {
type Root = NixNar;
type Parent = KStructUnit;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
*self_rc.len_body.borrow_mut() = _io.read_u8le()?.into();
*self_rc.body.borrow_mut() = bytes_to_str(&_io.read_bytes(*self_rc.len_body() as usize)?.into(), "ASCII")?;
*self_rc.padding.borrow_mut() = _io.read_bytes(modulo(((8 as i32) - (((*self_rc.len_body() as u64) % (8 as u64)) as i32)) as i64, 8 as i64) as usize)?.into();
let _tmpa = *self_rc.padding();
if !(if ((_tmpa.len() as i32) != (0 as i32)) { ((_tmpa.iter().max().ok_or(KError::EmptyIterator)? as u8) == (0 as u8)) } else { true }) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/padded_str/seq/2".to_string() }));
}
Ok(())
}
}
impl NixNar_PaddedStr {
}
impl NixNar_PaddedStr {
pub fn len_body(&self) -> Ref<'_, u64> {
self.len_body.borrow()
}
}
impl NixNar_PaddedStr {
pub fn body(&self) -> Ref<'_, String> {
self.body.borrow()
}
}
impl NixNar_PaddedStr {
pub fn padding(&self) -> Ref<'_, Vec<u8>> {
self.padding.borrow()
}
}
impl NixNar_PaddedStr {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A directory node, containing a list of entries. Entries must be
* ordered by their names. Each name must be greater than the previous
* one when compared lexicographically as arrays of unsigned bytes.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeDirectory {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_Node>,
pub _self: SharedType<Self>,
entries: RefCell<Vec<OptRc<NixNar_TypeDirectory_DirEntry>>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar_TypeDirectory {
type Root = NixNar;
type Parent = NixNar_Node;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
*self_rc.entries.borrow_mut() = Vec::new();
{
let mut _i = 0;
while {
let t = Self::read_into::<_, NixNar_TypeDirectory_DirEntry>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
self_rc.entries.borrow_mut().push(t);
let _t_entries = self_rc.entries.borrow();
let _tmpa = _t_entries.last().unwrap();
let _tmpa = self_rc.entries()[_i as usize];
if !(if ((((_i as i32) != (0 as i32))) && (!(*_tmpa.is_terminator()?))) { *_tmpa.body().name().body() > *self_rc.entries()[((_i as i32) - (1 as i32)) as usize].body().name().body() } else { true }) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/seq/0".to_string() }));
}
_i += 1;
let x = !(*_tmpa.is_terminator()?);
x
} {}
}
Ok(())
}
}
impl NixNar_TypeDirectory {
}
impl NixNar_TypeDirectory {
pub fn entries(&self) -> Ref<'_, Vec<OptRc<NixNar_TypeDirectory_DirEntry>>> {
self.entries.borrow()
}
}
impl NixNar_TypeDirectory {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A single entry within a directory, or the terminator, i.e. the `)`
* token that ends the directory node.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeDirectory_DirEntry {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_TypeDirectory>,
pub _self: SharedType<Self>,
kind: RefCell<OptRc<NixNar_PaddedStr>>,
body: RefCell<OptRc<NixNar_TypeDirectory_DirEntryBody>>,
_io: RefCell<BytesReader>,
f_is_terminator: Cell<bool>,
is_terminator: RefCell<bool>,
}
impl KStruct for NixNar_TypeDirectory_DirEntry {
type Root = NixNar;
type Parent = NixNar_TypeDirectory;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.kind.borrow_mut() = t;
let _tmpa = *self_rc.kind();
if !( ((*_tmpa.body() == "entry".to_string()) || (*_tmpa.body() == ")".to_string())) ) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry/seq/0".to_string() }));
}
if !(*self_rc.is_terminator()?) {
let t = Self::read_into::<_, NixNar_TypeDirectory_DirEntryBody>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
*self_rc.body.borrow_mut() = t;
}
Ok(())
}
}
impl NixNar_TypeDirectory_DirEntry {
pub fn is_terminator(
&self
) -> KResult<Ref<'_, bool>> {
let _io = self._io.borrow();
let _rrc = self._root.get_value().borrow().upgrade();
let _prc = self._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
if self.f_is_terminator.get() {
return Ok(self.is_terminator.borrow());
}
self.f_is_terminator.set(true);
*self.is_terminator.borrow_mut() = (*self.kind().body() == ")".to_string()) as bool;
Ok(self.is_terminator.borrow())
}
}
/**
* Must be `entry` (for a child node) or `)` (for the terminator).
*/
impl NixNar_TypeDirectory_DirEntry {
pub fn kind(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.kind.borrow()
}
}
/**
* The entry itself. The terminator has no `body`.
*/
impl NixNar_TypeDirectory_DirEntry {
pub fn body(&self) -> Ref<'_, OptRc<NixNar_TypeDirectory_DirEntryBody>> {
self.body.borrow()
}
}
impl NixNar_TypeDirectory_DirEntry {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeDirectory_DirEntryBody {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_TypeDirectory_DirEntry>,
pub _self: SharedType<Self>,
open_paren: RefCell<OptRc<NixNar_PaddedStr>>,
name_key: RefCell<OptRc<NixNar_PaddedStr>>,
name: RefCell<OptRc<NixNar_PaddedBytes>>,
node_key: RefCell<OptRc<NixNar_PaddedStr>>,
node: RefCell<OptRc<NixNar_Node>>,
close_paren: RefCell<OptRc<NixNar_PaddedStr>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar_TypeDirectory_DirEntryBody {
type Root = NixNar;
type Parent = NixNar_TypeDirectory_DirEntry;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.open_paren.borrow_mut() = t;
let _tmpa = *self_rc.open_paren();
if !(*_tmpa.body().as_ref().unwrap() == "(".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry_body/seq/0".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.name_key.borrow_mut() = t;
let _tmpa = *self_rc.name_key();
if !(*_tmpa.body().as_ref().unwrap() == "name".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry_body/seq/1".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedBytes>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.name.borrow_mut() = t;
let _tmpa = *self_rc.name();
if !( ((((_tmpa.body().as_ref().unwrap().len() as i32) != (0 as i32))) && (((_tmpa.body().as_ref().unwrap().len() as i32) <= (255 as i32))) && (*_tmpa.body().as_ref().unwrap() != vec![0x2eu8]) && (*_tmpa.body().as_ref().unwrap() != vec![0x2eu8, 0x2eu8]) && (((*_tmpa.body().as_ref().unwrap().iter().min().ok_or(KError::EmptyIterator)? as u8) != (0 as u8)))) ) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry_body/seq/2".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.node_key.borrow_mut() = t;
let _tmpa = *self_rc.node_key();
if !(*_tmpa.body().as_ref().unwrap() == "node".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry_body/seq/3".to_string() }));
}
let f = |t : &mut NixNar_Node| Ok(t.set_params((((*_prc.as_ref().unwrap()._parent.get_value().borrow().upgrade().as_ref().unwrap()._parent.get_value().borrow().upgrade().as_ref().unwrap().depth() as i32) + (1 as i32))).try_into().map_err(|_| KError::CastError)?));
let t = Self::read_into_with_init::<_, NixNar_Node>(&*_io, Some(self_rc._root.clone()), None, &f)?.into();
*self_rc.node.borrow_mut() = t;
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.close_paren.borrow_mut() = t;
let _tmpa = *self_rc.close_paren();
if !(*_tmpa.body().as_ref().unwrap() == ")".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_directory/types/dir_entry_body/seq/5".to_string() }));
}
Ok(())
}
}
impl NixNar_TypeDirectory_DirEntryBody {
}
impl NixNar_TypeDirectory_DirEntryBody {
pub fn open_paren(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.open_paren.borrow()
}
}
impl NixNar_TypeDirectory_DirEntryBody {
pub fn name_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.name_key.borrow()
}
}
/**
* Name of the entry, as raw bytes. Nix 2.36.0
* [doesn't decode names as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L245-L248)
* either, and real NARs contain names that aren't valid UTF-8.
*
* The name must be 1 to 255 bytes long, must not be `.` or `..`, and
* must not contain a null byte. Nix also rejects names that contain
* `/`, but this Kaitai Struct implementation doesn't check that: as
* of KS 0.11, expressions can't search a byte array for a given
* byte. A null byte can be detected anyway: 0 is the smallest
* possible byte value, so a name contains a null byte exactly if its
* smallest byte is 0.
*/
impl NixNar_TypeDirectory_DirEntryBody {
pub fn name(&self) -> Ref<'_, OptRc<NixNar_PaddedBytes>> {
self.name.borrow()
}
}
impl NixNar_TypeDirectory_DirEntryBody {
pub fn node_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.node_key.borrow()
}
}
/**
* The child node.
*/
impl NixNar_TypeDirectory_DirEntryBody {
pub fn node(&self) -> Ref<'_, OptRc<NixNar_Node>> {
self.node.borrow()
}
}
impl NixNar_TypeDirectory_DirEntryBody {
pub fn close_paren(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.close_paren.borrow()
}
}
impl NixNar_TypeDirectory_DirEntryBody {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A regular file node.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeRegular {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_Node>,
pub _self: SharedType<Self>,
first_key: RefCell<OptRc<NixNar_PaddedStr>>,
executable_val: RefCell<OptRc<NixNar_PaddedStr>>,
contents_key: RefCell<OptRc<NixNar_PaddedStr>>,
file_data: RefCell<OptRc<NixNar_TypeRegular_FileContent>>,
_io: RefCell<BytesReader>,
f_is_executable: Cell<bool>,
is_executable: RefCell<bool>,
}
impl KStruct for NixNar_TypeRegular {
type Root = NixNar;
type Parent = NixNar_Node;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.first_key.borrow_mut() = t;
let _tmpa = *self_rc.first_key();
if !( ((*_tmpa.body().as_ref().unwrap() == "executable".to_string()) || (*_tmpa.body().as_ref().unwrap() == "contents".to_string())) ) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_regular/seq/0".to_string() }));
}
if *self_rc.is_executable()? {
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.executable_val.borrow_mut() = t;
let _tmpa = *self_rc.executable_val();
if !(*_tmpa.body().as_ref().unwrap() == "".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_regular/seq/1".to_string() }));
}
}
if *self_rc.is_executable()? {
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.contents_key.borrow_mut() = t;
let _tmpa = *self_rc.contents_key();
if !(*_tmpa.body().as_ref().unwrap() == "contents".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_regular/seq/2".to_string() }));
}
}
let t = Self::read_into::<_, NixNar_TypeRegular_FileContent>(&*_io, Some(self_rc._root.clone()), Some(self_rc._self.clone()))?.into();
*self_rc.file_data.borrow_mut() = t;
Ok(())
}
}
impl NixNar_TypeRegular {
/**
* True if the file has the `executable` attribute.
*/
pub fn is_executable(
&self
) -> KResult<Ref<'_, bool>> {
let _io = self._io.borrow();
let _rrc = self._root.get_value().borrow().upgrade();
let _prc = self._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
if self.f_is_executable.get() {
return Ok(self.is_executable.borrow());
}
self.f_is_executable.set(true);
*self.is_executable.borrow_mut() = (*self.first_key().body() == "executable".to_string()) as bool;
Ok(self.is_executable.borrow())
}
}
/**
* Either `executable` or `contents`.
*/
impl NixNar_TypeRegular {
pub fn first_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.first_key.borrow()
}
}
/**
* Must be empty.
*/
impl NixNar_TypeRegular {
pub fn executable_val(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.executable_val.borrow()
}
}
impl NixNar_TypeRegular {
pub fn contents_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.contents_key.borrow()
}
}
impl NixNar_TypeRegular {
pub fn file_data(&self) -> Ref<'_, OptRc<NixNar_TypeRegular_FileContent>> {
self.file_data.borrow()
}
}
impl NixNar_TypeRegular {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* The contents of the file, prefixed with their length and padded
* with null bytes to the next 8-byte boundary.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeRegular_FileContent {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_TypeRegular>,
pub _self: SharedType<Self>,
len_contents: RefCell<u64>,
unnamed1: RefCell<Vec<u8>>,
contents: RefCell<Vec<u8>>,
padding: RefCell<Vec<u8>>,
_io: RefCell<BytesReader>,
f_nar_offset: Cell<bool>,
nar_offset: RefCell<i32>,
}
impl KStruct for NixNar_TypeRegular_FileContent {
type Root = NixNar;
type Parent = NixNar_TypeRegular;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
*self_rc.len_contents.borrow_mut() = _io.read_u8le()?.into();
if ((*self_rc.nar_offset()? as i32) < (0 as i32)) {
*self_rc.unnamed1.borrow_mut() = _io.read_bytes(0 as usize)?.into();
}
*self_rc.contents.borrow_mut() = _io.read_bytes(*self_rc.len_contents() as usize)?.into();
*self_rc.padding.borrow_mut() = _io.read_bytes(modulo(((8 as i32) - (((*self_rc.len_contents() as u64) % (8 as u64)) as i32)) as i64, 8 as i64) as usize)?.into();
let _tmpa = *self_rc.padding();
if !(if ((_tmpa.len() as i32) != (0 as i32)) { ((_tmpa.iter().max().ok_or(KError::EmptyIterator)? as u8) == (0 as u8)) } else { true }) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_regular/types/file_content/seq/3".to_string() }));
}
Ok(())
}
}
impl NixNar_TypeRegular_FileContent {
/**
* Offset of `contents` in the stream. It corresponds to
* [`narOffset`](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/include/nix/util/source-accessor.hh#L134-L138)
* in Nix.
*/
pub fn nar_offset(
&self
) -> KResult<Ref<'_, i32>> {
let _io = self._io.borrow();
let _rrc = self._root.get_value().borrow().upgrade();
let _prc = self._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
if self.f_nar_offset.get() {
return Ok(self.nar_offset.borrow());
}
self.f_nar_offset.set(true);
*self.nar_offset.borrow_mut() = (_io.pos()) as i32;
Ok(self.nar_offset.borrow())
}
}
impl NixNar_TypeRegular_FileContent {
pub fn len_contents(&self) -> Ref<'_, u64> {
self.len_contents.borrow()
}
}
impl NixNar_TypeRegular_FileContent {
pub fn unnamed1(&self) -> Ref<'_, Vec<u8>> {
self.unnamed1.borrow()
}
}
/**
* This Kaitai Struct implementation keeps the contents of every
* file in memory, so parsing a NAR needs at least as much memory
* as all its files together.
*/
impl NixNar_TypeRegular_FileContent {
pub fn contents(&self) -> Ref<'_, Vec<u8>> {
self.contents.borrow()
}
}
impl NixNar_TypeRegular_FileContent {
pub fn padding(&self) -> Ref<'_, Vec<u8>> {
self.padding.borrow()
}
}
impl NixNar_TypeRegular_FileContent {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}
/**
* A symbolic link node.
*/
#[derive(Default, Debug, Clone)]
pub struct NixNar_TypeSymlink {
pub _root: SharedType<NixNar>,
pub _parent: SharedType<NixNar_Node>,
pub _self: SharedType<Self>,
target_key: RefCell<OptRc<NixNar_PaddedStr>>,
target_val: RefCell<OptRc<NixNar_PaddedBytes>>,
_io: RefCell<BytesReader>,
}
impl KStruct for NixNar_TypeSymlink {
type Root = NixNar;
type Parent = NixNar_Node;
fn read<S: KStream>(
self_rc: &OptRc<Self>,
_io: &S,
_root: SharedType<Self::Root>,
_parent: SharedType<Self::Parent>,
) -> KResult<()> {
*self_rc._io.borrow_mut() = _io.clone();
self_rc._root.set(_root.get());
self_rc._parent.set(_parent.get());
self_rc._self.set(Ok(self_rc.clone()));
let _rrc = self_rc._root.get_value().borrow().upgrade();
let _prc = self_rc._parent.get_value().borrow().upgrade();
let _r = _rrc.as_ref().unwrap();
let t = Self::read_into::<_, NixNar_PaddedStr>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.target_key.borrow_mut() = t;
let _tmpa = *self_rc.target_key();
if !(*_tmpa.body().as_ref().unwrap() == "target".to_string()) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_symlink/seq/0".to_string() }));
}
let t = Self::read_into::<_, NixNar_PaddedBytes>(&*_io, Some(self_rc._root.clone()), None)?.into();
*self_rc.target_val.borrow_mut() = t;
let _tmpa = *self_rc.target_val();
if !( ((((_tmpa.body().as_ref().unwrap().len() as i32) != (0 as i32))) && (_tmpa.body().as_ref().unwrap().len() <= 4095) && (((*_tmpa.body().as_ref().unwrap().iter().min().ok_or(KError::EmptyIterator)? as u8) != (0 as u8)))) ) {
return Err(KError::ValidationFailed(ValidationFailedError { kind: ValidationKind::Expr, src_path: "/types/type_symlink/seq/1".to_string() }));
}
Ok(())
}
}
impl NixNar_TypeSymlink {
}
/**
* Must be `target`.
*/
impl NixNar_TypeSymlink {
pub fn target_key(&self) -> Ref<'_, OptRc<NixNar_PaddedStr>> {
self.target_key.borrow()
}
}
/**
* The destination path of the symlink, as raw bytes. Nix 2.36.0
* [doesn't decode targets as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L280-L282)
* either.
*
* The target must be 1 to 4095 bytes long and must not contain a null
* byte.
*/
impl NixNar_TypeSymlink {
pub fn target_val(&self) -> Ref<'_, OptRc<NixNar_PaddedBytes>> {
self.target_val.borrow()
}
}
impl NixNar_TypeSymlink {
pub fn _io(&self) -> Ref<'_, BytesReader> {
self._io.borrow()
}
}