zipruby 0.2.6 → 0.2.8
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/ext/extconf.rb +3 -0
- data/ext/tmpfile.c +173 -0
- data/ext/tmpfile.h +9 -0
- data/ext/zip.h +3 -0
- data/ext/zip_close.c +3 -3
- data/ext/zip_open.c +4 -0
- data/ext/zipruby.h +1 -1
- data/ext/zipruby_archive.c +165 -1
- data/ext/zipruby_archive.h +2 -0
- data/ext/zipruby_zip.c +1 -0
- data/ext/zipruby_zip_source_proc.c +6 -3
- data/zipruby.c +345 -4
- metadata +4 -4
- data/ext/foo.txt +0 -315
- data/ext/zipruby_archive.c.r112 +0 -978
data/ext/zipruby_archive.c.r112
DELETED
@@ -1,978 +0,0 @@
|
|
1
|
-
#include <errno.h>
|
2
|
-
|
3
|
-
#include "zip.h"
|
4
|
-
#include "zipruby.h"
|
5
|
-
#include "zipruby_archive.h"
|
6
|
-
#include "zipruby_zip_source_proc.h"
|
7
|
-
#include "ruby.h"
|
8
|
-
#include "rubyio.h"
|
9
|
-
|
10
|
-
static VALUE zipruby_archive_alloc(VALUE klass);
|
11
|
-
static void zipruby_archive_free(struct zipruby_archive *p);
|
12
|
-
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
|
13
|
-
static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
|
14
|
-
static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password);
|
15
|
-
static VALUE zipruby_archive_close(VALUE self);
|
16
|
-
static VALUE zipruby_archive_num_files(VALUE self);
|
17
|
-
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
|
18
|
-
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
|
19
|
-
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
|
20
|
-
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
|
21
|
-
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
|
22
|
-
static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self);
|
23
|
-
static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
|
24
|
-
static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE source);
|
25
|
-
static VALUE zipruby_archive_replace_file(VALUE self, VALUE index, VALUE fname);
|
26
|
-
static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file);
|
27
|
-
static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
|
28
|
-
static VALUE zipruby_archive_add_or_replace_buffer(VALUE self, VALUE name, VALUE source);
|
29
|
-
static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
|
30
|
-
static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE self);
|
31
|
-
static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
|
32
|
-
static VALUE zipruby_archive_update(VALUE self, VALUE srcarchive);
|
33
|
-
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
|
34
|
-
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
|
35
|
-
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self);
|
36
|
-
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self);
|
37
|
-
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment);
|
38
|
-
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index);
|
39
|
-
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name);
|
40
|
-
static VALUE zipruby_archive_funchange(VALUE self, VALUE index);
|
41
|
-
static VALUE zipruby_archive_funchange_all(VALUE self);
|
42
|
-
static VALUE zipruby_archive_unchange(VALUE self);
|
43
|
-
static VALUE zipruby_archive_revert(VALUE self);
|
44
|
-
static VALUE zipruby_archive_each(VALUE self);
|
45
|
-
|
46
|
-
extern VALUE Zip;
|
47
|
-
VALUE Archive;
|
48
|
-
extern VALUE File;
|
49
|
-
extern VALUE Stat;
|
50
|
-
extern VALUE Error;
|
51
|
-
|
52
|
-
void Init_zipruby_archive() {
|
53
|
-
Archive = rb_define_class_under(Zip, "Archive", rb_cObject);
|
54
|
-
rb_define_alloc_func(Archive, zipruby_archive_alloc);
|
55
|
-
rb_include_module(Archive, rb_mEnumerable);
|
56
|
-
rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
|
57
|
-
rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
|
58
|
-
rb_define_singleton_method(Archive, "encrypt", zipruby_archive_s_encrypt, 2);
|
59
|
-
rb_define_method(Archive, "close", zipruby_archive_close, 0);
|
60
|
-
rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
|
61
|
-
rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
|
62
|
-
rb_define_method(Archive, "fopen", zipruby_archive_fopen, -1);
|
63
|
-
rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
|
64
|
-
rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
|
65
|
-
rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
|
66
|
-
rb_define_method(Archive, "add_filep", zipruby_archive_add_filep, -1);
|
67
|
-
rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
|
68
|
-
rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, 2);
|
69
|
-
rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, 2);
|
70
|
-
rb_define_method(Archive, "replace_filep", zipruby_archive_replace_filep, 2);
|
71
|
-
rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
|
72
|
-
rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, 2);
|
73
|
-
rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
|
74
|
-
rb_define_method(Archive, "add_or_replace_filep", zipruby_archive_add_or_replace_filep, -1);
|
75
|
-
rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
|
76
|
-
rb_define_method(Archive, "update", zipruby_archive_update, 1);
|
77
|
-
rb_define_method(Archive, "<<", zipruby_archive_add_filep, -1);
|
78
|
-
rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
|
79
|
-
rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
|
80
|
-
rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
|
81
|
-
rb_define_method(Archive, "locate_name", zipruby_archive_locate_name, -1);
|
82
|
-
rb_define_method(Archive, "get_fcomment", zipruby_archive_get_fcomment, -1);
|
83
|
-
rb_define_method(Archive, "set_fcomment", zipruby_archive_set_fcomment, 2);
|
84
|
-
rb_define_method(Archive, "fdelete", zipruby_archive_fdelete, 1);
|
85
|
-
rb_define_method(Archive, "frename", zipruby_archive_frename, 2);
|
86
|
-
rb_define_method(Archive, "funchange", zipruby_archive_funchange, 1);
|
87
|
-
rb_define_method(Archive, "funchange_all", zipruby_archive_funchange_all, 0);
|
88
|
-
rb_define_method(Archive, "unchange", zipruby_archive_unchange, 0);
|
89
|
-
rb_define_method(Archive, "frevert", zipruby_archive_unchange, 1);
|
90
|
-
rb_define_method(Archive, "revert", zipruby_archive_revert, 0);
|
91
|
-
rb_define_method(Archive, "each", zipruby_archive_each, 0);
|
92
|
-
}
|
93
|
-
|
94
|
-
static VALUE zipruby_archive_alloc(VALUE klass) {
|
95
|
-
struct zipruby_archive *p = ALLOC(struct zipruby_archive);
|
96
|
-
|
97
|
-
p->archive = NULL;
|
98
|
-
|
99
|
-
return Data_Wrap_Struct(klass, 0, zipruby_archive_free, p);
|
100
|
-
}
|
101
|
-
|
102
|
-
static void zipruby_archive_free(struct zipruby_archive *p) {
|
103
|
-
xfree(p);
|
104
|
-
}
|
105
|
-
|
106
|
-
/* */
|
107
|
-
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
|
108
|
-
VALUE path, flags;
|
109
|
-
VALUE archive;
|
110
|
-
struct zipruby_archive *p_archive;
|
111
|
-
int i_flags = 0;
|
112
|
-
int errorp;
|
113
|
-
|
114
|
-
rb_scan_args(argc, argv, "11", &path, &flags);
|
115
|
-
Check_Type(path, T_STRING);
|
116
|
-
|
117
|
-
if (!NIL_P(flags)) {
|
118
|
-
i_flags = NUM2INT(flags);
|
119
|
-
}
|
120
|
-
|
121
|
-
archive = rb_funcall(Archive, rb_intern("new"), 0);
|
122
|
-
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
123
|
-
|
124
|
-
if ((p_archive->archive = zip_open(StringValuePtr(path), i_flags, &errorp)) == NULL) {
|
125
|
-
char errstr[ERRSTR_BUFSIZE];
|
126
|
-
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
127
|
-
rb_raise(Error, "Open archive failed - %s: %s", StringValuePtr(path), errstr);
|
128
|
-
}
|
129
|
-
|
130
|
-
if (rb_block_given_p()) {
|
131
|
-
VALUE retval;
|
132
|
-
int status;
|
133
|
-
|
134
|
-
retval = rb_protect(rb_yield, archive, &status);
|
135
|
-
zipruby_archive_close(archive);
|
136
|
-
|
137
|
-
if (status != 0) {
|
138
|
-
rb_jump_tag(status);
|
139
|
-
}
|
140
|
-
|
141
|
-
return retval;
|
142
|
-
} else {
|
143
|
-
return archive;
|
144
|
-
}
|
145
|
-
}
|
146
|
-
|
147
|
-
/* */
|
148
|
-
static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
|
149
|
-
int errorp, wrongpwd;
|
150
|
-
long pwdlen;
|
151
|
-
|
152
|
-
Check_Type(path, T_STRING);
|
153
|
-
Check_Type(password, T_STRING);
|
154
|
-
pwdlen = RSTRING(password)->len;
|
155
|
-
|
156
|
-
if (pwdlen < 1) {
|
157
|
-
rb_raise(Error, "Decrypt archive failed - %s: Password is empty", StringValuePtr(path));
|
158
|
-
} else if (pwdlen > 0xff) {
|
159
|
-
rb_raise(Error, "Decrypt archive failed - %s: Password is too long", StringValuePtr(path));
|
160
|
-
}
|
161
|
-
|
162
|
-
if (zip_decrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp, &wrongpwd) == -1) {
|
163
|
-
if (wrongpwd) {
|
164
|
-
rb_raise(Error, "Decrypt archive failed - %s: Wrong password", StringValuePtr(path));
|
165
|
-
} else {
|
166
|
-
char errstr[ERRSTR_BUFSIZE];
|
167
|
-
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
168
|
-
rb_raise(Error, "Decrypt archive failed - %s: %s", StringValuePtr(path), errstr);
|
169
|
-
}
|
170
|
-
}
|
171
|
-
|
172
|
-
return Qnil;
|
173
|
-
}
|
174
|
-
|
175
|
-
/* */
|
176
|
-
static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
|
177
|
-
int errorp;
|
178
|
-
long pwdlen;
|
179
|
-
|
180
|
-
Check_Type(path, T_STRING);
|
181
|
-
Check_Type(password, T_STRING);
|
182
|
-
pwdlen = RSTRING(password)->len;
|
183
|
-
|
184
|
-
if (pwdlen < 1) {
|
185
|
-
rb_raise(Error, "Encrypt archive failed - %s: Password is empty", StringValuePtr(path));
|
186
|
-
} else if (pwdlen > 0xff) {
|
187
|
-
rb_raise(Error, "Encrypt archive failed - %s: Password is too long", StringValuePtr(path));
|
188
|
-
}
|
189
|
-
|
190
|
-
if (zip_encrypt(StringValuePtr(path), StringValuePtr(password), pwdlen, &errorp) == -1) {
|
191
|
-
char errstr[ERRSTR_BUFSIZE];
|
192
|
-
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
193
|
-
rb_raise(Error, "Encrypt archive failed - %s: %s", StringValuePtr(path), errstr);
|
194
|
-
}
|
195
|
-
|
196
|
-
return Qnil;
|
197
|
-
}
|
198
|
-
|
199
|
-
/* */
|
200
|
-
static VALUE zipruby_archive_close(VALUE self) {
|
201
|
-
struct zipruby_archive *p_archive;
|
202
|
-
|
203
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
204
|
-
Check_Archive(p_archive);
|
205
|
-
|
206
|
-
if (zip_close(p_archive->archive) == -1) {
|
207
|
-
zip_unchange_all(p_archive->archive);
|
208
|
-
zip_unchange_archive(p_archive->archive);
|
209
|
-
rb_raise(Error, "Close archive failed: %s", zip_strerror(p_archive->archive));
|
210
|
-
}
|
211
|
-
|
212
|
-
p_archive->archive = NULL;
|
213
|
-
|
214
|
-
return Qnil;
|
215
|
-
}
|
216
|
-
|
217
|
-
/* */
|
218
|
-
static VALUE zipruby_archive_num_files(VALUE self) {
|
219
|
-
struct zipruby_archive *p_archive;
|
220
|
-
int num_files;
|
221
|
-
|
222
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
223
|
-
Check_Archive(p_archive);
|
224
|
-
num_files = zip_get_num_files(p_archive->archive);
|
225
|
-
|
226
|
-
return INT2NUM(num_files);
|
227
|
-
}
|
228
|
-
|
229
|
-
/* */
|
230
|
-
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self) {
|
231
|
-
VALUE index, flags;
|
232
|
-
struct zipruby_archive *p_archive;
|
233
|
-
int i_flags = 0;
|
234
|
-
const char *name;
|
235
|
-
|
236
|
-
rb_scan_args(argc, argv, "11", &index, &flags);
|
237
|
-
Check_Type(index, T_FIXNUM);
|
238
|
-
|
239
|
-
if (!NIL_P(flags)) {
|
240
|
-
i_flags = NUM2INT(flags);
|
241
|
-
}
|
242
|
-
|
243
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
244
|
-
Check_Archive(p_archive);
|
245
|
-
|
246
|
-
if ((name = zip_get_name(p_archive->archive, NUM2INT(index), i_flags)) == NULL) {
|
247
|
-
rb_raise(Error, "Get name failed at %d: %s", index, zip_strerror(p_archive->archive));
|
248
|
-
}
|
249
|
-
|
250
|
-
return (name != NULL) ? rb_str_new2(name) : Qnil;
|
251
|
-
}
|
252
|
-
|
253
|
-
/* */
|
254
|
-
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self) {
|
255
|
-
VALUE index, flags, stat_flags, file;
|
256
|
-
|
257
|
-
rb_scan_args(argc, argv, "12", &index, &flags, &stat_flags);
|
258
|
-
file = rb_funcall(File, rb_intern("new"), 4, self, index, flags, stat_flags);
|
259
|
-
|
260
|
-
if (rb_block_given_p()) {
|
261
|
-
VALUE retval;
|
262
|
-
int status;
|
263
|
-
|
264
|
-
retval = rb_protect(rb_yield, file, &status);
|
265
|
-
rb_funcall(file, rb_intern("close"), 0);
|
266
|
-
|
267
|
-
if (status != 0) {
|
268
|
-
rb_jump_tag(status);
|
269
|
-
}
|
270
|
-
|
271
|
-
return retval;
|
272
|
-
} else {
|
273
|
-
return file;
|
274
|
-
}
|
275
|
-
}
|
276
|
-
|
277
|
-
/* */
|
278
|
-
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self) {
|
279
|
-
VALUE index, flags;
|
280
|
-
|
281
|
-
rb_scan_args(argc, argv, "11", &index, &flags);
|
282
|
-
|
283
|
-
return rb_funcall(Stat, rb_intern("new"), 3, self, index, flags);
|
284
|
-
}
|
285
|
-
|
286
|
-
/* */
|
287
|
-
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
|
288
|
-
struct zipruby_archive *p_archive;
|
289
|
-
struct zip_source *zsource;
|
290
|
-
char *data;
|
291
|
-
off_t len;
|
292
|
-
|
293
|
-
Check_Type(name, T_STRING);
|
294
|
-
Check_Type(source, T_STRING);
|
295
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
296
|
-
Check_Archive(p_archive);
|
297
|
-
|
298
|
-
len = RSTRING(source)->len;
|
299
|
-
|
300
|
-
if ((data = malloc(len)) == NULL) {
|
301
|
-
rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
|
302
|
-
}
|
303
|
-
|
304
|
-
memset(data, 0, len);
|
305
|
-
memcpy(data, StringValuePtr(source), len);
|
306
|
-
|
307
|
-
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
308
|
-
free(data);
|
309
|
-
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
310
|
-
}
|
311
|
-
|
312
|
-
if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
|
313
|
-
zip_source_free(zsource);
|
314
|
-
zip_unchange_all(p_archive->archive);
|
315
|
-
zip_unchange_archive(p_archive->archive);
|
316
|
-
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
317
|
-
}
|
318
|
-
|
319
|
-
return Qnil;
|
320
|
-
}
|
321
|
-
|
322
|
-
/* */
|
323
|
-
static VALUE zipruby_archive_replace_buffer(VALUE self, VALUE index, VALUE source) {
|
324
|
-
struct zipruby_archive *p_archive;
|
325
|
-
struct zip_source *zsource;
|
326
|
-
int i_index;
|
327
|
-
char *data;
|
328
|
-
off_t len;
|
329
|
-
|
330
|
-
if (!rb_obj_is_instance_of(index, rb_cString) && !rb_obj_is_instance_of(index, rb_cFixnum)) {
|
331
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
|
332
|
-
}
|
333
|
-
|
334
|
-
Check_Type(source, T_STRING);
|
335
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
336
|
-
Check_Archive(p_archive);
|
337
|
-
|
338
|
-
if (rb_obj_is_instance_of(index, rb_cFixnum)) {
|
339
|
-
i_index = NUM2INT(index);
|
340
|
-
} else if ((i_index = zip_name_locate(p_archive->archive, StringValuePtr(index), ZIP_FL_NOCASE)) == -1) {
|
341
|
-
rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", StringValuePtr(index));
|
342
|
-
}
|
343
|
-
|
344
|
-
len = RSTRING(source)->len;
|
345
|
-
|
346
|
-
if ((data = malloc(len)) == NULL) {
|
347
|
-
rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
|
348
|
-
}
|
349
|
-
|
350
|
-
memcpy(data, StringValuePtr(source), len);
|
351
|
-
|
352
|
-
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
353
|
-
free(data);
|
354
|
-
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
355
|
-
}
|
356
|
-
|
357
|
-
if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
|
358
|
-
zip_source_free(zsource);
|
359
|
-
zip_unchange_all(p_archive->archive);
|
360
|
-
zip_unchange_archive(p_archive->archive);
|
361
|
-
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
362
|
-
}
|
363
|
-
|
364
|
-
return Qnil;
|
365
|
-
}
|
366
|
-
|
367
|
-
/* */
|
368
|
-
static VALUE zipruby_archive_add_or_replace_buffer(VALUE self, VALUE name, VALUE source) {
|
369
|
-
struct zipruby_archive *p_archive;
|
370
|
-
int index;
|
371
|
-
|
372
|
-
Check_Type(name, T_STRING);
|
373
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
374
|
-
Check_Archive(p_archive);
|
375
|
-
|
376
|
-
index = zip_name_locate(p_archive->archive, StringValuePtr(name), ZIP_FL_NOCASE);
|
377
|
-
|
378
|
-
if (index >= 0) {
|
379
|
-
return zipruby_archive_replace_buffer(self, INT2NUM(index), source);
|
380
|
-
} else {
|
381
|
-
return zipruby_archive_add_buffer(self, name, source);
|
382
|
-
}
|
383
|
-
}
|
384
|
-
|
385
|
-
/* */
|
386
|
-
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
|
387
|
-
VALUE name, fname;
|
388
|
-
struct zipruby_archive *p_archive;
|
389
|
-
struct zip_source *zsource;
|
390
|
-
|
391
|
-
rb_scan_args(argc, argv, "11", &name, &fname);
|
392
|
-
|
393
|
-
if (NIL_P(fname)) {
|
394
|
-
fname = name;
|
395
|
-
name = Qnil;
|
396
|
-
}
|
397
|
-
|
398
|
-
Check_Type(fname, T_STRING);
|
399
|
-
|
400
|
-
if (NIL_P(name)) {
|
401
|
-
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
|
402
|
-
}
|
403
|
-
|
404
|
-
Check_Type(name, T_STRING);
|
405
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
406
|
-
Check_Archive(p_archive);
|
407
|
-
|
408
|
-
if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
|
409
|
-
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
410
|
-
}
|
411
|
-
|
412
|
-
if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
|
413
|
-
zip_source_free(zsource);
|
414
|
-
zip_unchange_all(p_archive->archive);
|
415
|
-
zip_unchange_archive(p_archive->archive);
|
416
|
-
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
417
|
-
}
|
418
|
-
|
419
|
-
return Qnil;
|
420
|
-
}
|
421
|
-
|
422
|
-
/* */
|
423
|
-
static VALUE zipruby_archive_replace_file(VALUE self, VALUE index, VALUE fname) {
|
424
|
-
struct zipruby_archive *p_archive;
|
425
|
-
struct zip_source *zsource;
|
426
|
-
int i_index;
|
427
|
-
|
428
|
-
if (!rb_obj_is_instance_of(index, rb_cString) && !rb_obj_is_instance_of(index, rb_cFixnum)) {
|
429
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
|
430
|
-
}
|
431
|
-
|
432
|
-
Check_Type(fname, T_STRING);
|
433
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
434
|
-
Check_Archive(p_archive);
|
435
|
-
|
436
|
-
if (rb_obj_is_instance_of(index, rb_cFixnum)) {
|
437
|
-
i_index = NUM2INT(index);
|
438
|
-
} else if ((i_index = zip_name_locate(p_archive->archive, StringValuePtr(index), ZIP_FL_NOCASE)) == -1) {
|
439
|
-
rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", StringValuePtr(index));
|
440
|
-
}
|
441
|
-
|
442
|
-
if ((zsource = zip_source_file(p_archive->archive, StringValuePtr(fname), 0, -1)) == NULL) {
|
443
|
-
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
444
|
-
}
|
445
|
-
|
446
|
-
if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
|
447
|
-
zip_source_free(zsource);
|
448
|
-
zip_unchange_all(p_archive->archive);
|
449
|
-
zip_unchange_archive(p_archive->archive);
|
450
|
-
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
451
|
-
}
|
452
|
-
|
453
|
-
return Qnil;
|
454
|
-
}
|
455
|
-
|
456
|
-
/* */
|
457
|
-
static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self) {
|
458
|
-
VALUE name, fname;
|
459
|
-
struct zipruby_archive *p_archive;
|
460
|
-
int index;
|
461
|
-
|
462
|
-
rb_scan_args(argc, argv, "11", &name, &fname);
|
463
|
-
|
464
|
-
if (NIL_P(fname)) {
|
465
|
-
fname = name;
|
466
|
-
name = Qnil;
|
467
|
-
}
|
468
|
-
|
469
|
-
Check_Type(fname, T_STRING);
|
470
|
-
|
471
|
-
if (NIL_P(name)) {
|
472
|
-
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
|
473
|
-
}
|
474
|
-
|
475
|
-
Check_Type(name, T_STRING);
|
476
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
477
|
-
Check_Archive(p_archive);
|
478
|
-
|
479
|
-
index = zip_name_locate(p_archive->archive, StringValuePtr(name), ZIP_FL_NOCASE);
|
480
|
-
|
481
|
-
if (index >= 0) {
|
482
|
-
return zipruby_archive_replace_file(self, INT2NUM(index), fname);
|
483
|
-
} else {
|
484
|
-
return zipruby_archive_add_file(argc, argv, self);
|
485
|
-
}
|
486
|
-
}
|
487
|
-
|
488
|
-
/* */
|
489
|
-
static VALUE zipruby_archive_add_filep(int argc, VALUE *argv, VALUE self) {
|
490
|
-
VALUE name, file, source;
|
491
|
-
|
492
|
-
rb_scan_args(argc, argv, "11", &name, &file);
|
493
|
-
|
494
|
-
if (NIL_P(file)) {
|
495
|
-
file = name;
|
496
|
-
name = Qnil;
|
497
|
-
}
|
498
|
-
|
499
|
-
Check_Type(file, T_FILE);
|
500
|
-
|
501
|
-
if (NIL_P(name)) {
|
502
|
-
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
|
503
|
-
}
|
504
|
-
|
505
|
-
source = rb_funcall(file, rb_intern("read"), 0);
|
506
|
-
|
507
|
-
return zipruby_archive_add_buffer(self, name, source);
|
508
|
-
}
|
509
|
-
|
510
|
-
/* */
|
511
|
-
static VALUE zipruby_archive_replace_filep(VALUE self, VALUE index, VALUE file) {
|
512
|
-
VALUE source;
|
513
|
-
|
514
|
-
Check_Type(file, T_FILE);
|
515
|
-
source = rb_funcall(file, rb_intern("read"), 0);
|
516
|
-
|
517
|
-
return zipruby_archive_replace_buffer(self, index, source);
|
518
|
-
}
|
519
|
-
|
520
|
-
/* */
|
521
|
-
static VALUE zipruby_archive_add_or_replace_filep(int argc, VALUE *argv, VALUE self) {
|
522
|
-
VALUE name, file;
|
523
|
-
struct zipruby_archive *p_archive;
|
524
|
-
int index;
|
525
|
-
|
526
|
-
rb_scan_args(argc, argv, "11", &name, &file);
|
527
|
-
|
528
|
-
if (NIL_P(file)) {
|
529
|
-
file = name;
|
530
|
-
name = Qnil;
|
531
|
-
}
|
532
|
-
|
533
|
-
Check_Type(file, T_FILE);
|
534
|
-
|
535
|
-
if (NIL_P(name)) {
|
536
|
-
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, rb_funcall(file, rb_intern("path"), 0));
|
537
|
-
}
|
538
|
-
|
539
|
-
Check_Type(name, T_STRING);
|
540
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
541
|
-
Check_Archive(p_archive);
|
542
|
-
|
543
|
-
index = zip_name_locate(p_archive->archive, StringValuePtr(name), ZIP_FL_NOCASE);
|
544
|
-
|
545
|
-
if (index >= 0) {
|
546
|
-
return zipruby_archive_replace_filep(self, INT2NUM(index), file);
|
547
|
-
} else {
|
548
|
-
return zipruby_archive_add_filep(argc, argv, self);
|
549
|
-
}
|
550
|
-
}
|
551
|
-
|
552
|
-
/* */
|
553
|
-
static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
|
554
|
-
VALUE name, mtime;
|
555
|
-
struct zipruby_archive *p_archive;
|
556
|
-
struct zip_source *zsource;
|
557
|
-
struct read_proc *z;
|
558
|
-
|
559
|
-
rb_scan_args(argc, argv, "11", &name, &mtime);
|
560
|
-
rb_need_block();
|
561
|
-
|
562
|
-
if (NIL_P(mtime)) {
|
563
|
-
mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
|
564
|
-
} else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
|
565
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
|
566
|
-
}
|
567
|
-
|
568
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
569
|
-
Check_Archive(p_archive);
|
570
|
-
|
571
|
-
if ((z = malloc(sizeof(struct read_proc))) == NULL) {
|
572
|
-
zip_unchange_all(p_archive->archive);
|
573
|
-
zip_unchange_archive(p_archive->archive);
|
574
|
-
rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", StringValuePtr(name));
|
575
|
-
}
|
576
|
-
|
577
|
-
z->proc = rb_block_proc();
|
578
|
-
z->mtime = mtime;
|
579
|
-
|
580
|
-
if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
|
581
|
-
free(z);
|
582
|
-
rb_raise(Error, "Add failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
583
|
-
}
|
584
|
-
|
585
|
-
if (zip_add(p_archive->archive, StringValuePtr(name), zsource) == -1) {
|
586
|
-
zip_source_free(zsource);
|
587
|
-
zip_unchange_all(p_archive->archive);
|
588
|
-
zip_unchange_archive(p_archive->archive);
|
589
|
-
rb_raise(Error, "Add file failed - %s: %s", StringValuePtr(name), zip_strerror(p_archive->archive));
|
590
|
-
}
|
591
|
-
|
592
|
-
return Qnil;
|
593
|
-
}
|
594
|
-
|
595
|
-
/* */
|
596
|
-
static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
|
597
|
-
VALUE index, mtime;
|
598
|
-
struct zipruby_archive *p_archive;
|
599
|
-
struct zip_source *zsource;
|
600
|
-
struct read_proc *z;
|
601
|
-
|
602
|
-
rb_scan_args(argc, argv, "11", &index, &mtime);
|
603
|
-
rb_need_block();
|
604
|
-
Check_Type(index, T_FIXNUM);
|
605
|
-
|
606
|
-
if (NIL_P(mtime)) {
|
607
|
-
mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
|
608
|
-
} else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
|
609
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
|
610
|
-
}
|
611
|
-
|
612
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
613
|
-
Check_Archive(p_archive);
|
614
|
-
|
615
|
-
if ((z = malloc(sizeof(struct read_proc))) == NULL) {
|
616
|
-
zip_unchange_all(p_archive->archive);
|
617
|
-
zip_unchange_archive(p_archive->archive);
|
618
|
-
rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
|
619
|
-
}
|
620
|
-
|
621
|
-
z->proc = rb_block_proc();
|
622
|
-
z->mtime = mtime;
|
623
|
-
|
624
|
-
if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
|
625
|
-
free(z);
|
626
|
-
rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
627
|
-
}
|
628
|
-
|
629
|
-
if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
|
630
|
-
zip_source_free(zsource);
|
631
|
-
zip_unchange_all(p_archive->archive);
|
632
|
-
zip_unchange_archive(p_archive->archive);
|
633
|
-
rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
634
|
-
}
|
635
|
-
|
636
|
-
return Qnil;
|
637
|
-
}
|
638
|
-
|
639
|
-
/* */
|
640
|
-
static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
|
641
|
-
VALUE name, mtime;
|
642
|
-
struct zipruby_archive *p_archive;
|
643
|
-
int index;
|
644
|
-
|
645
|
-
rb_scan_args(argc, argv, "11", &name, &mtime);
|
646
|
-
Check_Type(name, T_STRING);
|
647
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
648
|
-
Check_Archive(p_archive);
|
649
|
-
|
650
|
-
index = zip_name_locate(p_archive->archive, StringValuePtr(name), ZIP_FL_NOCASE);
|
651
|
-
|
652
|
-
if (index >= 0) {
|
653
|
-
VALUE args[] = { INT2NUM(index), mtime };
|
654
|
-
return zipruby_archive_replace_function(2, args, self);
|
655
|
-
} else {
|
656
|
-
return zipruby_archive_add_function(argc, argv, self);
|
657
|
-
}
|
658
|
-
}
|
659
|
-
|
660
|
-
/* */
|
661
|
-
static VALUE zipruby_archive_update(VALUE self, VALUE srcarchive) {
|
662
|
-
struct zipruby_archive *p_archive, *p_srcarchive;
|
663
|
-
int i, num_files;
|
664
|
-
|
665
|
-
if (!rb_obj_is_instance_of(srcarchive, Archive)) {
|
666
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
|
667
|
-
}
|
668
|
-
|
669
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
670
|
-
Check_Archive(p_archive);
|
671
|
-
Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
|
672
|
-
Check_Archive(p_srcarchive);
|
673
|
-
|
674
|
-
num_files = zip_get_num_files(p_srcarchive->archive);
|
675
|
-
|
676
|
-
for (i = 0; i < num_files; i++) {
|
677
|
-
struct zip_source *zsource;
|
678
|
-
struct zip_file *fzip;
|
679
|
-
struct zip_stat sb;
|
680
|
-
char *buf;
|
681
|
-
const char *name;
|
682
|
-
int index, error;
|
683
|
-
|
684
|
-
zip_stat_init(&sb);
|
685
|
-
|
686
|
-
if (zip_stat_index(p_srcarchive->archive, i, 0, &sb)) {
|
687
|
-
zip_unchange_all(p_archive->archive);
|
688
|
-
zip_unchange_archive(p_archive->archive);
|
689
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
690
|
-
}
|
691
|
-
|
692
|
-
if ((buf = malloc(sb.size)) == NULL) {
|
693
|
-
zip_unchange_all(p_archive->archive);
|
694
|
-
zip_unchange_archive(p_archive->archive);
|
695
|
-
rb_raise(rb_eRuntimeError, "Update archive failed: Cannot allocate memory");
|
696
|
-
}
|
697
|
-
|
698
|
-
fzip = zip_fopen_index(p_srcarchive->archive, i, 0);
|
699
|
-
|
700
|
-
if (fzip == NULL) {
|
701
|
-
free(buf);
|
702
|
-
zip_unchange_all(p_archive->archive);
|
703
|
-
zip_unchange_archive(p_archive->archive);
|
704
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
705
|
-
}
|
706
|
-
|
707
|
-
if (zip_fread(fzip, buf, sb.size) == -1) {
|
708
|
-
free(buf);
|
709
|
-
zip_fclose(fzip);
|
710
|
-
zip_unchange_all(p_archive->archive);
|
711
|
-
zip_unchange_archive(p_archive->archive);
|
712
|
-
rb_raise(Error, "Update archive failed: %s", zip_file_strerror(fzip));
|
713
|
-
}
|
714
|
-
|
715
|
-
if ((error = zip_fclose(fzip)) != 0) {
|
716
|
-
char errstr[ERRSTR_BUFSIZE];
|
717
|
-
free(buf);
|
718
|
-
zip_unchange_all(p_archive->archive);
|
719
|
-
zip_unchange_archive(p_archive->archive);
|
720
|
-
zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
|
721
|
-
rb_raise(Error, "Update archive failed: %s", errstr);
|
722
|
-
}
|
723
|
-
|
724
|
-
if ((zsource = zip_source_buffer(p_archive->archive, buf, sb.size, 1)) == NULL) {
|
725
|
-
free(buf);
|
726
|
-
zip_unchange_all(p_archive->archive);
|
727
|
-
zip_unchange_archive(p_archive->archive);
|
728
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
729
|
-
}
|
730
|
-
|
731
|
-
if ((name = zip_get_name(p_srcarchive->archive, i, 0)) == NULL) {
|
732
|
-
zip_source_free(zsource);
|
733
|
-
zip_unchange_all(p_archive->archive);
|
734
|
-
zip_unchange_archive(p_archive->archive);
|
735
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
736
|
-
}
|
737
|
-
|
738
|
-
index = zip_name_locate(p_archive->archive, name, ZIP_FL_NOCASE);
|
739
|
-
|
740
|
-
if (index >= 0) {
|
741
|
-
if (zip_replace(p_archive->archive, i, zsource) == -1) {
|
742
|
-
zip_source_free(zsource);
|
743
|
-
zip_unchange_all(p_archive->archive);
|
744
|
-
zip_unchange_archive(p_archive->archive);
|
745
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
746
|
-
}
|
747
|
-
} else {
|
748
|
-
if (zip_add(p_archive->archive, name, zsource) == -1) {
|
749
|
-
zip_source_free(zsource);
|
750
|
-
zip_unchange_all(p_archive->archive);
|
751
|
-
zip_unchange_archive(p_archive->archive);
|
752
|
-
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
753
|
-
}
|
754
|
-
}
|
755
|
-
}
|
756
|
-
|
757
|
-
return Qnil;
|
758
|
-
}
|
759
|
-
|
760
|
-
/* */
|
761
|
-
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self) {
|
762
|
-
VALUE flags;
|
763
|
-
struct zipruby_archive *p_archive;
|
764
|
-
const char *comment;
|
765
|
-
int lenp, i_flags = 0;
|
766
|
-
|
767
|
-
rb_scan_args(argc, argv, "01", &flags);
|
768
|
-
|
769
|
-
if (!NIL_P(flags)) {
|
770
|
-
i_flags = NUM2INT(flags);
|
771
|
-
}
|
772
|
-
|
773
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
774
|
-
Check_Archive(p_archive);
|
775
|
-
|
776
|
-
// XXX: How is the error checked?
|
777
|
-
comment = zip_get_archive_comment(p_archive->archive, &lenp, i_flags);
|
778
|
-
|
779
|
-
return comment ? rb_str_new(comment, lenp) : Qnil;
|
780
|
-
}
|
781
|
-
|
782
|
-
/* */
|
783
|
-
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
|
784
|
-
struct zipruby_archive *p_archive;
|
785
|
-
const char *s_comment = NULL;
|
786
|
-
int len = 0;
|
787
|
-
|
788
|
-
if (!NIL_P(comment)) {
|
789
|
-
Check_Type(comment, T_STRING);
|
790
|
-
s_comment = StringValuePtr(comment);
|
791
|
-
len = RSTRING(comment)->len;
|
792
|
-
}
|
793
|
-
|
794
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
795
|
-
Check_Archive(p_archive);
|
796
|
-
|
797
|
-
if (zip_set_archive_comment(p_archive->archive, s_comment, len) == -1) {
|
798
|
-
zip_unchange_all(p_archive->archive);
|
799
|
-
zip_unchange_archive(p_archive->archive);
|
800
|
-
rb_raise(Error, "Comment archived failed: %s", zip_strerror(p_archive->archive));
|
801
|
-
}
|
802
|
-
|
803
|
-
return Qnil;
|
804
|
-
}
|
805
|
-
|
806
|
-
/* */
|
807
|
-
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
|
808
|
-
VALUE fname, flags;
|
809
|
-
struct zipruby_archive *p_archive;
|
810
|
-
int i_flags = 0;
|
811
|
-
|
812
|
-
rb_scan_args(argc, argv, "11", &fname, &flags);
|
813
|
-
Check_Type(fname, T_STRING);
|
814
|
-
|
815
|
-
if (!NIL_P(flags)) {
|
816
|
-
i_flags = NUM2INT(flags);
|
817
|
-
}
|
818
|
-
|
819
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
820
|
-
Check_Archive(p_archive);
|
821
|
-
|
822
|
-
return INT2NUM(zip_name_locate(p_archive->archive, StringValuePtr(fname), i_flags));
|
823
|
-
}
|
824
|
-
|
825
|
-
/* */
|
826
|
-
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self) {
|
827
|
-
VALUE index, flags;
|
828
|
-
struct zipruby_archive *p_archive;
|
829
|
-
const char *comment;
|
830
|
-
int lenp, i_flags = 0;
|
831
|
-
|
832
|
-
rb_scan_args(argc, argv, "11", &index, &flags);
|
833
|
-
|
834
|
-
if (!NIL_P(flags)) {
|
835
|
-
i_flags = NUM2INT(flags);
|
836
|
-
}
|
837
|
-
|
838
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
839
|
-
Check_Archive(p_archive);
|
840
|
-
|
841
|
-
// XXX: How is the error checked?
|
842
|
-
comment = zip_get_file_comment(p_archive->archive, NUM2INT(index), &lenp, i_flags);
|
843
|
-
|
844
|
-
return comment ? rb_str_new(comment, lenp) : Qnil;
|
845
|
-
}
|
846
|
-
|
847
|
-
/* */
|
848
|
-
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment) {
|
849
|
-
struct zipruby_archive *p_archive;
|
850
|
-
char *s_comment = NULL;
|
851
|
-
int len = 0;
|
852
|
-
|
853
|
-
if (!NIL_P(comment)) {
|
854
|
-
Check_Type(comment, T_STRING);
|
855
|
-
s_comment = StringValuePtr(comment);
|
856
|
-
len = RSTRING(comment)->len;
|
857
|
-
}
|
858
|
-
|
859
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
860
|
-
Check_Archive(p_archive);
|
861
|
-
|
862
|
-
if (zip_set_file_comment(p_archive->archive, NUM2INT(index), s_comment, len) == -1) {
|
863
|
-
zip_unchange_all(p_archive->archive);
|
864
|
-
zip_unchange_archive(p_archive->archive);
|
865
|
-
rb_raise(Error, "Comment file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
866
|
-
}
|
867
|
-
|
868
|
-
return Qnil;
|
869
|
-
}
|
870
|
-
|
871
|
-
/* */
|
872
|
-
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
|
873
|
-
struct zipruby_archive *p_archive;
|
874
|
-
|
875
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
876
|
-
Check_Archive(p_archive);
|
877
|
-
|
878
|
-
if (zip_delete(p_archive->archive, NUM2INT(index)) == -1) {
|
879
|
-
zip_unchange_all(p_archive->archive);
|
880
|
-
zip_unchange_archive(p_archive->archive);
|
881
|
-
rb_raise(Error, "Delete file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
882
|
-
}
|
883
|
-
|
884
|
-
return Qnil;
|
885
|
-
}
|
886
|
-
|
887
|
-
/* */
|
888
|
-
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
|
889
|
-
struct zipruby_archive *p_archive;
|
890
|
-
|
891
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
892
|
-
Check_Archive(p_archive);
|
893
|
-
|
894
|
-
if (zip_rename(p_archive->archive, NUM2INT(index), StringValuePtr(name)) == -1) {
|
895
|
-
zip_unchange_all(p_archive->archive);
|
896
|
-
zip_unchange_archive(p_archive->archive);
|
897
|
-
rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
898
|
-
}
|
899
|
-
|
900
|
-
return Qnil;
|
901
|
-
}
|
902
|
-
|
903
|
-
/* */
|
904
|
-
static VALUE zipruby_archive_funchange(VALUE self, VALUE index) {
|
905
|
-
struct zipruby_archive *p_archive;
|
906
|
-
|
907
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
908
|
-
Check_Archive(p_archive);
|
909
|
-
|
910
|
-
if (zip_unchange(p_archive->archive, NUM2INT(index)) == -1) {
|
911
|
-
zip_unchange_all(p_archive->archive);
|
912
|
-
zip_unchange_archive(p_archive->archive);
|
913
|
-
rb_raise(Error, "Unchange file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
914
|
-
}
|
915
|
-
|
916
|
-
return Qnil;
|
917
|
-
}
|
918
|
-
|
919
|
-
/* */
|
920
|
-
static VALUE zipruby_archive_funchange_all(VALUE self) {
|
921
|
-
struct zipruby_archive *p_archive;
|
922
|
-
|
923
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
924
|
-
Check_Archive(p_archive);
|
925
|
-
|
926
|
-
if (zip_unchange_all(p_archive->archive) == -1) {
|
927
|
-
rb_raise(Error, "Unchange all file failed: %s", zip_strerror(p_archive->archive));
|
928
|
-
}
|
929
|
-
|
930
|
-
return Qnil;
|
931
|
-
}
|
932
|
-
|
933
|
-
/* */
|
934
|
-
static VALUE zipruby_archive_unchange(VALUE self) {
|
935
|
-
struct zipruby_archive *p_archive;
|
936
|
-
|
937
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
938
|
-
Check_Archive(p_archive);
|
939
|
-
|
940
|
-
if (zip_unchange_archive(p_archive->archive) == -1) {
|
941
|
-
rb_raise(Error, "Unchange archive failed: %s", zip_strerror(p_archive->archive));
|
942
|
-
}
|
943
|
-
|
944
|
-
return Qnil;
|
945
|
-
}
|
946
|
-
|
947
|
-
/* */
|
948
|
-
static VALUE zipruby_archive_revert(VALUE self) {
|
949
|
-
zipruby_archive_funchange_all(self);
|
950
|
-
zipruby_archive_unchange(self);
|
951
|
-
|
952
|
-
return Qnil;
|
953
|
-
}
|
954
|
-
|
955
|
-
/* */
|
956
|
-
static VALUE zipruby_archive_each(VALUE self) {
|
957
|
-
struct zipruby_archive *p_archive;
|
958
|
-
int i, num_files;
|
959
|
-
|
960
|
-
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
961
|
-
Check_Archive(p_archive);
|
962
|
-
num_files = zip_get_num_files(p_archive->archive);
|
963
|
-
|
964
|
-
for (i = 0; i < num_files; i++) {
|
965
|
-
VALUE file;
|
966
|
-
int status;
|
967
|
-
|
968
|
-
file = rb_funcall(File, rb_intern("new"), 2, self, INT2NUM(i));
|
969
|
-
rb_protect(rb_yield, file, &status);
|
970
|
-
rb_funcall(file, rb_intern("close"), 0);
|
971
|
-
|
972
|
-
if (status != 0) {
|
973
|
-
rb_jump_tag(status);
|
974
|
-
}
|
975
|
-
}
|
976
|
-
|
977
|
-
return Qnil;
|
978
|
-
}
|