EntryBuilder

Struct EntryBuilder 

Source
pub struct EntryBuilder { /* private fields */ }
Expand description

A builder for creating a NormalEntry.

This builder provides a flexible way to construct entries for PNA archives by specifying the entry type (file, directory, symbolic link, hard link), content, and metadata. It handles compression and encryption transparently according to the provided WriteOptions.

§Entry Types

  • Files: Created with new_file(), supports data writing via the Write trait
  • Directories: Created with new_dir(), have no data payload
  • Symbolic links: Created with new_symlink(), data is the link target path
  • Hard links: Created with new_hard_link(), data is the link target path

§Write Trait Behavior

For file entries, the Write trait is fully functional. Data written via write_all() or similar methods is automatically compressed and encrypted according to the WriteOptions provided at construction time. The original (uncompressed) file size is tracked separately.

For directory entries, the Write trait is implemented but writing data has no effect. Directories do not store data payloads in PNA archives.

For symbolic link and hard link entries, do not use the Write trait. Instead, the link target is provided to the constructor (new_symlink() or new_hard_link()).

§Metadata

Metadata (timestamps, permissions, extended attributes) can be set at any time before calling build(). The order does not matter - you can set metadata before, during, or after writing data to file entries.

§Compression and Encryption

When data is written to a file entry:

  1. Data is compressed according to WriteOptions compression settings
  2. Compressed data is encrypted according to WriteOptions encryption settings
  3. Encrypted data is buffered into chunks
  4. Chunks are finalized when build() is called

This happens transparently - you just write raw data and the builder handles the rest.

§Important Notes

  • Each builder can only be built once (build() consumes self)
  • File entries with no data written will have zero size
  • Compression and encryption are applied during writes, not at build time
  • The build() method finalizes compression/encryption streams
  • Building a directory or file without calling write methods is valid

§Examples

§Creating a file entry

use libpna::{EntryBuilder, WriteOptions};

let mut builder = EntryBuilder::new_file("my_file.txt".into(), WriteOptions::store())?;
builder.write_all(b"This is the file content.")?;
let entry = builder.build()?;

§Creating a file entry with extended attributes

use libpna::{EntryBuilder, WriteOptions, ExtendedAttribute};

let mut builder = EntryBuilder::new_file("data.txt".into(), WriteOptions::store())?;
builder.write_all(b"file content")?;
builder.add_xattr(ExtendedAttribute::new("user.comment".into(), b"important".to_vec()));
let entry = builder.build()?;

§Creating a directory entry

use libpna::EntryBuilder;

let builder = EntryBuilder::new_dir("my_dir/".into());
let entry = builder.build()?;

§Creating a symbolic link entry

use libpna::EntryBuilder;

let builder = EntryBuilder::new_symlink(
    "link_name".into(),
    "target/file.txt".into()
)?;
let entry = builder.build()?;

Implementations§

Source§

impl EntryBuilder

Source

pub const fn new_dir(name: EntryName) -> EntryBuilder

Creates a new directory with the given name.

§Arguments
  • name - The name of the entry to create.
§Returns

A new EntryBuilder.

Source

pub fn new_file( name: EntryName, option: impl WriteOption, ) -> Result<EntryBuilder, Error>

Creates a new file with the given name and write options.

§Arguments
  • name - The name of the entry to create.
  • option - The options for writing the entry.
§Returns

A Result containing the new EntryBuilder, or an I/O error if creation fails.

§Errors

Returns an error if initialization fails.

Creates a new symbolic link with the given name and link.

§Arguments
  • name - The name of the entry to create.
  • source - The entry reference the symlink points to.
§Returns

A new EntryBuilder.

§Errors

Returns an error if initialization fails.

§Examples
use libpna::{EntryBuilder, EntryName, EntryReference};

let builder = EntryBuilder::new_symlink(
    EntryName::try_from("path/of/target").unwrap(),
    EntryReference::try_from("path/of/source").unwrap(),
)
.unwrap();
let entry = builder.build().unwrap();
👎Deprecated since 0.27.2: Use EntryBuilder::new_symlink instead

Creates a new symbolic link with the given name and link.

§Deprecated

Use EntryBuilder::new_symlink instead.

§Arguments
  • name - The name of the entry to create.
  • source - The entry reference the symlink points to.
§Errors

Returns an error if initialization fails.

Creates a new hard link with the given name and link.

§Arguments
  • name - The name of the entry to create.
  • link - The name of the entry reference.
§Returns

A new EntryBuilder.

§Errors

Returns an error if initialization fails.

§Examples
use libpna::{EntryBuilder, EntryName, EntryReference};

