use std::path::Path;
use crate::{ExtractOptions, FileMode};
use super::file::File;
use super::list::{ListEntries, ListOptions};
use super::store::Store;
use super::tree::ArchiveOptions;
#[derive(Debug)]
pub struct Archive<'conn> {
store: Store<'conn>,
umask: FileMode,
}
impl<'conn> Archive<'conn> {
pub(super) fn new(tx: rusqlite::Transaction<'conn>) -> Self {
Self {
store: Store::new(tx),
umask: FileMode::OTHER_W,
}
}
pub(super) fn into_tx(self) -> rusqlite::Transaction<'conn> {
self.store.into_tx()
}
pub(super) fn init(&mut self, fail_if_exists: bool) -> crate::Result<()> {
self.store.create_table(fail_if_exists)
}
pub fn open<'ar, P: AsRef<Path>>(&'ar mut self, path: P) -> crate::Result<File<'conn, 'ar>> {
File::new(path.as_ref(), &mut self.store, self.umask)
}
pub fn list(&mut self) -> crate::Result<ListEntries> {
self.store.list_files(&ListOptions::new())
}
pub fn list_with(&mut self, opts: &ListOptions) -> crate::Result<ListEntries> {
if opts.is_invalid {
return Err(crate::Error::InvalidArgs {
reason: String::from(
"Mutually exclusive options where used together in `ListOptions`.",
),
});
}
self.store.list_files(opts)
}
pub fn archive<P: AsRef<Path>, Q: AsRef<Path>>(&mut self, from: P, to: Q) -> crate::Result<()> {
self.archive_with(from, to, &Default::default())
}
pub fn archive_with<P: AsRef<Path>, Q: AsRef<Path>>(
&mut self,
from: P,
to: Q,
opts: &ArchiveOptions,
) -> crate::Result<()> {
self.archive_tree(
from.as_ref(),
to.as_ref(),
opts,
#[cfg(unix)]
&super::mode::UnixModeAdapter,
#[cfg(windows)]
&super::mode::WindowsModeAdapter,
)
}
pub fn extract<P: AsRef<Path>, Q: AsRef<Path>>(&mut self, from: P, to: Q) -> crate::Result<()> {
self.extract_with(from, to, &Default::default())
}
pub fn extract_with<P: AsRef<Path>, Q: AsRef<Path>>(
&mut self,
from: P,
to: Q,
opts: &ExtractOptions,
) -> crate::Result<()> {
self.extract_tree(
from.as_ref(),
to.as_ref(),
opts,
#[cfg(unix)]
&super::mode::UnixModeAdapter,
#[cfg(windows)]
&super::mode::WindowsModeAdapter,
)
}
pub fn umask(&self) -> FileMode {
self.umask
}
pub fn set_umask(&mut self, mode: FileMode) {
self.umask = mode;
}
}