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
//! 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> Writer<W> {
/// 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()?;
let disk_path = disk_path.as_ref();
let meta = EntryMeta::from_path(disk_path)?;
if meta.is_directory {
self.add_directory(archive_path, meta)
} else {
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()?;
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta {
is_directory: true,
..meta
},
uncompressed_size: 0,
};
self.entries.push(entry);
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()?;
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta::anti_item(),
uncompressed_size: 0,
};
self.entries.push(entry);
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()?;
let entry = PendingEntry {
path: archive_path,
meta: EntryMeta::anti_directory(),
uncompressed_size: 0,
};
self.entries.push(entry);
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()?;
if self.options.solid.is_solid() {
self.buffer_entry_solid(archive_path, source, meta)
} else {
self.compress_entry_non_solid(archive_path, source, meta)
}
}
/// 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)
}
}