zipruby 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of zipruby might be problematic. Click here for more details.
- data/README.txt +131 -0
- data/ext/extconf.rb +1 -1
- data/ext/libzip.mswin32.patch +113 -0
- data/ext/libzip.vcproj +308 -0
- data/ext/libzip.vcproj.IBM-94B792EFFD1.sugawara.user +37 -0
- data/ext/mkstemp.c +143 -0
- data/ext/zip.h +211 -0
- data/ext/zip_add.c +52 -0
- data/ext/zip_add_dir.c +83 -0
- data/ext/zip_close.c +566 -0
- data/ext/zip_delete.c +61 -0
- data/ext/zip_dirent.c +531 -0
- data/ext/zip_entry_free.c +55 -0
- data/ext/zip_entry_new.c +81 -0
- data/ext/zip_err_str.c +72 -0
- data/ext/zip_error.c +104 -0
- data/ext/zip_error_clear.c +47 -0
- data/ext/zip_error_get.c +47 -0
- data/ext/zip_error_get_sys_type.c +50 -0
- data/ext/zip_error_strerror.c +93 -0
- data/ext/zip_error_to_str.c +77 -0
- data/ext/zip_fclose.c +74 -0
- data/ext/zip_file_error_clear.c +47 -0
- data/ext/zip_file_error_get.c +47 -0
- data/ext/zip_file_get_offset.c +77 -0
- data/ext/zip_file_strerror.c +47 -0
- data/ext/zip_fopen.c +52 -0
- data/ext/zip_fopen_index.c +219 -0
- data/ext/zip_fread.c +125 -0
- data/ext/zip_free.c +83 -0
- data/ext/zip_get_archive_comment.c +63 -0
- data/ext/zip_get_file_comment.c +61 -0
- data/ext/zip_get_name.c +74 -0
- data/ext/zip_get_num_files.c +50 -0
- data/ext/zip_memdup.c +58 -0
- data/ext/zip_name_locate.c +95 -0
- data/ext/zip_new.c +71 -0
- data/ext/zip_open.c +520 -0
- data/ext/zip_rename.c +52 -0
- data/ext/zip_replace.c +81 -0
- data/ext/zip_set_archive_comment.c +68 -0
- data/ext/zip_set_file_comment.c +69 -0
- data/ext/zip_set_name.c +77 -0
- data/ext/zip_source_buffer.c +160 -0
- data/ext/zip_source_file.c +71 -0
- data/ext/zip_source_filep.c +176 -0
- data/ext/zip_source_free.c +54 -0
- data/ext/zip_source_function.c +62 -0
- data/ext/zip_source_zip.c +189 -0
- data/ext/zip_stat.c +52 -0
- data/ext/zip_stat_index.c +93 -0
- data/ext/zip_stat_init.c +53 -0
- data/ext/zip_strerror.c +47 -0
- data/ext/zip_unchange.c +84 -0
- data/ext/zip_unchange_all.c +56 -0
- data/ext/zip_unchange_archive.c +52 -0
- data/ext/zip_unchange_data.c +53 -0
- data/ext/zipint.h +240 -0
- data/ext/zipruby.h +1 -1
- metadata +63 -5
data/ext/zip_close.c
ADDED
@@ -0,0 +1,566 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_close.c,v 1.65 2007/02/28 10:44:15 wiz Exp $
|
3
|
+
|
4
|
+
zip_close.c -- close zip archive and update changes
|
5
|
+
Copyright (C) 1999, 2004, 2005, 2007 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include <stdio.h>
|
39
|
+
#include <stdlib.h>
|
40
|
+
#include <string.h>
|
41
|
+
#include <errno.h>
|
42
|
+
#include <sys/types.h>
|
43
|
+
#include <sys/stat.h>
|
44
|
+
|
45
|
+
#ifdef _WIN32
|
46
|
+
#include <windows.h>
|
47
|
+
#include <io.h>
|
48
|
+
#define close(f) _close(f)
|
49
|
+
#define rename(s, d) (MoveFileExA((s), (d), MOVEFILE_REPLACE_EXISTING) ? 0 : -1)
|
50
|
+
#endif
|
51
|
+
|
52
|
+
#include "zip.h"
|
53
|
+
#include "zipint.h"
|
54
|
+
|
55
|
+
static int add_data(struct zip *, int, struct zip_dirent *, FILE *);
|
56
|
+
static int add_data_comp(zip_source_callback, void *, struct zip_stat *,
|
57
|
+
FILE *, struct zip_error *);
|
58
|
+
static int add_data_uncomp(zip_source_callback, void *, struct zip_stat *,
|
59
|
+
FILE *, struct zip_error *);
|
60
|
+
static void ch_set_error(struct zip_error *, zip_source_callback, void *);
|
61
|
+
static int copy_data(FILE *, off_t, FILE *, struct zip_error *);
|
62
|
+
static int _zip_cdir_set_comment(struct zip_cdir *, struct zip *);
|
63
|
+
static int _zip_changed(struct zip *, int *);
|
64
|
+
static char *_zip_create_temp_output(struct zip *, FILE **);
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
int
|
69
|
+
zip_close(struct zip *za)
|
70
|
+
{
|
71
|
+
int survivors;
|
72
|
+
int i, j, error;
|
73
|
+
char *temp;
|
74
|
+
FILE *out;
|
75
|
+
#ifndef _WIN32
|
76
|
+
mode_t mask;
|
77
|
+
#endif
|
78
|
+
struct zip_cdir *cd;
|
79
|
+
struct zip_dirent de;
|
80
|
+
int reopen_on_error;
|
81
|
+
|
82
|
+
reopen_on_error = 0;
|
83
|
+
|
84
|
+
if (za == NULL)
|
85
|
+
return -1;
|
86
|
+
|
87
|
+
if (!_zip_changed(za, &survivors)) {
|
88
|
+
_zip_free(za);
|
89
|
+
return 0;
|
90
|
+
}
|
91
|
+
|
92
|
+
/* don't create zip files with no entries */
|
93
|
+
if (survivors == 0) {
|
94
|
+
if (za->zn) {
|
95
|
+
if (remove(za->zn) != 0) {
|
96
|
+
_zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
|
97
|
+
return -1;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
_zip_free(za);
|
101
|
+
return 0;
|
102
|
+
}
|
103
|
+
|
104
|
+
if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL)
|
105
|
+
return -1;
|
106
|
+
|
107
|
+
for (i=0; i<survivors; i++)
|
108
|
+
_zip_dirent_init(&cd->entry[i]);
|
109
|
+
|
110
|
+
if (_zip_cdir_set_comment(cd, za) == -1) {
|
111
|
+
_zip_cdir_free(cd);
|
112
|
+
return -1;
|
113
|
+
}
|
114
|
+
|
115
|
+
if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
|
116
|
+
_zip_cdir_free(cd);
|
117
|
+
return -1;
|
118
|
+
}
|
119
|
+
|
120
|
+
error = 0;
|
121
|
+
for (i=j=0; i<za->nentry; i++) {
|
122
|
+
if (za->entry[i].state == ZIP_ST_DELETED)
|
123
|
+
continue;
|
124
|
+
|
125
|
+
/* create new local directory entry */
|
126
|
+
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
|
127
|
+
_zip_dirent_init(&de);
|
128
|
+
/* use it as central directory entry */
|
129
|
+
memcpy(cd->entry+j, &de, sizeof(cd->entry[j]));
|
130
|
+
|
131
|
+
/* set/update file name */
|
132
|
+
if (za->entry[i].ch_filename == NULL) {
|
133
|
+
if (za->entry[i].state == ZIP_ST_REPLACED) {
|
134
|
+
de.filename = strdup(za->cdir->entry[i].filename);
|
135
|
+
de.filename_len = strlen(de.filename);
|
136
|
+
cd->entry[j].filename = za->cdir->entry[i].filename;
|
137
|
+
cd->entry[j].filename_len = de.filename_len;
|
138
|
+
}
|
139
|
+
else {
|
140
|
+
de.filename = strdup("-");
|
141
|
+
de.filename_len = 1;
|
142
|
+
cd->entry[j].filename = "-";
|
143
|
+
cd->entry[j].filename_len = de.filename_len;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
/* copy existing directory entries */
|
149
|
+
if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) {
|
150
|
+
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
151
|
+
error = 1;
|
152
|
+
break;
|
153
|
+
}
|
154
|
+
if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) {
|
155
|
+
error = 1;
|
156
|
+
break;
|
157
|
+
}
|
158
|
+
if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
|
159
|
+
de.crc = za->cdir->entry[i].crc;
|
160
|
+
de.comp_size = za->cdir->entry[i].comp_size;
|
161
|
+
de.uncomp_size = za->cdir->entry[i].uncomp_size;
|
162
|
+
de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
|
163
|
+
}
|
164
|
+
memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j]));
|
165
|
+
}
|
166
|
+
|
167
|
+
if (za->entry[i].ch_filename) {
|
168
|
+
free(de.filename);
|
169
|
+
if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) {
|
170
|
+
error = 1;
|
171
|
+
break;
|
172
|
+
}
|
173
|
+
de.filename_len = strlen(de.filename);
|
174
|
+
cd->entry[j].filename = za->entry[i].ch_filename;
|
175
|
+
cd->entry[j].filename_len = de.filename_len;
|
176
|
+
}
|
177
|
+
|
178
|
+
if (za->entry[i].ch_comment_len != -1) {
|
179
|
+
/* as the rest of cd entries, its malloc/free is done by za */
|
180
|
+
cd->entry[j].comment = za->entry[i].ch_comment;
|
181
|
+
cd->entry[j].comment_len = za->entry[i].ch_comment_len;
|
182
|
+
}
|
183
|
+
|
184
|
+
cd->entry[j].offset = ftello(out);
|
185
|
+
|
186
|
+
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
|
187
|
+
if (add_data(za, i, &de, out) < 0) {
|
188
|
+
error = 1;
|
189
|
+
break;
|
190
|
+
}
|
191
|
+
cd->entry[j].last_mod = de.last_mod;
|
192
|
+
cd->entry[j].comp_method = de.comp_method;
|
193
|
+
cd->entry[j].comp_size = de.comp_size;
|
194
|
+
cd->entry[j].uncomp_size = de.uncomp_size;
|
195
|
+
cd->entry[j].crc = de.crc;
|
196
|
+
}
|
197
|
+
else {
|
198
|
+
if (_zip_dirent_write(&de, out, 1, &za->error) < 0) {
|
199
|
+
error = 1;
|
200
|
+
break;
|
201
|
+
}
|
202
|
+
/* we just read the local dirent, file is at correct position */
|
203
|
+
if (copy_data(za->zp, cd->entry[j].comp_size, out,
|
204
|
+
&za->error) < 0) {
|
205
|
+
error = 1;
|
206
|
+
break;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
j++;
|
211
|
+
|
212
|
+
_zip_dirent_finalize(&de);
|
213
|
+
}
|
214
|
+
|
215
|
+
if (!error) {
|
216
|
+
if (_zip_cdir_write(cd, out, &za->error) < 0)
|
217
|
+
error = 1;
|
218
|
+
}
|
219
|
+
|
220
|
+
/* pointers in cd entries are owned by za */
|
221
|
+
cd->nentry = 0;
|
222
|
+
_zip_cdir_free(cd);
|
223
|
+
|
224
|
+
if (error) {
|
225
|
+
_zip_dirent_finalize(&de);
|
226
|
+
fclose(out);
|
227
|
+
remove(temp);
|
228
|
+
free(temp);
|
229
|
+
return -1;
|
230
|
+
}
|
231
|
+
|
232
|
+
if (fclose(out) != 0) {
|
233
|
+
_zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
|
234
|
+
remove(temp);
|
235
|
+
free(temp);
|
236
|
+
return -1;
|
237
|
+
}
|
238
|
+
|
239
|
+
if (za->zp) {
|
240
|
+
fclose(za->zp);
|
241
|
+
za->zp = NULL;
|
242
|
+
reopen_on_error = 1;
|
243
|
+
}
|
244
|
+
if (rename(temp, za->zn) != 0) {
|
245
|
+
_zip_error_set(&za->error, ZIP_ER_RENAME, errno);
|
246
|
+
remove(temp);
|
247
|
+
free(temp);
|
248
|
+
if (reopen_on_error) {
|
249
|
+
/* ignore errors, since we're already in an error case */
|
250
|
+
za->zp = fopen(za->zn, "rb");
|
251
|
+
}
|
252
|
+
return -1;
|
253
|
+
}
|
254
|
+
#ifndef _WIN32
|
255
|
+
mask = umask(0);
|
256
|
+
umask(mask);
|
257
|
+
chmod(za->zn, 0666&~mask);
|
258
|
+
#endif
|
259
|
+
|
260
|
+
_zip_free(za);
|
261
|
+
free(temp);
|
262
|
+
|
263
|
+
return 0;
|
264
|
+
}
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
static int
|
269
|
+
add_data(struct zip *za, int idx, struct zip_dirent *de, FILE *ft)
|
270
|
+
{
|
271
|
+
off_t offstart, offend;
|
272
|
+
zip_source_callback cb;
|
273
|
+
void *ud;
|
274
|
+
struct zip_stat st;
|
275
|
+
|
276
|
+
cb = za->entry[idx].source->f;
|
277
|
+
ud = za->entry[idx].source->ud;
|
278
|
+
|
279
|
+
if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < (ssize_t)sizeof(st)) {
|
280
|
+
ch_set_error(&za->error, cb, ud);
|
281
|
+
return -1;
|
282
|
+
}
|
283
|
+
|
284
|
+
if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) {
|
285
|
+
ch_set_error(&za->error, cb, ud);
|
286
|
+
return -1;
|
287
|
+
}
|
288
|
+
|
289
|
+
offstart = ftello(ft);
|
290
|
+
|
291
|
+
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
|
292
|
+
return -1;
|
293
|
+
|
294
|
+
if (st.comp_method != ZIP_CM_STORE) {
|
295
|
+
if (add_data_comp(cb, ud, &st, ft, &za->error) < 0)
|
296
|
+
return -1;
|
297
|
+
}
|
298
|
+
else {
|
299
|
+
if (add_data_uncomp(cb, ud, &st, ft, &za->error) < 0)
|
300
|
+
return -1;
|
301
|
+
}
|
302
|
+
|
303
|
+
if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) {
|
304
|
+
ch_set_error(&za->error, cb, ud);
|
305
|
+
return -1;
|
306
|
+
}
|
307
|
+
|
308
|
+
offend = ftello(ft);
|
309
|
+
|
310
|
+
if (fseeko(ft, offstart, SEEK_SET) < 0) {
|
311
|
+
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
312
|
+
return -1;
|
313
|
+
}
|
314
|
+
|
315
|
+
de->comp_method = st.comp_method;
|
316
|
+
de->last_mod = st.mtime;
|
317
|
+
de->crc = st.crc;
|
318
|
+
de->uncomp_size = st.size;
|
319
|
+
de->comp_size = st.comp_size;
|
320
|
+
|
321
|
+
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
|
322
|
+
return -1;
|
323
|
+
|
324
|
+
if (fseeko(ft, offend, SEEK_SET) < 0) {
|
325
|
+
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
326
|
+
return -1;
|
327
|
+
}
|
328
|
+
|
329
|
+
return 0;
|
330
|
+
}
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
static int
|
335
|
+
add_data_comp(zip_source_callback cb, void *ud, struct zip_stat *st,FILE *ft,
|
336
|
+
struct zip_error *error)
|
337
|
+
{
|
338
|
+
char buf[BUFSIZE];
|
339
|
+
ssize_t n;
|
340
|
+
|
341
|
+
st->comp_size = 0;
|
342
|
+
while ((n=cb(ud, buf, sizeof(buf), ZIP_SOURCE_READ)) > 0) {
|
343
|
+
if (fwrite(buf, 1, n, ft) != (size_t)n) {
|
344
|
+
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
345
|
+
return -1;
|
346
|
+
}
|
347
|
+
|
348
|
+
st->comp_size += n;
|
349
|
+
}
|
350
|
+
if (n < 0) {
|
351
|
+
ch_set_error(error, cb, ud);
|
352
|
+
return -1;
|
353
|
+
}
|
354
|
+
|
355
|
+
return 0;
|
356
|
+
}
|
357
|
+
|
358
|
+
|
359
|
+
|
360
|
+
static int
|
361
|
+
add_data_uncomp(zip_source_callback cb, void *ud, struct zip_stat *st,
|
362
|
+
FILE *ft, struct zip_error *error)
|
363
|
+
{
|
364
|
+
char b1[BUFSIZE], b2[BUFSIZE];
|
365
|
+
int end, flush, ret;
|
366
|
+
ssize_t n;
|
367
|
+
size_t n2;
|
368
|
+
z_stream zstr;
|
369
|
+
|
370
|
+
st->comp_method = ZIP_CM_DEFLATE;
|
371
|
+
st->comp_size = st->size = 0;
|
372
|
+
st->crc = crc32(0, NULL, 0);
|
373
|
+
|
374
|
+
zstr.zalloc = Z_NULL;
|
375
|
+
zstr.zfree = Z_NULL;
|
376
|
+
zstr.opaque = NULL;
|
377
|
+
zstr.avail_in = 0;
|
378
|
+
zstr.avail_out = 0;
|
379
|
+
|
380
|
+
/* -15: undocumented feature of zlib to _not_ write a zlib header */
|
381
|
+
deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9,
|
382
|
+
Z_DEFAULT_STRATEGY);
|
383
|
+
|
384
|
+
zstr.next_out = (Bytef *)b2;
|
385
|
+
zstr.avail_out = sizeof(b2);
|
386
|
+
zstr.avail_in = 0;
|
387
|
+
|
388
|
+
flush = 0;
|
389
|
+
end = 0;
|
390
|
+
while (!end) {
|
391
|
+
if (zstr.avail_in == 0 && !flush) {
|
392
|
+
if ((n=cb(ud, b1, sizeof(b1), ZIP_SOURCE_READ)) < 0) {
|
393
|
+
ch_set_error(error, cb, ud);
|
394
|
+
deflateEnd(&zstr);
|
395
|
+
return -1;
|
396
|
+
}
|
397
|
+
if (n > 0) {
|
398
|
+
zstr.avail_in = n;
|
399
|
+
zstr.next_in = (Bytef *)b1;
|
400
|
+
st->size += n;
|
401
|
+
st->crc = crc32(st->crc, (Bytef *)b1, n);
|
402
|
+
}
|
403
|
+
else
|
404
|
+
flush = Z_FINISH;
|
405
|
+
}
|
406
|
+
|
407
|
+
ret = deflate(&zstr, flush);
|
408
|
+
if (ret != Z_OK && ret != Z_STREAM_END) {
|
409
|
+
_zip_error_set(error, ZIP_ER_ZLIB, ret);
|
410
|
+
return -1;
|
411
|
+
}
|
412
|
+
|
413
|
+
if (zstr.avail_out != sizeof(b2)) {
|
414
|
+
n2 = sizeof(b2) - zstr.avail_out;
|
415
|
+
|
416
|
+
if (fwrite(b2, 1, n2, ft) != n2) {
|
417
|
+
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
418
|
+
return -1;
|
419
|
+
}
|
420
|
+
|
421
|
+
zstr.next_out = (Bytef *)b2;
|
422
|
+
zstr.avail_out = sizeof(b2);
|
423
|
+
st->comp_size += n2;
|
424
|
+
}
|
425
|
+
|
426
|
+
if (ret == Z_STREAM_END) {
|
427
|
+
deflateEnd(&zstr);
|
428
|
+
end = 1;
|
429
|
+
}
|
430
|
+
}
|
431
|
+
|
432
|
+
return 0;
|
433
|
+
}
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
static void
|
438
|
+
ch_set_error(struct zip_error *error, zip_source_callback cb, void *ud)
|
439
|
+
{
|
440
|
+
int e[2];
|
441
|
+
|
442
|
+
if ((cb(ud, e, sizeof(e), ZIP_SOURCE_ERROR)) < (ssize_t)sizeof(e)) {
|
443
|
+
error->zip_err = ZIP_ER_INTERNAL;
|
444
|
+
error->sys_err = 0;
|
445
|
+
}
|
446
|
+
else {
|
447
|
+
error->zip_err = e[0];
|
448
|
+
error->sys_err = e[1];
|
449
|
+
}
|
450
|
+
}
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
static int
|
455
|
+
copy_data(FILE *fs, off_t len, FILE *ft, struct zip_error *error)
|
456
|
+
{
|
457
|
+
char buf[BUFSIZE];
|
458
|
+
int n, nn;
|
459
|
+
|
460
|
+
if (len == 0)
|
461
|
+
return 0;
|
462
|
+
|
463
|
+
while (len > 0) {
|
464
|
+
nn = len > sizeof(buf) ? sizeof(buf) : len;
|
465
|
+
if ((n=fread(buf, 1, nn, fs)) < 0) {
|
466
|
+
_zip_error_set(error, ZIP_ER_READ, errno);
|
467
|
+
return -1;
|
468
|
+
}
|
469
|
+
else if (n == 0) {
|
470
|
+
_zip_error_set(error, ZIP_ER_EOF, 0);
|
471
|
+
return -1;
|
472
|
+
}
|
473
|
+
|
474
|
+
if (fwrite(buf, 1, n, ft) != (size_t)n) {
|
475
|
+
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
476
|
+
return -1;
|
477
|
+
}
|
478
|
+
|
479
|
+
len -= n;
|
480
|
+
}
|
481
|
+
|
482
|
+
return 0;
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
static int
|
488
|
+
_zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
|
489
|
+
{
|
490
|
+
if (src->ch_comment_len != -1) {
|
491
|
+
dest->comment = _zip_memdup(src->ch_comment,
|
492
|
+
src->ch_comment_len, &src->error);
|
493
|
+
if (dest->comment == NULL)
|
494
|
+
return -1;
|
495
|
+
dest->comment_len = src->ch_comment_len;
|
496
|
+
} else {
|
497
|
+
if (src->cdir && src->cdir->comment) {
|
498
|
+
dest->comment = _zip_memdup(src->cdir->comment,
|
499
|
+
src->cdir->comment_len, &src->error);
|
500
|
+
if (dest->comment == NULL)
|
501
|
+
return -1;
|
502
|
+
dest->comment_len = src->cdir->comment_len;
|
503
|
+
}
|
504
|
+
}
|
505
|
+
|
506
|
+
return 0;
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
static int
|
512
|
+
_zip_changed(struct zip *za, int *survivorsp)
|
513
|
+
{
|
514
|
+
int changed, i, survivors;
|
515
|
+
|
516
|
+
changed = survivors = 0;
|
517
|
+
|
518
|
+
if (za->ch_comment_len != -1)
|
519
|
+
changed = 1;
|
520
|
+
|
521
|
+
for (i=0; i<za->nentry; i++) {
|
522
|
+
if ((za->entry[i].state != ZIP_ST_UNCHANGED)
|
523
|
+
|| (za->entry[i].ch_comment_len != -1))
|
524
|
+
changed = 1;
|
525
|
+
if (za->entry[i].state != ZIP_ST_DELETED)
|
526
|
+
survivors++;
|
527
|
+
}
|
528
|
+
|
529
|
+
*survivorsp = survivors;
|
530
|
+
|
531
|
+
return changed;
|
532
|
+
}
|
533
|
+
|
534
|
+
|
535
|
+
|
536
|
+
static char *
|
537
|
+
_zip_create_temp_output(struct zip *za, FILE **outp)
|
538
|
+
{
|
539
|
+
char *temp;
|
540
|
+
int tfd;
|
541
|
+
FILE *tfp;
|
542
|
+
|
543
|
+
if ((temp=(char *)malloc(strlen(za->zn)+8)) == NULL) {
|
544
|
+
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
545
|
+
return NULL;
|
546
|
+
}
|
547
|
+
|
548
|
+
sprintf(temp, "%s.XXXXXX", za->zn);
|
549
|
+
|
550
|
+
if ((tfd=mkstemp(temp)) == -1) {
|
551
|
+
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
|
552
|
+
free(temp);
|
553
|
+
return NULL;
|
554
|
+
}
|
555
|
+
|
556
|
+
if ((tfp=fdopen(tfd, "r+b")) == NULL) {
|
557
|
+
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
|
558
|
+
close(tfd);
|
559
|
+
remove(temp);
|
560
|
+
free(temp);
|
561
|
+
return NULL;
|
562
|
+
}
|
563
|
+
|
564
|
+
*outp = tfp;
|
565
|
+
return temp;
|
566
|
+
}
|
data/ext/zip_delete.c
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_delete.c,v 1.16 2005/06/09 19:27:16 wiz Exp $
|
3
|
+
|
4
|
+
zip_delete.c -- delete file from zip archive
|
5
|
+
Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include "zip.h"
|
39
|
+
#include "zipint.h"
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int
|
44
|
+
zip_delete(struct zip *za, int idx)
|
45
|
+
{
|
46
|
+
if (idx < 0 || idx >= za->nentry) {
|
47
|
+
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
48
|
+
return -1;
|
49
|
+
}
|
50
|
+
|
51
|
+
/* allow duplicate file names, because the file will
|
52
|
+
* be removed directly afterwards */
|
53
|
+
if (_zip_unchange(za, idx, 1) != 0)
|
54
|
+
return -1;
|
55
|
+
|
56
|
+
za->entry[idx].state = ZIP_ST_DELETED;
|
57
|
+
|
58
|
+
return 0;
|
59
|
+
}
|
60
|
+
|
61
|
+
|