Skip to content

Commit

Permalink
Add Permissions for unzip on Unix, remove some Rc<_>.
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Oct 27, 2017
1 parent 1fc32be commit 64d3203
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zipcs"
version = "0.3.3"
version = "0.3.4"
authors = ["Wspsxing <biluohc@qq.com>"]
description = "Useful tools collection."
repository = "https://github.com/biluohc/zipcs"
Expand Down
4 changes: 1 addition & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cargo build --release
### Help

```sh
zipcs 0.3.3
zipcs 0.3.4
Useful tools collection.
Wspsxing <biluohc@qq.com>
Repo: https://github.com/biluohc/zipcs
Expand Down Expand Up @@ -50,6 +50,4 @@ CAMMANDS:
* [The Release Page](https://github.com/biluohc/zipcs/releases)

### Ps
* 未做预防覆盖原有文件和目录的处理,后果概不负责。
* 未处理文件目录权限(即Zip原有的权限没了)。
* 所依赖的[zip-rs](https://github.com/mvdnes/zip-rs)库目前不支持加密,所以目前不支持密码。
8 changes: 3 additions & 5 deletions src/coll/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io::{Read, Write};
use std::process::exit;
use std::path::Path;
use std::fs::File;
use std::rc::Rc;

#[derive(Debug, Default)]
pub struct Files {
Expand All @@ -24,18 +23,17 @@ impl Files {
}
pub fn call(self) {
dbstln!("Config_file_: {:?}", self);
let config = Rc::from(self);

for str in &config.strs {
if let Err(e) = file_handle(str, config.clone()) {
for str in &self.strs {
if let Err(e) = file_handle(str, &self) {
errln!("{}", e);
exit(1);
}
}
}
}

fn file_handle(file_name: &str, config: Rc<Files>) -> Result<(), String> {
fn file_handle(file_name: &str, config: &Files) -> Result<(), String> {
let mut file = File::open(file_name).map_err(|e| {
format!("{:?} open fails: {}", file_name, e)
})?;
Expand Down
18 changes: 5 additions & 13 deletions src/coll/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ffi::OsString;
use std::process::exit;
use std::error::Error;
use std::borrow::Cow;
use std::sync::Arc;
use std::fs::rename;

#[derive(Debug)]
Expand All @@ -29,16 +28,9 @@ impl Paths {
}
pub fn call(self) {
dbstln!("Config_path: {:?}", self);
let depth = self.depth.clone();
let config = Arc::from(self);
for str in &config.strs {
let depth = depth.clone();
if let Err(e) = path_recurse(
Path::new(str).to_owned().into_os_string(),
depth,
config.clone(),
)
{
let depth = self.depth;
for str in &self.strs {
if let Err(e) = path_recurse(Path::new(str).to_owned().into_os_string(), depth, &self) {
errln!("{}", e);
exit(1);
}
Expand All @@ -58,7 +50,7 @@ impl Default for Paths {
}
}

fn path_recurse(path: OsString, mut depth: Option<usize>, config: Arc<Paths>) -> Result<(), String> {
fn path_recurse(path: OsString, mut depth: Option<usize>, config: &Paths) -> Result<(), String> {
let path_decode_result = decode(&path, &config.charset);
if config.charset != CharSet::UTF_8 && path_decode_result.is_ok() {
let str = path_decode_result.unwrap();
Expand Down Expand Up @@ -100,7 +92,7 @@ fn path_recurse(path: OsString, mut depth: Option<usize>, config: Arc<Paths>) ->
format!("{:?}'s entry read fails: {}", path, e.description())
})?;
dbstln!("{:?}", entry.path());
path_recurse(entry.path().into_os_string(), depth, config.clone())?;
path_recurse(entry.path().into_os_string(), depth, config)?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/coll/ping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl RegexList {
I: IntoIterator<Item = S>,
{
let mut rl = Self::default();
for re in res.into_iter() {
for re in res {
rl.0.push(Regex::new(re.as_ref())?);
}
Ok(rl)
Expand Down
32 changes: 25 additions & 7 deletions src/coll/unzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use super::consts::*;
use zip::result::ZipError;
use zip::read::ZipArchive;

use std::fs::{File, create_dir_all};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

use std::fs::{File, create_dir_all, Permissions, set_permissions};
use std::io::{copy, BufReader};
use std::ffi::OsString;
use std::error::Error;
use std::fs::read_dir;
use std::path::Path;
use std::rc::Rc;
use std;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -57,11 +59,9 @@ impl Zips {
}
pub fn call(self) -> Result<(), String> {
dbln!("Config_zip: {:?}", self);
let config = Rc::from(self);

for zip_arch_path in config.zips() {
let config = config.clone();
if let Err(e) = for_zip_arch_file(zip_arch_path, config) {
for zip_arch_path in self.zips() {
if let Err(e) = for_zip_arch_file(zip_arch_path, &self) {
return Err(format!("{:?} -> {:?}", zip_arch_path, e));
}
}
Expand All @@ -81,7 +81,7 @@ impl Zips {
}
}

fn for_zip_arch_file(zip_arch_path: &str, config: Rc<Zips>) -> Result<(), ZipCSError> {
fn for_zip_arch_file(zip_arch_path: &str, config: &Zips) -> Result<(), ZipCSError> {
let zip_arch_path_ = Path::new(zip_arch_path);
let zip_arch = File::open(zip_arch_path)?;
let reader = BufReader::new(zip_arch);
Expand Down Expand Up @@ -153,6 +153,7 @@ fn for_zip_arch_file(zip_arch_path: &str, config: Rc<Zips>) -> Result<(), ZipCSE
continue;
}
};
// Get name
let name = {
if let Ok(o) = config.charset().decode(file.name_raw()) {
o
Expand All @@ -165,6 +166,7 @@ fn for_zip_arch_file(zip_arch_path: &str, config: Rc<Zips>) -> Result<(), ZipCSE
let mut path = outdir_path.to_path_buf();
path.push(&name);

// create dir/file
if name.ends_with('/') {
println!("${}-> {:?}", i, path.as_path());
create_dir_all(&path)?;
Expand All @@ -178,6 +180,22 @@ fn for_zip_arch_file(zip_arch_path: &str, config: Rc<Zips>) -> Result<(), ZipCSE
let mut outfile = File::create(&path)?;
copy(&mut file, &mut outfile)?;
}

// Get/Set permissions
#[allow(unused_must_use)]
#[cfg(unix)]
{
if let Some(mode) = file.unix_mode() {
set_permissions(&path, Permissions::from_mode(mode)).map_err(|e| {
eprintln!(
"fs::set_permissions({}, {:?}) occurs error: {}",
path.as_path().display(),
mode,
e.description()
)
});
}
}
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unknown_lints,const_static_lifetime)]
// crate's info
pub const NAME: &'static str = env!("CARGO_PKG_NAME");
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cargo build --release
## Help
```sh
zipcs 0.3.3
zipcs 0.3.4
Useful tools collection.
Wspsxing <biluohc@qq.com>
Repo: https://github.com/biluohc/zipcs
Expand Down Expand Up @@ -49,8 +49,6 @@ CAMMANDS:
* [The Release Page](https://github.com/biluohc/zipcs/releases)
## Ps
* 未做预防覆盖原有文件和目录的处理,后果概不负责。
* 未处理文件目录权限(即Zip原有的权限没了)。
* 所依赖的[zip-rs](https://github.com/mvdnes/zip-rs)库目前不支持加密,所以目前不支持密码。
*/

Expand Down

0 comments on commit 64d3203

Please sign in to comment.