let builder = EntryBuilder::new_hard_link(
    EntryName::try_from("path/of/target").unwrap(),
    EntryReference::try_from("path/of/source").unwrap(),
)
.unwrap();
let entry = builder.build().unwrap();
Source

pub fn created( &mut self, since_unix_epoch: impl Into<Option<Duration>>, ) -> &mut EntryBuilder

Sets the creation timestamp of the entry.

§Arguments
  • since_unix_epoch - The duration since the Unix epoch to set the creation timestamp to.
§Returns

A mutable reference to the EntryBuilder with the creation timestamp set.

Source

pub fn modified( &mut self, since_unix_epoch: impl Into<Option<Duration>>, ) -> &mut EntryBuilder

Sets the last modified timestamp of the entry.

§Arguments
  • since_unix_epoch - The duration since the Unix epoch to set the last modified timestamp to.
§Returns

A mutable reference to the EntryBuilder with the last modified timestamp set.

Source

pub fn accessed( &mut self, since_unix_epoch: impl Into<Option<Duration>>, ) -> &mut EntryBuilder

Sets the last accessed timestamp of the entry.

§Arguments
  • since_unix_epoch - The duration since the Unix epoch to set the last accessed timestamp to.
§Returns

A mutable reference to the EntryBuilder with the last modified timestamp set.

Source

pub fn permission( &mut self, permission: impl Into<Option<Permission>>, ) -> &mut EntryBuilder

Sets the permission of the entry to the given owner, group, and permissions.

§Arguments
  • permission - A Permission struct containing the owner, group, and permissions to set for the entry.
§Returns

A mutable reference to the EntryBuilder with the permission set.

Source

pub fn file_size(&mut self, store: bool) -> &mut EntryBuilder

Sets retention of raw file size data for entry.

§Arguments
  • store - If true retention data of raw file size for entry, otherwise not.
§Returns

A mutable reference to the EntryBuilder with the store set.

Source

pub fn add_xattr(&mut self, xattr: ExtendedAttribute) -> &mut EntryBuilder

Adds ExtendedAttribute to the entry.

§Arguments
  • xattr - The extended attribute.
§Returns

A mutable reference to the EntryBuilder with the creation timestamp set.

Source

pub fn add_extra_chunk<T>(&mut self, chunk: T) -> &mut EntryBuilder
where T: Into<RawChunk>,

Adds extra chunk to the entry.

§Arguments
  • chunk - The extra chunk.
§Returns

A mutable reference to the EntryBuilder with the creation timestamp set.

Source

pub fn build(self) -> Result<NormalEntry, Error>

Builds the entry and returns a Result containing the new NormalEntry.

§Returns

A Result containing the new NormalEntry, or an I/O error if the build fails.

§Errors

Returns an error if an I/O error occurs while building entry into buffer.

Trait Implementations§

Source§

impl EntryBuilderExt for EntryBuilder

Source§

fn add_metadata(&mut self, metadata: &Metadata) -> &mut Self

Sets metadata from a Metadata instance.

§Examples
use pna::{EntryBuilder, Metadata, WriteOptions, prelude::*};
use std::fs;

let fs_meta = fs::metadata("some_file.txt")?;
let metadata = Metadata::from_metadata(&fs_meta)?;

let mut builder = EntryBuilder::new_file("some_file.txt".try_into().unwrap(), WriteOptions::store())?;
builder.add_metadata(&metadata);
Source§

fn created_time(&mut self, time: impl Into<Option<SystemTime>>) -> &mut Self

Sets the created time using SystemTime.

§Examples
use pna::{EntryBuilder, WriteOptions, prelude::*};
use std::time::SystemTime;

let mut builder = EntryBuilder::new_file("file.txt".try_into().unwrap(), WriteOptions::store())?;
builder.created_time(SystemTime::now());
Source§

fn modified_time(&mut self, time: impl Into<Option<SystemTime>>) -> &mut Self

Sets the modified time using SystemTime.

§Examples
use pna::{EntryBuilder, WriteOptions, prelude::*};
use std::time::SystemTime;

let mut builder = EntryBuilder::new_file("file.txt".try_into().unwrap(), WriteOptions::store())?;
builder.modified_time(SystemTime::now());
Source§

fn accessed_time(&mut self, time: impl Into<Option<SystemTime>>) -> &mut Self

Sets the accessed time using SystemTime.

§Examples
use pna::{EntryBuilder, WriteOptions, prelude::*};
use std::time::SystemTime;

let mut builder = EntryBuilder::new_file("file.txt".try_into().unwrap(), WriteOptions::store())?;
builder.accessed_time(SystemTime::now());
Source§

impl Write for EntryBuilder

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V