-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathextract.rs
More file actions
150 lines (140 loc) · 4.78 KB
/
extract.rs
File metadata and controls
150 lines (140 loc) · 4.78 KB
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
use std::error::Error;
use std::fs;
use std::io;
use zip::ZipArchive;
use zip::result::ZipError;
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {:?} <filename>", args[0]);
return Err("Wrong usage".into());
}
let file_arg = &args[1];
if file_arg.contains("..") || file_arg.contains('/') || file_arg.contains('\\') {
eprintln!(
"Error: invalid filename {:?}. Directory separators and \"..\" are not allowed.",
file_arg
);
return Err("Invalid filename".into());
}
// Build a path to the archive relative to a safe base directory (current working directory)
let base_dir = match std::env::current_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("Error: unable to determine current directory: {e}");
return Err("Unable to get the current_dir".into());
}
};
let candidate_path = base_dir.join(std::path::Path::new(file_arg));
// Validate the path without requiring the file to exist
let out_root = candidate_path.components().collect::<std::path::PathBuf>();
if !out_root.starts_with(&base_dir) {
eprintln!(
"Error: path {:?} escapes the allowed directory.",
candidate_path.display()
);
return Err("Invalid path".into());
}
let mut archive = match fs::File::open(&out_root)
.map_err(ZipError::from)
.and_then(ZipArchive::new)
{
Ok(archive) => archive,
Err(e) => {
eprintln!(
"Error: unable to open archive {:?}: {e}",
out_root.display()
);
return Err(e.into());
}
};
let mut some_files_failed = false;
for i in 0..archive.len() {
let mut file = match archive.by_index(i) {
Ok(file) => file,
Err(e) => {
eprintln!("Error: unable to open file {i} in archive: {e}");
some_files_failed = true;
continue;
}
};
let out_path = match file.enclosed_name() {
Some(path) => path,
None => {
eprintln!(
"Error: unable to extract file {:?} because it has an invalid path.",
file.name()
);
some_files_failed = true;
continue;
}
};
let comment = file.comment();
if !comment.is_empty() {
println!("File {i} comment: {comment:?}");
}
if file.is_dir() {
if let Err(e) = fs::create_dir_all(&out_path) {
eprintln!(
"Error: unable to extract directory {i} to {:?}: {e}",
out_path.display()
);
some_files_failed = true;
continue;
} else {
println!("Directory {i} extracted to {:?}", out_path.display());
}
} else {
if let Some(p) = out_path.parent()
&& !p.exists()
&& let Err(e) = fs::create_dir_all(p)
{
eprintln!(
"Error: unable to create parent directory {p:?} of file {}: {e}",
p.display()
);
some_files_failed = true;
continue;
}
match fs::File::create(&out_path)
.and_then(|mut outfile| io::copy(&mut file, &mut outfile))
{
Ok(bytes_extracted) => {
println!(
"File {} extracted to {:?} ({bytes_extracted} bytes)",
i,
out_path.display(),
);
}
Err(e) => {
eprintln!(
"Error: unable to extract file {i} to {:?}: {e}",
out_path.display()
);
some_files_failed = true;
continue;
}
}
}
// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode()
&& let Err(e) = fs::set_permissions(&out_path, fs::Permissions::from_mode(mode))
{
eprintln!(
"Error: unable to change permissions of file {i} ({:?}): {e}",
out_path.display()
);
some_files_failed = true;
}
}
}
if some_files_failed {
eprintln!("Error: some files failed to extract; see above errors.");
Err("Extraction partially failed".into())
} else {
Ok(())
}
}