Crate nuts_archive

Source
Expand description

A storage application inspired by the tar tool.

The archive is an application based on the nuts container. Inspired by the tar tool you can store files, directories and symlinks in a nuts container.

  • Entries can be appended at the end of the archive.
  • They cannot be removed from the archive.
  • You can travere the archive from the first to the last entry in the archive.

§Create a new archive

use nuts_archive::ArchiveFactory;
use nuts_container::{Cipher, Container, CreateOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend};
use tempfile::{Builder, TempDir};

// Let's create an archive service (with a directory backend) in a temporary directory
let tmp_dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();
let backend_options = CreateOptions::for_path(tmp_dir);
let container_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
    .with_password_callback(|| Ok(b"123".to_vec()))
    .build::<DirectoryBackend<TempDir>>()
    .unwrap();
let container = Container::create(backend_options, container_options).unwrap();
let archive = Container::create_service::<ArchiveFactory>(container).unwrap();

// Fetch some information
let info = archive.info();
assert_eq!(info.blocks, 0);
assert_eq!(info.files, 0);

§Open an existing archive

use nuts_archive::ArchiveFactory;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};

let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();

{
    // This will create an empty archive in a temporary directory.

    let backend_options = CreateOptions::for_path(dir.path().to_owned());
    let container_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
        .with_password_callback(|| Ok(b"123".to_vec()))
        .build::<DirectoryBackend<&TempDir>>()
        .unwrap();
    let container = Container::create(backend_options, container_options).unwrap();

    Container::create_service::<ArchiveFactory>(container).unwrap();
}

// Open the archive service (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(dir);
let container_options = OpenOptionsBuilder::new()
    .with_password_callback(|| Ok(b"123".to_vec()))
    .build::<DirectoryBackend<TempDir>>()
    .unwrap();
let container = Container::open(backend_options, container_options).unwrap();

let archive = Container::open_service::<ArchiveFactory>(container, false).unwrap();

// Fetch some information
let info = archive.info();
assert_eq!(info.blocks, 0);
assert_eq!(info.files, 0);

§Append an entry at the end of the archive

use nuts_archive::ArchiveFactory;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};

let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();

{
    // This will create an empty archive in a temporary directory.

    let backend_options = CreateOptions::for_path(dir.path().to_owned());
    let container_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
        .with_password_callback(|| Ok(b"123".to_vec()))
        .build::<DirectoryBackend<&TempDir>>()
        .unwrap();
    let container = Container::create(backend_options, container_options).unwrap();

    Container::create_service::<ArchiveFactory>(container).unwrap();
}

// Open the archive (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(dir);
let container_options = OpenOptionsBuilder::new()
    .with_password_callback(|| Ok(b"123".to_vec()))
    .build::<DirectoryBackend<TempDir>>()
    .unwrap();
let container = Container::open(backend_options, container_options).unwrap();

let mut archive = Container::open_service::<ArchiveFactory>(container, false).unwrap();

// Append a new file entry
let mut entry = archive.append_file("sample file").build().unwrap();
entry.write_all("some sample data".as_bytes()).unwrap();

// Append a new directory entry
archive
    .append_directory("sample directory")
    .build()
    .unwrap();

// Append a new symlink entry
archive
    .append_symlink("sample symlink", "target")
    .build()
    .unwrap();

§Loop through all entries in the archive

use nuts_archive::ArchiveFactory;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};

let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();

{
    // This will create an empty archive in a temporary directory.

    let backend_options = CreateOptions::for_path(dir.path().to_owned());
    let container_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
        .with_password_callback(|| Ok(b"123".to_vec()))
        .build::<DirectoryBackend<&TempDir>>()
        .unwrap();
    let container = Container::create(backend_options, container_options).unwrap();

    Container::create_service::<ArchiveFactory>(container).unwrap();
}

// Open the archive (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(dir);
let container_options = OpenOptionsBuilder::new()
    .with_password_callback(|| Ok(b"123".to_vec()))
    .build::<DirectoryBackend<TempDir>>()
    .unwrap();
let container = Container::open(backend_options, container_options).unwrap();

// Open the archive and append some entries
let mut archive = Container::open_service::<ArchiveFactory>(container, false).unwrap();

archive.append_file("f1").build().unwrap();
archive.append_directory("f2").build().unwrap();
archive.append_symlink("f3", "target").build().unwrap();

// Go through the archive
let entry = archive.first().unwrap().unwrap();
assert!(entry.is_file());
assert_eq!(entry.name(), "f1");

let entry = entry.next().unwrap().unwrap();
assert!(entry.is_directory());
assert_eq!(entry.name(), "f2");

let entry = entry.next().unwrap().unwrap();
assert!(entry.is_symlink());
assert_eq!(entry.name(), "f3");

assert!(entry.next().is_none());

Structs§

Archive
The archive.
ArchiveFactory
DirectoryBuilder
Builder for an new directory entry.
DirectoryEntry
A directory entry of the archive.
EntryMut
A mutable entry of the archive.
FileBuilder
Builder for an new file entry.
FileEntry
A file entry of the archive.
Info
Information/statistics from the archive.
SymlinkBuilder
Builder for an new symlink entry.
SymlinkEntry
A symlink entry of the archive.

Enums§

Entry
An entry of the archive.
Error
Error type of this library.
Group

Type Aliases§

ArchiveResult