1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use ataf::compression::Decompressor;
use clap::ArgMatches;
use std::{
io::{BufReader, IsTerminal, Read},
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
macro_rules! println_if_terminal {
($fmt:expr $(, $args:expr)* $(,)?) => {
if std::io::stdout().is_terminal() {
println!($fmt $(, $args)*);
}
};
}
pub fn run(matches: &ArgMatches) -> i32 {
let threads = matches.get_one::<usize>("threads").unwrap();
let input = matches.get_one::<PathBuf>("input");
let output = matches.get_one::<PathBuf>("output").unwrap();
println_if_terminal!("extracting archive with the following options:");
println_if_terminal!("number of threads: {}", threads);
let reader: Box<dyn std::io::Read> = match input {
Some(path) => Box::new(std::fs::File::open(path).unwrap()),
None => Box::new(std::io::stdin()),
};
let mut archive =
ataf::archive::read::Archive::new(BufReader::with_capacity(1024 * 1024, reader));
let decompressor: Box<dyn Decompressor> = match archive.header().unwrap().compression.as_str() {
"none" => Box::new(ataf::compression::NoDecompressor),
#[cfg(feature = "flate2")]
"flate2" => Box::new(ataf::compression::Flate2Decompressor::new(*threads)),
#[cfg(feature = "brotli")]
"brotli" => Box::new(ataf::compression::BrotliDecompressor::new(*threads)),
#[cfg(feature = "lz4")]
"lz4" => Box::new(ataf::compression::Lz4Decompressor::new(*threads)),
_ => {
eprintln!(
"ERROR unsupported compression format: {}",
archive.header().unwrap().compression
);
return 1;
}
};
let mut entries = archive.entries(decompressor).unwrap();
while let Some(entry) = entries.next_entry() {
match entry {
Ok(mut entry) => {
println!(
"processing: {}, size: {}",
entry.header().path,
*entry.header().size
);
let mut path = Path::new(&entry.header().path);
if path.is_absolute() {
let mut components = path.components();
components.next();
path = components.as_path();
}
let destination = output.join(path);
if let Some(parent) = destination.parent()
&& !parent.exists()
&& let Err(err) = std::fs::create_dir_all(parent)
{
eprintln!("ERROR error creating parent directory: {}", err);
}
match entry.header().r#type {
ataf::spec::ArchiveEntryHeaderType::File => {
let mut writer = match std::fs::File::create(&destination) {
Ok(file) => file,
Err(err) => {
eprintln!(
"ERROR error creating file {}: {}",
destination.display(),
err
);
continue;
}
};
if let Err(err) = std::io::copy(&mut entry, &mut writer) {
eprintln!(
"ERROR error writing to file {}: {}",
destination.display(),
err
);
continue;
}
writer
.set_modified(
SystemTime::UNIX_EPOCH + Duration::from_secs(*entry.header().mtime),
)
.unwrap();
#[cfg(target_family = "unix")]
{
use std::os::unix::fs::PermissionsExt;
writer
.set_permissions(std::fs::Permissions::from_mode(
entry.header().mode,
))
.unwrap();
}
}
ataf::spec::ArchiveEntryHeaderType::Directory => {
if let Err(err) = std::fs::create_dir(&destination) {
eprintln!(
"ERROR error creating directory {}: {}",
destination.display(),
err
);
continue;
}
}
ataf::spec::ArchiveEntryHeaderType::SymlinkFile => {
let mut symlink_target = String::new();
symlink_target.reserve_exact(*entry.header().size as usize);
if let Err(err) = entry.read_to_string(&mut symlink_target) {
eprintln!(
"ERROR error reading symlink target {}: {}",
entry.header().path,
err
);
continue;
}
#[cfg(target_family = "unix")]
{
if let Err(err) =
std::os::unix::fs::symlink(symlink_target, &destination)
{
eprintln!(
"ERROR error creating symlink {}: {}",
destination.display(),
err
);
continue;
}
}
#[cfg(target_family = "windows")]
{
if let Err(err) =
std::os::windows::fs::symlink_file(symlink_target, &destination)
{
eprintln!(
"ERROR error creating symlink {}: {}",
destination.display(),
err
);
continue;
}
}
}
ataf::spec::ArchiveEntryHeaderType::SymlinkDirectory => {
let mut symlink_target = String::new();
symlink_target.reserve_exact(*entry.header().size as usize);
if let Err(err) = entry.read_to_string(&mut symlink_target) {
eprintln!(
"ERROR error reading symlink target {}: {}",
entry.header().path,
err
);
continue;
}
#[cfg(target_family = "unix")]
{
if let Err(err) =
std::os::unix::fs::symlink(symlink_target, &destination)
{
eprintln!(
"ERROR error creating symlink {}: {}",
destination.display(),
err
);
continue;
}
}
#[cfg(target_family = "windows")]
{
if let Err(err) =
std::os::windows::fs::symlink_dir(symlink_target, &destination)
{
eprintln!(
"ERROR error creating symlink {}: {}",
destination.display(),
err
);
continue;
}
}
}
}
}
Err(err) => {
eprintln!("ERROR error reading entry: {}", err);
return 1;
}
}
}
0
}