2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2018-02-10 01:32:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-22 11:44:38 +03:00
|
|
|
use std::fs::{self, File};
|
2018-12-10 21:20:21 +03:00
|
|
|
/// Wrappers around the `zip-rs` library to compress and decompress zip archives.
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::io;
|
2019-03-23 23:39:50 +03:00
|
|
|
use std::panic;
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::path::Path;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::zip_rs::result::{ZipError, ZipResult};
|
|
|
|
use self::zip_rs::write::FileOptions;
|
|
|
|
use zip as zip_rs;
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-12-10 21:20:21 +03:00
|
|
|
/// Compress a source directory recursively into a zip file.
|
|
|
|
/// Permissions are set to 644 by default to avoid any
|
2018-02-10 01:32:16 +03:00
|
|
|
/// unwanted execution bits.
|
|
|
|
pub fn compress(src_dir: &Path, dst_file: &File) -> ZipResult<()> {
|
|
|
|
if !Path::new(src_dir).is_dir() {
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(ZipError::Io(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"Source must be a directory.",
|
|
|
|
)));
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let options = FileOptions::default()
|
2018-12-10 21:20:21 +03:00
|
|
|
.compression_method(zip_rs::CompressionMethod::Stored)
|
2018-02-10 01:32:16 +03:00
|
|
|
.unix_permissions(0o644);
|
|
|
|
|
|
|
|
let mut zip = zip_rs::ZipWriter::new(dst_file);
|
|
|
|
let walkdir = WalkDir::new(src_dir.to_str().unwrap());
|
|
|
|
let it = walkdir.into_iter();
|
|
|
|
|
|
|
|
for dent in it.filter_map(|e| e.ok()) {
|
|
|
|
let path = dent.path();
|
2018-09-26 23:38:44 +03:00
|
|
|
let name = path
|
|
|
|
.strip_prefix(Path::new(src_dir))
|
2018-02-10 01:32:16 +03:00
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if path.is_file() {
|
|
|
|
zip.start_file(name, options)?;
|
|
|
|
let mut f = File::open(path)?;
|
|
|
|
io::copy(&mut f, &mut zip)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zip.finish()?;
|
|
|
|
dst_file.sync_all()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decompress a source file into the provided destination path.
|
2019-02-25 09:57:56 +03:00
|
|
|
pub fn decompress<R, F>(src_file: R, dest: &Path, expected: F) -> ZipResult<usize>
|
2018-03-04 03:19:54 +03:00
|
|
|
where
|
2019-03-23 23:39:50 +03:00
|
|
|
R: io::Read + io::Seek + panic::UnwindSafe,
|
|
|
|
F: Fn(&Path) -> bool + panic::UnwindSafe,
|
2018-03-04 03:19:54 +03:00
|
|
|
{
|
2019-02-25 09:57:56 +03:00
|
|
|
let mut decompressed = 0;
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2019-03-23 23:39:50 +03:00
|
|
|
// catch the panic to avoid the thread quit
|
|
|
|
panic::set_hook(Box::new(|panic_info| {
|
|
|
|
error!(
|
|
|
|
"panic occurred: {:?}",
|
|
|
|
panic_info.payload().downcast_ref::<&str>().unwrap()
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
let result = panic::catch_unwind(move || {
|
|
|
|
let mut archive = zip_rs::ZipArchive::new(src_file)?;
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2019-03-23 23:39:50 +03:00
|
|
|
for i in 0..archive.len() {
|
|
|
|
let mut file = archive.by_index(i)?;
|
|
|
|
let san_name = file.sanitized_name();
|
|
|
|
if san_name.to_str().unwrap_or("").replace("\\", "/") != file.name().replace("\\", "/")
|
|
|
|
|| !expected(&san_name)
|
|
|
|
{
|
|
|
|
info!(
|
|
|
|
"ignoring a suspicious file: {}, got {:?}",
|
|
|
|
file.name(),
|
|
|
|
san_name.to_str()
|
|
|
|
);
|
|
|
|
continue;
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
2019-03-23 23:39:50 +03:00
|
|
|
let file_path = dest.join(san_name);
|
|
|
|
|
|
|
|
if (&*file.name()).ends_with('/') {
|
|
|
|
fs::create_dir_all(&file_path)?;
|
|
|
|
} else {
|
|
|
|
if let Some(p) = file_path.parent() {
|
|
|
|
if !p.exists() {
|
|
|
|
fs::create_dir_all(&p)?;
|
|
|
|
}
|
2019-02-10 00:14:27 +03:00
|
|
|
}
|
2019-03-23 23:39:50 +03:00
|
|
|
let res = fs::File::create(&file_path);
|
|
|
|
let mut outfile = match res {
|
|
|
|
Err(e) => {
|
|
|
|
error!("{:?}", e);
|
|
|
|
return Err(zip::result::ZipError::Io(e));
|
|
|
|
}
|
|
|
|
Ok(r) => r,
|
|
|
|
};
|
|
|
|
io::copy(&mut file, &mut outfile)?;
|
|
|
|
decompressed += 1;
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2019-03-23 23:39:50 +03:00
|
|
|
// Get and Set permissions
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
if let Some(mode) = file.unix_mode() {
|
|
|
|
fs::set_permissions(
|
|
|
|
&file_path.to_str().unwrap(),
|
|
|
|
PermissionsExt::from_mode(mode),
|
|
|
|
)?;
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 23:39:50 +03:00
|
|
|
Ok(decompressed)
|
|
|
|
});
|
|
|
|
match result {
|
|
|
|
Ok(res) => match res {
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
Ok(_) => res,
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
error!("panic occurred on zip::decompress!");
|
|
|
|
Err(zip::result::ZipError::InvalidArchive(
|
|
|
|
"panic occurred on zip::decompress",
|
|
|
|
))
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
2018-09-26 23:38:44 +03:00
|
|
|
}
|