// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 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.
 * @see <a href="https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md">Git tag "2.36.0"</a>
 */
public class NixNar extends KaitaiStruct {
    public static NixNar fromFile(String fileName) throws IOException {
        return new NixNar(new ByteBufferKaitaiStream(fileName));
    }

    public NixNar(KaitaiStream _io) {
        this(_io, null, null);
    }

    public NixNar(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public NixNar(KaitaiStream _io, KaitaiStruct _parent, NixNar _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.magic = new PaddedStr(this._io, this, _root);
        {
            PaddedStr _it = this.magic;
            if (!(_it.body().equals("nix-archive-1"))) {
                throw new KaitaiStream.ValidationExprError(this.magic, this._io, "/seq/0");
            }
        }
        this.rootNode = new Node(this._io, this, _root, 0);
    }

    public void _fetchInstances() {
        this.magic._fetchInstances();
        this.rootNode._fetchInstances();
    }

    /**
     * A single file system node (regular file, directory, or symlink).
     */
    public static class Node extends KaitaiStruct {

        public Node(KaitaiStream _io, int depth) {
            this(_io, null, null, depth);
        }

        public Node(KaitaiStream _io, KaitaiStruct _parent, int depth) {
            this(_io, _parent, null, depth);
        }

        public Node(KaitaiStream _io, KaitaiStruct _parent, NixNar _root, int depth) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            this.depth = depth;
            _read();
        }
        private void _read() {
            this._unnamed0 = this._io.readBytes(0);
            {
                byte[] _it = this._unnamed0;
                if (!(depth() < 64)) {
                    throw new KaitaiStream.ValidationExprError(this._unnamed0, this._io, "/types/node/seq/0");
                }
            }
            this.openParen = new PaddedStr(this._io, this, _root);
            {
                PaddedStr _it = this.openParen;
                if (!(_it.body().equals("("))) {
                    throw new KaitaiStream.ValidationExprError(this.openParen, this._io, "/types/node/seq/1");
                }
            }
            this.typeKey = new PaddedStr(this._io, this, _root);
            {
                PaddedStr _it = this.typeKey;
                if (!(_it.body().equals("type"))) {
                    throw new KaitaiStream.ValidationExprError(this.typeKey, this._io, "/types/node/seq/2");
                }
            }
            this.typeVal = new PaddedStr(this._io, this, _root);
            {
                PaddedStr _it = this.typeVal;
                if (!( ((_it.body().equals("regular")) || (_it.body().equals("directory")) || (_it.body().equals("symlink"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.typeVal, this._io, "/types/node/seq/3");
                }
            }
            switch (typeVal().body()) {
            case "directory": {
                this.body = new TypeDirectory(this._io, this, _root);
                break;
            }
            case "regular": {
                this.body = new TypeRegular(this._io, this, _root);
                break;
            }
            case "symlink": {
                this.body = new TypeSymlink(this._io, this, _root);
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                this.closeParen = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, this._io, "/types/node/seq/5");
                    }
                }
            }
        }

        public void _fetchInstances() {
            this.openParen._fetchInstances();
            this.typeKey._fetchInstances();
            this.typeVal._fetchInstances();
            switch (typeVal().body()) {
            case "directory": {
                ((TypeDirectory) (this.body))._fetchInstances();
                break;
            }
            case "regular": {
                ((TypeRegular) (this.body))._fetchInstances();
                break;
            }
            case "symlink": {
                ((TypeSymlink) (this.body))._fetchInstances();
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                this.closeParen._fetchInstances();
            }
        }
        private byte[] _unnamed0;
        private PaddedStr openParen;
        private PaddedStr typeKey;
        private PaddedStr typeVal;
        private KaitaiStruct body;
        private PaddedStr closeParen;
        private int depth;
        private NixNar _root;
        private KaitaiStruct _parent;
        public byte[] _unnamed0() { return _unnamed0; }

        /**
         * Must be `(`, a token starting the node definition.
         */
        public PaddedStr openParen() { return openParen; }

        /**
         * Must be `type`.
         */
        public PaddedStr typeKey() { return typeKey; }

        /**
         * Type of the node (`regular`, `directory`, or `symlink`).
         */
        public PaddedStr typeVal() { return typeVal; }
        public KaitaiStruct body() { return body; }

        /**
         * 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`.
         */
        public PaddedStr closeParen() { return closeParen; }

        /**
         * Nesting depth of the node. The root node has depth 0.
         */
        public int depth() { return depth; }
        public NixNar _root() { return _root; }
        public KaitaiStruct _parent() { return _parent; }
    }

    /**
     * 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.
     */
    public static class PaddedBytes extends KaitaiStruct {
        public static PaddedBytes fromFile(String fileName) throws IOException {
            return new PaddedBytes(new ByteBufferKaitaiStream(fileName));
        }

        public PaddedBytes(KaitaiStream _io) {
            this(_io, null, null);
        }

        public PaddedBytes(KaitaiStream _io, KaitaiStruct _parent) {
            this(_io, _parent, null);
        }

        public PaddedBytes(KaitaiStream _io, KaitaiStruct _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.lenBody = this._io.readU8le();
            this.body = this._io.readBytes(lenBody());
            this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8));
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/padded_bytes/seq/2");
                }
            }
        }

        public void _fetchInstances() {
        }
        private long lenBody;
        private byte[] body;
        private byte[] padding;
        private NixNar _root;
        private KaitaiStruct _parent;
        public long lenBody() { return lenBody; }
        public byte[] body() { return body; }
        public byte[] padding() { return padding; }
        public NixNar _root() { return _root; }
        public KaitaiStruct _parent() { return _parent; }
    }

    /**
     * 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.
     */
    public static class PaddedStr extends KaitaiStruct {
        public static PaddedStr fromFile(String fileName) throws IOException {
            return new PaddedStr(new ByteBufferKaitaiStream(fileName));
        }

        public PaddedStr(KaitaiStream _io) {
            this(_io, null, null);
        }

        public PaddedStr(KaitaiStream _io, KaitaiStruct _parent) {
            this(_io, _parent, null);
        }

        public PaddedStr(KaitaiStream _io, KaitaiStruct _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.lenBody = this._io.readU8le();
            this.body = new String(this._io.readBytes(lenBody()), StandardCharsets.US_ASCII);
            this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8));
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/padded_str/seq/2");
                }
            }
        }

        public void _fetchInstances() {
        }
        private long lenBody;
        private String body;
        private byte[] padding;
        private NixNar _root;
        private KaitaiStruct _parent;
        public long lenBody() { return lenBody; }
        public String body() { return body; }
        public byte[] padding() { return padding; }
        public NixNar _root() { return _root; }
        public KaitaiStruct _parent() { return _parent; }
    }

    /**
     * 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.
     */
    public static class TypeDirectory extends KaitaiStruct {
        public static TypeDirectory fromFile(String fileName) throws IOException {
            return new TypeDirectory(new ByteBufferKaitaiStream(fileName));
        }

        public TypeDirectory(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeDirectory(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeDirectory(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.entries = new ArrayList<DirEntry>();
            {
                DirEntry _it;
                int i = 0;
                do {
                    _it = new DirEntry(this._io, this, _root);
                    this.entries.add(_it);
                    {
                        DirEntry _it = this.entries.get(((Number) (i)).intValue());
                        if (!(( ((i != 0) && (!(_it.isTerminator())))  ? (KaitaiStream.byteArrayCompare(_it.body().name().body(), entries().get(((Number) (i - 1)).intValue()).body().name().body()) > 0) : true))) {
                            throw new KaitaiStream.ValidationExprError(this.entries.get(((Number) (i)).intValue()), this._io, "/types/type_directory/seq/0");
                        }
                    }
                    i++;
                } while (!(_it.isTerminator()));
            }
        }

        public void _fetchInstances() {
            for (int i = 0; i < this.entries.size(); i++) {
                this.entries.get(((Number) (i)).intValue())._fetchInstances();
            }
        }

        /**
         * A single entry within a directory, or the terminator, i.e. the `)`
         * token that ends the directory node.
         */
        public static class DirEntry extends KaitaiStruct {
            public static DirEntry fromFile(String fileName) throws IOException {
                return new DirEntry(new ByteBufferKaitaiStream(fileName));
            }

            public DirEntry(KaitaiStream _io) {
                this(_io, null, null);
            }

            public DirEntry(KaitaiStream _io, NixNar.TypeDirectory _parent) {
                this(_io, _parent, null);
            }

            public DirEntry(KaitaiStream _io, NixNar.TypeDirectory _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
                _read();
            }
            private void _read() {
                this.kind = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.kind;
                    if (!( ((_it.body().equals("entry")) || (_it.body().equals(")"))) )) {
                        throw new KaitaiStream.ValidationExprError(this.kind, this._io, "/types/type_directory/types/dir_entry/seq/0");
                    }
                }
                if (!(isTerminator())) {
                    this.body = new DirEntryBody(this._io, this, _root);
                }
            }

            public void _fetchInstances() {
                this.kind._fetchInstances();
                if (!(isTerminator())) {
                    this.body._fetchInstances();
                }
            }
            private Boolean isTerminator;
            public Boolean isTerminator() {
                if (this.isTerminator != null)
                    return this.isTerminator;
                this.isTerminator = kind().body().equals(")");
                return this.isTerminator;
            }
            private PaddedStr kind;
            private DirEntryBody body;
            private NixNar _root;
            private NixNar.TypeDirectory _parent;

            /**
             * Must be `entry` (for a child node) or `)` (for the terminator).
             */
            public PaddedStr kind() { return kind; }

            /**
             * The entry itself. The terminator has no `body`.
             */
            public DirEntryBody body() { return body; }
            public NixNar _root() { return _root; }
            public NixNar.TypeDirectory _parent() { return _parent; }
        }
        public static class DirEntryBody extends KaitaiStruct {
            public static DirEntryBody fromFile(String fileName) throws IOException {
                return new DirEntryBody(new ByteBufferKaitaiStream(fileName));
            }

            public DirEntryBody(KaitaiStream _io) {
                this(_io, null, null);
            }

            public DirEntryBody(KaitaiStream _io, NixNar.TypeDirectory.DirEntry _parent) {
                this(_io, _parent, null);
            }

            public DirEntryBody(KaitaiStream _io, NixNar.TypeDirectory.DirEntry _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
                _read();
            }
            private void _read() {
                this.openParen = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.openParen;
                    if (!(_it.body().equals("("))) {
                        throw new KaitaiStream.ValidationExprError(this.openParen, this._io, "/types/type_directory/types/dir_entry_body/seq/0");
                    }
                }
                this.nameKey = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.nameKey;
                    if (!(_it.body().equals("name"))) {
                        throw new KaitaiStream.ValidationExprError(this.nameKey, this._io, "/types/type_directory/types/dir_entry_body/seq/1");
                    }
                }
                this.name = new PaddedBytes(this._io, this, _root);
                {
                    PaddedBytes _it = this.name;
                    if (!( ((_it.body().length != 0) && (_it.body().length <= 255) && (!Arrays.equals(_it.body(), new byte[] { 46 })) && (!Arrays.equals(_it.body(), new byte[] { 46, 46 })) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                        throw new KaitaiStream.ValidationExprError(this.name, this._io, "/types/type_directory/types/dir_entry_body/seq/2");
                    }
                }
                this.nodeKey = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.nodeKey;
                    if (!(_it.body().equals("node"))) {
                        throw new KaitaiStream.ValidationExprError(this.nodeKey, this._io, "/types/type_directory/types/dir_entry_body/seq/3");
                    }
                }
                this.node = new Node(this._io, this, _root, _parent()._parent()._parent().depth() + 1);
                this.closeParen = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, this._io, "/types/type_directory/types/dir_entry_body/seq/5");
                    }
                }
            }

            public void _fetchInstances() {
                this.openParen._fetchInstances();
                this.nameKey._fetchInstances();
                this.name._fetchInstances();
                this.nodeKey._fetchInstances();
                this.node._fetchInstances();
                this.closeParen._fetchInstances();
            }
            private PaddedStr openParen;
            private PaddedStr nameKey;
            private PaddedBytes name;
            private PaddedStr nodeKey;
            private Node node;
            private PaddedStr closeParen;
            private NixNar _root;
            private NixNar.TypeDirectory.DirEntry _parent;
            public PaddedStr openParen() { return openParen; }
            public PaddedStr nameKey() { return nameKey; }

            /**
             * 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.
             */
            public PaddedBytes name() { return name; }
            public PaddedStr nodeKey() { return nodeKey; }

            /**
             * The child node.
             */
            public Node node() { return node; }
            public PaddedStr closeParen() { return closeParen; }
            public NixNar _root() { return _root; }
            public NixNar.TypeDirectory.DirEntry _parent() { return _parent; }
        }
        private List<DirEntry> entries;
        private NixNar _root;
        private NixNar.Node _parent;
        public List<DirEntry> entries() { return entries; }
        public NixNar _root() { return _root; }
        public NixNar.Node _parent() { return _parent; }
    }

    /**
     * A regular file node.
     */
    public static class TypeRegular extends KaitaiStruct {
        public static TypeRegular fromFile(String fileName) throws IOException {
            return new TypeRegular(new ByteBufferKaitaiStream(fileName));
        }

        public TypeRegular(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeRegular(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeRegular(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.firstKey = new PaddedStr(this._io, this, _root);
            {
                PaddedStr _it = this.firstKey;
                if (!( ((_it.body().equals("executable")) || (_it.body().equals("contents"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.firstKey, this._io, "/types/type_regular/seq/0");
                }
            }
            if (isExecutable()) {
                this.executableVal = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.executableVal;
                    if (!(_it.body().equals(""))) {
                        throw new KaitaiStream.ValidationExprError(this.executableVal, this._io, "/types/type_regular/seq/1");
                    }
                }
            }
            if (isExecutable()) {
                this.contentsKey = new PaddedStr(this._io, this, _root);
                {
                    PaddedStr _it = this.contentsKey;
                    if (!(_it.body().equals("contents"))) {
                        throw new KaitaiStream.ValidationExprError(this.contentsKey, this._io, "/types/type_regular/seq/2");
                    }
                }
            }
            this.fileData = new FileContent(this._io, this, _root);
        }

        public void _fetchInstances() {
            this.firstKey._fetchInstances();
            if (isExecutable()) {
                this.executableVal._fetchInstances();
            }
            if (isExecutable()) {
                this.contentsKey._fetchInstances();
            }
            this.fileData._fetchInstances();
        }

        /**
         * The contents of the file, prefixed with their length and padded
         * with null bytes to the next 8-byte boundary.
         */
        public static class FileContent extends KaitaiStruct {
            public static FileContent fromFile(String fileName) throws IOException {
                return new FileContent(new ByteBufferKaitaiStream(fileName));
            }

            public FileContent(KaitaiStream _io) {
                this(_io, null, null);
            }

            public FileContent(KaitaiStream _io, NixNar.TypeRegular _parent) {
                this(_io, _parent, null);
            }

            public FileContent(KaitaiStream _io, NixNar.TypeRegular _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
                _read();
            }
            private void _read() {
                this.lenContents = this._io.readU8le();
                if (narOffset() < 0) {
                    this._unnamed1 = this._io.readBytes(0);
                }
                this.contents = this._io.readBytes(lenContents());
                this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenContents(), 8), 8));
                {
                    byte[] _it = this.padding;
                    if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                        throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/type_regular/types/file_content/seq/3");
                    }
                }
            }

            public void _fetchInstances() {
                if (narOffset() < 0) {
                }
            }
            private Integer narOffset;

            /**
             * 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.
             */
            public Integer narOffset() {
                if (this.narOffset != null)
                    return this.narOffset;
                this.narOffset = ((Number) (_io().pos())).intValue();
                return this.narOffset;
            }
            private long lenContents;
            private byte[] _unnamed1;
            private byte[] contents;
            private byte[] padding;
            private NixNar _root;
            private NixNar.TypeRegular _parent;
            public long lenContents() { return lenContents; }
            public byte[] _unnamed1() { return _unnamed1; }

            /**
             * 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.
             */
            public byte[] contents() { return contents; }
            public byte[] padding() { return padding; }
            public NixNar _root() { return _root; }
            public NixNar.TypeRegular _parent() { return _parent; }
        }
        private Boolean isExecutable;

        /**
         * True if the file has the `executable` attribute.
         */
        public Boolean isExecutable() {
            if (this.isExecutable != null)
                return this.isExecutable;
            this.isExecutable = firstKey().body().equals("executable");
            return this.isExecutable;
        }
        private PaddedStr firstKey;
        private PaddedStr executableVal;
        private PaddedStr contentsKey;
        private FileContent fileData;
        private NixNar _root;
        private NixNar.Node _parent;

        /**
         * Either `executable` or `contents`.
         */
        public PaddedStr firstKey() { return firstKey; }

        /**
         * Must be empty.
         */
        public PaddedStr executableVal() { return executableVal; }
        public PaddedStr contentsKey() { return contentsKey; }
        public FileContent fileData() { return fileData; }
        public NixNar _root() { return _root; }
        public NixNar.Node _parent() { return _parent; }
    }

    /**
     * A symbolic link node.
     */
    public static class TypeSymlink extends KaitaiStruct {
        public static TypeSymlink fromFile(String fileName) throws IOException {
            return new TypeSymlink(new ByteBufferKaitaiStream(fileName));
        }

        public TypeSymlink(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeSymlink(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeSymlink(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
        private void _read() {
            this.targetKey = new PaddedStr(this._io, this, _root);
            {
                PaddedStr _it = this.targetKey;
                if (!(_it.body().equals("target"))) {
                    throw new KaitaiStream.ValidationExprError(this.targetKey, this._io, "/types/type_symlink/seq/0");
                }
            }
            this.targetVal = new PaddedBytes(this._io, this, _root);
            {
                PaddedBytes _it = this.targetVal;
                if (!( ((_it.body().length != 0) && (_it.body().length <= 4095) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                    throw new KaitaiStream.ValidationExprError(this.targetVal, this._io, "/types/type_symlink/seq/1");
                }
            }
        }

        public void _fetchInstances() {
            this.targetKey._fetchInstances();
            this.targetVal._fetchInstances();
        }
        private PaddedStr targetKey;
        private PaddedBytes targetVal;
        private NixNar _root;
        private NixNar.Node _parent;

        /**
         * Must be `target`.
         */
        public PaddedStr targetKey() { return targetKey; }

        /**
         * 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.
         */
        public PaddedBytes targetVal() { return targetVal; }
        public NixNar _root() { return _root; }
        public NixNar.Node _parent() { return _parent; }
    }
    private PaddedStr magic;
    private Node rootNode;
    private NixNar _root;
    private KaitaiStruct _parent;

    /**
     * Magic string, must be `nix-archive-1`.
     */
    public PaddedStr magic() { return magic; }

    /**
     * The root of the archive, which is always a single node.
     */
    public Node rootNode() { return rootNode; }
    public NixNar _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}
