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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Entry input methods.
//!
//! This module provides methods for adding entries to an archive from
//! various sources: files, streams, and byte slices.
use std::fs::File;
use std::io::{BufReader, Read, Seek, Write};
use std::path::Path;
use crate::{ArchivePath, Error, Result};
use super::options::EntryMeta;
use super::{PendingEntry, Writer};
impl<W: Write + Seek + Send> Writer<W> {
/// Checks that entries arrive in sorted order, when that was asked for.
///
/// Recorded here rather than checked at the end, so the caller learns which
/// entry broke the order while it is still theirs to fix.
fn check_order(&self, archive_path: &ArchivePath) -> Result<()> {
if !self.options.deterministic {
return Ok(());
}
let path = archive_path.as_str();
if let Some(previous) = &self.last_path {
if path < previous.as_str() {
return Err(Error::InvalidArchivePath(format!(
"deterministic mode requires entries in sorted order, \
but '{path}' was added after '{previous}'"
)));
}
}
Ok(())
}
/// Records a path as added, once the entry is really in.
///
/// Kept separate from the check so that an add which fails - a missing
/// file, a rejected option - does not advance the position and reject the
/// perfectly good entry that follows it.
///
/// Recorded whether or not the setting is on, since the options can be
/// replaced between entries: skipping this while it was off left the check
/// comparing against whatever was last added before that, so turning it on
/// mid-archive accepted an entry that broke the order it was meant to
/// enforce.
fn record_order(&mut self, archive_path: &ArchivePath) {
match &mut self.last_path {
Some(last) => {
last.clear();
last.push_str(archive_path.as_str());
}
None => self.last_path = Some(archive_path.as_str().to_string()),
}
}
/// Adds a file from a filesystem path.
///
/// # Arguments
///
/// * `disk_path` - Path to the file on disk
/// * `archive_path` - Path within the archive
///
/// # Errors
///
/// Returns an error if the file cannot be read or if the writer is in an invalid state.
pub fn add_path(
&mut self,
disk_path: impl AsRef<Path>,
archive_path: ArchivePath,
) -> Result<()> {
self.ensure_accepting_entries()?;
self.check_order(&archive_path)?;
let disk_path = disk_path.as_ref();
let meta = EntryMeta::from_path(disk_path)?;
if meta.is_directory {
return self.add_directory(archive_path, meta);
}
let file = File::open(disk_path).map_err(Error::Io)?;
let mut reader = BufReader::new(file);
self.add_stream(archive_path, &mut reader, meta)
}
/// Adds a directory entry.
///
/// # Arguments
///
/// * `archive_path` - Path within the archive
/// * `meta` - Entry metadata
///
/// # Errors
///
/// Returns an error if the writer is in an invalid state.
pub fn add_directory(&mut self, archive_path: ArchivePath, meta: EntryMeta) -> Result<()> {
self.ensure_accepting_entries()?;
// Settled first, so anything waiting under the previous password is
// written - and the archive keyed - before this entry is judged.
self.settle_stale_buffers()?;
#[cfg(feature = "aes")]
{
let options = self.options.clone();
self.check_password(&options)?;
}
self.check_order(&archive_path)?;
let recorded = archive_path.clone();
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta {
is_directory: true,
..meta
},
uncompressed_size: 0,
};
// Entries waiting in the batch were added before this one and have to
// reach the entry list first, or the archive lists them out of order.
self.flush_buffered_entries()?;
self.entries.push(entry);
self.record_order(&recorded);
Ok(())
}
/// Adds an anti-item entry (file marked for deletion in incremental backups).
///
/// Anti-items are empty entries that indicate a file or directory should
/// be deleted when the incremental archive is applied. This is useful for
/// incremental backup systems.
///
/// # Arguments
///
/// * `archive_path` - Path within the archive to mark for deletion
///
/// # Example
///
/// ```rust,ignore
/// let mut writer = Writer::create_file("incremental.7z")?;
/// writer.add_anti_item(ArchivePath::new("deleted_file.txt")?)?;
/// writer.finish()?;
/// ```
pub fn add_anti_item(&mut self, archive_path: ArchivePath) -> Result<()> {
self.ensure_accepting_entries()?;
// Settled first, so anything waiting under the previous password is
// written - and the archive keyed - before this entry is judged.
self.settle_stale_buffers()?;
#[cfg(feature = "aes")]
{
let options = self.options.clone();
self.check_password(&options)?;
}
self.check_order(&archive_path)?;
let recorded = archive_path.clone();
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta::anti_item(),
uncompressed_size: 0,
};
// Entries waiting in the batch were added before this one and have to
// reach the entry list first, or the archive lists them out of order.
self.flush_buffered_entries()?;
self.entries.push(entry);
self.record_order(&recorded);
Ok(())
}
/// Adds an anti-item directory (directory marked for deletion).
///
/// # Arguments
///
/// * `archive_path` - Directory path within the archive to mark for deletion
pub fn add_anti_directory(&mut self, archive_path: ArchivePath) -> Result<()> {
self.ensure_accepting_entries()?;
// Settled first, so anything waiting under the previous password is
// written - and the archive keyed - before this entry is judged.
self.settle_stale_buffers()?;
#[cfg(feature = "aes")]
{
let options = self.options.clone();
self.check_password(&options)?;
}
self.check_order(&archive_path)?;
let recorded = archive_path.clone();
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta::anti_directory(),
uncompressed_size: 0,
};
// Entries waiting in the batch were added before this one and have to
// reach the entry list first, or the archive lists them out of order.
self.flush_buffered_entries()?;
self.entries.push(entry);
self.record_order(&recorded);
Ok(())
}
/// Adds data from a stream.
///
/// # Arguments
///
/// * `archive_path` - Path within the archive
/// * `source` - Reader providing the data
/// * `meta` - Entry metadata
///
/// # Errors
///
/// Returns an error if compression fails or if the writer is in an invalid state.
pub fn add_stream(
&mut self,
archive_path: ArchivePath,
source: &mut dyn Read,
meta: EntryMeta,
) -> Result<()> {
self.ensure_accepting_entries()?;
// Anything buffered under options that have since been replaced is
// written out with those options before this entry is accepted.
// Settled first, so anything waiting under the previous password is
// written - and the archive keyed - before this entry is judged.
self.settle_stale_buffers()?;
#[cfg(feature = "aes")]
{
let options = self.options.clone();
self.check_password(&options)?;
}
self.check_order(&archive_path)?;
let recorded = archive_path.clone();
// A large entry is compressed straight into the sink rather than read
// into memory first, so that archiving a file does not require as much
// memory as the file is long.
let size = meta.size;
let added = if super::streaming_entry::can_stream(&self.options, size) {
self.compress_entry_streaming(archive_path, source, meta, size)
} else if self.options.solid.is_solid() {
self.buffer_entry_solid(archive_path, source, meta)
} else {
self.compress_entry_non_solid(archive_path, source, meta)
};
if added.is_ok() {
self.record_order(&recorded);
}
added
}
/// Adds data from a byte slice.
///
/// # Arguments
///
/// * `archive_path` - Path within the archive
/// * `data` - The data to add
///
/// # Errors
///
/// Returns an error if compression fails or if the writer is in an invalid state.
pub fn add_bytes(&mut self, archive_path: ArchivePath, data: &[u8]) -> Result<()> {
let meta = EntryMeta::file(data.len() as u64);
let mut cursor = std::io::Cursor::new(data);
self.add_stream(archive_path, &mut cursor, meta)
}
}