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
use crate::error::ZipErrorT;
use crate::ffi;
use crate::file::{Encoding, File, LocateFlag, OpenFlag as FileOpenFlag};
use crate::source::Source;
use crate::Error;
use crate::Result;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::ptr::null_mut;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum OpenFlag {
CheckConsistency,
Create,
Exclusive,
Truncate,
ReadOnly,
}
#[derive(Debug)]
pub struct Archive {
handle: *mut ffi::zip_t,
}
impl Archive {
pub fn open<S, F>(mut source: Source<S>, flags: F) -> Result<Archive>
where
F: AsRef<[OpenFlag]>,
{
let mut flags_value = 0;
for flag in flags.as_ref() {
match flag {
OpenFlag::CheckConsistency => flags_value |= ffi::ZIP_CHECKCONS,
OpenFlag::Create => flags_value |= ffi::ZIP_CREATE,
OpenFlag::Exclusive => flags_value |= ffi::ZIP_EXCL,
OpenFlag::Truncate => flags_value |= ffi::ZIP_TRUNCATE,
OpenFlag::ReadOnly => flags_value |= ffi::ZIP_RDONLY,
}
}
unsafe {
let mut error = ZipErrorT::default();
let handle =
ffi::zip_open_from_source(source.handle_mut(), flags_value as _, &mut *error);
if handle.is_null() {
Err(error.into())
} else {
source.taken();
Ok(Archive { handle })
}
}
}
fn error(&mut self) -> ZipErrorT<&mut ffi::zip_error_t> {
unsafe {
let error = ffi::zip_get_error(self.handle);
(&mut *error).into()
}
}
fn close_mut(&mut self) -> Result<()> {
if self.handle.is_null() {
Ok(())
} else {
let result = unsafe { ffi::zip_close(self.handle) };
if result == 0 {
self.handle = null_mut();
Ok(())
} else {
Err(self.error().into())
}
}
}
pub fn close(mut self) -> std::result::Result<(), (Self, Error)> {
match self.close_mut() {
Ok(()) => Ok(()),
Err(e) => Err((self, e)),
}
}
fn discard_mut(&mut self) {
if !self.handle.is_null() {
unsafe {
ffi::zip_discard(self.handle);
}
}
}
pub fn discard(mut self) {
self.discard_mut()
}
pub fn add<N, S>(
&mut self,
name: N,
mut source: Source<S>,
encoding: Encoding,
overwrite: bool,
) -> Result<u64>
where
N: AsRef<CStr>,
{
let mut flags = match encoding {
Encoding::Guess => ffi::ZIP_FL_ENC_GUESS,
Encoding::Utf8 => ffi::ZIP_FL_ENC_UTF_8,
Encoding::Cp437 => ffi::ZIP_FL_ENC_CP437,
};
if overwrite {
flags |= ffi::ZIP_FL_OVERWRITE;
}
let response = unsafe {
ffi::zip_file_add(
self.handle,
name.as_ref().as_ptr(),
source.handle_mut(),
flags as _,
)
};
if response == -1 {
Err(self.error().into())
} else {
source.taken();
Ok(response as _)
}
}
pub fn replace<S>(&mut self, index: u64, mut source: Source<S>) -> Result<()> {
let response =
unsafe { ffi::zip_file_replace(self.handle, index as _, source.handle_mut(), 0) };
if response == -1 {
Err(self.error().into())
} else {
source.taken();
Ok(())
}
}
pub fn open_file<N, O, L>(
&mut self,
name: N,
open_flags: O,
locate_flags: L,
) -> Result<File<'_>>
where
N: AsRef<CStr>,
O: AsRef<[FileOpenFlag]>,
L: AsRef<[LocateFlag]>,
{
let mut flags_value = 0;
for flag in open_flags.as_ref() {
match flag {
FileOpenFlag::Compressed => flags_value |= ffi::ZIP_FL_COMPRESSED,
FileOpenFlag::Unchanged => flags_value |= ffi::ZIP_FL_UNCHANGED,
}
}
for flag in locate_flags.as_ref() {
match flag {
LocateFlag::NoCase => flags_value |= ffi::ZIP_FL_NOCASE,
LocateFlag::NoDir => flags_value |= ffi::ZIP_FL_NODIR,
LocateFlag::EncodingRaw => flags_value |= ffi::ZIP_FL_ENC_RAW,
LocateFlag::EncodingGuess => flags_value |= ffi::ZIP_FL_ENC_GUESS,
LocateFlag::EncodingStrict => flags_value |= ffi::ZIP_FL_ENC_STRICT,
}
}
let handle =
unsafe { ffi::zip_fopen(self.handle, name.as_ref().as_ptr(), flags_value as _) };
if handle.is_null() {
Err(self.error().into())
} else {
Ok(File {
handle,
phantom: PhantomData,
})
}
}
}
impl Drop for Archive {
fn drop(&mut self) {
if let Err(_) = self.close_mut() {
self.discard_mut()
}
}
}