Skip to content

Commit

Permalink
Merge pull request #38 from tamada/release/v0.7.1
Browse files Browse the repository at this point in the history
Release/v0.7.1
  • Loading branch information
tamada authored Jan 26, 2025
2 parents 28696e8 + ab8e0c2 commit fb35075
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 175 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "totebag"
version = "0.7.0"
version = "0.7.1"
description = "A tool for extracting/archiving files and directories in multiple formats."
repository = "https://github.com/tamada/totebag"
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# totebag

[![Version](https://shields.io/badge/Version-0.7.0-blue)](https://github.com/tamada/totebag/releases/tag/v0.7.0)
[![Version](https://shields.io/badge/Version-0.7.1-blue)](https://github.com/tamada/totebag/releases/tag/v0.7.1)
[![MIT License](https://shields.io/badge/License-MIT-blue)](https://github.com/tamada/totebag/blob/main/LICENSE)
[![docker](https://shields.io/badge/Docker-0.7.0-blue?logo=docker)](https://github.com/tamada/totebag/pkgs/container/totebag)
[![docker](https://shields.io/badge/Docker-0.7.1-blue?logo=docker)](https://github.com/tamada/totebag/pkgs/container/totebag)

[![build](https://github.com/tamada/totebag/actions/workflows/build.yaml/badge.svg)](https://github.com/tamada/totebag/actions/workflows/build.yaml)
[![Rust Report Card](https://rust-reportcard.xuri.me/badge/github.com/tamada/totebag)](https://rust-reportcard.xuri.me/report/github.com/tamada/totebag)
Expand Down Expand Up @@ -67,7 +67,7 @@ brew install tamada/tap/totebag
## :whale: Docker
```sh
docker run -it --rm -v $PWD:/workdir ghcr.io/tamada/totebag:0.7.0 [OPTIONS] [ARGUMENTS]...
docker run -it --rm -v $PWD:/workdir ghcr.io/tamada/totebag:0.7.1 [OPTIONS] [ARGUMENTS]...
```
- **Working directory**: `/workdir`
Expand Down
4 changes: 2 additions & 2 deletions dockers/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.16 AS builder

ARG VERSION=0.7.0
ARG VERSION=0.7.1
ARG TARGETPLATFORM
ARG PLATFORM=${TARGETPLATFORM#linux/}

Expand All @@ -12,7 +12,7 @@ RUN apk add --no-cache curl tar gzip \

FROM alpine:3.16

ARG VERSION=0.7.0
ARG VERSION=0.7.1

LABEL org.opencontainers.image.source https://github.com/tamada/totebag

Expand Down
36 changes: 17 additions & 19 deletions src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ impl<'a> TargetPath<'a> {

/// Returns the directory traversing walker for the given path of this instance.
pub fn walker(&self) -> Walk {
let mut builder = WalkBuilder::new(&self.base_path);
let mut builder = WalkBuilder::new(self.base_path);
build_walker_impl(self.opts, &mut builder);
builder.build()
}
}

impl<'a> Archiver {
impl Archiver {
pub fn perform(&self) -> Result<()> {
let archiver = match create_archiver(&self.archive_file) {
Ok(a) => a,
Expand All @@ -104,7 +104,7 @@ impl<'a> Archiver {
let paths = self
.targets
.iter()
.map(|item| TargetPath::new(item, &self))
.map(|item| TargetPath::new(item, self))
.collect::<Vec<TargetPath>>();

log::info!("{:?}: {}", self.archive_file, self.archive_file.exists());
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<'a> Archiver {
r.insert(IgnoreType::GitGlobal);
r.insert(IgnoreType::GitExclude);
} else {
r.insert(it.clone());
r.insert(it);
}
}
r.into_iter().collect()
Expand Down Expand Up @@ -225,21 +225,19 @@ fn create_archiver(dest: &PathBuf) -> Result<Box<dyn ToteArchiver>> {

let format = find_format(dest);
match format {
Ok(format) => {
return match format {
Format::Cab => Ok(Box::new(CabArchiver {})),
Format::LHA => Ok(Box::new(LhaArchiver {})),
Format::Rar => Ok(Box::new(RarArchiver {})),
Format::SevenZ => Ok(Box::new(SevenZArchiver {})),
Format::Tar => Ok(Box::new(TarArchiver {})),
Format::TarBz2 => Ok(Box::new(TarBz2Archiver {})),
Format::TarGz => Ok(Box::new(TarGzArchiver {})),
Format::TarXz => Ok(Box::new(TarXzArchiver {})),
Format::TarZstd => Ok(Box::new(TarZstdArchiver {})),
Format::Zip => Ok(Box::new(ZipArchiver::new())),
_ => Err(ToteError::UnknownFormat(format.to_string())),
}
}
Ok(format) => match format {
Format::Cab => Ok(Box::new(CabArchiver {})),
Format::LHA => Ok(Box::new(LhaArchiver {})),
Format::Rar => Ok(Box::new(RarArchiver {})),
Format::SevenZ => Ok(Box::new(SevenZArchiver {})),
Format::Tar => Ok(Box::new(TarArchiver {})),
Format::TarBz2 => Ok(Box::new(TarBz2Archiver {})),
Format::TarGz => Ok(Box::new(TarGzArchiver {})),
Format::TarXz => Ok(Box::new(TarXzArchiver {})),
Format::TarZstd => Ok(Box::new(TarZstdArchiver {})),
Format::Zip => Ok(Box::new(ZipArchiver::new())),
_ => Err(ToteError::UnknownFormat(format.to_string())),
},
Err(msg) => Err(msg),
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/archiver/cab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ fn write_entry(writer: &mut CabinetWriter<File>, path: PathBuf) -> Result<()> {
fn collect_entries<'a>(tps: &'a Vec<TargetPath>) -> Vec<(PathBuf, &'a TargetPath<'a>)> {
let mut r = vec![];
for tp in tps {
for entry in tp.walker() {
if let Ok(t) = entry {
let path = t.into_path();
if path.is_file() {
r.push((path, tp));
}
for t in tp.walker().flatten() {
let path = t.into_path();
if path.is_file() {
r.push((path, tp));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/archiver/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zip::write::SimpleFileOptions;
use zip::DateTime;

pub fn create_file_opts(target: &PathBuf) -> SimpleFileOptions {
let metadata = std::fs::metadata(&target).unwrap();
let metadata = std::fs::metadata(target).unwrap();
let mod_time = DateTime::try_from(OffsetDateTime::from(metadata.modified().unwrap()));

SimpleFileOptions::default()
Expand Down
14 changes: 6 additions & 8 deletions src/archiver/sevenz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ impl ToteArchiver for SevenZArchiver {
};
let mut errs = vec![];
for tp in tps {
for entry in tp.walker() {
if let Ok(t) = entry {
let path = t.into_path();
if path.is_file() {
if let Err(e) = process_file(&mut w, &path, &tp.dest_path(&path)) {
errs.push(e);
}
for t in tp.walker().flatten() {
let path = t.into_path();
if path.is_file() {
if let Err(e) = process_file(&mut w, &path, &tp.dest_path(&path)) {
errs.push(e);
}
}
}
Expand All @@ -51,7 +49,7 @@ impl ToteArchiver for SevenZArchiver {
fn process_file(szw: &mut SevenZWriter<File>, target: &PathBuf, dest_path: &PathBuf) -> Result<()> {
let name = &dest_path.to_str().unwrap();
if let Err(e) = szw.push_archive_entry(
SevenZArchiveEntry::from_path(&dest_path, name.to_string()),
SevenZArchiveEntry::from_path(dest_path, name.to_string()),
Some(File::open(target).unwrap()),
) {
return Err(ToteError::Archiver(e.to_string()));
Expand Down
22 changes: 10 additions & 12 deletions src/archiver/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ fn write_tar<W: Write>(tps: Vec<TargetPath>, f: W) -> Result<()> {
let mut builder = tar::Builder::new(f);
let mut errs = vec![];
for tp in tps {
for entry in tp.walker() {
if let Ok(t) = entry {
let path = t.into_path();
let dest_dir = tp.dest_path(&path);
if path.is_file() {
if let Err(e) = process_file(&mut builder, &path, &dest_dir) {
errs.push(e);
}
} else if path.is_dir() {
if let Err(e) = builder.append_dir(&dest_dir, &path) {
errs.push(ToteError::Archiver(e.to_string()));
}
for t in tp.walker().flatten() {
let path = t.into_path();
let dest_dir = tp.dest_path(&path);
if path.is_file() {
if let Err(e) = process_file(&mut builder, &path, &dest_dir) {
errs.push(e);
}
} else if path.is_dir() {
if let Err(e) = builder.append_dir(&dest_dir, &path) {
errs.push(ToteError::Archiver(e.to_string()));
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/archiver/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ impl ToteArchiver for ZipArchiver {
let mut errs = vec![];
let mut zw = zip::ZipWriter::new(file);
for tp in tps {
for entry in tp.walker() {
if let Ok(t) = entry {
let path = t.into_path();
if path.is_file() {
if let Err(e) = self.process_file(&mut zw, path, &tp) {
errs.push(e);
}
for t in tp.walker().flatten() {
let path = t.into_path();
if path.is_file() {
if let Err(e) = self.process_file(&mut zw, path, &tp) {
errs.push(e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum LogLevel {

impl CliOpts {
pub fn run_mode(&mut self) -> Result<RunMode> {
if self.args.len() == 0 {
if self.args.is_empty() {
return Err(ToteError::NoArgumentsGiven);
}
if self.mode == RunMode::Auto {
Expand Down
63 changes: 30 additions & 33 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
use chrono::NaiveDateTime;
use std::fmt::Display;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use typed_builder::TypedBuilder;

use super::format::{find_format, Format};
Expand Down Expand Up @@ -82,12 +82,12 @@ pub struct PathUtils<'a> {
e: &'a Extractor,
}

impl<'a> PathUtils<'a> {
impl PathUtils<'_> {
pub fn base_dir(&self) -> PathBuf {
self.e.base_dir()
}

fn destination(&self, target: PathBuf) -> Result<PathBuf> {
fn destination<P: AsRef<Path>>(&self, target: P) -> Result<PathBuf> {
self.e.destination(target)
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Extractor {
Ok(e) => e,
Err(e) => return Err(e),
};
extractor.list(&self.archive_file)
extractor.list(self.archive_file.clone())
}

/// Execute extraction of the archive file.
Expand All @@ -131,7 +131,7 @@ impl Extractor {
Err(e) => return Err(e),
};
match self.can_extract() {
Ok(_) => extractor.perform(&self.archive_file, PathUtils { e: self }),
Ok(_) => extractor.perform(self.archive_file.clone(), PathUtils { e: &self }),
Err(e) => Err(e),
}
}
Expand All @@ -151,8 +151,7 @@ impl Extractor {
fn base_dir(&self) -> PathBuf {
if self.use_archive_name_dir {
if let Some(stem) = self.archive_file.file_stem() {
let dir_name = stem.to_str().unwrap();
self.destination.join(dir_name)
self.destination.join(stem)
} else {
self.destination.clone()
}
Expand All @@ -162,7 +161,7 @@ impl Extractor {
}

/// Return the path of the `target` file for output.
fn destination(&self, target: PathBuf) -> Result<PathBuf> {
fn destination<P: AsRef<Path>>(&self, target: P) -> Result<PathBuf> {
let base = self.base_dir();
let dest = base.join(target);
if dest.exists() && !self.overwrite {
Expand All @@ -187,37 +186,35 @@ impl Extractor {
/// The trait for extracting the archive file.
pub(crate) trait ToteExtractor {
/// returns the entry list of the given archive file.
fn list(&self, archive_file: &PathBuf) -> Result<Vec<Entry>>;
fn list(&self, archive_file: PathBuf) -> Result<Vec<Entry>>;
/// extract the given archive file into the specified directory with the given options.
fn perform(&self, archive_file: &PathBuf, opts: PathUtils) -> Result<()>;
fn perform(&self, archive_file: PathBuf, opts: PathUtils) -> Result<()>;
#[cfg(test)]
/// returns the supported format of the extractor.
fn format(&self) -> Format;
}

/// Returns the extractor for the given archive file.
/// The supported format is `cab`, `lha`, `rar`, `7z`, `tar`, `tar.gz`, `tar.bz2`, `tar.xz`, `tar.zst`, and `zip`.
fn create(file: &PathBuf) -> Result<Box<dyn ToteExtractor>> {
let format = find_format(&file);
fn create(file: &Path) -> Result<Box<dyn ToteExtractor>> {
let format = find_format(file);
match format {
Ok(format) => {
return match format {
Format::Cab => Ok(Box::new(cab::CabExtractor {})),
Format::LHA => Ok(Box::new(lha::LhaExtractor {})),
Format::Rar => Ok(Box::new(rar::RarExtractor {})),
Format::SevenZ => Ok(Box::new(sevenz::SevenZExtractor {})),
Format::Tar => Ok(Box::new(tar::TarExtractor {})),
Format::TarBz2 => Ok(Box::new(tar::TarBz2Extractor {})),
Format::TarGz => Ok(Box::new(tar::TarGzExtractor {})),
Format::TarXz => Ok(Box::new(tar::TarXzExtractor {})),
Format::TarZstd => Ok(Box::new(tar::TarZstdExtractor {})),
Format::Zip => Ok(Box::new(zip::ZipExtractor {})),
Format::Unknown(s) => Err(ToteError::UnknownFormat(format!(
"{}: unsupported format",
s
))),
}
}
Ok(format) => match format {
Format::Cab => Ok(Box::new(cab::CabExtractor {})),
Format::LHA => Ok(Box::new(lha::LhaExtractor {})),
Format::Rar => Ok(Box::new(rar::RarExtractor {})),
Format::SevenZ => Ok(Box::new(sevenz::SevenZExtractor {})),
Format::Tar => Ok(Box::new(tar::TarExtractor {})),
Format::TarBz2 => Ok(Box::new(tar::TarBz2Extractor {})),
Format::TarGz => Ok(Box::new(tar::TarGzExtractor {})),
Format::TarXz => Ok(Box::new(tar::TarXzExtractor {})),
Format::TarZstd => Ok(Box::new(tar::TarZstdExtractor {})),
Format::Zip => Ok(Box::new(zip::ZipExtractor {})),
Format::Unknown(s) => Err(ToteError::UnknownFormat(format!(
"{}: unsupported format",
s
))),
},
Err(msg) => Err(msg),
}
}
Expand All @@ -234,17 +231,17 @@ mod tests {
.use_archive_name_dir(true)
.build();
assert_eq!(opts1.base_dir(), PathBuf::from("./archive"));
if let Ok(t) = opts1.destination("text1.txt".into()) {
if let Ok(t) = opts1.destination("text1.txt") {
assert_eq!(t, PathBuf::from("./archive/text1.txt"));
}
if let Ok(t) = opts1.destination("text2.txt".into()) {
if let Ok(t) = opts1.destination("text2.txt") {
assert_eq!(t, PathBuf::from("./archive/text2.txt"));
}

let archive_file = PathBuf::from("/tmp/archive.zip");
let opts2 = Extractor::builder().archive_file(archive_file).build();
assert_eq!(opts2.base_dir(), PathBuf::from("."));
if let Ok(t) = opts2.destination("./text1.txt".into()) {
if let Ok(t) = opts2.destination("./text1.txt") {
assert_eq!(t, PathBuf::from("./text1.txt"));
}
}
Expand Down
Loading

0 comments on commit fb35075

Please sign in to comment.