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::Archive;
use nuts_container::{Cipher, Container, CreateOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend};
use tempfile::{Builder, TempDir};
// Let's create a container (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 contaner_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<TempDir>>::create(backend_options, contaner_options).unwrap();
// Now create an archive inside the container
let archive = Archive::create(container, false).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::Archive;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};
// This will create an empty archive in a temporary directory.
let tmp_dir = {
let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();
let backend_options = CreateOptions::for_path(&dir);
let contaner_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<&TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<&TempDir>>::create(backend_options, contaner_options)
.unwrap();
Archive::create(container, false).unwrap();
dir
};
// Open the container (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(tmp_dir);
let container_options = OpenOptionsBuilder::new()
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<TempDir>>::open(backend_options, container_options).unwrap();
// Open the archive
let archive = Archive::open(container).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::Archive;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};
// This will create an empty archive in a temporary directory.
let tmp_dir = {
let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();
let backend_options = CreateOptions::for_path(&dir);
let contaner_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<&TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<&TempDir>>::create(backend_options, contaner_options)
.unwrap();
Archive::create(container, false).unwrap();
dir
};
// Open the container (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(tmp_dir);
let container_options = OpenOptionsBuilder::new()
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<TempDir>>::open(backend_options, container_options).unwrap();
// Open the archive
let mut archive = Archive::open(container).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::Archive;
use nuts_container::{Cipher, Container, CreateOptionsBuilder, OpenOptionsBuilder};
use nuts_directory::{CreateOptions, DirectoryBackend, OpenOptions};
use tempfile::{Builder, TempDir};
// This will create an empty archive in a temporary directory.
let tmp_dir = {
let dir = Builder::new().prefix("nuts-archive").tempdir().unwrap();
let backend_options = CreateOptions::for_path(&dir);
let contaner_options = CreateOptionsBuilder::new(Cipher::Aes128Gcm)
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<&TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<&TempDir>>::create(backend_options, contaner_options)
.unwrap();
Archive::create(container, false).unwrap();
dir
};
// Open the container (with a directory backend) from the temporary directory.
let backend_options = OpenOptions::for_path(tmp_dir);
let container_options = OpenOptionsBuilder::new()
.with_password_callback(|| Ok(b"123".to_vec()))
.build::<DirectoryBackend<TempDir>>()
.unwrap();
let container =
Container::<DirectoryBackend<TempDir>>::open(backend_options, container_options).unwrap();
// Open the archive and append some entries
let mut archive = Archive::open(container).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§
- The archive.
- Builder for an new directory entry.
- A directory entry of the archive.
- A mutable entry of the archive.
- Builder for an new file entry.
- A file entry of the archive.
- Information/statistics from the archive.
- Builder for an new symlink entry.
- A symlink entry of the archive.
Enums§
- An entry of the archive.
- Error type of this library.