zipruby 0.2.9 → 0.3.0

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.

@@ -1,1326 +1,1330 @@
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 "tmpfile.h"
8
- #include "ruby.h"
9
- #include "rubyio.h"
10
-
11
- static VALUE zipruby_archive_alloc(VALUE klass);
12
- static void zipruby_archive_mark(struct zipruby_archive *p);
13
- static void zipruby_archive_free(struct zipruby_archive *p);
14
- static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
15
- static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self);
16
- static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
17
- static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password);
18
- static VALUE zipruby_archive_close(VALUE self);
19
- static VALUE zipruby_archive_num_files(VALUE self);
20
- static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
21
- static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
22
- static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
23
- static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
24
- static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
25
- static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io);
26
- static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
27
- static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self);
28
- static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self);
29
- static VALUE zipruby_archive_replace_io(int argc, VALUE* argv, VALUE self);
30
- static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
31
- static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self);
32
- static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
33
- static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self);
34
- static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
35
- static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self);
36
- static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
37
- static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
38
- static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self);
39
- static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self);
40
- static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment);
41
- static VALUE zipruby_archive_fdelete(VALUE self, VALUE index);
42
- static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name);
43
- static VALUE zipruby_archive_funchange(VALUE self, VALUE index);
44
- static VALUE zipruby_archive_funchange_all(VALUE self);
45
- static VALUE zipruby_archive_unchange(VALUE self);
46
- static VALUE zipruby_archive_revert(VALUE self);
47
- static VALUE zipruby_archive_each(VALUE self);
48
- static VALUE zipruby_archive_commit(VALUE self);
49
- static VALUE zipruby_archive_is_open(VALUE self);
50
- static VALUE zipruby_archive_decrypt(VALUE self, VALUE password);
51
- static VALUE zipruby_archive_encrypt(VALUE self, VALUE password);
52
- static VALUE zipruby_archive_read(VALUE self);
53
-
54
- extern VALUE Zip;
55
- VALUE Archive;
56
- extern VALUE File;
57
- extern VALUE Stat;
58
- extern VALUE Error;
59
-
60
- void Init_zipruby_archive() {
61
- Archive = rb_define_class_under(Zip, "Archive", rb_cObject);
62
- rb_define_alloc_func(Archive, zipruby_archive_alloc);
63
- rb_include_module(Archive, rb_mEnumerable);
64
- rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
65
- rb_define_singleton_method(Archive, "open_buffer", zipruby_archive_s_open_buffer, -1);
66
- rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
67
- rb_define_singleton_method(Archive, "encrypt", zipruby_archive_s_encrypt, 2);
68
- rb_define_method(Archive, "close", zipruby_archive_close, 0);
69
- rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
70
- rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
71
- rb_define_method(Archive, "fopen", zipruby_archive_fopen, -1);
72
- rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
73
- rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
74
- rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
75
- rb_define_method(Archive, "add_io", zipruby_archive_add_io, 2);
76
- rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
77
- rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, -1);
78
- rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, -1);
79
- rb_define_method(Archive, "replace_io", zipruby_archive_replace_io, -1);
80
- rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
81
- rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, -1);
82
- rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
83
- rb_define_method(Archive, "add_or_replace_io", zipruby_archive_add_or_replace_io, -1);
84
- rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
85
- rb_define_method(Archive, "update", zipruby_archive_update, -1);
86
- rb_define_method(Archive, "<<", zipruby_archive_add_io, 2);
87
- rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
88
- rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
89
- rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
90
- rb_define_method(Archive, "locate_name", zipruby_archive_locate_name, -1);
91
- rb_define_method(Archive, "get_fcomment", zipruby_archive_get_fcomment, -1);
92
- rb_define_method(Archive, "set_fcomment", zipruby_archive_set_fcomment, 2);
93
- rb_define_method(Archive, "fdelete", zipruby_archive_fdelete, 1);
94
- rb_define_method(Archive, "frename", zipruby_archive_frename, 2);
95
- rb_define_method(Archive, "funchange", zipruby_archive_funchange, 1);
96
- rb_define_method(Archive, "funchange_all", zipruby_archive_funchange_all, 0);
97
- rb_define_method(Archive, "unchange", zipruby_archive_unchange, 0);
98
- rb_define_method(Archive, "frevert", zipruby_archive_unchange, 1);
99
- rb_define_method(Archive, "revert", zipruby_archive_revert, 0);
100
- rb_define_method(Archive, "each", zipruby_archive_each, 0);
101
- rb_define_method(Archive, "commit", zipruby_archive_commit, 0);
102
- rb_define_method(Archive, "open?", zipruby_archive_is_open, 0);
103
- rb_define_method(Archive, "decrypt", zipruby_archive_decrypt, 1);
104
- rb_define_method(Archive, "encrypt", zipruby_archive_encrypt, 1);
105
- rb_define_method(Archive, "read", zipruby_archive_read, 0);
106
- }
107
-
108
- static VALUE zipruby_archive_alloc(VALUE klass) {
109
- struct zipruby_archive *p = ALLOC(struct zipruby_archive);
110
-
111
- p->archive = NULL;
112
- p->path = Qnil;
113
- p->flags = 0;
114
- p->tmpfilnam = NULL;
115
- p->buffer = Qnil;
116
-
117
- return Data_Wrap_Struct(klass, zipruby_archive_mark, zipruby_archive_free, p);
118
- }
119
-
120
- static void zipruby_archive_mark(struct zipruby_archive *p) {
121
- rb_gc_mark(p->path);
122
- rb_gc_mark(p->buffer);
123
- }
124
-
125
- static void zipruby_archive_free(struct zipruby_archive *p) {
126
- if (p->tmpfilnam) {
127
- zipruby_rmtmp(p->tmpfilnam);
128
- free(p->tmpfilnam);
129
- }
130
-
131
- xfree(p);
132
- }
133
-
134
- /* */
135
- static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
136
- VALUE path, flags;
137
- VALUE archive;
138
- struct zipruby_archive *p_archive;
139
- int i_flags = 0;
140
- int errorp;
141
-
142
- rb_scan_args(argc, argv, "11", &path, &flags);
143
- Check_Type(path, T_STRING);
144
-
145
- if (!NIL_P(flags)) {
146
- i_flags = NUM2INT(flags);
147
- }
148
-
149
- archive = rb_funcall(Archive, rb_intern("new"), 0);
150
- Data_Get_Struct(archive, struct zipruby_archive, p_archive);
151
-
152
- if ((p_archive->archive = zip_open(RSTRING_PTR(path), i_flags, &errorp)) == NULL) {
153
- char errstr[ERRSTR_BUFSIZE];
154
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
155
- rb_raise(Error, "Open archive failed - %s: %s", RSTRING_PTR(path), errstr);
156
- }
157
-
158
- p_archive->path = path;
159
- p_archive->flags = i_flags;
160
-
161
- if (rb_block_given_p()) {
162
- VALUE retval;
163
- int status;
164
-
165
- retval = rb_protect(rb_yield, archive, &status);
166
- zipruby_archive_close(archive);
167
-
168
- if (status != 0) {
169
- rb_jump_tag(status);
170
- }
171
-
172
- return retval;
173
- } else {
174
- return archive;
175
- }
176
- }
177
-
178
- /* */
179
- static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
180
- VALUE buffer, flags;
181
- VALUE archive;
182
- struct zipruby_archive *p_archive;
183
- void *data = NULL;
184
- int len = 0, i_flags = 0;
185
- int errorp;
186
-
187
- rb_scan_args(argc, argv, "02", &buffer, &flags);
188
-
189
- if (FIXNUM_P(buffer) && NIL_P(flags)) {
190
- flags = buffer;
191
- buffer = Qnil;
192
- }
193
-
194
- if (!NIL_P(flags)) {
195
- i_flags = NUM2INT(flags);
196
- }
197
-
198
- if (i_flags & ZIP_CREATE) {
199
- if (!NIL_P(buffer)) {
200
- Check_Type(buffer, T_STRING);
201
- }
202
-
203
- i_flags = (i_flags | ZIP_TRUNC);
204
- } else if (TYPE(buffer) == T_STRING) {
205
- data = RSTRING_PTR(buffer);
206
- len = RSTRING_LEN(buffer);
207
- } else if (rb_obj_is_instance_of(buffer, rb_cProc)) {
208
- data = (void *) buffer;
209
- len = -1;
210
- } else {
211
- rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Proc)", rb_class2name(CLASS_OF(buffer)));
212
- }
213
-
214
- archive = rb_funcall(Archive, rb_intern("new"), 0);
215
- Data_Get_Struct(archive, struct zipruby_archive, p_archive);
216
-
217
- if ((p_archive->tmpfilnam = zipruby_tmpnam(data, len)) == NULL) {
218
- rb_raise(Error, "Open archive failed: Failed to create temporary file");
219
- }
220
-
221
- if ((p_archive->archive = zip_open(p_archive->tmpfilnam, i_flags, &errorp)) == NULL) {
222
- char errstr[ERRSTR_BUFSIZE];
223
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
224
- rb_raise(Error, "Open archive failed: %s", errstr);
225
- }
226
-
227
- p_archive->path = rb_str_new2(p_archive->tmpfilnam);
228
- p_archive->flags = i_flags;
229
- p_archive->buffer = buffer;
230
-
231
- if (rb_block_given_p()) {
232
- VALUE retval;
233
- int status;
234
-
235
- retval = rb_protect(rb_yield, archive, &status);
236
- zipruby_archive_close(archive);
237
-
238
- if (status != 0) {
239
- rb_jump_tag(status);
240
- }
241
-
242
- return retval;
243
- } else {
244
- return archive;
245
- }
246
- }
247
-
248
- /* */
249
- static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
250
- int res;
251
- int errorp, wrongpwd;
252
- long pwdlen;
253
-
254
- Check_Type(path, T_STRING);
255
- Check_Type(password, T_STRING);
256
- pwdlen = RSTRING_LEN(password);
257
-
258
- if (pwdlen < 1) {
259
- rb_raise(Error, "Decrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
260
- } else if (pwdlen > 0xff) {
261
- rb_raise(Error, "Decrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
262
- }
263
-
264
- res = zip_decrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp, &wrongpwd);
265
-
266
- if (res == -1) {
267
- if (wrongpwd) {
268
- rb_raise(Error, "Decrypt archive failed - %s: Wrong password", RSTRING_PTR(path));
269
- } else {
270
- char errstr[ERRSTR_BUFSIZE];
271
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
272
- rb_raise(Error, "Decrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
273
- }
274
- }
275
-
276
- return (res > 0) ? Qtrue : Qfalse;
277
- }
278
-
279
- /* */
280
- static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
281
- int res;
282
- int errorp;
283
- long pwdlen;
284
-
285
- Check_Type(path, T_STRING);
286
- Check_Type(password, T_STRING);
287
- pwdlen = RSTRING_LEN(password);
288
-
289
- if (pwdlen < 1) {
290
- rb_raise(Error, "Encrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
291
- } else if (pwdlen > 0xff) {
292
- rb_raise(Error, "Encrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
293
- }
294
-
295
- res = zip_encrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp);
296
-
297
- if (res == -1) {
298
- char errstr[ERRSTR_BUFSIZE];
299
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
300
- rb_raise(Error, "Encrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
301
- }
302
-
303
- return (res > 0) ? Qtrue : Qfalse;
304
- }
305
-
306
- /* */
307
- static VALUE zipruby_archive_close(VALUE self) {
308
- struct zipruby_archive *p_archive;
309
- int changed, survivors;
310
-
311
- if (!zipruby_archive_is_open(self)) {
312
- return Qfalse;
313
- }
314
-
315
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
316
- Check_Archive(p_archive);
317
-
318
- changed = _zip_changed(p_archive->archive, &survivors);
319
-
320
- if (zip_close(p_archive->archive) == -1) {
321
- zip_unchange_all(p_archive->archive);
322
- zip_unchange_archive(p_archive->archive);
323
- rb_raise(Error, "Close archive failed: %s", zip_strerror(p_archive->archive));
324
- }
325
-
326
- if (!NIL_P(p_archive->buffer) && changed) {
327
- rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
328
- }
329
-
330
- zipruby_rmtmp(p_archive->tmpfilnam);
331
- p_archive->archive = NULL;
332
- p_archive->flags = 0;
333
-
334
- return Qtrue;
335
- }
336
-
337
- /* */
338
- static VALUE zipruby_archive_num_files(VALUE self) {
339
- struct zipruby_archive *p_archive;
340
- int num_files;
341
-
342
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
343
- Check_Archive(p_archive);
344
- num_files = zip_get_num_files(p_archive->archive);
345
-
346
- return INT2NUM(num_files);
347
- }
348
-
349
- /* */
350
- static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self) {
351
- VALUE index, flags;
352
- struct zipruby_archive *p_archive;
353
- int i_flags = 0;
354
- const char *name;
355
-
356
- rb_scan_args(argc, argv, "11", &index, &flags);
357
- Check_Type(index, T_FIXNUM);
358
-
359
- if (!NIL_P(flags)) {
360
- i_flags = NUM2INT(flags);
361
- }
362
-
363
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
364
- Check_Archive(p_archive);
365
-
366
- if ((name = zip_get_name(p_archive->archive, NUM2INT(index), i_flags)) == NULL) {
367
- rb_raise(Error, "Get name failed at %d: %s", index, zip_strerror(p_archive->archive));
368
- }
369
-
370
- return (name != NULL) ? rb_str_new2(name) : Qnil;
371
- }
372
-
373
- /* */
374
- static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self) {
375
- VALUE index, flags, stat_flags, file;
376
-
377
- rb_scan_args(argc, argv, "12", &index, &flags, &stat_flags);
378
- file = rb_funcall(File, rb_intern("new"), 4, self, index, flags, stat_flags);
379
-
380
- if (rb_block_given_p()) {
381
- VALUE retval;
382
- int status;
383
-
384
- retval = rb_protect(rb_yield, file, &status);
385
- rb_funcall(file, rb_intern("close"), 0);
386
-
387
- if (status != 0) {
388
- rb_jump_tag(status);
389
- }
390
-
391
- return retval;
392
- } else {
393
- return file;
394
- }
395
- }
396
-
397
- /* */
398
- static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self) {
399
- VALUE index, flags;
400
-
401
- rb_scan_args(argc, argv, "11", &index, &flags);
402
-
403
- return rb_funcall(Stat, rb_intern("new"), 3, self, index, flags);
404
- }
405
-
406
- /* */
407
- static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
408
- struct zipruby_archive *p_archive;
409
- struct zip_source *zsource;
410
- char *data;
411
- off_t len;
412
-
413
- Check_Type(name, T_STRING);
414
- Check_Type(source, T_STRING);
415
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
416
- Check_Archive(p_archive);
417
-
418
- len = RSTRING_LEN(source);
419
-
420
- if ((data = malloc(len)) == NULL) {
421
- rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
422
- }
423
-
424
- memset(data, 0, len);
425
- memcpy(data, RSTRING_PTR(source), len);
426
-
427
- if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
428
- free(data);
429
- rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
430
- }
431
-
432
- if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
433
- zip_source_free(zsource);
434
- zip_unchange_all(p_archive->archive);
435
- zip_unchange_archive(p_archive->archive);
436
- rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
437
- }
438
-
439
- return Qnil;
440
- }
441
-
442
- /* */
443
- static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
444
- struct zipruby_archive *p_archive;
445
- struct zip_source *zsource;
446
- VALUE index, source, flags;
447
- int i_index, i_flags = 0;
448
- char *data;
449
- off_t len;
450
-
451
- rb_scan_args(argc, argv, "21", &index, &source, &flags);
452
-
453
- if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
454
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
455
- }
456
-
457
- if (!NIL_P(flags)) {
458
- i_flags = NUM2INT(flags);
459
- }
460
-
461
- Check_Type(source, T_STRING);
462
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
463
- Check_Archive(p_archive);
464
-
465
- if (FIXNUM_P(index)) {
466
- i_index = NUM2INT(index);
467
- } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
468
- rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
469
- }
470
-
471
- len = RSTRING_LEN(source);
472
-
473
- if ((data = malloc(len)) == NULL) {
474
- rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
475
- }
476
-
477
- memcpy(data, RSTRING_PTR(source), len);
478
-
479
- if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
480
- free(data);
481
- rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
482
- }
483
-
484
- if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
485
- zip_source_free(zsource);
486
- zip_unchange_all(p_archive->archive);
487
- zip_unchange_archive(p_archive->archive);
488
- rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
489
- }
490
-
491
- return Qnil;
492
- }
493
-
494
- /* */
495
- static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self) {
496
- struct zipruby_archive *p_archive;
497
- VALUE name, source, flags;
498
- int index, i_flags = 0;
499
-
500
- rb_scan_args(argc, argv, "21", &name, &source, &flags);
501
-
502
- if (!NIL_P(flags)) {
503
- i_flags = NUM2INT(flags);
504
- }
505
-
506
- Check_Type(name, T_STRING);
507
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
508
- Check_Archive(p_archive);
509
-
510
- index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
511
-
512
- if (index >= 0) {
513
- VALUE _args[] = { INT2NUM(index), source };
514
- return zipruby_archive_replace_buffer(2, _args, self);
515
- } else {
516
- return zipruby_archive_add_buffer(self, name, source);
517
- }
518
- }
519
-
520
- /* */
521
- static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
522
- VALUE name, fname;
523
- struct zipruby_archive *p_archive;
524
- struct zip_source *zsource;
525
-
526
- rb_scan_args(argc, argv, "11", &name, &fname);
527
-
528
- if (NIL_P(fname)) {
529
- fname = name;
530
- name = Qnil;
531
- }
532
-
533
- Check_Type(fname, T_STRING);
534
-
535
- if (NIL_P(name)) {
536
- name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
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
- if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
544
- rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
545
- }
546
-
547
- if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
548
- zip_source_free(zsource);
549
- zip_unchange_all(p_archive->archive);
550
- zip_unchange_archive(p_archive->archive);
551
- rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
552
- }
553
-
554
- return Qnil;
555
- }
556
-
557
- /* */
558
- static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self) {
559
- struct zipruby_archive *p_archive;
560
- struct zip_source *zsource;
561
- VALUE index, fname, flags;
562
- int i_index, i_flags = 0;
563
-
564
- rb_scan_args(argc, argv, "21", &index, &fname, &flags);
565
-
566
- if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
567
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
568
- }
569
-
570
- if (!NIL_P(flags)) {
571
- i_flags = NUM2INT(flags);
572
- }
573
-
574
- Check_Type(fname, T_STRING);
575
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
576
- Check_Archive(p_archive);
577
-
578
- if (FIXNUM_P(index)) {
579
- i_index = NUM2INT(index);
580
- } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
581
- rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
582
- }
583
-
584
- if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
585
- rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
586
- }
587
-
588
- if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
589
- zip_source_free(zsource);
590
- zip_unchange_all(p_archive->archive);
591
- zip_unchange_archive(p_archive->archive);
592
- rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
593
- }
594
-
595
- return Qnil;
596
- }
597
-
598
- /* */
599
- static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self) {
600
- VALUE name, fname, flags;
601
- struct zipruby_archive *p_archive;
602
- int index, i_flags = 0;
603
-
604
- rb_scan_args(argc, argv, "12", &name, &fname, &flags);
605
-
606
- if (NIL_P(flags) && FIXNUM_P(fname)) {
607
- flags = fname;
608
- fname = Qnil;
609
- }
610
-
611
- if (NIL_P(fname)) {
612
- fname = name;
613
- name = Qnil;
614
- }
615
-
616
- Check_Type(fname, T_STRING);
617
-
618
- if (NIL_P(name)) {
619
- name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
620
- }
621
-
622
- if (!NIL_P(flags)) {
623
- i_flags = NUM2INT(flags);
624
- }
625
-
626
- Check_Type(name, T_STRING);
627
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
628
- Check_Archive(p_archive);
629
-
630
- index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
631
-
632
- if (index >= 0) {
633
- VALUE _args[] = { INT2NUM(index), fname };
634
- return zipruby_archive_replace_file(2, _args, self);
635
- } else {
636
- VALUE _args[] = { name, fname };
637
- return zipruby_archive_add_file(2, _args, self);
638
- }
639
- }
640
-
641
- /* */
642
- static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io) {
643
- VALUE source;
644
-
645
- Check_IO(io);
646
- source = rb_funcall(io, rb_intern("read"), 0);
647
-
648
- return zipruby_archive_add_buffer(self, name, source);
649
- }
650
-
651
- /* */
652
- static VALUE zipruby_archive_replace_io(int argc, VALUE *argv, VALUE self) {
653
- VALUE source, io, index, flags;
654
- VALUE _args[3];
655
-
656
- rb_scan_args(argc, argv, "21", &index, &io, &flags);
657
- Check_IO(io);
658
- source = rb_funcall(io, rb_intern("read"), 0);
659
-
660
- _args[0] = index;
661
- _args[1] = source;
662
- _args[2] = flags;
663
- return zipruby_archive_replace_file(2, _args, self);
664
- }
665
-
666
- /* */
667
- static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self) {
668
- VALUE name, io, flags;
669
- struct zipruby_archive *p_archive;
670
- int index, i_flags = 0;
671
-
672
- rb_scan_args(argc, argv, "21", &name, &io, &flags);
673
- Check_IO(io);
674
-
675
- if (!NIL_P(flags)) {
676
- i_flags = NUM2INT(flags);
677
- }
678
-
679
- Check_Type(name, T_STRING);
680
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
681
- Check_Archive(p_archive);
682
-
683
- index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
684
-
685
- if (index >= 0) {
686
- VALUE _args[] = {INT2NUM(index), io, flags};
687
- return zipruby_archive_replace_io(2, _args, self);
688
- } else {
689
- return zipruby_archive_add_io(self ,name, io);
690
- }
691
- }
692
-
693
- /* */
694
- static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
695
- VALUE name, mtime;
696
- struct zipruby_archive *p_archive;
697
- struct zip_source *zsource;
698
- struct read_proc *z;
699
-
700
- rb_scan_args(argc, argv, "11", &name, &mtime);
701
- rb_need_block();
702
- Check_Type(name, T_STRING);
703
-
704
- if (NIL_P(mtime)) {
705
- mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
706
- } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
707
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
708
- }
709
-
710
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
711
- Check_Archive(p_archive);
712
-
713
- if ((z = malloc(sizeof(struct read_proc))) == NULL) {
714
- zip_unchange_all(p_archive->archive);
715
- zip_unchange_archive(p_archive->archive);
716
- rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", RSTRING_PTR(name));
717
- }
718
-
719
- z->proc = rb_block_proc();
720
- z->mtime = mtime;
721
-
722
- if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
723
- free(z);
724
- rb_raise(Error, "Add failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
725
- }
726
-
727
- if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
728
- zip_source_free(zsource);
729
- zip_unchange_all(p_archive->archive);
730
- zip_unchange_archive(p_archive->archive);
731
- rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
732
- }
733
-
734
- return Qnil;
735
- }
736
-
737
- /* */
738
- static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
739
- VALUE index, mtime;
740
- struct zipruby_archive *p_archive;
741
- struct zip_source *zsource;
742
- struct read_proc *z;
743
-
744
- rb_scan_args(argc, argv, "11", &index, &mtime);
745
- rb_need_block();
746
- Check_Type(index, T_FIXNUM);
747
-
748
- if (NIL_P(mtime)) {
749
- mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
750
- } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
751
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
752
- }
753
-
754
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
755
- Check_Archive(p_archive);
756
-
757
- if ((z = malloc(sizeof(struct read_proc))) == NULL) {
758
- zip_unchange_all(p_archive->archive);
759
- zip_unchange_archive(p_archive->archive);
760
- rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
761
- }
762
-
763
- z->proc = rb_block_proc();
764
- z->mtime = mtime;
765
-
766
- if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
767
- free(z);
768
- rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
769
- }
770
-
771
- if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
772
- zip_source_free(zsource);
773
- zip_unchange_all(p_archive->archive);
774
- zip_unchange_archive(p_archive->archive);
775
- rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
776
- }
777
-
778
- return Qnil;
779
- }
780
-
781
- /* */
782
- static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
783
- VALUE name, mtime, flags;
784
- struct zipruby_archive *p_archive;
785
- int index, i_flags = 0;
786
-
787
- rb_scan_args(argc, argv, "12", &name, &mtime, &flags);
788
-
789
- if (NIL_P(flags) && FIXNUM_P(mtime)) {
790
- flags = mtime;
791
- mtime = Qnil;
792
- }
793
-
794
- if (!NIL_P(flags)) {
795
- i_flags = NUM2INT(flags);
796
- }
797
-
798
- Check_Type(name, T_STRING);
799
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
800
- Check_Archive(p_archive);
801
-
802
- index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
803
-
804
- if (index >= 0) {
805
- VALUE _args[] = { INT2NUM(index), mtime };
806
- return zipruby_archive_replace_function(2, _args, self);
807
- } else {
808
- VALUE _args[] = { name, mtime };
809
- return zipruby_archive_add_function(2, _args, self);
810
- }
811
- }
812
-
813
- /* */
814
- static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self) {
815
- struct zipruby_archive *p_archive, *p_srcarchive;
816
- VALUE srcarchive, flags;
817
- int i, num_files, i_flags = 0;
818
-
819
- rb_scan_args(argc, argv, "11", &srcarchive, &flags);
820
-
821
- if (!rb_obj_is_instance_of(srcarchive, Archive)) {
822
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
823
- }
824
-
825
- if (!NIL_P(flags)) {
826
- i_flags = NUM2INT(flags);
827
- }
828
-
829
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
830
- Check_Archive(p_archive);
831
- Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
832
- Check_Archive(p_srcarchive);
833
-
834
- num_files = zip_get_num_files(p_srcarchive->archive);
835
-
836
- for (i = 0; i < num_files; i++) {
837
- struct zip_source *zsource;
838
- struct zip_file *fzip;
839
- struct zip_stat sb;
840
- char *buf;
841
- const char *name;
842
- int index, error;
843
-
844
- zip_stat_init(&sb);
845
-
846
- if (zip_stat_index(p_srcarchive->archive, i, 0, &sb)) {
847
- zip_unchange_all(p_archive->archive);
848
- zip_unchange_archive(p_archive->archive);
849
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
850
- }
851
-
852
- if ((buf = malloc(sb.size)) == NULL) {
853
- zip_unchange_all(p_archive->archive);
854
- zip_unchange_archive(p_archive->archive);
855
- rb_raise(rb_eRuntimeError, "Update archive failed: Cannot allocate memory");
856
- }
857
-
858
- fzip = zip_fopen_index(p_srcarchive->archive, i, 0);
859
-
860
- if (fzip == NULL) {
861
- free(buf);
862
- zip_unchange_all(p_archive->archive);
863
- zip_unchange_archive(p_archive->archive);
864
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
865
- }
866
-
867
- if (zip_fread(fzip, buf, sb.size) == -1) {
868
- free(buf);
869
- zip_fclose(fzip);
870
- zip_unchange_all(p_archive->archive);
871
- zip_unchange_archive(p_archive->archive);
872
- rb_raise(Error, "Update archive failed: %s", zip_file_strerror(fzip));
873
- }
874
-
875
- if ((error = zip_fclose(fzip)) != 0) {
876
- char errstr[ERRSTR_BUFSIZE];
877
- free(buf);
878
- zip_unchange_all(p_archive->archive);
879
- zip_unchange_archive(p_archive->archive);
880
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
881
- rb_raise(Error, "Update archive failed: %s", errstr);
882
- }
883
-
884
- if ((zsource = zip_source_buffer(p_archive->archive, buf, sb.size, 1)) == NULL) {
885
- free(buf);
886
- zip_unchange_all(p_archive->archive);
887
- zip_unchange_archive(p_archive->archive);
888
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
889
- }
890
-
891
- if ((name = zip_get_name(p_srcarchive->archive, i, 0)) == NULL) {
892
- zip_source_free(zsource);
893
- zip_unchange_all(p_archive->archive);
894
- zip_unchange_archive(p_archive->archive);
895
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
896
- }
897
-
898
- index = zip_name_locate(p_archive->archive, name, i_flags);
899
-
900
- if (index >= 0) {
901
- if (zip_replace(p_archive->archive, i, zsource) == -1) {
902
- zip_source_free(zsource);
903
- zip_unchange_all(p_archive->archive);
904
- zip_unchange_archive(p_archive->archive);
905
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
906
- }
907
- } else {
908
- if (zip_add(p_archive->archive, name, zsource) == -1) {
909
- zip_source_free(zsource);
910
- zip_unchange_all(p_archive->archive);
911
- zip_unchange_archive(p_archive->archive);
912
- rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
913
- }
914
- }
915
- }
916
-
917
- return Qnil;
918
- }
919
-
920
- /* */
921
- static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self) {
922
- VALUE flags;
923
- struct zipruby_archive *p_archive;
924
- const char *comment;
925
- int lenp, i_flags = 0;
926
-
927
- rb_scan_args(argc, argv, "01", &flags);
928
-
929
- if (!NIL_P(flags)) {
930
- i_flags = NUM2INT(flags);
931
- }
932
-
933
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
934
- Check_Archive(p_archive);
935
-
936
- // XXX: How is the error checked?
937
- comment = zip_get_archive_comment(p_archive->archive, &lenp, i_flags);
938
-
939
- return comment ? rb_str_new(comment, lenp) : Qnil;
940
- }
941
-
942
- /* */
943
- static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
944
- struct zipruby_archive *p_archive;
945
- const char *s_comment = NULL;
946
- int len = 0;
947
-
948
- if (!NIL_P(comment)) {
949
- Check_Type(comment, T_STRING);
950
- s_comment = RSTRING_PTR(comment);
951
- len = RSTRING_LEN(comment);
952
- }
953
-
954
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
955
- Check_Archive(p_archive);
956
-
957
- if (zip_set_archive_comment(p_archive->archive, s_comment, len) == -1) {
958
- zip_unchange_all(p_archive->archive);
959
- zip_unchange_archive(p_archive->archive);
960
- rb_raise(Error, "Comment archived failed: %s", zip_strerror(p_archive->archive));
961
- }
962
-
963
- return Qnil;
964
- }
965
-
966
- /* */
967
- static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
968
- VALUE fname, flags;
969
- struct zipruby_archive *p_archive;
970
- int i_flags = 0;
971
-
972
- rb_scan_args(argc, argv, "11", &fname, &flags);
973
- Check_Type(fname, T_STRING);
974
-
975
- if (!NIL_P(flags)) {
976
- i_flags = NUM2INT(flags);
977
- }
978
-
979
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
980
- Check_Archive(p_archive);
981
-
982
- return INT2NUM(zip_name_locate(p_archive->archive, RSTRING_PTR(fname), i_flags));
983
- }
984
-
985
- /* */
986
- static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self) {
987
- VALUE index, flags;
988
- struct zipruby_archive *p_archive;
989
- const char *comment;
990
- int lenp, i_flags = 0;
991
-
992
- rb_scan_args(argc, argv, "11", &index, &flags);
993
-
994
- if (!NIL_P(flags)) {
995
- i_flags = NUM2INT(flags);
996
- }
997
-
998
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
999
- Check_Archive(p_archive);
1000
-
1001
- // XXX: How is the error checked?
1002
- comment = zip_get_file_comment(p_archive->archive, NUM2INT(index), &lenp, i_flags);
1003
-
1004
- return comment ? rb_str_new(comment, lenp) : Qnil;
1005
- }
1006
-
1007
- /* */
1008
- static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment) {
1009
- struct zipruby_archive *p_archive;
1010
- char *s_comment = NULL;
1011
- int len = 0;
1012
-
1013
- if (!NIL_P(comment)) {
1014
- Check_Type(comment, T_STRING);
1015
- s_comment = RSTRING_PTR(comment);
1016
- len = RSTRING_LEN(comment);
1017
- }
1018
-
1019
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1020
- Check_Archive(p_archive);
1021
-
1022
- if (zip_set_file_comment(p_archive->archive, NUM2INT(index), s_comment, len) == -1) {
1023
- zip_unchange_all(p_archive->archive);
1024
- zip_unchange_archive(p_archive->archive);
1025
- rb_raise(Error, "Comment file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1026
- }
1027
-
1028
- return Qnil;
1029
- }
1030
-
1031
- /* */
1032
- static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
1033
- struct zipruby_archive *p_archive;
1034
-
1035
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1036
- Check_Archive(p_archive);
1037
-
1038
- if (zip_delete(p_archive->archive, NUM2INT(index)) == -1) {
1039
- zip_unchange_all(p_archive->archive);
1040
- zip_unchange_archive(p_archive->archive);
1041
- rb_raise(Error, "Delete file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1042
- }
1043
-
1044
- return Qnil;
1045
- }
1046
-
1047
- /* */
1048
- static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
1049
- struct zipruby_archive *p_archive;
1050
-
1051
- Check_Type(name, T_STRING);
1052
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1053
- Check_Archive(p_archive);
1054
-
1055
- if (zip_rename(p_archive->archive, NUM2INT(index), RSTRING_PTR(name)) == -1) {
1056
- zip_unchange_all(p_archive->archive);
1057
- zip_unchange_archive(p_archive->archive);
1058
- rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1059
- }
1060
-
1061
- return Qnil;
1062
- }
1063
-
1064
- /* */
1065
- static VALUE zipruby_archive_funchange(VALUE self, VALUE index) {
1066
- struct zipruby_archive *p_archive;
1067
-
1068
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1069
- Check_Archive(p_archive);
1070
-
1071
- if (zip_unchange(p_archive->archive, NUM2INT(index)) == -1) {
1072
- zip_unchange_all(p_archive->archive);
1073
- zip_unchange_archive(p_archive->archive);
1074
- rb_raise(Error, "Unchange file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1075
- }
1076
-
1077
- return Qnil;
1078
- }
1079
-
1080
- /* */
1081
- static VALUE zipruby_archive_funchange_all(VALUE self) {
1082
- struct zipruby_archive *p_archive;
1083
-
1084
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1085
- Check_Archive(p_archive);
1086
-
1087
- if (zip_unchange_all(p_archive->archive) == -1) {
1088
- rb_raise(Error, "Unchange all file failed: %s", zip_strerror(p_archive->archive));
1089
- }
1090
-
1091
- return Qnil;
1092
- }
1093
-
1094
- /* */
1095
- static VALUE zipruby_archive_unchange(VALUE self) {
1096
- struct zipruby_archive *p_archive;
1097
-
1098
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1099
- Check_Archive(p_archive);
1100
-
1101
- if (zip_unchange_archive(p_archive->archive) == -1) {
1102
- rb_raise(Error, "Unchange archive failed: %s", zip_strerror(p_archive->archive));
1103
- }
1104
-
1105
- return Qnil;
1106
- }
1107
-
1108
- /* */
1109
- static VALUE zipruby_archive_revert(VALUE self) {
1110
- zipruby_archive_funchange_all(self);
1111
- zipruby_archive_unchange(self);
1112
-
1113
- return Qnil;
1114
- }
1115
-
1116
- /* */
1117
- static VALUE zipruby_archive_each(VALUE self) {
1118
- struct zipruby_archive *p_archive;
1119
- int i, num_files;
1120
-
1121
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1122
- Check_Archive(p_archive);
1123
- num_files = zip_get_num_files(p_archive->archive);
1124
-
1125
- for (i = 0; i < num_files; i++) {
1126
- VALUE file;
1127
- int status;
1128
-
1129
- file = rb_funcall(File, rb_intern("new"), 2, self, INT2NUM(i));
1130
- rb_protect(rb_yield, file, &status);
1131
- rb_funcall(file, rb_intern("close"), 0);
1132
-
1133
- if (status != 0) {
1134
- rb_jump_tag(status);
1135
- }
1136
- }
1137
-
1138
- return Qnil;
1139
- }
1140
-
1141
- /* */
1142
- static VALUE zipruby_archive_commit(VALUE self) {
1143
- struct zipruby_archive *p_archive;
1144
- int changed, survivors;
1145
- int errorp;
1146
-
1147
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1148
- Check_Archive(p_archive);
1149
-
1150
- changed = _zip_changed(p_archive->archive, &survivors);
1151
-
1152
- if (zip_close(p_archive->archive) == -1) {
1153
- zip_unchange_all(p_archive->archive);
1154
- zip_unchange_archive(p_archive->archive);
1155
- rb_raise(Error, "Commit archive failed: %s", zip_strerror(p_archive->archive));
1156
- }
1157
-
1158
- if (!NIL_P(p_archive->buffer) && changed) {
1159
- rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1160
- }
1161
-
1162
- p_archive->archive = NULL;
1163
- p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1164
-
1165
- if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1166
- char errstr[ERRSTR_BUFSIZE];
1167
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1168
- rb_raise(Error, "Commit archive failed: %s", errstr);
1169
- }
1170
-
1171
- return Qnil;
1172
- }
1173
-
1174
- /* */
1175
- static VALUE zipruby_archive_is_open(VALUE self) {
1176
- struct zipruby_archive *p_archive;
1177
-
1178
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1179
- return (p_archive->archive != NULL) ? Qtrue : Qfalse;
1180
- }
1181
-
1182
- /* */
1183
- static VALUE zipruby_archive_decrypt(VALUE self, VALUE password) {
1184
- VALUE retval;
1185
- struct zipruby_archive *p_archive;
1186
- long pwdlen;
1187
- int changed, survivors;
1188
- int errorp;
1189
-
1190
- Check_Type(password, T_STRING);
1191
- pwdlen = RSTRING_LEN(password);
1192
-
1193
- if (pwdlen < 1) {
1194
- rb_raise(Error, "Decrypt archive failed: Password is empty");
1195
- } else if (pwdlen > 0xff) {
1196
- rb_raise(Error, "Decrypt archive failed: Password is too long");
1197
- }
1198
-
1199
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1200
- Check_Archive(p_archive);
1201
-
1202
- changed = _zip_changed(p_archive->archive, &survivors);
1203
-
1204
- if (zip_close(p_archive->archive) == -1) {
1205
- zip_unchange_all(p_archive->archive);
1206
- zip_unchange_archive(p_archive->archive);
1207
- rb_raise(Error, "Decrypt archive failed: %s", zip_strerror(p_archive->archive));
1208
- }
1209
-
1210
- if (!NIL_P(p_archive->buffer) && changed) {
1211
- rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1212
- }
1213
-
1214
- p_archive->archive = NULL;
1215
- p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1216
-
1217
- retval = zipruby_archive_s_decrypt(Archive, p_archive->path, password);
1218
-
1219
- if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1220
- char errstr[ERRSTR_BUFSIZE];
1221
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1222
- rb_raise(Error, "Decrypt archive failed: %s", errstr);
1223
- }
1224
-
1225
- return retval;
1226
- }
1227
-
1228
- /* */
1229
- static VALUE zipruby_archive_encrypt(VALUE self, VALUE password) {
1230
- VALUE retval;
1231
- struct zipruby_archive *p_archive;
1232
- long pwdlen;
1233
- int changed, survivors;
1234
- int errorp;
1235
-
1236
- Check_Type(password, T_STRING);
1237
- pwdlen = RSTRING_LEN(password);
1238
-
1239
- if (pwdlen < 1) {
1240
- rb_raise(Error, "Encrypt archive failed: Password is empty");
1241
- } else if (pwdlen > 0xff) {
1242
- rb_raise(Error, "Encrypt archive failed: Password is too long");
1243
- }
1244
-
1245
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1246
- Check_Archive(p_archive);
1247
-
1248
- changed = _zip_changed(p_archive->archive, &survivors);
1249
-
1250
- if (zip_close(p_archive->archive) == -1) {
1251
- zip_unchange_all(p_archive->archive);
1252
- zip_unchange_archive(p_archive->archive);
1253
- rb_raise(Error, "Encrypt archive failed: %s", zip_strerror(p_archive->archive));
1254
- }
1255
-
1256
- if (!NIL_P(p_archive->buffer) && changed) {
1257
- rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1258
- }
1259
-
1260
- p_archive->archive = NULL;
1261
- p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1262
-
1263
- retval = zipruby_archive_s_encrypt(Archive, p_archive->path, password);
1264
-
1265
- if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1266
- char errstr[ERRSTR_BUFSIZE];
1267
- zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1268
- rb_raise(Error, "Encrypt archive failed: %s", errstr);
1269
- }
1270
-
1271
- return retval;
1272
- }
1273
-
1274
- /* */
1275
- static VALUE zipruby_archive_read(VALUE self) {
1276
- VALUE retval = Qnil;
1277
- struct zipruby_archive *p_archive;
1278
- FILE *fzip;
1279
- char buf[DATA_BUFSIZE];
1280
- ssize_t n;
1281
- int block_given;
1282
-
1283
- Data_Get_Struct(self, struct zipruby_archive, p_archive);
1284
-
1285
- if (NIL_P(p_archive->path)) {
1286
- rb_raise(rb_eRuntimeError, "invalid Zip::Archive");
1287
- }
1288
-
1289
- #ifdef _WIN32
1290
- if (fopen_s(&fzip, RSTRING_PTR(p_archive->path), "rb") != 0) {
1291
- rb_raise(Error, "Read archive failed: Cannot open archive");
1292
- }
1293
- #else
1294
- if ((fzip = fopen(RSTRING_PTR(p_archive->path), "rb")) == NULL) {
1295
- rb_raise(Error, "Read archive failed: Cannot open archive");
1296
- }
1297
- #endif
1298
-
1299
- block_given = rb_block_given_p();
1300
-
1301
- while ((n = fread(buf, 1, sizeof(buf), fzip)) > 0) {
1302
- if (block_given) {
1303
- rb_yield(rb_str_new(buf, n));
1304
- } else {
1305
- if (NIL_P(retval)) {
1306
- retval = rb_str_new(buf, n);
1307
- } else {
1308
- rb_str_buf_cat(retval, buf, n);
1309
- }
1310
- }
1311
- }
1312
-
1313
- #ifdef RUBY_WIN32_H
1314
- #undef fclose
1315
- fclose(fzip);
1316
- #define fclose(f) rb_w32_fclose(f)
1317
- #else
1318
- fclose(fzip);
1319
- #endif
1320
-
1321
- if (n == -1) {
1322
- rb_raise(Error, "Read archive failed");
1323
- }
1324
-
1325
- return retval;
1326
- }
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 "tmpfile.h"
8
+ #include "ruby.h"
9
+ #ifndef RUBY_VM
10
+ #include "rubyio.h"
11
+ #endif
12
+
13
+ static VALUE zipruby_archive_alloc(VALUE klass);
14
+ static void zipruby_archive_mark(struct zipruby_archive *p);
15
+ static void zipruby_archive_free(struct zipruby_archive *p);
16
+ static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
17
+ static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self);
18
+ static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
19
+ static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password);
20
+ static VALUE zipruby_archive_close(VALUE self);
21
+ static VALUE zipruby_archive_num_files(VALUE self);
22
+ static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
23
+ static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
24
+ static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
25
+ static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
26
+ static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
27
+ static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io);
28
+ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
29
+ static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self);
30
+ static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self);
31
+ static VALUE zipruby_archive_replace_io(int argc, VALUE* argv, VALUE self);
32
+ static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
33
+ static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self);
34
+ static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
35
+ static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self);
36
+ static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
37
+ static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self);
38
+ static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
39
+ static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
40
+ static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self);
41
+ static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self);
42
+ static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment);
43
+ static VALUE zipruby_archive_fdelete(VALUE self, VALUE index);
44
+ static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name);
45
+ static VALUE zipruby_archive_funchange(VALUE self, VALUE index);
46
+ static VALUE zipruby_archive_funchange_all(VALUE self);
47
+ static VALUE zipruby_archive_unchange(VALUE self);
48
+ static VALUE zipruby_archive_revert(VALUE self);
49
+ static VALUE zipruby_archive_each(VALUE self);
50
+ static VALUE zipruby_archive_commit(VALUE self);
51
+ static VALUE zipruby_archive_is_open(VALUE self);
52
+ static VALUE zipruby_archive_decrypt(VALUE self, VALUE password);
53
+ static VALUE zipruby_archive_encrypt(VALUE self, VALUE password);
54
+ static VALUE zipruby_archive_read(VALUE self);
55
+
56
+ extern VALUE Zip;
57
+ VALUE Archive;
58
+ extern VALUE File;
59
+ extern VALUE Stat;
60
+ extern VALUE Error;
61
+
62
+ void Init_zipruby_archive() {
63
+ Archive = rb_define_class_under(Zip, "Archive", rb_cObject);
64
+ rb_define_alloc_func(Archive, zipruby_archive_alloc);
65
+ rb_include_module(Archive, rb_mEnumerable);
66
+ rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
67
+ rb_define_singleton_method(Archive, "open_buffer", zipruby_archive_s_open_buffer, -1);
68
+ rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
69
+ rb_define_singleton_method(Archive, "encrypt", zipruby_archive_s_encrypt, 2);
70
+ rb_define_method(Archive, "close", zipruby_archive_close, 0);
71
+ rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
72
+ rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
73
+ rb_define_method(Archive, "fopen", zipruby_archive_fopen, -1);
74
+ rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
75
+ rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
76
+ rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
77
+ rb_define_method(Archive, "add_io", zipruby_archive_add_io, 2);
78
+ rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
79
+ rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, -1);
80
+ rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, -1);
81
+ rb_define_method(Archive, "replace_io", zipruby_archive_replace_io, -1);
82
+ rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
83
+ rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, -1);
84
+ rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
85
+ rb_define_method(Archive, "add_or_replace_io", zipruby_archive_add_or_replace_io, -1);
86
+ rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
87
+ rb_define_method(Archive, "update", zipruby_archive_update, -1);
88
+ rb_define_method(Archive, "<<", zipruby_archive_add_io, 2);
89
+ rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
90
+ rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
91
+ rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
92
+ rb_define_method(Archive, "locate_name", zipruby_archive_locate_name, -1);
93
+ rb_define_method(Archive, "get_fcomment", zipruby_archive_get_fcomment, -1);
94
+ rb_define_method(Archive, "set_fcomment", zipruby_archive_set_fcomment, 2);
95
+ rb_define_method(Archive, "fdelete", zipruby_archive_fdelete, 1);
96
+ rb_define_method(Archive, "frename", zipruby_archive_frename, 2);
97
+ rb_define_method(Archive, "funchange", zipruby_archive_funchange, 1);
98
+ rb_define_method(Archive, "funchange_all", zipruby_archive_funchange_all, 0);
99
+ rb_define_method(Archive, "unchange", zipruby_archive_unchange, 0);
100
+ rb_define_method(Archive, "frevert", zipruby_archive_unchange, 1);
101
+ rb_define_method(Archive, "revert", zipruby_archive_revert, 0);
102
+ rb_define_method(Archive, "each", zipruby_archive_each, 0);
103
+ rb_define_method(Archive, "commit", zipruby_archive_commit, 0);
104
+ rb_define_method(Archive, "open?", zipruby_archive_is_open, 0);
105
+ rb_define_method(Archive, "decrypt", zipruby_archive_decrypt, 1);
106
+ rb_define_method(Archive, "encrypt", zipruby_archive_encrypt, 1);
107
+ rb_define_method(Archive, "read", zipruby_archive_read, 0);
108
+ }
109
+
110
+ static VALUE zipruby_archive_alloc(VALUE klass) {
111
+ struct zipruby_archive *p = ALLOC(struct zipruby_archive);
112
+
113
+ p->archive = NULL;
114
+ p->path = Qnil;
115
+ p->flags = 0;
116
+ p->tmpfilnam = NULL;
117
+ p->buffer = Qnil;
118
+
119
+ return Data_Wrap_Struct(klass, zipruby_archive_mark, zipruby_archive_free, p);
120
+ }
121
+
122
+ static void zipruby_archive_mark(struct zipruby_archive *p) {
123
+ rb_gc_mark(p->path);
124
+ rb_gc_mark(p->buffer);
125
+ }
126
+
127
+ static void zipruby_archive_free(struct zipruby_archive *p) {
128
+ if (p->tmpfilnam) {
129
+ zipruby_rmtmp(p->tmpfilnam);
130
+ free(p->tmpfilnam);
131
+ }
132
+
133
+ xfree(p);
134
+ }
135
+
136
+ /* */
137
+ static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
138
+ VALUE path, flags;
139
+ VALUE archive;
140
+ struct zipruby_archive *p_archive;
141
+ int i_flags = 0;
142
+ int errorp;
143
+
144
+ rb_scan_args(argc, argv, "11", &path, &flags);
145
+ Check_Type(path, T_STRING);
146
+
147
+ if (!NIL_P(flags)) {
148
+ i_flags = NUM2INT(flags);
149
+ }
150
+
151
+ archive = rb_funcall(Archive, rb_intern("new"), 0);
152
+ Data_Get_Struct(archive, struct zipruby_archive, p_archive);
153
+
154
+ if ((p_archive->archive = zip_open(RSTRING_PTR(path), i_flags, &errorp)) == NULL) {
155
+ char errstr[ERRSTR_BUFSIZE];
156
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
157
+ rb_raise(Error, "Open archive failed - %s: %s", RSTRING_PTR(path), errstr);
158
+ }
159
+
160
+ p_archive->path = path;
161
+ p_archive->flags = i_flags;
162
+
163
+ if (rb_block_given_p()) {
164
+ VALUE retval;
165
+ int status;
166
+
167
+ retval = rb_protect(rb_yield, archive, &status);
168
+ zipruby_archive_close(archive);
169
+
170
+ if (status != 0) {
171
+ rb_jump_tag(status);
172
+ }
173
+
174
+ return retval;
175
+ } else {
176
+ return archive;
177
+ }
178
+ }
179
+
180
+ /* */
181
+ static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
182
+ VALUE buffer, flags;
183
+ VALUE archive;
184
+ struct zipruby_archive *p_archive;
185
+ void *data = NULL;
186
+ int len = 0, i_flags = 0;
187
+ int errorp;
188
+
189
+ rb_scan_args(argc, argv, "02", &buffer, &flags);
190
+
191
+ if (FIXNUM_P(buffer) && NIL_P(flags)) {
192
+ flags = buffer;
193
+ buffer = Qnil;
194
+ }
195
+
196
+ if (!NIL_P(flags)) {
197
+ i_flags = NUM2INT(flags);
198
+ }
199
+
200
+ if (i_flags & ZIP_CREATE) {
201
+ if (!NIL_P(buffer)) {
202
+ Check_Type(buffer, T_STRING);
203
+ }
204
+
205
+ i_flags = (i_flags | ZIP_TRUNC);
206
+ } else if (TYPE(buffer) == T_STRING) {
207
+ data = RSTRING_PTR(buffer);
208
+ len = RSTRING_LEN(buffer);
209
+ } else if (rb_obj_is_instance_of(buffer, rb_cProc)) {
210
+ data = (void *) buffer;
211
+ len = -1;
212
+ } else {
213
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Proc)", rb_class2name(CLASS_OF(buffer)));
214
+ }
215
+
216
+ archive = rb_funcall(Archive, rb_intern("new"), 0);
217
+ Data_Get_Struct(archive, struct zipruby_archive, p_archive);
218
+
219
+ if ((p_archive->tmpfilnam = zipruby_tmpnam(data, len)) == NULL) {
220
+ rb_raise(Error, "Open archive failed: Failed to create temporary file");
221
+ }
222
+
223
+ if ((p_archive->archive = zip_open(p_archive->tmpfilnam, i_flags, &errorp)) == NULL) {
224
+ char errstr[ERRSTR_BUFSIZE];
225
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
226
+ rb_raise(Error, "Open archive failed: %s", errstr);
227
+ }
228
+
229
+ p_archive->path = rb_str_new2(p_archive->tmpfilnam);
230
+ p_archive->flags = i_flags;
231
+ p_archive->buffer = buffer;
232
+
233
+ if (rb_block_given_p()) {
234
+ VALUE retval;
235
+ int status;
236
+
237
+ retval = rb_protect(rb_yield, archive, &status);
238
+ zipruby_archive_close(archive);
239
+
240
+ if (status != 0) {
241
+ rb_jump_tag(status);
242
+ }
243
+
244
+ return retval;
245
+ } else {
246
+ return archive;
247
+ }
248
+ }
249
+
250
+ /* */
251
+ static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
252
+ int res;
253
+ int errorp, wrongpwd;
254
+ long pwdlen;
255
+
256
+ Check_Type(path, T_STRING);
257
+ Check_Type(password, T_STRING);
258
+ pwdlen = RSTRING_LEN(password);
259
+
260
+ if (pwdlen < 1) {
261
+ rb_raise(Error, "Decrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
262
+ } else if (pwdlen > 0xff) {
263
+ rb_raise(Error, "Decrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
264
+ }
265
+
266
+ res = zip_decrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp, &wrongpwd);
267
+
268
+ if (res == -1) {
269
+ if (wrongpwd) {
270
+ rb_raise(Error, "Decrypt archive failed - %s: Wrong password", RSTRING_PTR(path));
271
+ } else {
272
+ char errstr[ERRSTR_BUFSIZE];
273
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
274
+ rb_raise(Error, "Decrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
275
+ }
276
+ }
277
+
278
+ return (res > 0) ? Qtrue : Qfalse;
279
+ }
280
+
281
+ /* */
282
+ static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
283
+ int res;
284
+ int errorp;
285
+ long pwdlen;
286
+
287
+ Check_Type(path, T_STRING);
288
+ Check_Type(password, T_STRING);
289
+ pwdlen = RSTRING_LEN(password);
290
+
291
+ if (pwdlen < 1) {
292
+ rb_raise(Error, "Encrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
293
+ } else if (pwdlen > 0xff) {
294
+ rb_raise(Error, "Encrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
295
+ }
296
+
297
+ res = zip_encrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp);
298
+
299
+ if (res == -1) {
300
+ char errstr[ERRSTR_BUFSIZE];
301
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
302
+ rb_raise(Error, "Encrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
303
+ }
304
+
305
+ return (res > 0) ? Qtrue : Qfalse;
306
+ }
307
+
308
+ /* */
309
+ static VALUE zipruby_archive_close(VALUE self) {
310
+ struct zipruby_archive *p_archive;
311
+ int changed, survivors;
312
+
313
+ if (!zipruby_archive_is_open(self)) {
314
+ return Qfalse;
315
+ }
316
+
317
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
318
+ Check_Archive(p_archive);
319
+
320
+ changed = _zip_changed(p_archive->archive, &survivors);
321
+
322
+ if (zip_close(p_archive->archive) == -1) {
323
+ zip_unchange_all(p_archive->archive);
324
+ zip_unchange_archive(p_archive->archive);
325
+ rb_raise(Error, "Close archive failed: %s", zip_strerror(p_archive->archive));
326
+ }
327
+
328
+ if (!NIL_P(p_archive->buffer) && changed) {
329
+ rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
330
+ }
331
+
332
+ zipruby_rmtmp(p_archive->tmpfilnam);
333
+ p_archive->archive = NULL;
334
+ p_archive->flags = 0;
335
+
336
+ return Qtrue;
337
+ }
338
+
339
+ /* */
340
+ static VALUE zipruby_archive_num_files(VALUE self) {
341
+ struct zipruby_archive *p_archive;
342
+ int num_files;
343
+
344
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
345
+ Check_Archive(p_archive);
346
+ num_files = zip_get_num_files(p_archive->archive);
347
+
348
+ return INT2NUM(num_files);
349
+ }
350
+
351
+ /* */
352
+ static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self) {
353
+ VALUE index, flags;
354
+ struct zipruby_archive *p_archive;
355
+ int i_flags = 0;
356
+ const char *name;
357
+
358
+ rb_scan_args(argc, argv, "11", &index, &flags);
359
+ Check_Type(index, T_FIXNUM);
360
+
361
+ if (!NIL_P(flags)) {
362
+ i_flags = NUM2INT(flags);
363
+ }
364
+
365
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
366
+ Check_Archive(p_archive);
367
+
368
+ if ((name = zip_get_name(p_archive->archive, NUM2INT(index), i_flags)) == NULL) {
369
+ rb_raise(Error, "Get name failed at %d: %s", index, zip_strerror(p_archive->archive));
370
+ }
371
+
372
+ return (name != NULL) ? rb_str_new2(name) : Qnil;
373
+ }
374
+
375
+ /* */
376
+ static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self) {
377
+ VALUE index, flags, stat_flags, file;
378
+
379
+ rb_scan_args(argc, argv, "12", &index, &flags, &stat_flags);
380
+ file = rb_funcall(File, rb_intern("new"), 4, self, index, flags, stat_flags);
381
+
382
+ if (rb_block_given_p()) {
383
+ VALUE retval;
384
+ int status;
385
+
386
+ retval = rb_protect(rb_yield, file, &status);
387
+ rb_funcall(file, rb_intern("close"), 0);
388
+
389
+ if (status != 0) {
390
+ rb_jump_tag(status);
391
+ }
392
+
393
+ return retval;
394
+ } else {
395
+ return file;
396
+ }
397
+ }
398
+
399
+ /* */
400
+ static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self) {
401
+ VALUE index, flags;
402
+
403
+ rb_scan_args(argc, argv, "11", &index, &flags);
404
+
405
+ return rb_funcall(Stat, rb_intern("new"), 3, self, index, flags);
406
+ }
407
+
408
+ /* */
409
+ static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
410
+ struct zipruby_archive *p_archive;
411
+ struct zip_source *zsource;
412
+ char *data;
413
+ size_t len;
414
+
415
+ Check_Type(name, T_STRING);
416
+ Check_Type(source, T_STRING);
417
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
418
+ Check_Archive(p_archive);
419
+
420
+ len = RSTRING_LEN(source);
421
+
422
+ if ((data = malloc(len)) == NULL) {
423
+ rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
424
+ }
425
+
426
+ memset(data, 0, len);
427
+ memcpy(data, RSTRING_PTR(source), len);
428
+
429
+ if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
430
+ free(data);
431
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
432
+ }
433
+
434
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
435
+ zip_source_free(zsource);
436
+ zip_unchange_all(p_archive->archive);
437
+ zip_unchange_archive(p_archive->archive);
438
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
439
+ }
440
+
441
+ return Qnil;
442
+ }
443
+
444
+ /* */
445
+ static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
446
+ struct zipruby_archive *p_archive;
447
+ struct zip_source *zsource;
448
+ VALUE index, source, flags;
449
+ int i_index, i_flags = 0;
450
+ char *data;
451
+ size_t len;
452
+
453
+ rb_scan_args(argc, argv, "21", &index, &source, &flags);
454
+
455
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
456
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
457
+ }
458
+
459
+ if (!NIL_P(flags)) {
460
+ i_flags = NUM2INT(flags);
461
+ }
462
+
463
+ Check_Type(source, T_STRING);
464
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
465
+ Check_Archive(p_archive);
466
+
467
+ if (FIXNUM_P(index)) {
468
+ i_index = NUM2INT(index);
469
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
470
+ rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
471
+ }
472
+
473
+ len = RSTRING_LEN(source);
474
+
475
+ if ((data = malloc(len)) == NULL) {
476
+ rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
477
+ }
478
+
479
+ memcpy(data, RSTRING_PTR(source), len);
480
+
481
+ if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
482
+ free(data);
483
+ rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
484
+ }
485
+
486
+ if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
487
+ zip_source_free(zsource);
488
+ zip_unchange_all(p_archive->archive);
489
+ zip_unchange_archive(p_archive->archive);
490
+ rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
491
+ }
492
+
493
+ return Qnil;
494
+ }
495
+
496
+ /* */
497
+ static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self) {
498
+ struct zipruby_archive *p_archive;
499
+ VALUE name, source, flags;
500
+ int index, i_flags = 0;
501
+
502
+ rb_scan_args(argc, argv, "21", &name, &source, &flags);
503
+
504
+ if (!NIL_P(flags)) {
505
+ i_flags = NUM2INT(flags);
506
+ }
507
+
508
+ Check_Type(name, T_STRING);
509
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
510
+ Check_Archive(p_archive);
511
+
512
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
513
+
514
+ if (index >= 0) {
515
+ VALUE _args[] = { INT2NUM(index), source };
516
+ return zipruby_archive_replace_buffer(2, _args, self);
517
+ } else {
518
+ return zipruby_archive_add_buffer(self, name, source);
519
+ }
520
+ }
521
+
522
+ /* */
523
+ static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
524
+ VALUE name, fname;
525
+ struct zipruby_archive *p_archive;
526
+ struct zip_source *zsource;
527
+
528
+ rb_scan_args(argc, argv, "11", &name, &fname);
529
+
530
+ if (NIL_P(fname)) {
531
+ fname = name;
532
+ name = Qnil;
533
+ }
534
+
535
+ Check_Type(fname, T_STRING);
536
+
537
+ if (NIL_P(name)) {
538
+ name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
539
+ }
540
+
541
+ Check_Type(name, T_STRING);
542
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
543
+ Check_Archive(p_archive);
544
+
545
+ if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
546
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
547
+ }
548
+
549
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
550
+ zip_source_free(zsource);
551
+ zip_unchange_all(p_archive->archive);
552
+ zip_unchange_archive(p_archive->archive);
553
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
554
+ }
555
+
556
+ return Qnil;
557
+ }
558
+
559
+ /* */
560
+ static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self) {
561
+ struct zipruby_archive *p_archive;
562
+ struct zip_source *zsource;
563
+ VALUE index, fname, flags;
564
+ int i_index, i_flags = 0;
565
+
566
+ rb_scan_args(argc, argv, "21", &index, &fname, &flags);
567
+
568
+ if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
569
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
570
+ }
571
+
572
+ if (!NIL_P(flags)) {
573
+ i_flags = NUM2INT(flags);
574
+ }
575
+
576
+ Check_Type(fname, T_STRING);
577
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
578
+ Check_Archive(p_archive);
579
+
580
+ if (FIXNUM_P(index)) {
581
+ i_index = NUM2INT(index);
582
+ } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
583
+ rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
584
+ }
585
+
586
+ if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
587
+ rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
588
+ }
589
+
590
+ if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
591
+ zip_source_free(zsource);
592
+ zip_unchange_all(p_archive->archive);
593
+ zip_unchange_archive(p_archive->archive);
594
+ rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
595
+ }
596
+
597
+ return Qnil;
598
+ }
599
+
600
+ /* */
601
+ static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self) {
602
+ VALUE name, fname, flags;
603
+ struct zipruby_archive *p_archive;
604
+ int index, i_flags = 0;
605
+
606
+ rb_scan_args(argc, argv, "12", &name, &fname, &flags);
607
+
608
+ if (NIL_P(flags) && FIXNUM_P(fname)) {
609
+ flags = fname;
610
+ fname = Qnil;
611
+ }
612
+
613
+ if (NIL_P(fname)) {
614
+ fname = name;
615
+ name = Qnil;
616
+ }
617
+
618
+ Check_Type(fname, T_STRING);
619
+
620
+ if (NIL_P(name)) {
621
+ name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
622
+ }
623
+
624
+ if (!NIL_P(flags)) {
625
+ i_flags = NUM2INT(flags);
626
+ }
627
+
628
+ Check_Type(name, T_STRING);
629
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
630
+ Check_Archive(p_archive);
631
+
632
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
633
+
634
+ if (index >= 0) {
635
+ VALUE _args[] = { INT2NUM(index), fname };
636
+ return zipruby_archive_replace_file(2, _args, self);
637
+ } else {
638
+ VALUE _args[] = { name, fname };
639
+ return zipruby_archive_add_file(2, _args, self);
640
+ }
641
+ }
642
+
643
+ /* */
644
+ static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io) {
645
+ VALUE source;
646
+
647
+ Check_IO(io);
648
+ source = rb_funcall(io, rb_intern("read"), 0);
649
+
650
+ return zipruby_archive_add_buffer(self, name, source);
651
+ }
652
+
653
+ /* */
654
+ static VALUE zipruby_archive_replace_io(int argc, VALUE *argv, VALUE self) {
655
+ VALUE source, io, index, flags;
656
+ VALUE _args[3];
657
+
658
+ rb_scan_args(argc, argv, "21", &index, &io, &flags);
659
+ Check_IO(io);
660
+ source = rb_funcall(io, rb_intern("read"), 0);
661
+
662
+ _args[0] = index;
663
+ _args[1] = source;
664
+ _args[2] = flags;
665
+ return zipruby_archive_replace_buffer(2, _args, self);
666
+ }
667
+
668
+ /* */
669
+ static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self) {
670
+ VALUE name, io, flags;
671
+ struct zipruby_archive *p_archive;
672
+ int index, i_flags = 0;
673
+
674
+ rb_scan_args(argc, argv, "21", &name, &io, &flags);
675
+ Check_IO(io);
676
+
677
+ if (!NIL_P(flags)) {
678
+ i_flags = NUM2INT(flags);
679
+ }
680
+
681
+ Check_Type(name, T_STRING);
682
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
683
+ Check_Archive(p_archive);
684
+
685
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
686
+
687
+ if (index >= 0) {
688
+ VALUE _args[] = {INT2NUM(index), io, flags};
689
+ return zipruby_archive_replace_io(2, _args, self);
690
+ } else {
691
+ return zipruby_archive_add_io(self, name, io);
692
+ }
693
+ }
694
+
695
+ /* */
696
+ static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
697
+ VALUE name, mtime;
698
+ struct zipruby_archive *p_archive;
699
+ struct zip_source *zsource;
700
+ struct read_proc *z;
701
+
702
+ rb_scan_args(argc, argv, "11", &name, &mtime);
703
+ rb_need_block();
704
+ Check_Type(name, T_STRING);
705
+
706
+ if (NIL_P(mtime)) {
707
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
708
+ } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
709
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
710
+ }
711
+
712
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
713
+ Check_Archive(p_archive);
714
+
715
+ if ((z = malloc(sizeof(struct read_proc))) == NULL) {
716
+ zip_unchange_all(p_archive->archive);
717
+ zip_unchange_archive(p_archive->archive);
718
+ rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", RSTRING_PTR(name));
719
+ }
720
+
721
+ z->proc = rb_block_proc();
722
+ z->mtime = mtime;
723
+
724
+ if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
725
+ free(z);
726
+ rb_raise(Error, "Add failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
727
+ }
728
+
729
+ if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
730
+ zip_source_free(zsource);
731
+ zip_unchange_all(p_archive->archive);
732
+ zip_unchange_archive(p_archive->archive);
733
+ rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
734
+ }
735
+
736
+ return Qnil;
737
+ }
738
+
739
+ /* */
740
+ static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
741
+ VALUE index, mtime;
742
+ struct zipruby_archive *p_archive;
743
+ struct zip_source *zsource;
744
+ struct read_proc *z;
745
+
746
+ rb_scan_args(argc, argv, "11", &index, &mtime);
747
+ rb_need_block();
748
+ Check_Type(index, T_FIXNUM);
749
+
750
+ if (NIL_P(mtime)) {
751
+ mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
752
+ } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
753
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
754
+ }
755
+
756
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
757
+ Check_Archive(p_archive);
758
+
759
+ if ((z = malloc(sizeof(struct read_proc))) == NULL) {
760
+ zip_unchange_all(p_archive->archive);
761
+ zip_unchange_archive(p_archive->archive);
762
+ rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
763
+ }
764
+
765
+ z->proc = rb_block_proc();
766
+ z->mtime = mtime;
767
+
768
+ if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
769
+ free(z);
770
+ rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
771
+ }
772
+
773
+ if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
774
+ zip_source_free(zsource);
775
+ zip_unchange_all(p_archive->archive);
776
+ zip_unchange_archive(p_archive->archive);
777
+ rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
778
+ }
779
+
780
+ return Qnil;
781
+ }
782
+
783
+ /* */
784
+ static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
785
+ VALUE name, mtime, flags;
786
+ struct zipruby_archive *p_archive;
787
+ int index, i_flags = 0;
788
+
789
+ rb_scan_args(argc, argv, "12", &name, &mtime, &flags);
790
+
791
+ if (NIL_P(flags) && FIXNUM_P(mtime)) {
792
+ flags = mtime;
793
+ mtime = Qnil;
794
+ }
795
+
796
+ if (!NIL_P(flags)) {
797
+ i_flags = NUM2INT(flags);
798
+ }
799
+
800
+ Check_Type(name, T_STRING);
801
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
802
+ Check_Archive(p_archive);
803
+
804
+ index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
805
+
806
+ if (index >= 0) {
807
+ VALUE _args[] = { INT2NUM(index), mtime };
808
+ return zipruby_archive_replace_function(2, _args, self);
809
+ } else {
810
+ VALUE _args[] = { name, mtime };
811
+ return zipruby_archive_add_function(2, _args, self);
812
+ }
813
+ }
814
+
815
+ /* */
816
+ static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self) {
817
+ struct zipruby_archive *p_archive, *p_srcarchive;
818
+ VALUE srcarchive, flags;
819
+ int i, num_files, i_flags = 0;
820
+
821
+ rb_scan_args(argc, argv, "11", &srcarchive, &flags);
822
+
823
+ if (!rb_obj_is_instance_of(srcarchive, Archive)) {
824
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
825
+ }
826
+
827
+ if (!NIL_P(flags)) {
828
+ i_flags = NUM2INT(flags);
829
+ }
830
+
831
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
832
+ Check_Archive(p_archive);
833
+ Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
834
+ Check_Archive(p_srcarchive);
835
+
836
+ num_files = zip_get_num_files(p_srcarchive->archive);
837
+
838
+ for (i = 0; i < num_files; i++) {
839
+ struct zip_source *zsource;
840
+ struct zip_file *fzip;
841
+ struct zip_stat sb;
842
+ char *buf;
843
+ const char *name;
844
+ int index, error;
845
+
846
+ zip_stat_init(&sb);
847
+
848
+ if (zip_stat_index(p_srcarchive->archive, i, 0, &sb)) {
849
+ zip_unchange_all(p_archive->archive);
850
+ zip_unchange_archive(p_archive->archive);
851
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
852
+ }
853
+
854
+ if ((buf = malloc(sb.size)) == NULL) {
855
+ zip_unchange_all(p_archive->archive);
856
+ zip_unchange_archive(p_archive->archive);
857
+ rb_raise(rb_eRuntimeError, "Update archive failed: Cannot allocate memory");
858
+ }
859
+
860
+ fzip = zip_fopen_index(p_srcarchive->archive, i, 0);
861
+
862
+ if (fzip == NULL) {
863
+ free(buf);
864
+ zip_unchange_all(p_archive->archive);
865
+ zip_unchange_archive(p_archive->archive);
866
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
867
+ }
868
+
869
+ if (zip_fread(fzip, buf, sb.size) == -1) {
870
+ free(buf);
871
+ zip_fclose(fzip);
872
+ zip_unchange_all(p_archive->archive);
873
+ zip_unchange_archive(p_archive->archive);
874
+ rb_raise(Error, "Update archive failed: %s", zip_file_strerror(fzip));
875
+ }
876
+
877
+ if ((error = zip_fclose(fzip)) != 0) {
878
+ char errstr[ERRSTR_BUFSIZE];
879
+ free(buf);
880
+ zip_unchange_all(p_archive->archive);
881
+ zip_unchange_archive(p_archive->archive);
882
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
883
+ rb_raise(Error, "Update archive failed: %s", errstr);
884
+ }
885
+
886
+ if ((zsource = zip_source_buffer(p_archive->archive, buf, sb.size, 1)) == NULL) {
887
+ free(buf);
888
+ zip_unchange_all(p_archive->archive);
889
+ zip_unchange_archive(p_archive->archive);
890
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
891
+ }
892
+
893
+ if ((name = zip_get_name(p_srcarchive->archive, i, 0)) == NULL) {
894
+ zip_source_free(zsource);
895
+ zip_unchange_all(p_archive->archive);
896
+ zip_unchange_archive(p_archive->archive);
897
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
898
+ }
899
+
900
+ index = zip_name_locate(p_archive->archive, name, i_flags);
901
+
902
+ if (index >= 0) {
903
+ if (zip_replace(p_archive->archive, i, zsource) == -1) {
904
+ zip_source_free(zsource);
905
+ zip_unchange_all(p_archive->archive);
906
+ zip_unchange_archive(p_archive->archive);
907
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
908
+ }
909
+ } else {
910
+ if (zip_add(p_archive->archive, name, zsource) == -1) {
911
+ zip_source_free(zsource);
912
+ zip_unchange_all(p_archive->archive);
913
+ zip_unchange_archive(p_archive->archive);
914
+ rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
915
+ }
916
+ }
917
+ }
918
+
919
+ return Qnil;
920
+ }
921
+
922
+ /* */
923
+ static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self) {
924
+ VALUE flags;
925
+ struct zipruby_archive *p_archive;
926
+ const char *comment;
927
+ int lenp, i_flags = 0;
928
+
929
+ rb_scan_args(argc, argv, "01", &flags);
930
+
931
+ if (!NIL_P(flags)) {
932
+ i_flags = NUM2INT(flags);
933
+ }
934
+
935
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
936
+ Check_Archive(p_archive);
937
+
938
+ // XXX: How is the error checked?
939
+ comment = zip_get_archive_comment(p_archive->archive, &lenp, i_flags);
940
+
941
+ return comment ? rb_str_new(comment, lenp) : Qnil;
942
+ }
943
+
944
+ /* */
945
+ static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
946
+ struct zipruby_archive *p_archive;
947
+ const char *s_comment = NULL;
948
+ int len = 0;
949
+
950
+ if (!NIL_P(comment)) {
951
+ Check_Type(comment, T_STRING);
952
+ s_comment = RSTRING_PTR(comment);
953
+ len = RSTRING_LEN(comment);
954
+ }
955
+
956
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
957
+ Check_Archive(p_archive);
958
+
959
+ if (zip_set_archive_comment(p_archive->archive, s_comment, len) == -1) {
960
+ zip_unchange_all(p_archive->archive);
961
+ zip_unchange_archive(p_archive->archive);
962
+ rb_raise(Error, "Comment archived failed: %s", zip_strerror(p_archive->archive));
963
+ }
964
+
965
+ return Qnil;
966
+ }
967
+
968
+ /* */
969
+ static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
970
+ VALUE fname, flags;
971
+ struct zipruby_archive *p_archive;
972
+ int i_flags = 0;
973
+
974
+ rb_scan_args(argc, argv, "11", &fname, &flags);
975
+ Check_Type(fname, T_STRING);
976
+
977
+ if (!NIL_P(flags)) {
978
+ i_flags = NUM2INT(flags);
979
+ }
980
+
981
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
982
+ Check_Archive(p_archive);
983
+
984
+ return INT2NUM(zip_name_locate(p_archive->archive, RSTRING_PTR(fname), i_flags));
985
+ }
986
+
987
+ /* */
988
+ static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self) {
989
+ VALUE index, flags;
990
+ struct zipruby_archive *p_archive;
991
+ const char *comment;
992
+ int lenp, i_flags = 0;
993
+
994
+ rb_scan_args(argc, argv, "11", &index, &flags);
995
+
996
+ if (!NIL_P(flags)) {
997
+ i_flags = NUM2INT(flags);
998
+ }
999
+
1000
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1001
+ Check_Archive(p_archive);
1002
+
1003
+ // XXX: How is the error checked?
1004
+ comment = zip_get_file_comment(p_archive->archive, NUM2INT(index), &lenp, i_flags);
1005
+
1006
+ return comment ? rb_str_new(comment, lenp) : Qnil;
1007
+ }
1008
+
1009
+ /* */
1010
+ static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment) {
1011
+ struct zipruby_archive *p_archive;
1012
+ char *s_comment = NULL;
1013
+ int len = 0;
1014
+
1015
+ if (!NIL_P(comment)) {
1016
+ Check_Type(comment, T_STRING);
1017
+ s_comment = RSTRING_PTR(comment);
1018
+ len = RSTRING_LEN(comment);
1019
+ }
1020
+
1021
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1022
+ Check_Archive(p_archive);
1023
+
1024
+ if (zip_set_file_comment(p_archive->archive, NUM2INT(index), s_comment, len) == -1) {
1025
+ zip_unchange_all(p_archive->archive);
1026
+ zip_unchange_archive(p_archive->archive);
1027
+ rb_raise(Error, "Comment file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1028
+ }
1029
+
1030
+ return Qnil;
1031
+ }
1032
+
1033
+ /* */
1034
+ static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
1035
+ struct zipruby_archive *p_archive;
1036
+
1037
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1038
+ Check_Archive(p_archive);
1039
+
1040
+ if (zip_delete(p_archive->archive, NUM2INT(index)) == -1) {
1041
+ zip_unchange_all(p_archive->archive);
1042
+ zip_unchange_archive(p_archive->archive);
1043
+ rb_raise(Error, "Delete file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1044
+ }
1045
+
1046
+ return Qnil;
1047
+ }
1048
+
1049
+ /* */
1050
+ static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
1051
+ struct zipruby_archive *p_archive;
1052
+
1053
+ Check_Type(name, T_STRING);
1054
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1055
+ Check_Archive(p_archive);
1056
+
1057
+ if (zip_rename(p_archive->archive, NUM2INT(index), RSTRING_PTR(name)) == -1) {
1058
+ zip_unchange_all(p_archive->archive);
1059
+ zip_unchange_archive(p_archive->archive);
1060
+ rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1061
+ }
1062
+
1063
+ return Qnil;
1064
+ }
1065
+
1066
+ /* */
1067
+ static VALUE zipruby_archive_funchange(VALUE self, VALUE index) {
1068
+ struct zipruby_archive *p_archive;
1069
+
1070
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1071
+ Check_Archive(p_archive);
1072
+
1073
+ if (zip_unchange(p_archive->archive, NUM2INT(index)) == -1) {
1074
+ zip_unchange_all(p_archive->archive);
1075
+ zip_unchange_archive(p_archive->archive);
1076
+ rb_raise(Error, "Unchange file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
1077
+ }
1078
+
1079
+ return Qnil;
1080
+ }
1081
+
1082
+ /* */
1083
+ static VALUE zipruby_archive_funchange_all(VALUE self) {
1084
+ struct zipruby_archive *p_archive;
1085
+
1086
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1087
+ Check_Archive(p_archive);
1088
+
1089
+ if (zip_unchange_all(p_archive->archive) == -1) {
1090
+ rb_raise(Error, "Unchange all file failed: %s", zip_strerror(p_archive->archive));
1091
+ }
1092
+
1093
+ return Qnil;
1094
+ }
1095
+
1096
+ /* */
1097
+ static VALUE zipruby_archive_unchange(VALUE self) {
1098
+ struct zipruby_archive *p_archive;
1099
+
1100
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1101
+ Check_Archive(p_archive);
1102
+
1103
+ if (zip_unchange_archive(p_archive->archive) == -1) {
1104
+ rb_raise(Error, "Unchange archive failed: %s", zip_strerror(p_archive->archive));
1105
+ }
1106
+
1107
+ return Qnil;
1108
+ }
1109
+
1110
+ /* */
1111
+ static VALUE zipruby_archive_revert(VALUE self) {
1112
+ zipruby_archive_funchange_all(self);
1113
+ zipruby_archive_unchange(self);
1114
+
1115
+ return Qnil;
1116
+ }
1117
+
1118
+ /* */
1119
+ static VALUE zipruby_archive_each(VALUE self) {
1120
+ struct zipruby_archive *p_archive;
1121
+ int i, num_files;
1122
+
1123
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1124
+ Check_Archive(p_archive);
1125
+ num_files = zip_get_num_files(p_archive->archive);
1126
+
1127
+ for (i = 0; i < num_files; i++) {
1128
+ VALUE file;
1129
+ int status;
1130
+
1131
+ file = rb_funcall(File, rb_intern("new"), 2, self, INT2NUM(i));
1132
+ rb_protect(rb_yield, file, &status);
1133
+ rb_funcall(file, rb_intern("close"), 0);
1134
+
1135
+ if (status != 0) {
1136
+ rb_jump_tag(status);
1137
+ }
1138
+ }
1139
+
1140
+ return Qnil;
1141
+ }
1142
+
1143
+ /* */
1144
+ static VALUE zipruby_archive_commit(VALUE self) {
1145
+ struct zipruby_archive *p_archive;
1146
+ int changed, survivors;
1147
+ int errorp;
1148
+
1149
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1150
+ Check_Archive(p_archive);
1151
+
1152
+ changed = _zip_changed(p_archive->archive, &survivors);
1153
+
1154
+ if (zip_close(p_archive->archive) == -1) {
1155
+ zip_unchange_all(p_archive->archive);
1156
+ zip_unchange_archive(p_archive->archive);
1157
+ rb_raise(Error, "Commit archive failed: %s", zip_strerror(p_archive->archive));
1158
+ }
1159
+
1160
+ if (!NIL_P(p_archive->buffer) && changed) {
1161
+ rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1162
+ }
1163
+
1164
+ p_archive->archive = NULL;
1165
+ p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1166
+
1167
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1168
+ char errstr[ERRSTR_BUFSIZE];
1169
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1170
+ rb_raise(Error, "Commit archive failed: %s", errstr);
1171
+ }
1172
+
1173
+ return Qnil;
1174
+ }
1175
+
1176
+ /* */
1177
+ static VALUE zipruby_archive_is_open(VALUE self) {
1178
+ struct zipruby_archive *p_archive;
1179
+
1180
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1181
+ return (p_archive->archive != NULL) ? Qtrue : Qfalse;
1182
+ }
1183
+
1184
+ /* */
1185
+ static VALUE zipruby_archive_decrypt(VALUE self, VALUE password) {
1186
+ VALUE retval;
1187
+ struct zipruby_archive *p_archive;
1188
+ long pwdlen;
1189
+ int changed, survivors;
1190
+ int errorp;
1191
+
1192
+ Check_Type(password, T_STRING);
1193
+ pwdlen = RSTRING_LEN(password);
1194
+
1195
+ if (pwdlen < 1) {
1196
+ rb_raise(Error, "Decrypt archive failed: Password is empty");
1197
+ } else if (pwdlen > 0xff) {
1198
+ rb_raise(Error, "Decrypt archive failed: Password is too long");
1199
+ }
1200
+
1201
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1202
+ Check_Archive(p_archive);
1203
+
1204
+ changed = _zip_changed(p_archive->archive, &survivors);
1205
+
1206
+ if (zip_close(p_archive->archive) == -1) {
1207
+ zip_unchange_all(p_archive->archive);
1208
+ zip_unchange_archive(p_archive->archive);
1209
+ rb_raise(Error, "Decrypt archive failed: %s", zip_strerror(p_archive->archive));
1210
+ }
1211
+
1212
+ if (!NIL_P(p_archive->buffer) && changed) {
1213
+ rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1214
+ }
1215
+
1216
+ p_archive->archive = NULL;
1217
+ p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1218
+
1219
+ retval = zipruby_archive_s_decrypt(Archive, p_archive->path, password);
1220
+
1221
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1222
+ char errstr[ERRSTR_BUFSIZE];
1223
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1224
+ rb_raise(Error, "Decrypt archive failed: %s", errstr);
1225
+ }
1226
+
1227
+ return retval;
1228
+ }
1229
+
1230
+ /* */
1231
+ static VALUE zipruby_archive_encrypt(VALUE self, VALUE password) {
1232
+ VALUE retval;
1233
+ struct zipruby_archive *p_archive;
1234
+ long pwdlen;
1235
+ int changed, survivors;
1236
+ int errorp;
1237
+
1238
+ Check_Type(password, T_STRING);
1239
+ pwdlen = RSTRING_LEN(password);
1240
+
1241
+ if (pwdlen < 1) {
1242
+ rb_raise(Error, "Encrypt archive failed: Password is empty");
1243
+ } else if (pwdlen > 0xff) {
1244
+ rb_raise(Error, "Encrypt archive failed: Password is too long");
1245
+ }
1246
+
1247
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1248
+ Check_Archive(p_archive);
1249
+
1250
+ changed = _zip_changed(p_archive->archive, &survivors);
1251
+
1252
+ if (zip_close(p_archive->archive) == -1) {
1253
+ zip_unchange_all(p_archive->archive);
1254
+ zip_unchange_archive(p_archive->archive);
1255
+ rb_raise(Error, "Encrypt archive failed: %s", zip_strerror(p_archive->archive));
1256
+ }
1257
+
1258
+ if (!NIL_P(p_archive->buffer) && changed) {
1259
+ rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
1260
+ }
1261
+
1262
+ p_archive->archive = NULL;
1263
+ p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
1264
+
1265
+ retval = zipruby_archive_s_encrypt(Archive, p_archive->path, password);
1266
+
1267
+ if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
1268
+ char errstr[ERRSTR_BUFSIZE];
1269
+ zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
1270
+ rb_raise(Error, "Encrypt archive failed: %s", errstr);
1271
+ }
1272
+
1273
+ return retval;
1274
+ }
1275
+
1276
+ /* */
1277
+ static VALUE zipruby_archive_read(VALUE self) {
1278
+ VALUE retval = Qnil;
1279
+ struct zipruby_archive *p_archive;
1280
+ FILE *fzip;
1281
+ char buf[DATA_BUFSIZE];
1282
+ ssize_t n;
1283
+ int block_given;
1284
+
1285
+ Data_Get_Struct(self, struct zipruby_archive, p_archive);
1286
+
1287
+ if (NIL_P(p_archive->path)) {
1288
+ rb_raise(rb_eRuntimeError, "invalid Zip::Archive");
1289
+ }
1290
+
1291
+ #ifdef _WIN32
1292
+ if (fopen_s(&fzip, RSTRING_PTR(p_archive->path), "rb") != 0) {
1293
+ rb_raise(Error, "Read archive failed: Cannot open archive");
1294
+ }
1295
+ #else
1296
+ if ((fzip = fopen(RSTRING_PTR(p_archive->path), "rb")) == NULL) {
1297
+ rb_raise(Error, "Read archive failed: Cannot open archive");
1298
+ }
1299
+ #endif
1300
+
1301
+ block_given = rb_block_given_p();
1302
+
1303
+ while ((n = fread(buf, 1, sizeof(buf), fzip)) > 0) {
1304
+ if (block_given) {
1305
+ rb_yield(rb_str_new(buf, n));
1306
+ } else {
1307
+ if (NIL_P(retval)) {
1308
+ retval = rb_str_new(buf, n);
1309
+ } else {
1310
+ rb_str_buf_cat(retval, buf, n);
1311
+ }
1312
+ }
1313
+ }
1314
+
1315
+ #if defined(RUBY_VM) && defined(_WIN32)
1316
+ _fclose_nolock(fzip);
1317
+ #elif defined(RUBY_WIN32_H)
1318
+ #undef fclose
1319
+ fclose(fzip);
1320
+ #define fclose(f) rb_w32_fclose(f)
1321
+ #else
1322
+ fclose(fzip);
1323
+ #endif
1324
+
1325
+ if (n == -1) {
1326
+ rb_raise(Error, "Read archive failed");
1327
+ }
1328
+
1329
+ return retval;
1330
+ }