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
extern crate walkdir;
use cyfs_sha2::{Digest, Sha256};
use std::error::Error;
use std::fs::File;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};
use zip;
use std::fmt::Write;
use std::io::{Read};
pub struct ZipPackage {
src_dir: PathBuf,
all: Vec<PathBuf>,
hash: Option<String>,
zip_file: Option<zip::ZipWriter<std::fs::File>>,
}
impl ZipPackage {
pub fn new() -> ZipPackage {
ZipPackage {
src_dir: PathBuf::from(""),
all: Vec::new(),
hash: None,
zip_file: None,
}
}
pub fn load(&mut self, dir: &Path) {
assert!(self.all.is_empty());
self.src_dir = dir.to_owned();
let is_ignore = |entry: &DirEntry| -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with("."))
.unwrap_or(false)
};
let walker = WalkDir::new(dir).into_iter();
for entry in walker.filter_entry(|e| !is_ignore(e)) {
let entry = entry.unwrap();
if entry.file_type().is_dir() {
continue;
}
let path = entry.path();
self.all.push(path.to_path_buf());
}
self.all.sort();
}
pub fn calc_hash(&mut self) -> Result<String, Box<dyn Error>> {
let mut hasher = Sha256::new();
for path in &self.all {
let ret = File::open(&path);
if let Err(e) = ret {
let msg = format!("open file error! file={}, err={}", path.display(), e);
error!("{}", msg);
return Err(Box::<dyn Error>::from(msg));
}
let mut file = ret.unwrap();
let ret = std::io::copy(&mut file, &mut hasher);
if let Err(e) = ret {
let msg = format!("read file error! file={}, err={}", path.display(), e);
error!("{}", msg);
return Err(Box::<dyn Error>::from(msg));
}
}
let hex = hasher.result();
let mut s = String::new();
for &byte in hex.as_slice() {
write!(&mut s, "{:X}", byte).expect("Unable to format hex string");
}
self.hash = Some(s.clone());
Ok(s)
}
pub fn begin_zip(&mut self, dest_file: &str) -> Result<(), Box<dyn Error>> {
assert!(self.zip_file.is_none());
use std::io::Write;
let target_file = File::create(dest_file).unwrap();
let mut zip = zip::ZipWriter::new(target_file);
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Bzip2);
let mut buffer = Vec::new();
for path in &self.all {
let name = path.strip_prefix(&self.src_dir).unwrap();
info!(
"adding file to zip {} => {} ...",
path.display(),
name.display()
);
let opt;
#[cfg(windows)]
{
opt = options;
}
#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
let metadata = std::fs::metadata(path)?;
opt = options.unix_permissions(metadata.permissions().mode());
}
zip.start_file(name.to_string_lossy(), opt)?;
let ret = File::open(path);
if let Err(e) = ret {
return Err(Box::new(e));
}
let mut f = ret.unwrap();
f.read_to_end(&mut buffer)?;
zip.write_all(&*buffer)?;
buffer.clear();
}
self.zip_file = Some(zip);
Ok(())
}
pub fn append_pkg_hash(&mut self) -> Result<(), Box<dyn Error>> {
assert!(self.zip_file.is_some());
if self.hash.is_none() {
if let Err(e) = self.calc_hash() {
error!("calc hash error! err={}", e);
return Err(e);
}
}
{
let options = zip::write::FileOptions::default()
.compression_method(zip::CompressionMethod::Bzip2);
let name = Path::new(".hash");
info!(
"adding .hash file to zip {} = {} ...",
name.display(),
self.hash.as_ref().unwrap()
);
let zip = self.zip_file.as_mut().unwrap();
zip.start_file(name.to_string_lossy(), options)?;
use std::io::Write;
zip.write_all(self.hash.as_ref().unwrap().as_bytes())?;
}
Ok(())
}
pub fn append_file(&mut self, path: &Path, bytes: &[u8]) -> Result<(), Box<dyn Error>> {
assert!(self.zip_file.is_some());
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Bzip2);
info!("adding file to zip {}...", path.display(),);
let zip = self.zip_file.as_mut().unwrap();
zip.start_file(path.to_string_lossy(), options)?;
use std::io::Write;
zip.write_all(bytes)?;
Ok(())
}
pub fn finish_zip(&mut self) -> Result<(), Box<dyn Error>> {
assert!(self.zip_file.is_some());
let mut zip = self.zip_file.take().unwrap();
zip.finish()?;
Ok(())
}
}