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
// Copyright (C) 2016-2022, 2025 Élisabeth HENRY.
//
// This file is part of Crowbook.
//
// Crowbook is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Caribon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
use crate::error::{Error, Result};
use std::fs::{self, DirBuilder, File};
use std::io;
use std::io::Write;
use std::ops::Drop;
use std::path::{Path, PathBuf};
use std::process::Command;
use rust_i18n::t;
/// Struct used to create zip (using filesystem and zip command)
pub struct Zipper {
args: Vec<String>,
path: PathBuf,
}
impl Zipper {
/// Creates new zipper
///
/// # Arguments
/// * `path`: the path to a temporary directory
/// (zipper will create a random dir in it and clean it later)
pub fn new(path: &str) -> Result<Zipper> {
let uuid = uuid::Uuid::new_v4();
let zipper_path = Path::new(path).join(uuid.as_simple().to_string());
DirBuilder::new()
.recursive(true)
.create(&zipper_path)
.map_err(|_| {
Error::zipper(t!(
"zipper.tmp_dir",
path = path
))
})?;
Ok(Zipper {
args: vec![],
path: zipper_path,
})
}
/// writes a content to a temporary file
pub fn write<P: AsRef<Path>>(&mut self, path: P, content: &[u8], add_args: bool) -> Result<()> {
let path = path.as_ref();
let file = format!("{}", path.display());
if path.starts_with("..") || path.is_absolute() {
return Err(Error::zipper(t!("zipper.verboten",
file = file
)));
}
let dest_file = self.path.join(path);
let dest_dir = dest_file.parent().unwrap();
if fs::metadata(dest_dir).is_err() {
// dir does not exist, create it
DirBuilder::new()
.recursive(true)
.create(dest_dir)
.map_err(|_| {
Error::zipper(t!(
"zipper.tpm_dir",
path = dest_dir.display()
))
})?;
}
if let Ok(mut f) = File::create(&dest_file) {
if f.write_all(content).is_ok() {
if add_args {
self.args.push(file);
}
Ok(())
} else {
Err(Error::zipper(t!(
"zipper.write_error",
file = file
)))
}
} else {
Err(Error::zipper(t!(
"zipper.create_error",
file = file
)))
}
}
/// run command and copy content of file output (supposed to result from the command) to current dir
pub fn run_command(
&mut self,
mut command: Command,
command_name: &str,
in_file: &str,
out: &mut dyn Write,
) -> Result<String> {
let res_output = command.output().map_err(|e| {
debug!(
"{}",
t!("zipper.command_output",
name = command_name,
error = e
)
);
Error::zipper(t!(
"zipper.command_error",
name = command_name
))
});
let output = res_output?;
if output.status.success() {
let mut file = File::open(self.path.join(in_file)).map_err(|_| {
debug!(
"{}",
t!("zipper.command_result_error",
command = command_name,
output = String::from_utf8_lossy(&output.stderr)
)
);
Error::zipper(t!(
"zipper.command_result_err",
command = command_name
))
})?;
io::copy(&mut file, out).map_err(|_| {
Error::zipper(t!("zipper.copy_error", file = in_file))
})?;
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
debug!(
"{}",
format!(
"{cmd}: {output}",
cmd = t!("zipper.command_no_success", command = command_name),
output = String::from_utf8_lossy(&output.stderr)
)
);
Err(Error::zipper(t!(
"zipper.command_no_success",
command = command_name
)))
}
}
/// generate a pdf file into given file name
pub fn generate_pdf(
&mut self,
command_name: &str,
tex_file: &str,
pdf_file: &mut dyn Write,
) -> Result<String> {
// first pass
let mut command = Command::new(command_name);
command.current_dir(&self.path).arg(tex_file);
let _ = command.output();
// second pass
let _ = command.output();
// third pass
// let mut command = Command::new(command_name);
// command.current_dir(&self.path);
// command.arg(tex_file);
self.run_command(command, command_name, "result.pdf", pdf_file)
}
}
impl Drop for Zipper {
fn drop(&mut self) {
// println!("Dir not deleted: {}", self.path.to_string_lossy());
// return;
if let Err(err) = fs::remove_dir_all(&self.path) {
println!(
"Error in zipper: could not delete temporary directory {}, error: {}",
self.path.to_string_lossy(),
err
);
}
}
}