zipruby 0.2.9-mswin32 → 0.3.0-mswin32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of zipruby might be problematic. Click here for more details.
- data/LICENSE.libzip +27 -0
- data/README.txt +3 -3
- data/lib/i386-mswin32/zipruby.so +0 -0
- data/zipruby.c +2200 -2200
- metadata +5 -3
data/zipruby.c
CHANGED
@@ -1,2201 +1,2201 @@
|
|
1
1
|
/* rdoc source */
|
2
|
-
#include <stdio.h>
|
3
|
-
#include <stdlib.h>
|
4
|
-
#include <string.h>
|
5
|
-
#include <sys/types.h>
|
6
|
-
#include <sys/stat.h>
|
7
|
-
|
8
|
-
#ifdef _WIN32
|
9
|
-
#
|
10
|
-
#include <
|
11
|
-
#
|
12
|
-
#include <
|
13
|
-
#
|
14
|
-
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#
|
86
|
-
int
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
#
|
142
|
-
|
143
|
-
#
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
#
|
178
|
-
|
179
|
-
#
|
180
|
-
|
181
|
-
#include "
|
182
|
-
#include "
|
183
|
-
#include "
|
184
|
-
#include "
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
#include
|
196
|
-
|
197
|
-
#include "
|
198
|
-
#include "
|
199
|
-
#include "
|
200
|
-
#include "
|
201
|
-
#include "
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
static VALUE
|
208
|
-
static
|
209
|
-
static
|
210
|
-
static VALUE
|
211
|
-
static VALUE
|
212
|
-
static VALUE
|
213
|
-
static VALUE
|
214
|
-
static VALUE
|
215
|
-
static VALUE
|
216
|
-
static VALUE
|
217
|
-
static VALUE
|
218
|
-
static VALUE
|
219
|
-
static VALUE
|
220
|
-
static VALUE
|
221
|
-
static VALUE
|
222
|
-
static VALUE
|
223
|
-
static VALUE
|
224
|
-
static VALUE
|
225
|
-
static VALUE
|
226
|
-
static VALUE
|
227
|
-
static VALUE
|
228
|
-
static VALUE
|
229
|
-
static VALUE
|
230
|
-
static VALUE
|
231
|
-
static VALUE
|
232
|
-
static VALUE
|
233
|
-
static VALUE
|
234
|
-
static VALUE
|
235
|
-
static VALUE
|
236
|
-
static VALUE
|
237
|
-
static VALUE
|
238
|
-
static VALUE
|
239
|
-
static VALUE
|
240
|
-
static VALUE
|
241
|
-
static VALUE
|
242
|
-
static VALUE
|
243
|
-
static VALUE
|
244
|
-
static VALUE
|
245
|
-
|
246
|
-
|
247
|
-
VALUE
|
248
|
-
|
249
|
-
|
250
|
-
extern VALUE
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
rb_define_method(Archive, "
|
265
|
-
rb_define_method(Archive, "
|
266
|
-
rb_define_method(Archive, "
|
267
|
-
rb_define_method(Archive, "
|
268
|
-
rb_define_method(Archive, "
|
269
|
-
rb_define_method(Archive, "
|
270
|
-
rb_define_method(Archive, "
|
271
|
-
rb_define_method(Archive, "
|
272
|
-
rb_define_method(Archive, "
|
273
|
-
rb_define_method(Archive, "
|
274
|
-
rb_define_method(Archive, "
|
275
|
-
rb_define_method(Archive, "
|
276
|
-
rb_define_method(Archive, "
|
277
|
-
rb_define_method(Archive, "
|
278
|
-
rb_define_method(Archive, "
|
279
|
-
rb_define_method(Archive, "
|
280
|
-
rb_define_method(Archive, "
|
281
|
-
rb_define_method(Archive, "
|
282
|
-
rb_define_method(Archive, "
|
283
|
-
rb_define_method(Archive, "
|
284
|
-
rb_define_method(Archive, "
|
285
|
-
rb_define_method(Archive, "
|
286
|
-
rb_define_method(Archive, "
|
287
|
-
rb_define_method(Archive, "
|
288
|
-
rb_define_method(Archive, "
|
289
|
-
rb_define_method(Archive, "
|
290
|
-
rb_define_method(Archive, "
|
291
|
-
rb_define_method(Archive, "
|
292
|
-
rb_define_method(Archive, "
|
293
|
-
rb_define_method(Archive, "
|
294
|
-
rb_define_method(Archive, "
|
295
|
-
rb_define_method(Archive, "
|
296
|
-
rb_define_method(Archive, "
|
297
|
-
rb_define_method(Archive, "
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
p
|
306
|
-
|
307
|
-
p->
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
}
|
389
|
-
|
390
|
-
if (
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
if ((p_archive->
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
pwdlen
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
rb_raise(Error, "Decrypt archive failed - %s:
|
465
|
-
}
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
}
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
pwdlen
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
}
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
num_files
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
if (!
|
650
|
-
|
651
|
-
}
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
if (
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
if (!
|
763
|
-
|
764
|
-
}
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
if ((
|
777
|
-
rb_raise(Error, "Replace file failed
|
778
|
-
}
|
779
|
-
|
780
|
-
if (
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
fname =
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
if (
|
815
|
-
|
816
|
-
}
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
}
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
}
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
}
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
if (!
|
1018
|
-
|
1019
|
-
}
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
}
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
}
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
}
|
1353
|
-
|
1354
|
-
p_archive->
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
}
|
1405
|
-
|
1406
|
-
p_archive->
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
}
|
1451
|
-
|
1452
|
-
p_archive->
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
#
|
1486
|
-
if ((fzip
|
1487
|
-
rb_raise(Error, "Read archive failed: Cannot open archive");
|
1488
|
-
}
|
1489
|
-
#
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
#
|
1510
|
-
|
1511
|
-
#
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
#
|
1536
|
-
|
1537
|
-
#include "zip.h"
|
1538
|
-
#include "zipint.h"
|
1539
|
-
#include "zipruby.h"
|
1540
|
-
#include "zipruby_archive.h"
|
1541
|
-
#include "zipruby_file.h"
|
1542
|
-
#include "zipruby_stat.h"
|
1543
|
-
#include "ruby.h"
|
1544
|
-
|
1545
|
-
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
1546
|
-
|
1547
|
-
static VALUE zipruby_file(VALUE klass);
|
1548
|
-
static VALUE zipruby_file_alloc(VALUE klass);
|
1549
|
-
static void zipruby_file_mark(struct zipruby_file *p);
|
1550
|
-
static void zipruby_file_free(struct zipruby_file *p);
|
1551
|
-
static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self);
|
1552
|
-
static VALUE zipruby_file_close(VALUE self);
|
1553
|
-
static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self);
|
1554
|
-
static VALUE zipruby_file_stat(VALUE self);
|
1555
|
-
static VALUE zipruby_file_get_comment(int argc, VALUE *argv, VALUE self);
|
1556
|
-
static VALUE zipruby_file_set_comment(VALUE self, VALUE comment);
|
1557
|
-
static VALUE zipruby_file_delete(VALUE self);
|
1558
|
-
static VALUE zipruby_file_rename(VALUE self, VALUE name);
|
1559
|
-
static VALUE zipruby_file_unchange(VALUE self);
|
1560
|
-
static VALUE zipruby_file_name(VALUE self);
|
1561
|
-
static VALUE zipruby_file_index(VALUE self);
|
1562
|
-
static VALUE zipruby_file_crc(VALUE self);
|
1563
|
-
static VALUE zipruby_file_size(VALUE self);
|
1564
|
-
static VALUE zipruby_file_mtime(VALUE self);
|
1565
|
-
static VALUE zipruby_file_comp_size(VALUE self);
|
1566
|
-
static VALUE zipruby_file_comp_method(VALUE self);
|
1567
|
-
static VALUE zipruby_file_encryption_method(VALUE self);
|
1568
|
-
|
1569
|
-
extern VALUE Zip;
|
1570
|
-
extern VALUE Archive;
|
1571
|
-
VALUE File;
|
1572
|
-
extern VALUE Stat;
|
1573
|
-
extern VALUE Error;
|
1574
|
-
|
1575
|
-
void Init_zipruby_file() {
|
1576
|
-
File = rb_define_class_under(Zip, "File", rb_cObject);
|
1577
|
-
rb_define_alloc_func(File, zipruby_file_alloc);
|
1578
|
-
rb_define_method(File, "initialize", zipruby_file_initialize, -1);
|
1579
|
-
rb_define_method(File, "close", zipruby_file_close, 0);
|
1580
|
-
rb_define_method(File, "read", zipruby_file_read, -1);
|
1581
|
-
rb_define_method(File, "stat", zipruby_file_stat, 0);
|
1582
|
-
rb_define_method(File, "get_comment", zipruby_file_get_comment, -1);
|
1583
|
-
rb_define_method(File, "comment", zipruby_file_get_comment, -1);
|
1584
|
-
rb_define_method(File, "comment=", zipruby_file_set_comment, 1);
|
1585
|
-
rb_define_method(File, "delete", zipruby_file_delete, 0);
|
1586
|
-
rb_define_method(File, "rename", zipruby_file_rename, 1);
|
1587
|
-
rb_define_method(File, "unchange", zipruby_file_unchange, 1);
|
1588
|
-
rb_define_method(File, "revert", zipruby_file_unchange, 1);
|
1589
|
-
rb_define_method(File, "name", zipruby_file_name, 0);
|
1590
|
-
rb_define_method(File, "index", zipruby_file_index, 0);
|
1591
|
-
rb_define_method(File, "crc", zipruby_file_crc, 0);
|
1592
|
-
rb_define_method(File, "size", zipruby_file_size, 0);
|
1593
|
-
rb_define_method(File, "mtime", zipruby_file_mtime, 0);
|
1594
|
-
rb_define_method(File, "comp_size", zipruby_file_comp_size, 0);
|
1595
|
-
rb_define_method(File, "comp_method", zipruby_file_comp_method, 0);
|
1596
|
-
rb_define_method(File, "encryption_method", zipruby_file_encryption_method, 0);
|
1597
|
-
}
|
1598
|
-
|
1599
|
-
static VALUE zipruby_file_alloc(VALUE klass) {
|
1600
|
-
struct zipruby_file *p = ALLOC(struct zipruby_file);
|
1601
|
-
|
1602
|
-
p->archive = NULL;
|
1603
|
-
p->file = NULL;
|
1604
|
-
p->sb = NULL;
|
1605
|
-
|
1606
|
-
return Data_Wrap_Struct(klass, zipruby_file_mark, zipruby_file_free, p);
|
1607
|
-
}
|
1608
|
-
|
1609
|
-
static void zipruby_file_mark(struct zipruby_file *p) {
|
1610
|
-
if (p->archive) { rb_gc_mark(p->v_archive); }
|
1611
|
-
if (p->sb) { rb_gc_mark(p->v_sb); }
|
1612
|
-
}
|
1613
|
-
|
1614
|
-
static void zipruby_file_free(struct zipruby_file *p) {
|
1615
|
-
xfree(p);
|
1616
|
-
}
|
1617
|
-
|
1618
|
-
/* */
|
1619
|
-
static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self) {
|
1620
|
-
VALUE archive, index, flags, stat_flags;
|
1621
|
-
struct zipruby_archive *p_archive;
|
1622
|
-
struct zipruby_file *p_file;
|
1623
|
-
struct zipruby_stat *p_stat;
|
1624
|
-
struct zip_file *fzip;
|
1625
|
-
char *fname = NULL;
|
1626
|
-
int i_index = -1, i_flags = 0;
|
1627
|
-
|
1628
|
-
rb_scan_args(argc, argv, "22", &archive, &index, &flags, &stat_flags);
|
1629
|
-
|
1630
|
-
if (!rb_obj_is_instance_of(archive, Archive)) {
|
1631
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(archive)));
|
1632
|
-
}
|
1633
|
-
|
1634
|
-
switch (TYPE(index)) {
|
1635
|
-
case T_STRING: fname = RSTRING_PTR(index); break;
|
1636
|
-
case T_FIXNUM: i_index = NUM2INT(index); break;
|
1637
|
-
default:
|
1638
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
|
1639
|
-
}
|
1640
|
-
|
1641
|
-
if (!NIL_P(flags)) {
|
1642
|
-
i_flags = NUM2INT(flags);
|
1643
|
-
}
|
1644
|
-
|
1645
|
-
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
1646
|
-
Check_Archive(p_archive);
|
1647
|
-
|
1648
|
-
if (fname) {
|
1649
|
-
fzip = zip_fopen(p_archive->archive, fname, i_flags);
|
1650
|
-
|
1651
|
-
if (fzip == NULL) {
|
1652
|
-
rb_raise(Error, "Open file failed - %s: %s", fname, zip_strerror(p_archive->archive));
|
1653
|
-
}
|
1654
|
-
} else {
|
1655
|
-
fzip = zip_fopen_index(p_archive->archive, i_index, i_flags);
|
1656
|
-
|
1657
|
-
if (fzip == NULL) {
|
1658
|
-
rb_raise(Error, "Open file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
1659
|
-
}
|
1660
|
-
}
|
1661
|
-
|
1662
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1663
|
-
p_file->v_archive = archive;
|
1664
|
-
p_file->archive = p_archive->archive;
|
1665
|
-
p_file->file = fzip;
|
1666
|
-
p_file->v_sb = rb_funcall(Stat, rb_intern("new"), 3, archive, index, stat_flags);
|
1667
|
-
Data_Get_Struct(p_file->v_sb, struct zipruby_stat, p_stat);
|
1668
|
-
p_file->sb = p_stat->sb;
|
1669
|
-
|
1670
|
-
return Qnil;
|
1671
|
-
}
|
1672
|
-
|
1673
|
-
/* */
|
1674
|
-
static VALUE zipruby_file_close(VALUE self) {
|
1675
|
-
struct zipruby_file *p_file;
|
1676
|
-
int error;
|
1677
|
-
|
1678
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1679
|
-
Check_File(p_file);
|
1680
|
-
|
1681
|
-
if ((error = zip_fclose(p_file->file)) != 0) {
|
1682
|
-
char errstr[ERRSTR_BUFSIZE];
|
1683
|
-
zip_unchange(p_file->archive, p_file->sb->index);
|
1684
|
-
zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
|
1685
|
-
rb_raise(Error, "Close file failed: %s", errstr);
|
1686
|
-
}
|
1687
|
-
|
1688
|
-
p_file->archive = NULL;
|
1689
|
-
p_file->file = NULL;
|
1690
|
-
p_file->sb = NULL;
|
1691
|
-
|
1692
|
-
return Qnil;
|
1693
|
-
}
|
1694
|
-
|
1695
|
-
/* */
|
1696
|
-
static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self) {
|
1697
|
-
VALUE size, retval = Qnil;
|
1698
|
-
struct zipruby_file *p_file;
|
1699
|
-
struct zip_stat sb;
|
1700
|
-
int block_given;
|
1701
|
-
size_t bytes_left;
|
1702
|
-
char buf[DATA_BUFSIZE];
|
1703
|
-
ssize_t n;
|
1704
|
-
|
1705
|
-
rb_scan_args(argc, argv, "01", &size);
|
1706
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1707
|
-
Check_File(p_file);
|
1708
|
-
zip_stat_init(&sb);
|
1709
|
-
|
1710
|
-
if (p_file->archive->cdir->entry[0].bitflags & ZIP_GPBF_ENCRYPTED) {
|
1711
|
-
rb_raise(Error, "Read file failed: File encrypted");
|
1712
|
-
}
|
1713
|
-
|
1714
|
-
if (zip_stat_index(p_file->archive, p_file->sb->index, 0, &sb)) {
|
1715
|
-
rb_raise(Error, "Read file failed: %s", zip_strerror(p_file->archive));
|
1716
|
-
}
|
1717
|
-
|
1718
|
-
if (NIL_P(size)) {
|
1719
|
-
bytes_left = sb.size;
|
1720
|
-
} else {
|
1721
|
-
bytes_left = NUM2LONG(size);
|
1722
|
-
}
|
1723
|
-
|
1724
|
-
if (bytes_left <= 0) {
|
1725
|
-
return Qnil;
|
1726
|
-
}
|
1727
|
-
|
1728
|
-
block_given = rb_block_given_p();
|
1729
|
-
|
1730
|
-
while ((n = zip_fread(p_file->file, buf, MIN(bytes_left, sizeof(buf)))) > 0) {
|
1731
|
-
if (block_given) {
|
1732
|
-
rb_yield(rb_str_new(buf, n));
|
1733
|
-
} else {
|
1734
|
-
if (NIL_P(retval)) {
|
1735
|
-
retval = rb_str_new(buf, n);
|
1736
|
-
} else {
|
1737
|
-
rb_str_buf_cat(retval, buf, n);
|
1738
|
-
}
|
1739
|
-
}
|
1740
|
-
|
1741
|
-
bytes_left -= n;
|
1742
|
-
}
|
1743
|
-
|
1744
|
-
if (n == -1) {
|
1745
|
-
rb_raise(Error, "Read file failed: %s", zip_file_strerror(p_file->file));
|
1746
|
-
}
|
1747
|
-
|
1748
|
-
return retval;
|
1749
|
-
}
|
1750
|
-
|
1751
|
-
/* */
|
1752
|
-
static VALUE zipruby_file_stat(VALUE self) {
|
1753
|
-
struct zipruby_file *p_file;
|
1754
|
-
|
1755
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1756
|
-
Check_File(p_file);
|
1757
|
-
|
1758
|
-
return p_file->v_sb;
|
1759
|
-
}
|
1760
|
-
|
1761
|
-
/* */
|
1762
|
-
static VALUE zipruby_file_get_comment(int argc, VALUE *argv, VALUE self) {
|
1763
|
-
VALUE flags;
|
1764
|
-
struct zipruby_file *p_file;
|
1765
|
-
const char *comment;
|
1766
|
-
int lenp, i_flags = 0;
|
1767
|
-
|
1768
|
-
rb_scan_args(argc, argv, "01", &flags);
|
1769
|
-
|
1770
|
-
if (!NIL_P(flags)) {
|
1771
|
-
i_flags = NUM2INT(flags);
|
1772
|
-
}
|
1773
|
-
|
1774
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1775
|
-
Check_File(p_file);
|
1776
|
-
|
1777
|
-
// XXX: How is the error checked?
|
1778
|
-
comment = zip_get_file_comment(p_file->archive, p_file->sb->index, &lenp, i_flags);
|
1779
|
-
|
1780
|
-
return comment ? rb_str_new(comment, lenp) : Qnil;
|
1781
|
-
}
|
1782
|
-
|
1783
|
-
/* */
|
1784
|
-
static VALUE zipruby_file_set_comment(VALUE self, VALUE comment) {
|
1785
|
-
struct zipruby_file *p_file;
|
1786
|
-
char *s_comment = NULL;
|
1787
|
-
int len = 0;
|
1788
|
-
|
1789
|
-
if (!NIL_P(comment)) {
|
1790
|
-
Check_Type(comment, T_STRING);
|
1791
|
-
s_comment = RSTRING_PTR(comment);
|
1792
|
-
len = RSTRING_LEN(comment);
|
1793
|
-
}
|
1794
|
-
|
1795
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1796
|
-
Check_File(p_file);
|
1797
|
-
|
1798
|
-
if (zip_set_file_comment(p_file->archive, p_file->sb->index, s_comment, len) == -1) {
|
1799
|
-
zip_unchange_all(p_file->archive);
|
1800
|
-
zip_unchange_archive(p_file->archive);
|
1801
|
-
rb_raise(Error, "Comment file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1802
|
-
}
|
1803
|
-
|
1804
|
-
return Qnil;
|
1805
|
-
}
|
1806
|
-
|
1807
|
-
/* */
|
1808
|
-
static VALUE zipruby_file_delete(VALUE self) {
|
1809
|
-
struct zipruby_file *p_file;
|
1810
|
-
|
1811
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1812
|
-
Check_File(p_file);
|
1813
|
-
|
1814
|
-
if (zip_delete(p_file->archive, p_file->sb->index) == -1) {
|
1815
|
-
zip_unchange_all(p_file->archive);
|
1816
|
-
zip_unchange_archive(p_file->archive);
|
1817
|
-
rb_raise(Error, "Delete file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1818
|
-
}
|
1819
|
-
|
1820
|
-
return Qnil;
|
1821
|
-
}
|
1822
|
-
|
1823
|
-
/* */
|
1824
|
-
static VALUE zipruby_file_rename(VALUE self, VALUE name) {
|
1825
|
-
struct zipruby_file *p_file;
|
1826
|
-
|
1827
|
-
Check_Type(name, T_STRING);
|
1828
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1829
|
-
Check_File(p_file);
|
1830
|
-
|
1831
|
-
if (zip_rename(p_file->archive, p_file->sb->index, RSTRING_PTR(name)) == -1) {
|
1832
|
-
zip_unchange_all(p_file->archive);
|
1833
|
-
zip_unchange_archive(p_file->archive);
|
1834
|
-
rb_raise(Error, "Rename file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1835
|
-
}
|
1836
|
-
|
1837
|
-
return Qnil;
|
1838
|
-
}
|
1839
|
-
|
1840
|
-
/* */
|
1841
|
-
static VALUE zipruby_file_unchange(VALUE self) {
|
1842
|
-
struct zipruby_file *p_file;
|
1843
|
-
|
1844
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1845
|
-
Check_File(p_file);
|
1846
|
-
|
1847
|
-
if (zip_unchange(p_file->archive, p_file->sb->index) == -1) {
|
1848
|
-
rb_raise(Error, "Unchange file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1849
|
-
}
|
1850
|
-
|
1851
|
-
return Qnil;
|
1852
|
-
}
|
1853
|
-
|
1854
|
-
/* */
|
1855
|
-
static VALUE zipruby_file_name(VALUE self) {
|
1856
|
-
struct zipruby_file *p_file;
|
1857
|
-
|
1858
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1859
|
-
Check_File(p_file);
|
1860
|
-
|
1861
|
-
return rb_funcall(p_file->v_sb, rb_intern("name"), 0);
|
1862
|
-
}
|
1863
|
-
|
1864
|
-
/* */
|
1865
|
-
static VALUE zipruby_file_index(VALUE self) {
|
1866
|
-
struct zipruby_file *p_file;
|
1867
|
-
|
1868
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1869
|
-
Check_File(p_file);
|
1870
|
-
|
1871
|
-
return rb_funcall(p_file->v_sb, rb_intern("index"), 0);
|
1872
|
-
}
|
1873
|
-
|
1874
|
-
/* */
|
1875
|
-
static VALUE zipruby_file_crc(VALUE self) {
|
1876
|
-
struct zipruby_file *p_file;
|
1877
|
-
|
1878
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1879
|
-
Check_File(p_file);
|
1880
|
-
|
1881
|
-
return rb_funcall(p_file->v_sb, rb_intern("crc"), 0);
|
1882
|
-
}
|
1883
|
-
|
1884
|
-
/* */
|
1885
|
-
static VALUE zipruby_file_size(VALUE self) {
|
1886
|
-
struct zipruby_file *p_file;
|
1887
|
-
|
1888
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1889
|
-
Check_File(p_file);
|
1890
|
-
|
1891
|
-
return rb_funcall(p_file->v_sb, rb_intern("size"), 0);
|
1892
|
-
}
|
1893
|
-
|
1894
|
-
/* */
|
1895
|
-
static VALUE zipruby_file_mtime(VALUE self) {
|
1896
|
-
struct zipruby_file *p_file;
|
1897
|
-
|
1898
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1899
|
-
Check_File(p_file);
|
1900
|
-
|
1901
|
-
return rb_funcall(p_file->v_sb, rb_intern("mtime"), 0);
|
1902
|
-
}
|
1903
|
-
|
1904
|
-
/* */
|
1905
|
-
static VALUE zipruby_file_comp_size(VALUE self) {
|
1906
|
-
struct zipruby_file *p_file;
|
1907
|
-
|
1908
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1909
|
-
Check_File(p_file);
|
1910
|
-
|
1911
|
-
return rb_funcall(p_file->v_sb, rb_intern("comp_size"), 0);
|
1912
|
-
}
|
1913
|
-
|
1914
|
-
/* */
|
1915
|
-
static VALUE zipruby_file_comp_method(VALUE self) {
|
1916
|
-
struct zipruby_file *p_file;
|
1917
|
-
|
1918
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1919
|
-
Check_File(p_file);
|
1920
|
-
|
1921
|
-
return rb_funcall(p_file->v_sb, rb_intern("comp_method"), 0);
|
1922
|
-
}
|
1923
|
-
|
1924
|
-
/* */
|
1925
|
-
static VALUE zipruby_file_encryption_method(VALUE self) {
|
1926
|
-
struct zipruby_file *p_file;
|
1927
|
-
|
1928
|
-
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1929
|
-
Check_File(p_file);
|
1930
|
-
|
1931
|
-
return rb_funcall(p_file->v_sb, rb_intern("encryption_method"), 0);
|
1932
|
-
}
|
1933
|
-
#include "zip.h"
|
1934
|
-
#include "zipruby.h"
|
1935
|
-
#include "zipruby_archive.h"
|
1936
|
-
#include "zipruby_stat.h"
|
1937
|
-
#include "ruby.h"
|
1938
|
-
|
1939
|
-
static VALUE zipruby_stat_alloc(VALUE klass);
|
1940
|
-
static void zipruby_stat_free(struct zipruby_stat *p);
|
1941
|
-
static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self);
|
1942
|
-
static VALUE zipruby_stat_name(VALUE self);
|
1943
|
-
static VALUE zipruby_stat_index(VALUE self);
|
1944
|
-
static VALUE zipruby_stat_crc(VALUE self);
|
1945
|
-
static VALUE zipruby_stat_size(VALUE self);
|
1946
|
-
static VALUE zipruby_stat_mtime(VALUE self);
|
1947
|
-
static VALUE zipruby_stat_comp_size(VALUE self);
|
1948
|
-
static VALUE zipruby_stat_comp_method(VALUE self);
|
1949
|
-
static VALUE zipruby_stat_encryption_method(VALUE self);
|
1950
|
-
|
1951
|
-
extern VALUE Zip;
|
1952
|
-
extern VALUE Archive;
|
1953
|
-
VALUE Stat;
|
1954
|
-
extern VALUE Error;
|
1955
|
-
|
1956
|
-
void Init_zipruby_stat() {
|
1957
|
-
Stat = rb_define_class_under(Zip, "Stat", rb_cObject);
|
1958
|
-
rb_define_alloc_func(Stat, zipruby_stat_alloc);
|
1959
|
-
rb_define_method(Stat, "initialize", zipruby_stat_initialize, -1);
|
1960
|
-
rb_define_method(Stat, "name", zipruby_stat_name, 0);
|
1961
|
-
rb_define_method(Stat, "index", zipruby_stat_index, 0);
|
1962
|
-
rb_define_method(Stat, "crc", zipruby_stat_crc, 0);
|
1963
|
-
rb_define_method(Stat, "size", zipruby_stat_size, 0);
|
1964
|
-
rb_define_method(Stat, "mtime", zipruby_stat_mtime, 0);
|
1965
|
-
rb_define_method(Stat, "comp_size", zipruby_stat_comp_size, 0);
|
1966
|
-
rb_define_method(Stat, "comp_method", zipruby_stat_comp_method, 0);
|
1967
|
-
rb_define_method(Stat, "encryption_method", zipruby_stat_encryption_method, 0);
|
1968
|
-
}
|
1969
|
-
|
1970
|
-
static VALUE zipruby_stat_alloc(VALUE klass) {
|
1971
|
-
struct zipruby_stat *p = ALLOC(struct zipruby_stat);
|
1972
|
-
|
1973
|
-
p->sb = ALLOC(struct zip_stat);
|
1974
|
-
zip_stat_init(p->sb);
|
1975
|
-
|
1976
|
-
return Data_Wrap_Struct(klass, 0, zipruby_stat_free, p);
|
1977
|
-
}
|
1978
|
-
|
1979
|
-
static void zipruby_stat_free(struct zipruby_stat *p) {
|
1980
|
-
xfree(p->sb);
|
1981
|
-
xfree(p);
|
1982
|
-
}
|
1983
|
-
|
1984
|
-
/* */
|
1985
|
-
static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
|
1986
|
-
VALUE archive, index, flags;
|
1987
|
-
struct zipruby_archive *p_archive;
|
1988
|
-
struct zipruby_stat *p_stat;
|
1989
|
-
char *fname = NULL;
|
1990
|
-
int i_index = -1, i_flags = 0;
|
1991
|
-
|
1992
|
-
rb_scan_args(argc, argv, "21", &archive, &index, &flags);
|
1993
|
-
|
1994
|
-
if (!rb_obj_is_instance_of(archive, Archive)) {
|
1995
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(archive)));
|
1996
|
-
}
|
1997
|
-
|
1998
|
-
switch (TYPE(index)) {
|
1999
|
-
case T_STRING: fname = RSTRING_PTR(index); break;
|
2000
|
-
case T_FIXNUM: i_index = NUM2INT(index); break;
|
2001
|
-
default:
|
2002
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
|
2003
|
-
}
|
2004
|
-
|
2005
|
-
if (!NIL_P(flags)) {
|
2006
|
-
i_flags = NUM2INT(flags);
|
2007
|
-
}
|
2008
|
-
|
2009
|
-
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
2010
|
-
Check_Archive(p_archive);
|
2011
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2012
|
-
|
2013
|
-
if (fname) {
|
2014
|
-
if (zip_stat(p_archive->archive, fname, i_flags, p_stat->sb) != 0) {
|
2015
|
-
rb_raise(Error, "Obtain file status failed - %s: %s", fname, zip_strerror(p_archive->archive));
|
2016
|
-
}
|
2017
|
-
} else {
|
2018
|
-
if (zip_stat_index(p_archive->archive, i_index, i_flags, p_stat->sb) != 0) {
|
2019
|
-
rb_raise(Error, "Obtain file status failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
2020
|
-
}
|
2021
|
-
}
|
2022
|
-
|
2023
|
-
return Qnil;
|
2024
|
-
}
|
2025
|
-
|
2026
|
-
/* */
|
2027
|
-
static VALUE zipruby_stat_name(VALUE self) {
|
2028
|
-
struct zipruby_stat *p_stat;
|
2029
|
-
|
2030
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2031
|
-
|
2032
|
-
return p_stat->sb->name ? rb_str_new2(p_stat->sb->name) : Qnil;
|
2033
|
-
}
|
2034
|
-
|
2035
|
-
/* */
|
2036
|
-
static VALUE zipruby_stat_index(VALUE self) {
|
2037
|
-
struct zipruby_stat *p_stat;
|
2038
|
-
|
2039
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2040
|
-
|
2041
|
-
return INT2NUM(p_stat->sb->index);
|
2042
|
-
}
|
2043
|
-
|
2044
|
-
/* */
|
2045
|
-
static VALUE zipruby_stat_crc(VALUE self) {
|
2046
|
-
struct zipruby_stat *p_stat;
|
2047
|
-
|
2048
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2049
|
-
|
2050
|
-
return UINT2NUM(p_stat->sb->crc);
|
2051
|
-
}
|
2052
|
-
|
2053
|
-
/* */
|
2054
|
-
static VALUE zipruby_stat_size(VALUE self) {
|
2055
|
-
struct zipruby_stat *p_stat;
|
2056
|
-
|
2057
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2058
|
-
|
2059
|
-
return LONG2NUM(p_stat->sb->size);
|
2060
|
-
}
|
2061
|
-
|
2062
|
-
/* */
|
2063
|
-
static VALUE zipruby_stat_mtime(VALUE self) {
|
2064
|
-
struct zipruby_stat *p_stat;
|
2065
|
-
|
2066
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2067
|
-
|
2068
|
-
return rb_funcall(rb_cTime, rb_intern("at"), 1, LONG2NUM((long) p_stat->sb->mtime));
|
2069
|
-
}
|
2070
|
-
|
2071
|
-
/* */
|
2072
|
-
static VALUE zipruby_stat_comp_size(VALUE self) {
|
2073
|
-
struct zipruby_stat *p_stat;
|
2074
|
-
|
2075
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2076
|
-
|
2077
|
-
return LONG2NUM(p_stat->sb->comp_size);
|
2078
|
-
}
|
2079
|
-
|
2080
|
-
/* */
|
2081
|
-
static VALUE zipruby_stat_comp_method(VALUE self) {
|
2082
|
-
struct zipruby_stat *p_stat;
|
2083
|
-
|
2084
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2085
|
-
|
2086
|
-
return INT2NUM(p_stat->sb->comp_method);
|
2087
|
-
}
|
2088
|
-
|
2089
|
-
/* */
|
2090
|
-
static VALUE zipruby_stat_encryption_method(VALUE self) {
|
2091
|
-
struct zipruby_stat *p_stat;
|
2092
|
-
|
2093
|
-
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2094
|
-
|
2095
|
-
return INT2NUM(p_stat->sb->encryption_method);
|
2096
|
-
}
|
2097
|
-
#include "ruby.h"
|
2098
|
-
#include "zip.h"
|
2099
|
-
#include "zipruby.h"
|
2100
|
-
#include "zipruby_zip.h"
|
2101
|
-
|
2102
|
-
VALUE Zip;
|
2103
|
-
|
2104
|
-
void Init_zipruby_zip() {
|
2105
|
-
Zip = rb_define_module("Zip");
|
2106
|
-
rb_define_const(Zip, "VERSION", rb_str_new2(VERSION));
|
2107
|
-
|
2108
|
-
rb_define_const(Zip, "CREATE", INT2NUM(ZIP_CREATE));
|
2109
|
-
rb_define_const(Zip, "EXCL", INT2NUM(ZIP_EXCL));
|
2110
|
-
rb_define_const(Zip, "CHECKCONS", INT2NUM(ZIP_CHECKCONS));
|
2111
|
-
rb_define_const(Zip, "TRUNC", INT2NUM(ZIP_TRUNC));
|
2112
|
-
|
2113
|
-
rb_define_const(Zip, "FL_NOCASE", INT2NUM(ZIP_FL_NOCASE));
|
2114
|
-
rb_define_const(Zip, "FL_NODIR", INT2NUM(ZIP_FL_NODIR));
|
2115
|
-
rb_define_const(Zip, "FL_COMPRESSED", INT2NUM(ZIP_FL_COMPRESSED));
|
2116
|
-
rb_define_const(Zip, "FL_UNCHANGED", INT2NUM(ZIP_FL_UNCHANGED));
|
2117
|
-
|
2118
|
-
rb_define_const(Zip, "CM_DEFAULT" , INT2NUM(ZIP_CM_DEFAULT));
|
2119
|
-
rb_define_const(Zip, "CM_STORE", INT2NUM(ZIP_CM_STORE));
|
2120
|
-
rb_define_const(Zip, "CM_SHRINK", INT2NUM(ZIP_CM_SHRINK));
|
2121
|
-
rb_define_const(Zip, "CM_REDUCE_1", INT2NUM(ZIP_CM_REDUCE_1));
|
2122
|
-
rb_define_const(Zip, "CM_REDUCE_2", INT2NUM(ZIP_CM_REDUCE_2));
|
2123
|
-
rb_define_const(Zip, "CM_REDUCE_3", INT2NUM(ZIP_CM_REDUCE_3));
|
2124
|
-
rb_define_const(Zip, "CM_REDUCE_4", INT2NUM(ZIP_CM_REDUCE_4));
|
2125
|
-
rb_define_const(Zip, "CM_IMPLODE", INT2NUM(ZIP_CM_IMPLODE));
|
2126
|
-
rb_define_const(Zip, "CM_DEFLATE", INT2NUM(ZIP_CM_DEFLATE));
|
2127
|
-
rb_define_const(Zip, "CM_DEFLATE64", INT2NUM(ZIP_CM_DEFLATE64));
|
2128
|
-
rb_define_const(Zip, "CM_PKWARE_IMPLODE", INT2NUM(ZIP_CM_PKWARE_IMPLODE));
|
2129
|
-
rb_define_const(Zip, "CM_BZIP2", INT2NUM(ZIP_CM_BZIP2));
|
2130
|
-
|
2131
|
-
rb_define_const(Zip, "EM_NONE", INT2NUM(ZIP_EM_NONE));
|
2132
|
-
rb_define_const(Zip, "EM_TRAD_PKWARE", INT2NUM(ZIP_EM_TRAD_PKWARE));
|
2133
|
-
// XXX: Strong Encryption Header not parsed yet
|
2134
|
-
}
|
2135
|
-
#include <string.h>
|
2136
|
-
|
2137
|
-
#include "zip.h"
|
2138
|
-
#include "zipint.h"
|
2139
|
-
#include "zipruby_zip_source_proc.h"
|
2140
|
-
#include "ruby.h"
|
2141
|
-
|
2142
|
-
static VALUE proc_call(VALUE proc) {
|
2143
|
-
return rb_funcall(proc, rb_intern("call"), 0);
|
2144
|
-
}
|
2145
|
-
|
2146
|
-
static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cmd cmd) {
|
2147
|
-
struct read_proc *z;
|
2148
|
-
VALUE src;
|
2149
|
-
char *buf;
|
2150
|
-
size_t n;
|
2151
|
-
|
2152
|
-
z = (struct read_proc *) state;
|
2153
|
-
buf = (char *) data;
|
2154
|
-
|
2155
|
-
switch (cmd) {
|
2156
|
-
case ZIP_SOURCE_OPEN:
|
2157
|
-
return 0;
|
2158
|
-
|
2159
|
-
case ZIP_SOURCE_READ:
|
2160
|
-
src = rb_protect(proc_call, z->proc, NULL);
|
2161
|
-
|
2162
|
-
if (TYPE(src) != T_STRING) {
|
2163
|
-
return 0;
|
2164
|
-
}
|
2165
|
-
|
2166
|
-
n = RSTRING_LEN(src);
|
2167
|
-
|
2168
|
-
if (n > 0) {
|
2169
|
-
n = (n > len) ? len : n;
|
2170
|
-
memcpy(buf, RSTRING_PTR(src), n);
|
2171
|
-
}
|
2172
|
-
|
2173
|
-
return n;
|
2174
|
-
|
2175
|
-
case ZIP_SOURCE_CLOSE:
|
2176
|
-
return 0;
|
2177
|
-
|
2178
|
-
case ZIP_SOURCE_STAT:
|
2179
|
-
{
|
2180
|
-
struct zip_stat *st = (struct zip_stat *)data;
|
2181
|
-
zip_stat_init(st);
|
2182
|
-
st->mtime = NUM2LONG(rb_funcall(z->mtime, rb_intern("tv_sec"), 0));
|
2183
|
-
return sizeof(*st);
|
2184
|
-
}
|
2185
|
-
|
2186
|
-
case ZIP_SOURCE_ERROR:
|
2187
|
-
return 0;
|
2188
|
-
|
2189
|
-
case ZIP_SOURCE_FREE:
|
2190
|
-
free(z);
|
2191
|
-
return 0;
|
2192
|
-
}
|
2193
|
-
|
2194
|
-
return -1;
|
2195
|
-
}
|
2196
|
-
|
2197
|
-
struct zip_source *zip_source_proc(struct zip *za, struct read_proc *z) {
|
2198
|
-
struct zip_source *zs;
|
2199
|
-
zs = zip_source_function(za, read_proc, z);
|
2200
|
-
return zs;
|
2201
|
-
}
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <stdlib.h>
|
4
|
+
#include <string.h>
|
5
|
+
#include <sys/types.h>
|
6
|
+
#include <sys/stat.h>
|
7
|
+
|
8
|
+
#ifdef _WIN32
|
9
|
+
#if RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8
|
10
|
+
#include <windows.h>
|
11
|
+
#endif
|
12
|
+
#include <io.h>
|
13
|
+
#include <fcntl.h>
|
14
|
+
#include <share.h>
|
15
|
+
#endif
|
16
|
+
|
17
|
+
#include "tmpfile.h"
|
18
|
+
#include "ruby.h"
|
19
|
+
|
20
|
+
#ifndef _WIN32
|
21
|
+
#ifndef HAVE_MKSTEMP
|
22
|
+
int _zip_mkstemp(char *);
|
23
|
+
#define mkstemp _zip_mkstemp
|
24
|
+
#endif
|
25
|
+
#endif
|
26
|
+
|
27
|
+
static int write_from_proc(VALUE proc, int fd);
|
28
|
+
static VALUE proc_call(VALUE proc);
|
29
|
+
|
30
|
+
char *zipruby_tmpnam(void *data, int len) {
|
31
|
+
char *filnam;
|
32
|
+
|
33
|
+
#ifdef _WIN32
|
34
|
+
int fd;
|
35
|
+
char tmpdirnam[_MAX_PATH];
|
36
|
+
char tmpfilnam[_MAX_PATH];
|
37
|
+
int namlen;
|
38
|
+
|
39
|
+
memset(tmpdirnam, 0, _MAX_PATH);
|
40
|
+
|
41
|
+
if (GetTempPathA(_MAX_PATH, tmpdirnam) == 0) {
|
42
|
+
return NULL;
|
43
|
+
}
|
44
|
+
|
45
|
+
memset(tmpfilnam, 0, _MAX_PATH);
|
46
|
+
|
47
|
+
if (GetTempFileNameA(tmpdirnam, "zrb", 0, tmpfilnam) == 0) {
|
48
|
+
return NULL;
|
49
|
+
}
|
50
|
+
|
51
|
+
namlen = strlen(tmpfilnam) + 1;
|
52
|
+
|
53
|
+
if ((filnam = calloc(namlen, sizeof(char))) == NULL) {
|
54
|
+
return NULL;
|
55
|
+
}
|
56
|
+
|
57
|
+
if (strcpy_s(filnam, namlen, tmpfilnam) != 0) {
|
58
|
+
free(filnam);
|
59
|
+
return NULL;
|
60
|
+
}
|
61
|
+
|
62
|
+
if (data) {
|
63
|
+
if ((_sopen_s(&fd, filnam, _O_WRONLY | _O_BINARY, _SH_DENYRD, _S_IWRITE)) != 0) {
|
64
|
+
free(filnam);
|
65
|
+
return NULL;
|
66
|
+
}
|
67
|
+
|
68
|
+
if (len < 0) {
|
69
|
+
if (write_from_proc((VALUE) data, fd) == -1) {
|
70
|
+
free(filnam);
|
71
|
+
return NULL;
|
72
|
+
}
|
73
|
+
} else {
|
74
|
+
if (_write(fd, data, len) == -1) {
|
75
|
+
free(filnam);
|
76
|
+
return NULL;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
if (_close(fd) == -1) {
|
81
|
+
free(filnam);
|
82
|
+
return NULL;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
#else
|
86
|
+
int fd;
|
87
|
+
#ifdef P_tmpdir
|
88
|
+
int namlen = 16 + strlen(P_tmpdir);
|
89
|
+
char *dirnam = P_tmpdir;
|
90
|
+
#else
|
91
|
+
int namlen = 20;
|
92
|
+
char *dirnam = "/tmp";
|
93
|
+
#endif
|
94
|
+
|
95
|
+
if ((filnam = calloc(namlen, sizeof(char))) == NULL) {
|
96
|
+
return NULL;
|
97
|
+
}
|
98
|
+
|
99
|
+
strcpy(filnam, dirnam);
|
100
|
+
strcat(filnam, "/zipruby.XXXXXX");
|
101
|
+
|
102
|
+
if ((fd = mkstemp(filnam)) == -1) {
|
103
|
+
free(filnam);
|
104
|
+
return NULL;
|
105
|
+
}
|
106
|
+
|
107
|
+
if (data) {
|
108
|
+
if (len < 0) {
|
109
|
+
if (write_from_proc((VALUE) data, fd) == -1) {
|
110
|
+
free(filnam);
|
111
|
+
return NULL;
|
112
|
+
}
|
113
|
+
} else {
|
114
|
+
if (write(fd, data, len) == -1) {
|
115
|
+
free(filnam);
|
116
|
+
return NULL;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
if (close(fd) == -1) {
|
122
|
+
free(filnam);
|
123
|
+
return NULL;
|
124
|
+
}
|
125
|
+
#endif
|
126
|
+
|
127
|
+
return filnam;
|
128
|
+
}
|
129
|
+
|
130
|
+
void zipruby_rmtmp(const char *tmpfilnam) {
|
131
|
+
struct stat st;
|
132
|
+
|
133
|
+
if (!tmpfilnam) {
|
134
|
+
return;
|
135
|
+
}
|
136
|
+
|
137
|
+
if (stat(tmpfilnam, &st) != 0) {
|
138
|
+
return;
|
139
|
+
}
|
140
|
+
|
141
|
+
#ifdef _WIN32
|
142
|
+
_unlink(tmpfilnam);
|
143
|
+
#else
|
144
|
+
unlink(tmpfilnam);
|
145
|
+
#endif
|
146
|
+
}
|
147
|
+
|
148
|
+
static int write_from_proc(VALUE proc, int fd) {
|
149
|
+
while (1) {
|
150
|
+
VALUE src = rb_protect(proc_call, proc, NULL);
|
151
|
+
|
152
|
+
if (TYPE(src) != T_STRING) {
|
153
|
+
break;
|
154
|
+
}
|
155
|
+
|
156
|
+
if (RSTRING_LEN(src) < 1) {
|
157
|
+
break;
|
158
|
+
}
|
159
|
+
|
160
|
+
#ifdef _WIN32
|
161
|
+
if (_write(fd, RSTRING_PTR(src), RSTRING_LEN(src)) == -1) {
|
162
|
+
return -1;
|
163
|
+
}
|
164
|
+
#else
|
165
|
+
if (write(fd, RSTRING_PTR(src), RSTRING_LEN(src)) == -1) {
|
166
|
+
return -1;
|
167
|
+
}
|
168
|
+
#endif
|
169
|
+
}
|
170
|
+
|
171
|
+
return 0;
|
172
|
+
}
|
173
|
+
|
174
|
+
static VALUE proc_call(VALUE proc) {
|
175
|
+
return rb_funcall(proc, rb_intern("call"), 0);
|
176
|
+
}
|
177
|
+
#ifdef _WIN32
|
178
|
+
__declspec(dllexport) void Init_zipruby(void);
|
179
|
+
#endif
|
180
|
+
|
181
|
+
#include "zipruby.h"
|
182
|
+
#include "zipruby_zip.h"
|
183
|
+
#include "zipruby_archive.h"
|
184
|
+
#include "zipruby_file.h"
|
185
|
+
#include "zipruby_stat.h"
|
186
|
+
#include "zipruby_error.h"
|
187
|
+
|
188
|
+
void Init_zipruby() {
|
189
|
+
Init_zipruby_zip();
|
190
|
+
Init_zipruby_archive();
|
191
|
+
Init_zipruby_file();
|
192
|
+
Init_zipruby_stat();
|
193
|
+
Init_zipruby_error();
|
194
|
+
}
|
195
|
+
#include <errno.h>
|
196
|
+
|
197
|
+
#include "zip.h"
|
198
|
+
#include "zipruby.h"
|
199
|
+
#include "zipruby_archive.h"
|
200
|
+
#include "zipruby_zip_source_proc.h"
|
201
|
+
#include "tmpfile.h"
|
202
|
+
#include "ruby.h"
|
203
|
+
#ifndef RUBY_VM
|
204
|
+
#include "rubyio.h"
|
205
|
+
#endif
|
206
|
+
|
207
|
+
static VALUE zipruby_archive_alloc(VALUE klass);
|
208
|
+
static void zipruby_archive_mark(struct zipruby_archive *p);
|
209
|
+
static void zipruby_archive_free(struct zipruby_archive *p);
|
210
|
+
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self);
|
211
|
+
static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self);
|
212
|
+
static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password);
|
213
|
+
static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password);
|
214
|
+
static VALUE zipruby_archive_close(VALUE self);
|
215
|
+
static VALUE zipruby_archive_num_files(VALUE self);
|
216
|
+
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self);
|
217
|
+
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self);
|
218
|
+
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self);
|
219
|
+
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source);
|
220
|
+
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self);
|
221
|
+
static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io);
|
222
|
+
static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self);
|
223
|
+
static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self);
|
224
|
+
static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self);
|
225
|
+
static VALUE zipruby_archive_replace_io(int argc, VALUE* argv, VALUE self);
|
226
|
+
static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self);
|
227
|
+
static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self);
|
228
|
+
static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self);
|
229
|
+
static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self);
|
230
|
+
static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self);
|
231
|
+
static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self);
|
232
|
+
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self);
|
233
|
+
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment);
|
234
|
+
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self);
|
235
|
+
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self);
|
236
|
+
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment);
|
237
|
+
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index);
|
238
|
+
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name);
|
239
|
+
static VALUE zipruby_archive_funchange(VALUE self, VALUE index);
|
240
|
+
static VALUE zipruby_archive_funchange_all(VALUE self);
|
241
|
+
static VALUE zipruby_archive_unchange(VALUE self);
|
242
|
+
static VALUE zipruby_archive_revert(VALUE self);
|
243
|
+
static VALUE zipruby_archive_each(VALUE self);
|
244
|
+
static VALUE zipruby_archive_commit(VALUE self);
|
245
|
+
static VALUE zipruby_archive_is_open(VALUE self);
|
246
|
+
static VALUE zipruby_archive_decrypt(VALUE self, VALUE password);
|
247
|
+
static VALUE zipruby_archive_encrypt(VALUE self, VALUE password);
|
248
|
+
static VALUE zipruby_archive_read(VALUE self);
|
249
|
+
|
250
|
+
extern VALUE Zip;
|
251
|
+
VALUE Archive;
|
252
|
+
extern VALUE File;
|
253
|
+
extern VALUE Stat;
|
254
|
+
extern VALUE Error;
|
255
|
+
|
256
|
+
void Init_zipruby_archive() {
|
257
|
+
Archive = rb_define_class_under(Zip, "Archive", rb_cObject);
|
258
|
+
rb_define_alloc_func(Archive, zipruby_archive_alloc);
|
259
|
+
rb_include_module(Archive, rb_mEnumerable);
|
260
|
+
rb_define_singleton_method(Archive, "open", zipruby_archive_s_open, -1);
|
261
|
+
rb_define_singleton_method(Archive, "open_buffer", zipruby_archive_s_open_buffer, -1);
|
262
|
+
rb_define_singleton_method(Archive, "decrypt", zipruby_archive_s_decrypt, 2);
|
263
|
+
rb_define_singleton_method(Archive, "encrypt", zipruby_archive_s_encrypt, 2);
|
264
|
+
rb_define_method(Archive, "close", zipruby_archive_close, 0);
|
265
|
+
rb_define_method(Archive, "num_files", zipruby_archive_num_files, 0);
|
266
|
+
rb_define_method(Archive, "get_name", zipruby_archive_get_name, -1);
|
267
|
+
rb_define_method(Archive, "fopen", zipruby_archive_fopen, -1);
|
268
|
+
rb_define_method(Archive, "get_stat", zipruby_archive_get_stat, -1);
|
269
|
+
rb_define_method(Archive, "add_buffer", zipruby_archive_add_buffer, 2);
|
270
|
+
rb_define_method(Archive, "add_file", zipruby_archive_add_file, -1);
|
271
|
+
rb_define_method(Archive, "add_io", zipruby_archive_add_io, 2);
|
272
|
+
rb_define_method(Archive, "add", zipruby_archive_add_function, -1);
|
273
|
+
rb_define_method(Archive, "replace_buffer", zipruby_archive_replace_buffer, -1);
|
274
|
+
rb_define_method(Archive, "replace_file", zipruby_archive_replace_file, -1);
|
275
|
+
rb_define_method(Archive, "replace_io", zipruby_archive_replace_io, -1);
|
276
|
+
rb_define_method(Archive, "replace", zipruby_archive_replace_function, -1);
|
277
|
+
rb_define_method(Archive, "add_or_replace_buffer", zipruby_archive_add_or_replace_buffer, -1);
|
278
|
+
rb_define_method(Archive, "add_or_replace_file", zipruby_archive_add_or_replace_file, -1);
|
279
|
+
rb_define_method(Archive, "add_or_replace_io", zipruby_archive_add_or_replace_io, -1);
|
280
|
+
rb_define_method(Archive, "add_or_replace", zipruby_archive_add_or_replace_function, -1);
|
281
|
+
rb_define_method(Archive, "update", zipruby_archive_update, -1);
|
282
|
+
rb_define_method(Archive, "<<", zipruby_archive_add_io, 2);
|
283
|
+
rb_define_method(Archive, "get_comment", zipruby_archive_get_comment, -1);
|
284
|
+
rb_define_method(Archive, "comment", zipruby_archive_get_comment, -1);
|
285
|
+
rb_define_method(Archive, "comment=", zipruby_archive_set_comment, 1);
|
286
|
+
rb_define_method(Archive, "locate_name", zipruby_archive_locate_name, -1);
|
287
|
+
rb_define_method(Archive, "get_fcomment", zipruby_archive_get_fcomment, -1);
|
288
|
+
rb_define_method(Archive, "set_fcomment", zipruby_archive_set_fcomment, 2);
|
289
|
+
rb_define_method(Archive, "fdelete", zipruby_archive_fdelete, 1);
|
290
|
+
rb_define_method(Archive, "frename", zipruby_archive_frename, 2);
|
291
|
+
rb_define_method(Archive, "funchange", zipruby_archive_funchange, 1);
|
292
|
+
rb_define_method(Archive, "funchange_all", zipruby_archive_funchange_all, 0);
|
293
|
+
rb_define_method(Archive, "unchange", zipruby_archive_unchange, 0);
|
294
|
+
rb_define_method(Archive, "frevert", zipruby_archive_unchange, 1);
|
295
|
+
rb_define_method(Archive, "revert", zipruby_archive_revert, 0);
|
296
|
+
rb_define_method(Archive, "each", zipruby_archive_each, 0);
|
297
|
+
rb_define_method(Archive, "commit", zipruby_archive_commit, 0);
|
298
|
+
rb_define_method(Archive, "open?", zipruby_archive_is_open, 0);
|
299
|
+
rb_define_method(Archive, "decrypt", zipruby_archive_decrypt, 1);
|
300
|
+
rb_define_method(Archive, "encrypt", zipruby_archive_encrypt, 1);
|
301
|
+
rb_define_method(Archive, "read", zipruby_archive_read, 0);
|
302
|
+
}
|
303
|
+
|
304
|
+
static VALUE zipruby_archive_alloc(VALUE klass) {
|
305
|
+
struct zipruby_archive *p = ALLOC(struct zipruby_archive);
|
306
|
+
|
307
|
+
p->archive = NULL;
|
308
|
+
p->path = Qnil;
|
309
|
+
p->flags = 0;
|
310
|
+
p->tmpfilnam = NULL;
|
311
|
+
p->buffer = Qnil;
|
312
|
+
|
313
|
+
return Data_Wrap_Struct(klass, zipruby_archive_mark, zipruby_archive_free, p);
|
314
|
+
}
|
315
|
+
|
316
|
+
static void zipruby_archive_mark(struct zipruby_archive *p) {
|
317
|
+
rb_gc_mark(p->path);
|
318
|
+
rb_gc_mark(p->buffer);
|
319
|
+
}
|
320
|
+
|
321
|
+
static void zipruby_archive_free(struct zipruby_archive *p) {
|
322
|
+
if (p->tmpfilnam) {
|
323
|
+
zipruby_rmtmp(p->tmpfilnam);
|
324
|
+
free(p->tmpfilnam);
|
325
|
+
}
|
326
|
+
|
327
|
+
xfree(p);
|
328
|
+
}
|
329
|
+
|
330
|
+
/* */
|
331
|
+
static VALUE zipruby_archive_s_open(int argc, VALUE *argv, VALUE self) {
|
332
|
+
VALUE path, flags;
|
333
|
+
VALUE archive;
|
334
|
+
struct zipruby_archive *p_archive;
|
335
|
+
int i_flags = 0;
|
336
|
+
int errorp;
|
337
|
+
|
338
|
+
rb_scan_args(argc, argv, "11", &path, &flags);
|
339
|
+
Check_Type(path, T_STRING);
|
340
|
+
|
341
|
+
if (!NIL_P(flags)) {
|
342
|
+
i_flags = NUM2INT(flags);
|
343
|
+
}
|
344
|
+
|
345
|
+
archive = rb_funcall(Archive, rb_intern("new"), 0);
|
346
|
+
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
347
|
+
|
348
|
+
if ((p_archive->archive = zip_open(RSTRING_PTR(path), i_flags, &errorp)) == NULL) {
|
349
|
+
char errstr[ERRSTR_BUFSIZE];
|
350
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
351
|
+
rb_raise(Error, "Open archive failed - %s: %s", RSTRING_PTR(path), errstr);
|
352
|
+
}
|
353
|
+
|
354
|
+
p_archive->path = path;
|
355
|
+
p_archive->flags = i_flags;
|
356
|
+
|
357
|
+
if (rb_block_given_p()) {
|
358
|
+
VALUE retval;
|
359
|
+
int status;
|
360
|
+
|
361
|
+
retval = rb_protect(rb_yield, archive, &status);
|
362
|
+
zipruby_archive_close(archive);
|
363
|
+
|
364
|
+
if (status != 0) {
|
365
|
+
rb_jump_tag(status);
|
366
|
+
}
|
367
|
+
|
368
|
+
return retval;
|
369
|
+
} else {
|
370
|
+
return archive;
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
/* */
|
375
|
+
static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
|
376
|
+
VALUE buffer, flags;
|
377
|
+
VALUE archive;
|
378
|
+
struct zipruby_archive *p_archive;
|
379
|
+
void *data = NULL;
|
380
|
+
int len = 0, i_flags = 0;
|
381
|
+
int errorp;
|
382
|
+
|
383
|
+
rb_scan_args(argc, argv, "02", &buffer, &flags);
|
384
|
+
|
385
|
+
if (FIXNUM_P(buffer) && NIL_P(flags)) {
|
386
|
+
flags = buffer;
|
387
|
+
buffer = Qnil;
|
388
|
+
}
|
389
|
+
|
390
|
+
if (!NIL_P(flags)) {
|
391
|
+
i_flags = NUM2INT(flags);
|
392
|
+
}
|
393
|
+
|
394
|
+
if (i_flags & ZIP_CREATE) {
|
395
|
+
if (!NIL_P(buffer)) {
|
396
|
+
Check_Type(buffer, T_STRING);
|
397
|
+
}
|
398
|
+
|
399
|
+
i_flags = (i_flags | ZIP_TRUNC);
|
400
|
+
} else if (TYPE(buffer) == T_STRING) {
|
401
|
+
data = RSTRING_PTR(buffer);
|
402
|
+
len = RSTRING_LEN(buffer);
|
403
|
+
} else if (rb_obj_is_instance_of(buffer, rb_cProc)) {
|
404
|
+
data = (void *) buffer;
|
405
|
+
len = -1;
|
406
|
+
} else {
|
407
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Proc)", rb_class2name(CLASS_OF(buffer)));
|
408
|
+
}
|
409
|
+
|
410
|
+
archive = rb_funcall(Archive, rb_intern("new"), 0);
|
411
|
+
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
412
|
+
|
413
|
+
if ((p_archive->tmpfilnam = zipruby_tmpnam(data, len)) == NULL) {
|
414
|
+
rb_raise(Error, "Open archive failed: Failed to create temporary file");
|
415
|
+
}
|
416
|
+
|
417
|
+
if ((p_archive->archive = zip_open(p_archive->tmpfilnam, i_flags, &errorp)) == NULL) {
|
418
|
+
char errstr[ERRSTR_BUFSIZE];
|
419
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
420
|
+
rb_raise(Error, "Open archive failed: %s", errstr);
|
421
|
+
}
|
422
|
+
|
423
|
+
p_archive->path = rb_str_new2(p_archive->tmpfilnam);
|
424
|
+
p_archive->flags = i_flags;
|
425
|
+
p_archive->buffer = buffer;
|
426
|
+
|
427
|
+
if (rb_block_given_p()) {
|
428
|
+
VALUE retval;
|
429
|
+
int status;
|
430
|
+
|
431
|
+
retval = rb_protect(rb_yield, archive, &status);
|
432
|
+
zipruby_archive_close(archive);
|
433
|
+
|
434
|
+
if (status != 0) {
|
435
|
+
rb_jump_tag(status);
|
436
|
+
}
|
437
|
+
|
438
|
+
return retval;
|
439
|
+
} else {
|
440
|
+
return archive;
|
441
|
+
}
|
442
|
+
}
|
443
|
+
|
444
|
+
/* */
|
445
|
+
static VALUE zipruby_archive_s_decrypt(VALUE self, VALUE path, VALUE password) {
|
446
|
+
int res;
|
447
|
+
int errorp, wrongpwd;
|
448
|
+
long pwdlen;
|
449
|
+
|
450
|
+
Check_Type(path, T_STRING);
|
451
|
+
Check_Type(password, T_STRING);
|
452
|
+
pwdlen = RSTRING_LEN(password);
|
453
|
+
|
454
|
+
if (pwdlen < 1) {
|
455
|
+
rb_raise(Error, "Decrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
|
456
|
+
} else if (pwdlen > 0xff) {
|
457
|
+
rb_raise(Error, "Decrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
|
458
|
+
}
|
459
|
+
|
460
|
+
res = zip_decrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp, &wrongpwd);
|
461
|
+
|
462
|
+
if (res == -1) {
|
463
|
+
if (wrongpwd) {
|
464
|
+
rb_raise(Error, "Decrypt archive failed - %s: Wrong password", RSTRING_PTR(path));
|
465
|
+
} else {
|
466
|
+
char errstr[ERRSTR_BUFSIZE];
|
467
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
468
|
+
rb_raise(Error, "Decrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
|
469
|
+
}
|
470
|
+
}
|
471
|
+
|
472
|
+
return (res > 0) ? Qtrue : Qfalse;
|
473
|
+
}
|
474
|
+
|
475
|
+
/* */
|
476
|
+
static VALUE zipruby_archive_s_encrypt(VALUE self, VALUE path, VALUE password) {
|
477
|
+
int res;
|
478
|
+
int errorp;
|
479
|
+
long pwdlen;
|
480
|
+
|
481
|
+
Check_Type(path, T_STRING);
|
482
|
+
Check_Type(password, T_STRING);
|
483
|
+
pwdlen = RSTRING_LEN(password);
|
484
|
+
|
485
|
+
if (pwdlen < 1) {
|
486
|
+
rb_raise(Error, "Encrypt archive failed - %s: Password is empty", RSTRING_PTR(path));
|
487
|
+
} else if (pwdlen > 0xff) {
|
488
|
+
rb_raise(Error, "Encrypt archive failed - %s: Password is too long", RSTRING_PTR(path));
|
489
|
+
}
|
490
|
+
|
491
|
+
res = zip_encrypt(RSTRING_PTR(path), RSTRING_PTR(password), pwdlen, &errorp);
|
492
|
+
|
493
|
+
if (res == -1) {
|
494
|
+
char errstr[ERRSTR_BUFSIZE];
|
495
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
496
|
+
rb_raise(Error, "Encrypt archive failed - %s: %s", RSTRING_PTR(path), errstr);
|
497
|
+
}
|
498
|
+
|
499
|
+
return (res > 0) ? Qtrue : Qfalse;
|
500
|
+
}
|
501
|
+
|
502
|
+
/* */
|
503
|
+
static VALUE zipruby_archive_close(VALUE self) {
|
504
|
+
struct zipruby_archive *p_archive;
|
505
|
+
int changed, survivors;
|
506
|
+
|
507
|
+
if (!zipruby_archive_is_open(self)) {
|
508
|
+
return Qfalse;
|
509
|
+
}
|
510
|
+
|
511
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
512
|
+
Check_Archive(p_archive);
|
513
|
+
|
514
|
+
changed = _zip_changed(p_archive->archive, &survivors);
|
515
|
+
|
516
|
+
if (zip_close(p_archive->archive) == -1) {
|
517
|
+
zip_unchange_all(p_archive->archive);
|
518
|
+
zip_unchange_archive(p_archive->archive);
|
519
|
+
rb_raise(Error, "Close archive failed: %s", zip_strerror(p_archive->archive));
|
520
|
+
}
|
521
|
+
|
522
|
+
if (!NIL_P(p_archive->buffer) && changed) {
|
523
|
+
rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
|
524
|
+
}
|
525
|
+
|
526
|
+
zipruby_rmtmp(p_archive->tmpfilnam);
|
527
|
+
p_archive->archive = NULL;
|
528
|
+
p_archive->flags = 0;
|
529
|
+
|
530
|
+
return Qtrue;
|
531
|
+
}
|
532
|
+
|
533
|
+
/* */
|
534
|
+
static VALUE zipruby_archive_num_files(VALUE self) {
|
535
|
+
struct zipruby_archive *p_archive;
|
536
|
+
int num_files;
|
537
|
+
|
538
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
539
|
+
Check_Archive(p_archive);
|
540
|
+
num_files = zip_get_num_files(p_archive->archive);
|
541
|
+
|
542
|
+
return INT2NUM(num_files);
|
543
|
+
}
|
544
|
+
|
545
|
+
/* */
|
546
|
+
static VALUE zipruby_archive_get_name(int argc, VALUE *argv, VALUE self) {
|
547
|
+
VALUE index, flags;
|
548
|
+
struct zipruby_archive *p_archive;
|
549
|
+
int i_flags = 0;
|
550
|
+
const char *name;
|
551
|
+
|
552
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
553
|
+
Check_Type(index, T_FIXNUM);
|
554
|
+
|
555
|
+
if (!NIL_P(flags)) {
|
556
|
+
i_flags = NUM2INT(flags);
|
557
|
+
}
|
558
|
+
|
559
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
560
|
+
Check_Archive(p_archive);
|
561
|
+
|
562
|
+
if ((name = zip_get_name(p_archive->archive, NUM2INT(index), i_flags)) == NULL) {
|
563
|
+
rb_raise(Error, "Get name failed at %d: %s", index, zip_strerror(p_archive->archive));
|
564
|
+
}
|
565
|
+
|
566
|
+
return (name != NULL) ? rb_str_new2(name) : Qnil;
|
567
|
+
}
|
568
|
+
|
569
|
+
/* */
|
570
|
+
static VALUE zipruby_archive_fopen(int argc, VALUE *argv, VALUE self) {
|
571
|
+
VALUE index, flags, stat_flags, file;
|
572
|
+
|
573
|
+
rb_scan_args(argc, argv, "12", &index, &flags, &stat_flags);
|
574
|
+
file = rb_funcall(File, rb_intern("new"), 4, self, index, flags, stat_flags);
|
575
|
+
|
576
|
+
if (rb_block_given_p()) {
|
577
|
+
VALUE retval;
|
578
|
+
int status;
|
579
|
+
|
580
|
+
retval = rb_protect(rb_yield, file, &status);
|
581
|
+
rb_funcall(file, rb_intern("close"), 0);
|
582
|
+
|
583
|
+
if (status != 0) {
|
584
|
+
rb_jump_tag(status);
|
585
|
+
}
|
586
|
+
|
587
|
+
return retval;
|
588
|
+
} else {
|
589
|
+
return file;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
|
593
|
+
/* */
|
594
|
+
static VALUE zipruby_archive_get_stat(int argc, VALUE *argv, VALUE self) {
|
595
|
+
VALUE index, flags;
|
596
|
+
|
597
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
598
|
+
|
599
|
+
return rb_funcall(Stat, rb_intern("new"), 3, self, index, flags);
|
600
|
+
}
|
601
|
+
|
602
|
+
/* */
|
603
|
+
static VALUE zipruby_archive_add_buffer(VALUE self, VALUE name, VALUE source) {
|
604
|
+
struct zipruby_archive *p_archive;
|
605
|
+
struct zip_source *zsource;
|
606
|
+
char *data;
|
607
|
+
size_t len;
|
608
|
+
|
609
|
+
Check_Type(name, T_STRING);
|
610
|
+
Check_Type(source, T_STRING);
|
611
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
612
|
+
Check_Archive(p_archive);
|
613
|
+
|
614
|
+
len = RSTRING_LEN(source);
|
615
|
+
|
616
|
+
if ((data = malloc(len)) == NULL) {
|
617
|
+
rb_raise(rb_eRuntimeError, "Add file failed: Cannot allocate memory");
|
618
|
+
}
|
619
|
+
|
620
|
+
memset(data, 0, len);
|
621
|
+
memcpy(data, RSTRING_PTR(source), len);
|
622
|
+
|
623
|
+
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
624
|
+
free(data);
|
625
|
+
rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
626
|
+
}
|
627
|
+
|
628
|
+
if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
|
629
|
+
zip_source_free(zsource);
|
630
|
+
zip_unchange_all(p_archive->archive);
|
631
|
+
zip_unchange_archive(p_archive->archive);
|
632
|
+
rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
633
|
+
}
|
634
|
+
|
635
|
+
return Qnil;
|
636
|
+
}
|
637
|
+
|
638
|
+
/* */
|
639
|
+
static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
|
640
|
+
struct zipruby_archive *p_archive;
|
641
|
+
struct zip_source *zsource;
|
642
|
+
VALUE index, source, flags;
|
643
|
+
int i_index, i_flags = 0;
|
644
|
+
char *data;
|
645
|
+
size_t len;
|
646
|
+
|
647
|
+
rb_scan_args(argc, argv, "21", &index, &source, &flags);
|
648
|
+
|
649
|
+
if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
|
650
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
|
651
|
+
}
|
652
|
+
|
653
|
+
if (!NIL_P(flags)) {
|
654
|
+
i_flags = NUM2INT(flags);
|
655
|
+
}
|
656
|
+
|
657
|
+
Check_Type(source, T_STRING);
|
658
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
659
|
+
Check_Archive(p_archive);
|
660
|
+
|
661
|
+
if (FIXNUM_P(index)) {
|
662
|
+
i_index = NUM2INT(index);
|
663
|
+
} else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
|
664
|
+
rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
|
665
|
+
}
|
666
|
+
|
667
|
+
len = RSTRING_LEN(source);
|
668
|
+
|
669
|
+
if ((data = malloc(len)) == NULL) {
|
670
|
+
rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
|
671
|
+
}
|
672
|
+
|
673
|
+
memcpy(data, RSTRING_PTR(source), len);
|
674
|
+
|
675
|
+
if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
|
676
|
+
free(data);
|
677
|
+
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
678
|
+
}
|
679
|
+
|
680
|
+
if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
|
681
|
+
zip_source_free(zsource);
|
682
|
+
zip_unchange_all(p_archive->archive);
|
683
|
+
zip_unchange_archive(p_archive->archive);
|
684
|
+
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
685
|
+
}
|
686
|
+
|
687
|
+
return Qnil;
|
688
|
+
}
|
689
|
+
|
690
|
+
/* */
|
691
|
+
static VALUE zipruby_archive_add_or_replace_buffer(int argc, VALUE *argv, VALUE self) {
|
692
|
+
struct zipruby_archive *p_archive;
|
693
|
+
VALUE name, source, flags;
|
694
|
+
int index, i_flags = 0;
|
695
|
+
|
696
|
+
rb_scan_args(argc, argv, "21", &name, &source, &flags);
|
697
|
+
|
698
|
+
if (!NIL_P(flags)) {
|
699
|
+
i_flags = NUM2INT(flags);
|
700
|
+
}
|
701
|
+
|
702
|
+
Check_Type(name, T_STRING);
|
703
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
704
|
+
Check_Archive(p_archive);
|
705
|
+
|
706
|
+
index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
|
707
|
+
|
708
|
+
if (index >= 0) {
|
709
|
+
VALUE _args[] = { INT2NUM(index), source };
|
710
|
+
return zipruby_archive_replace_buffer(2, _args, self);
|
711
|
+
} else {
|
712
|
+
return zipruby_archive_add_buffer(self, name, source);
|
713
|
+
}
|
714
|
+
}
|
715
|
+
|
716
|
+
/* */
|
717
|
+
static VALUE zipruby_archive_add_file(int argc, VALUE *argv, VALUE self) {
|
718
|
+
VALUE name, fname;
|
719
|
+
struct zipruby_archive *p_archive;
|
720
|
+
struct zip_source *zsource;
|
721
|
+
|
722
|
+
rb_scan_args(argc, argv, "11", &name, &fname);
|
723
|
+
|
724
|
+
if (NIL_P(fname)) {
|
725
|
+
fname = name;
|
726
|
+
name = Qnil;
|
727
|
+
}
|
728
|
+
|
729
|
+
Check_Type(fname, T_STRING);
|
730
|
+
|
731
|
+
if (NIL_P(name)) {
|
732
|
+
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
|
733
|
+
}
|
734
|
+
|
735
|
+
Check_Type(name, T_STRING);
|
736
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
737
|
+
Check_Archive(p_archive);
|
738
|
+
|
739
|
+
if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
|
740
|
+
rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
741
|
+
}
|
742
|
+
|
743
|
+
if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
|
744
|
+
zip_source_free(zsource);
|
745
|
+
zip_unchange_all(p_archive->archive);
|
746
|
+
zip_unchange_archive(p_archive->archive);
|
747
|
+
rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
748
|
+
}
|
749
|
+
|
750
|
+
return Qnil;
|
751
|
+
}
|
752
|
+
|
753
|
+
/* */
|
754
|
+
static VALUE zipruby_archive_replace_file(int argc, VALUE* argv, VALUE self) {
|
755
|
+
struct zipruby_archive *p_archive;
|
756
|
+
struct zip_source *zsource;
|
757
|
+
VALUE index, fname, flags;
|
758
|
+
int i_index, i_flags = 0;
|
759
|
+
|
760
|
+
rb_scan_args(argc, argv, "21", &index, &fname, &flags);
|
761
|
+
|
762
|
+
if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
|
763
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
|
764
|
+
}
|
765
|
+
|
766
|
+
if (!NIL_P(flags)) {
|
767
|
+
i_flags = NUM2INT(flags);
|
768
|
+
}
|
769
|
+
|
770
|
+
Check_Type(fname, T_STRING);
|
771
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
772
|
+
Check_Archive(p_archive);
|
773
|
+
|
774
|
+
if (FIXNUM_P(index)) {
|
775
|
+
i_index = NUM2INT(index);
|
776
|
+
} else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
|
777
|
+
rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
|
778
|
+
}
|
779
|
+
|
780
|
+
if ((zsource = zip_source_file(p_archive->archive, RSTRING_PTR(fname), 0, -1)) == NULL) {
|
781
|
+
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
782
|
+
}
|
783
|
+
|
784
|
+
if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
|
785
|
+
zip_source_free(zsource);
|
786
|
+
zip_unchange_all(p_archive->archive);
|
787
|
+
zip_unchange_archive(p_archive->archive);
|
788
|
+
rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
789
|
+
}
|
790
|
+
|
791
|
+
return Qnil;
|
792
|
+
}
|
793
|
+
|
794
|
+
/* */
|
795
|
+
static VALUE zipruby_archive_add_or_replace_file(int argc, VALUE *argv, VALUE self) {
|
796
|
+
VALUE name, fname, flags;
|
797
|
+
struct zipruby_archive *p_archive;
|
798
|
+
int index, i_flags = 0;
|
799
|
+
|
800
|
+
rb_scan_args(argc, argv, "12", &name, &fname, &flags);
|
801
|
+
|
802
|
+
if (NIL_P(flags) && FIXNUM_P(fname)) {
|
803
|
+
flags = fname;
|
804
|
+
fname = Qnil;
|
805
|
+
}
|
806
|
+
|
807
|
+
if (NIL_P(fname)) {
|
808
|
+
fname = name;
|
809
|
+
name = Qnil;
|
810
|
+
}
|
811
|
+
|
812
|
+
Check_Type(fname, T_STRING);
|
813
|
+
|
814
|
+
if (NIL_P(name)) {
|
815
|
+
name = rb_funcall(rb_cFile, rb_intern("basename"), 1, fname);
|
816
|
+
}
|
817
|
+
|
818
|
+
if (!NIL_P(flags)) {
|
819
|
+
i_flags = NUM2INT(flags);
|
820
|
+
}
|
821
|
+
|
822
|
+
Check_Type(name, T_STRING);
|
823
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
824
|
+
Check_Archive(p_archive);
|
825
|
+
|
826
|
+
index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
|
827
|
+
|
828
|
+
if (index >= 0) {
|
829
|
+
VALUE _args[] = { INT2NUM(index), fname };
|
830
|
+
return zipruby_archive_replace_file(2, _args, self);
|
831
|
+
} else {
|
832
|
+
VALUE _args[] = { name, fname };
|
833
|
+
return zipruby_archive_add_file(2, _args, self);
|
834
|
+
}
|
835
|
+
}
|
836
|
+
|
837
|
+
/* */
|
838
|
+
static VALUE zipruby_archive_add_io(VALUE self, VALUE name, VALUE io) {
|
839
|
+
VALUE source;
|
840
|
+
|
841
|
+
Check_IO(io);
|
842
|
+
source = rb_funcall(io, rb_intern("read"), 0);
|
843
|
+
|
844
|
+
return zipruby_archive_add_buffer(self, name, source);
|
845
|
+
}
|
846
|
+
|
847
|
+
/* */
|
848
|
+
static VALUE zipruby_archive_replace_io(int argc, VALUE *argv, VALUE self) {
|
849
|
+
VALUE source, io, index, flags;
|
850
|
+
VALUE _args[3];
|
851
|
+
|
852
|
+
rb_scan_args(argc, argv, "21", &index, &io, &flags);
|
853
|
+
Check_IO(io);
|
854
|
+
source = rb_funcall(io, rb_intern("read"), 0);
|
855
|
+
|
856
|
+
_args[0] = index;
|
857
|
+
_args[1] = source;
|
858
|
+
_args[2] = flags;
|
859
|
+
return zipruby_archive_replace_buffer(2, _args, self);
|
860
|
+
}
|
861
|
+
|
862
|
+
/* */
|
863
|
+
static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self) {
|
864
|
+
VALUE name, io, flags;
|
865
|
+
struct zipruby_archive *p_archive;
|
866
|
+
int index, i_flags = 0;
|
867
|
+
|
868
|
+
rb_scan_args(argc, argv, "21", &name, &io, &flags);
|
869
|
+
Check_IO(io);
|
870
|
+
|
871
|
+
if (!NIL_P(flags)) {
|
872
|
+
i_flags = NUM2INT(flags);
|
873
|
+
}
|
874
|
+
|
875
|
+
Check_Type(name, T_STRING);
|
876
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
877
|
+
Check_Archive(p_archive);
|
878
|
+
|
879
|
+
index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
|
880
|
+
|
881
|
+
if (index >= 0) {
|
882
|
+
VALUE _args[] = {INT2NUM(index), io, flags};
|
883
|
+
return zipruby_archive_replace_io(2, _args, self);
|
884
|
+
} else {
|
885
|
+
return zipruby_archive_add_io(self, name, io);
|
886
|
+
}
|
887
|
+
}
|
888
|
+
|
889
|
+
/* */
|
890
|
+
static VALUE zipruby_archive_add_function(int argc, VALUE *argv, VALUE self) {
|
891
|
+
VALUE name, mtime;
|
892
|
+
struct zipruby_archive *p_archive;
|
893
|
+
struct zip_source *zsource;
|
894
|
+
struct read_proc *z;
|
895
|
+
|
896
|
+
rb_scan_args(argc, argv, "11", &name, &mtime);
|
897
|
+
rb_need_block();
|
898
|
+
Check_Type(name, T_STRING);
|
899
|
+
|
900
|
+
if (NIL_P(mtime)) {
|
901
|
+
mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
|
902
|
+
} else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
|
903
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
|
904
|
+
}
|
905
|
+
|
906
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
907
|
+
Check_Archive(p_archive);
|
908
|
+
|
909
|
+
if ((z = malloc(sizeof(struct read_proc))) == NULL) {
|
910
|
+
zip_unchange_all(p_archive->archive);
|
911
|
+
zip_unchange_archive(p_archive->archive);
|
912
|
+
rb_raise(rb_eRuntimeError, "Add failed - %s: Cannot allocate memory", RSTRING_PTR(name));
|
913
|
+
}
|
914
|
+
|
915
|
+
z->proc = rb_block_proc();
|
916
|
+
z->mtime = mtime;
|
917
|
+
|
918
|
+
if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
|
919
|
+
free(z);
|
920
|
+
rb_raise(Error, "Add failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
921
|
+
}
|
922
|
+
|
923
|
+
if (zip_add(p_archive->archive, RSTRING_PTR(name), zsource) == -1) {
|
924
|
+
zip_source_free(zsource);
|
925
|
+
zip_unchange_all(p_archive->archive);
|
926
|
+
zip_unchange_archive(p_archive->archive);
|
927
|
+
rb_raise(Error, "Add file failed - %s: %s", RSTRING_PTR(name), zip_strerror(p_archive->archive));
|
928
|
+
}
|
929
|
+
|
930
|
+
return Qnil;
|
931
|
+
}
|
932
|
+
|
933
|
+
/* */
|
934
|
+
static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
|
935
|
+
VALUE index, mtime;
|
936
|
+
struct zipruby_archive *p_archive;
|
937
|
+
struct zip_source *zsource;
|
938
|
+
struct read_proc *z;
|
939
|
+
|
940
|
+
rb_scan_args(argc, argv, "11", &index, &mtime);
|
941
|
+
rb_need_block();
|
942
|
+
Check_Type(index, T_FIXNUM);
|
943
|
+
|
944
|
+
if (NIL_P(mtime)) {
|
945
|
+
mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
|
946
|
+
} else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
|
947
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
|
948
|
+
}
|
949
|
+
|
950
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
951
|
+
Check_Archive(p_archive);
|
952
|
+
|
953
|
+
if ((z = malloc(sizeof(struct read_proc))) == NULL) {
|
954
|
+
zip_unchange_all(p_archive->archive);
|
955
|
+
zip_unchange_archive(p_archive->archive);
|
956
|
+
rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", NUM2INT(index));
|
957
|
+
}
|
958
|
+
|
959
|
+
z->proc = rb_block_proc();
|
960
|
+
z->mtime = mtime;
|
961
|
+
|
962
|
+
if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
|
963
|
+
free(z);
|
964
|
+
rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
965
|
+
}
|
966
|
+
|
967
|
+
if (zip_replace(p_archive->archive, NUM2INT(index), zsource) == -1) {
|
968
|
+
zip_source_free(zsource);
|
969
|
+
zip_unchange_all(p_archive->archive);
|
970
|
+
zip_unchange_archive(p_archive->archive);
|
971
|
+
rb_raise(Error, "Replace failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
972
|
+
}
|
973
|
+
|
974
|
+
return Qnil;
|
975
|
+
}
|
976
|
+
|
977
|
+
/* */
|
978
|
+
static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
|
979
|
+
VALUE name, mtime, flags;
|
980
|
+
struct zipruby_archive *p_archive;
|
981
|
+
int index, i_flags = 0;
|
982
|
+
|
983
|
+
rb_scan_args(argc, argv, "12", &name, &mtime, &flags);
|
984
|
+
|
985
|
+
if (NIL_P(flags) && FIXNUM_P(mtime)) {
|
986
|
+
flags = mtime;
|
987
|
+
mtime = Qnil;
|
988
|
+
}
|
989
|
+
|
990
|
+
if (!NIL_P(flags)) {
|
991
|
+
i_flags = NUM2INT(flags);
|
992
|
+
}
|
993
|
+
|
994
|
+
Check_Type(name, T_STRING);
|
995
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
996
|
+
Check_Archive(p_archive);
|
997
|
+
|
998
|
+
index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);
|
999
|
+
|
1000
|
+
if (index >= 0) {
|
1001
|
+
VALUE _args[] = { INT2NUM(index), mtime };
|
1002
|
+
return zipruby_archive_replace_function(2, _args, self);
|
1003
|
+
} else {
|
1004
|
+
VALUE _args[] = { name, mtime };
|
1005
|
+
return zipruby_archive_add_function(2, _args, self);
|
1006
|
+
}
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
/* */
|
1010
|
+
static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self) {
|
1011
|
+
struct zipruby_archive *p_archive, *p_srcarchive;
|
1012
|
+
VALUE srcarchive, flags;
|
1013
|
+
int i, num_files, i_flags = 0;
|
1014
|
+
|
1015
|
+
rb_scan_args(argc, argv, "11", &srcarchive, &flags);
|
1016
|
+
|
1017
|
+
if (!rb_obj_is_instance_of(srcarchive, Archive)) {
|
1018
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
|
1019
|
+
}
|
1020
|
+
|
1021
|
+
if (!NIL_P(flags)) {
|
1022
|
+
i_flags = NUM2INT(flags);
|
1023
|
+
}
|
1024
|
+
|
1025
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1026
|
+
Check_Archive(p_archive);
|
1027
|
+
Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
|
1028
|
+
Check_Archive(p_srcarchive);
|
1029
|
+
|
1030
|
+
num_files = zip_get_num_files(p_srcarchive->archive);
|
1031
|
+
|
1032
|
+
for (i = 0; i < num_files; i++) {
|
1033
|
+
struct zip_source *zsource;
|
1034
|
+
struct zip_file *fzip;
|
1035
|
+
struct zip_stat sb;
|
1036
|
+
char *buf;
|
1037
|
+
const char *name;
|
1038
|
+
int index, error;
|
1039
|
+
|
1040
|
+
zip_stat_init(&sb);
|
1041
|
+
|
1042
|
+
if (zip_stat_index(p_srcarchive->archive, i, 0, &sb)) {
|
1043
|
+
zip_unchange_all(p_archive->archive);
|
1044
|
+
zip_unchange_archive(p_archive->archive);
|
1045
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
1046
|
+
}
|
1047
|
+
|
1048
|
+
if ((buf = malloc(sb.size)) == NULL) {
|
1049
|
+
zip_unchange_all(p_archive->archive);
|
1050
|
+
zip_unchange_archive(p_archive->archive);
|
1051
|
+
rb_raise(rb_eRuntimeError, "Update archive failed: Cannot allocate memory");
|
1052
|
+
}
|
1053
|
+
|
1054
|
+
fzip = zip_fopen_index(p_srcarchive->archive, i, 0);
|
1055
|
+
|
1056
|
+
if (fzip == NULL) {
|
1057
|
+
free(buf);
|
1058
|
+
zip_unchange_all(p_archive->archive);
|
1059
|
+
zip_unchange_archive(p_archive->archive);
|
1060
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
1061
|
+
}
|
1062
|
+
|
1063
|
+
if (zip_fread(fzip, buf, sb.size) == -1) {
|
1064
|
+
free(buf);
|
1065
|
+
zip_fclose(fzip);
|
1066
|
+
zip_unchange_all(p_archive->archive);
|
1067
|
+
zip_unchange_archive(p_archive->archive);
|
1068
|
+
rb_raise(Error, "Update archive failed: %s", zip_file_strerror(fzip));
|
1069
|
+
}
|
1070
|
+
|
1071
|
+
if ((error = zip_fclose(fzip)) != 0) {
|
1072
|
+
char errstr[ERRSTR_BUFSIZE];
|
1073
|
+
free(buf);
|
1074
|
+
zip_unchange_all(p_archive->archive);
|
1075
|
+
zip_unchange_archive(p_archive->archive);
|
1076
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
|
1077
|
+
rb_raise(Error, "Update archive failed: %s", errstr);
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
if ((zsource = zip_source_buffer(p_archive->archive, buf, sb.size, 1)) == NULL) {
|
1081
|
+
free(buf);
|
1082
|
+
zip_unchange_all(p_archive->archive);
|
1083
|
+
zip_unchange_archive(p_archive->archive);
|
1084
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
1085
|
+
}
|
1086
|
+
|
1087
|
+
if ((name = zip_get_name(p_srcarchive->archive, i, 0)) == NULL) {
|
1088
|
+
zip_source_free(zsource);
|
1089
|
+
zip_unchange_all(p_archive->archive);
|
1090
|
+
zip_unchange_archive(p_archive->archive);
|
1091
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
|
1092
|
+
}
|
1093
|
+
|
1094
|
+
index = zip_name_locate(p_archive->archive, name, i_flags);
|
1095
|
+
|
1096
|
+
if (index >= 0) {
|
1097
|
+
if (zip_replace(p_archive->archive, i, zsource) == -1) {
|
1098
|
+
zip_source_free(zsource);
|
1099
|
+
zip_unchange_all(p_archive->archive);
|
1100
|
+
zip_unchange_archive(p_archive->archive);
|
1101
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
1102
|
+
}
|
1103
|
+
} else {
|
1104
|
+
if (zip_add(p_archive->archive, name, zsource) == -1) {
|
1105
|
+
zip_source_free(zsource);
|
1106
|
+
zip_unchange_all(p_archive->archive);
|
1107
|
+
zip_unchange_archive(p_archive->archive);
|
1108
|
+
rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
|
1109
|
+
}
|
1110
|
+
}
|
1111
|
+
}
|
1112
|
+
|
1113
|
+
return Qnil;
|
1114
|
+
}
|
1115
|
+
|
1116
|
+
/* */
|
1117
|
+
static VALUE zipruby_archive_get_comment(int argc, VALUE *argv, VALUE self) {
|
1118
|
+
VALUE flags;
|
1119
|
+
struct zipruby_archive *p_archive;
|
1120
|
+
const char *comment;
|
1121
|
+
int lenp, i_flags = 0;
|
1122
|
+
|
1123
|
+
rb_scan_args(argc, argv, "01", &flags);
|
1124
|
+
|
1125
|
+
if (!NIL_P(flags)) {
|
1126
|
+
i_flags = NUM2INT(flags);
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1130
|
+
Check_Archive(p_archive);
|
1131
|
+
|
1132
|
+
// XXX: How is the error checked?
|
1133
|
+
comment = zip_get_archive_comment(p_archive->archive, &lenp, i_flags);
|
1134
|
+
|
1135
|
+
return comment ? rb_str_new(comment, lenp) : Qnil;
|
1136
|
+
}
|
1137
|
+
|
1138
|
+
/* */
|
1139
|
+
static VALUE zipruby_archive_set_comment(VALUE self, VALUE comment) {
|
1140
|
+
struct zipruby_archive *p_archive;
|
1141
|
+
const char *s_comment = NULL;
|
1142
|
+
int len = 0;
|
1143
|
+
|
1144
|
+
if (!NIL_P(comment)) {
|
1145
|
+
Check_Type(comment, T_STRING);
|
1146
|
+
s_comment = RSTRING_PTR(comment);
|
1147
|
+
len = RSTRING_LEN(comment);
|
1148
|
+
}
|
1149
|
+
|
1150
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1151
|
+
Check_Archive(p_archive);
|
1152
|
+
|
1153
|
+
if (zip_set_archive_comment(p_archive->archive, s_comment, len) == -1) {
|
1154
|
+
zip_unchange_all(p_archive->archive);
|
1155
|
+
zip_unchange_archive(p_archive->archive);
|
1156
|
+
rb_raise(Error, "Comment archived failed: %s", zip_strerror(p_archive->archive));
|
1157
|
+
}
|
1158
|
+
|
1159
|
+
return Qnil;
|
1160
|
+
}
|
1161
|
+
|
1162
|
+
/* */
|
1163
|
+
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
|
1164
|
+
VALUE fname, flags;
|
1165
|
+
struct zipruby_archive *p_archive;
|
1166
|
+
int i_flags = 0;
|
1167
|
+
|
1168
|
+
rb_scan_args(argc, argv, "11", &fname, &flags);
|
1169
|
+
Check_Type(fname, T_STRING);
|
1170
|
+
|
1171
|
+
if (!NIL_P(flags)) {
|
1172
|
+
i_flags = NUM2INT(flags);
|
1173
|
+
}
|
1174
|
+
|
1175
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1176
|
+
Check_Archive(p_archive);
|
1177
|
+
|
1178
|
+
return INT2NUM(zip_name_locate(p_archive->archive, RSTRING_PTR(fname), i_flags));
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
/* */
|
1182
|
+
static VALUE zipruby_archive_get_fcomment(int argc, VALUE *argv, VALUE self) {
|
1183
|
+
VALUE index, flags;
|
1184
|
+
struct zipruby_archive *p_archive;
|
1185
|
+
const char *comment;
|
1186
|
+
int lenp, i_flags = 0;
|
1187
|
+
|
1188
|
+
rb_scan_args(argc, argv, "11", &index, &flags);
|
1189
|
+
|
1190
|
+
if (!NIL_P(flags)) {
|
1191
|
+
i_flags = NUM2INT(flags);
|
1192
|
+
}
|
1193
|
+
|
1194
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1195
|
+
Check_Archive(p_archive);
|
1196
|
+
|
1197
|
+
// XXX: How is the error checked?
|
1198
|
+
comment = zip_get_file_comment(p_archive->archive, NUM2INT(index), &lenp, i_flags);
|
1199
|
+
|
1200
|
+
return comment ? rb_str_new(comment, lenp) : Qnil;
|
1201
|
+
}
|
1202
|
+
|
1203
|
+
/* */
|
1204
|
+
static VALUE zipruby_archive_set_fcomment(VALUE self, VALUE index, VALUE comment) {
|
1205
|
+
struct zipruby_archive *p_archive;
|
1206
|
+
char *s_comment = NULL;
|
1207
|
+
int len = 0;
|
1208
|
+
|
1209
|
+
if (!NIL_P(comment)) {
|
1210
|
+
Check_Type(comment, T_STRING);
|
1211
|
+
s_comment = RSTRING_PTR(comment);
|
1212
|
+
len = RSTRING_LEN(comment);
|
1213
|
+
}
|
1214
|
+
|
1215
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1216
|
+
Check_Archive(p_archive);
|
1217
|
+
|
1218
|
+
if (zip_set_file_comment(p_archive->archive, NUM2INT(index), s_comment, len) == -1) {
|
1219
|
+
zip_unchange_all(p_archive->archive);
|
1220
|
+
zip_unchange_archive(p_archive->archive);
|
1221
|
+
rb_raise(Error, "Comment file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
1222
|
+
}
|
1223
|
+
|
1224
|
+
return Qnil;
|
1225
|
+
}
|
1226
|
+
|
1227
|
+
/* */
|
1228
|
+
static VALUE zipruby_archive_fdelete(VALUE self, VALUE index) {
|
1229
|
+
struct zipruby_archive *p_archive;
|
1230
|
+
|
1231
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1232
|
+
Check_Archive(p_archive);
|
1233
|
+
|
1234
|
+
if (zip_delete(p_archive->archive, NUM2INT(index)) == -1) {
|
1235
|
+
zip_unchange_all(p_archive->archive);
|
1236
|
+
zip_unchange_archive(p_archive->archive);
|
1237
|
+
rb_raise(Error, "Delete file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
1238
|
+
}
|
1239
|
+
|
1240
|
+
return Qnil;
|
1241
|
+
}
|
1242
|
+
|
1243
|
+
/* */
|
1244
|
+
static VALUE zipruby_archive_frename(VALUE self, VALUE index, VALUE name) {
|
1245
|
+
struct zipruby_archive *p_archive;
|
1246
|
+
|
1247
|
+
Check_Type(name, T_STRING);
|
1248
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1249
|
+
Check_Archive(p_archive);
|
1250
|
+
|
1251
|
+
if (zip_rename(p_archive->archive, NUM2INT(index), RSTRING_PTR(name)) == -1) {
|
1252
|
+
zip_unchange_all(p_archive->archive);
|
1253
|
+
zip_unchange_archive(p_archive->archive);
|
1254
|
+
rb_raise(Error, "Rename file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
1255
|
+
}
|
1256
|
+
|
1257
|
+
return Qnil;
|
1258
|
+
}
|
1259
|
+
|
1260
|
+
/* */
|
1261
|
+
static VALUE zipruby_archive_funchange(VALUE self, VALUE index) {
|
1262
|
+
struct zipruby_archive *p_archive;
|
1263
|
+
|
1264
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1265
|
+
Check_Archive(p_archive);
|
1266
|
+
|
1267
|
+
if (zip_unchange(p_archive->archive, NUM2INT(index)) == -1) {
|
1268
|
+
zip_unchange_all(p_archive->archive);
|
1269
|
+
zip_unchange_archive(p_archive->archive);
|
1270
|
+
rb_raise(Error, "Unchange file failed at %d: %s", NUM2INT(index), zip_strerror(p_archive->archive));
|
1271
|
+
}
|
1272
|
+
|
1273
|
+
return Qnil;
|
1274
|
+
}
|
1275
|
+
|
1276
|
+
/* */
|
1277
|
+
static VALUE zipruby_archive_funchange_all(VALUE self) {
|
1278
|
+
struct zipruby_archive *p_archive;
|
1279
|
+
|
1280
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1281
|
+
Check_Archive(p_archive);
|
1282
|
+
|
1283
|
+
if (zip_unchange_all(p_archive->archive) == -1) {
|
1284
|
+
rb_raise(Error, "Unchange all file failed: %s", zip_strerror(p_archive->archive));
|
1285
|
+
}
|
1286
|
+
|
1287
|
+
return Qnil;
|
1288
|
+
}
|
1289
|
+
|
1290
|
+
/* */
|
1291
|
+
static VALUE zipruby_archive_unchange(VALUE self) {
|
1292
|
+
struct zipruby_archive *p_archive;
|
1293
|
+
|
1294
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1295
|
+
Check_Archive(p_archive);
|
1296
|
+
|
1297
|
+
if (zip_unchange_archive(p_archive->archive) == -1) {
|
1298
|
+
rb_raise(Error, "Unchange archive failed: %s", zip_strerror(p_archive->archive));
|
1299
|
+
}
|
1300
|
+
|
1301
|
+
return Qnil;
|
1302
|
+
}
|
1303
|
+
|
1304
|
+
/* */
|
1305
|
+
static VALUE zipruby_archive_revert(VALUE self) {
|
1306
|
+
zipruby_archive_funchange_all(self);
|
1307
|
+
zipruby_archive_unchange(self);
|
1308
|
+
|
1309
|
+
return Qnil;
|
1310
|
+
}
|
1311
|
+
|
1312
|
+
/* */
|
1313
|
+
static VALUE zipruby_archive_each(VALUE self) {
|
1314
|
+
struct zipruby_archive *p_archive;
|
1315
|
+
int i, num_files;
|
1316
|
+
|
1317
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1318
|
+
Check_Archive(p_archive);
|
1319
|
+
num_files = zip_get_num_files(p_archive->archive);
|
1320
|
+
|
1321
|
+
for (i = 0; i < num_files; i++) {
|
1322
|
+
VALUE file;
|
1323
|
+
int status;
|
1324
|
+
|
1325
|
+
file = rb_funcall(File, rb_intern("new"), 2, self, INT2NUM(i));
|
1326
|
+
rb_protect(rb_yield, file, &status);
|
1327
|
+
rb_funcall(file, rb_intern("close"), 0);
|
1328
|
+
|
1329
|
+
if (status != 0) {
|
1330
|
+
rb_jump_tag(status);
|
1331
|
+
}
|
1332
|
+
}
|
1333
|
+
|
1334
|
+
return Qnil;
|
1335
|
+
}
|
1336
|
+
|
1337
|
+
/* */
|
1338
|
+
static VALUE zipruby_archive_commit(VALUE self) {
|
1339
|
+
struct zipruby_archive *p_archive;
|
1340
|
+
int changed, survivors;
|
1341
|
+
int errorp;
|
1342
|
+
|
1343
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1344
|
+
Check_Archive(p_archive);
|
1345
|
+
|
1346
|
+
changed = _zip_changed(p_archive->archive, &survivors);
|
1347
|
+
|
1348
|
+
if (zip_close(p_archive->archive) == -1) {
|
1349
|
+
zip_unchange_all(p_archive->archive);
|
1350
|
+
zip_unchange_archive(p_archive->archive);
|
1351
|
+
rb_raise(Error, "Commit archive failed: %s", zip_strerror(p_archive->archive));
|
1352
|
+
}
|
1353
|
+
|
1354
|
+
if (!NIL_P(p_archive->buffer) && changed) {
|
1355
|
+
rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
|
1356
|
+
}
|
1357
|
+
|
1358
|
+
p_archive->archive = NULL;
|
1359
|
+
p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
|
1360
|
+
|
1361
|
+
if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
|
1362
|
+
char errstr[ERRSTR_BUFSIZE];
|
1363
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
1364
|
+
rb_raise(Error, "Commit archive failed: %s", errstr);
|
1365
|
+
}
|
1366
|
+
|
1367
|
+
return Qnil;
|
1368
|
+
}
|
1369
|
+
|
1370
|
+
/* */
|
1371
|
+
static VALUE zipruby_archive_is_open(VALUE self) {
|
1372
|
+
struct zipruby_archive *p_archive;
|
1373
|
+
|
1374
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1375
|
+
return (p_archive->archive != NULL) ? Qtrue : Qfalse;
|
1376
|
+
}
|
1377
|
+
|
1378
|
+
/* */
|
1379
|
+
static VALUE zipruby_archive_decrypt(VALUE self, VALUE password) {
|
1380
|
+
VALUE retval;
|
1381
|
+
struct zipruby_archive *p_archive;
|
1382
|
+
long pwdlen;
|
1383
|
+
int changed, survivors;
|
1384
|
+
int errorp;
|
1385
|
+
|
1386
|
+
Check_Type(password, T_STRING);
|
1387
|
+
pwdlen = RSTRING_LEN(password);
|
1388
|
+
|
1389
|
+
if (pwdlen < 1) {
|
1390
|
+
rb_raise(Error, "Decrypt archive failed: Password is empty");
|
1391
|
+
} else if (pwdlen > 0xff) {
|
1392
|
+
rb_raise(Error, "Decrypt archive failed: Password is too long");
|
1393
|
+
}
|
1394
|
+
|
1395
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1396
|
+
Check_Archive(p_archive);
|
1397
|
+
|
1398
|
+
changed = _zip_changed(p_archive->archive, &survivors);
|
1399
|
+
|
1400
|
+
if (zip_close(p_archive->archive) == -1) {
|
1401
|
+
zip_unchange_all(p_archive->archive);
|
1402
|
+
zip_unchange_archive(p_archive->archive);
|
1403
|
+
rb_raise(Error, "Decrypt archive failed: %s", zip_strerror(p_archive->archive));
|
1404
|
+
}
|
1405
|
+
|
1406
|
+
if (!NIL_P(p_archive->buffer) && changed) {
|
1407
|
+
rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
|
1408
|
+
}
|
1409
|
+
|
1410
|
+
p_archive->archive = NULL;
|
1411
|
+
p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
|
1412
|
+
|
1413
|
+
retval = zipruby_archive_s_decrypt(Archive, p_archive->path, password);
|
1414
|
+
|
1415
|
+
if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
|
1416
|
+
char errstr[ERRSTR_BUFSIZE];
|
1417
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
1418
|
+
rb_raise(Error, "Decrypt archive failed: %s", errstr);
|
1419
|
+
}
|
1420
|
+
|
1421
|
+
return retval;
|
1422
|
+
}
|
1423
|
+
|
1424
|
+
/* */
|
1425
|
+
static VALUE zipruby_archive_encrypt(VALUE self, VALUE password) {
|
1426
|
+
VALUE retval;
|
1427
|
+
struct zipruby_archive *p_archive;
|
1428
|
+
long pwdlen;
|
1429
|
+
int changed, survivors;
|
1430
|
+
int errorp;
|
1431
|
+
|
1432
|
+
Check_Type(password, T_STRING);
|
1433
|
+
pwdlen = RSTRING_LEN(password);
|
1434
|
+
|
1435
|
+
if (pwdlen < 1) {
|
1436
|
+
rb_raise(Error, "Encrypt archive failed: Password is empty");
|
1437
|
+
} else if (pwdlen > 0xff) {
|
1438
|
+
rb_raise(Error, "Encrypt archive failed: Password is too long");
|
1439
|
+
}
|
1440
|
+
|
1441
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1442
|
+
Check_Archive(p_archive);
|
1443
|
+
|
1444
|
+
changed = _zip_changed(p_archive->archive, &survivors);
|
1445
|
+
|
1446
|
+
if (zip_close(p_archive->archive) == -1) {
|
1447
|
+
zip_unchange_all(p_archive->archive);
|
1448
|
+
zip_unchange_archive(p_archive->archive);
|
1449
|
+
rb_raise(Error, "Encrypt archive failed: %s", zip_strerror(p_archive->archive));
|
1450
|
+
}
|
1451
|
+
|
1452
|
+
if (!NIL_P(p_archive->buffer) && changed) {
|
1453
|
+
rb_funcall(p_archive->buffer, rb_intern("replace"), 1, rb_funcall(self, rb_intern("read"), 0));
|
1454
|
+
}
|
1455
|
+
|
1456
|
+
p_archive->archive = NULL;
|
1457
|
+
p_archive->flags = (p_archive->flags & ~(ZIP_CREATE | ZIP_EXCL));
|
1458
|
+
|
1459
|
+
retval = zipruby_archive_s_encrypt(Archive, p_archive->path, password);
|
1460
|
+
|
1461
|
+
if ((p_archive->archive = zip_open(RSTRING_PTR(p_archive->path), p_archive->flags, &errorp)) == NULL) {
|
1462
|
+
char errstr[ERRSTR_BUFSIZE];
|
1463
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
|
1464
|
+
rb_raise(Error, "Encrypt archive failed: %s", errstr);
|
1465
|
+
}
|
1466
|
+
|
1467
|
+
return retval;
|
1468
|
+
}
|
1469
|
+
|
1470
|
+
/* */
|
1471
|
+
static VALUE zipruby_archive_read(VALUE self) {
|
1472
|
+
VALUE retval = Qnil;
|
1473
|
+
struct zipruby_archive *p_archive;
|
1474
|
+
FILE *fzip;
|
1475
|
+
char buf[DATA_BUFSIZE];
|
1476
|
+
ssize_t n;
|
1477
|
+
int block_given;
|
1478
|
+
|
1479
|
+
Data_Get_Struct(self, struct zipruby_archive, p_archive);
|
1480
|
+
|
1481
|
+
if (NIL_P(p_archive->path)) {
|
1482
|
+
rb_raise(rb_eRuntimeError, "invalid Zip::Archive");
|
1483
|
+
}
|
1484
|
+
|
1485
|
+
#ifdef _WIN32
|
1486
|
+
if (fopen_s(&fzip, RSTRING_PTR(p_archive->path), "rb") != 0) {
|
1487
|
+
rb_raise(Error, "Read archive failed: Cannot open archive");
|
1488
|
+
}
|
1489
|
+
#else
|
1490
|
+
if ((fzip = fopen(RSTRING_PTR(p_archive->path), "rb")) == NULL) {
|
1491
|
+
rb_raise(Error, "Read archive failed: Cannot open archive");
|
1492
|
+
}
|
1493
|
+
#endif
|
1494
|
+
|
1495
|
+
block_given = rb_block_given_p();
|
1496
|
+
|
1497
|
+
while ((n = fread(buf, 1, sizeof(buf), fzip)) > 0) {
|
1498
|
+
if (block_given) {
|
1499
|
+
rb_yield(rb_str_new(buf, n));
|
1500
|
+
} else {
|
1501
|
+
if (NIL_P(retval)) {
|
1502
|
+
retval = rb_str_new(buf, n);
|
1503
|
+
} else {
|
1504
|
+
rb_str_buf_cat(retval, buf, n);
|
1505
|
+
}
|
1506
|
+
}
|
1507
|
+
}
|
1508
|
+
|
1509
|
+
#if defined(RUBY_VM) && defined(_WIN32)
|
1510
|
+
_fclose_nolock(fzip);
|
1511
|
+
#elif defined(RUBY_WIN32_H)
|
1512
|
+
#undef fclose
|
1513
|
+
fclose(fzip);
|
1514
|
+
#define fclose(f) rb_w32_fclose(f)
|
1515
|
+
#else
|
1516
|
+
fclose(fzip);
|
1517
|
+
#endif
|
1518
|
+
|
1519
|
+
if (n == -1) {
|
1520
|
+
rb_raise(Error, "Read archive failed");
|
1521
|
+
}
|
1522
|
+
|
1523
|
+
return retval;
|
1524
|
+
}
|
1525
|
+
#include "zipruby.h"
|
1526
|
+
#include "zipruby_error.h"
|
1527
|
+
#include "ruby.h"
|
1528
|
+
|
1529
|
+
extern VALUE Zip;
|
1530
|
+
VALUE Error;
|
1531
|
+
|
1532
|
+
void Init_zipruby_error() {
|
1533
|
+
Error = rb_define_class_under(Zip, "Error", rb_eStandardError);
|
1534
|
+
}
|
1535
|
+
#include <errno.h>
|
1536
|
+
|
1537
|
+
#include "zip.h"
|
1538
|
+
#include "zipint.h"
|
1539
|
+
#include "zipruby.h"
|
1540
|
+
#include "zipruby_archive.h"
|
1541
|
+
#include "zipruby_file.h"
|
1542
|
+
#include "zipruby_stat.h"
|
1543
|
+
#include "ruby.h"
|
1544
|
+
|
1545
|
+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
1546
|
+
|
1547
|
+
static VALUE zipruby_file(VALUE klass);
|
1548
|
+
static VALUE zipruby_file_alloc(VALUE klass);
|
1549
|
+
static void zipruby_file_mark(struct zipruby_file *p);
|
1550
|
+
static void zipruby_file_free(struct zipruby_file *p);
|
1551
|
+
static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self);
|
1552
|
+
static VALUE zipruby_file_close(VALUE self);
|
1553
|
+
static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self);
|
1554
|
+
static VALUE zipruby_file_stat(VALUE self);
|
1555
|
+
static VALUE zipruby_file_get_comment(int argc, VALUE *argv, VALUE self);
|
1556
|
+
static VALUE zipruby_file_set_comment(VALUE self, VALUE comment);
|
1557
|
+
static VALUE zipruby_file_delete(VALUE self);
|
1558
|
+
static VALUE zipruby_file_rename(VALUE self, VALUE name);
|
1559
|
+
static VALUE zipruby_file_unchange(VALUE self);
|
1560
|
+
static VALUE zipruby_file_name(VALUE self);
|
1561
|
+
static VALUE zipruby_file_index(VALUE self);
|
1562
|
+
static VALUE zipruby_file_crc(VALUE self);
|
1563
|
+
static VALUE zipruby_file_size(VALUE self);
|
1564
|
+
static VALUE zipruby_file_mtime(VALUE self);
|
1565
|
+
static VALUE zipruby_file_comp_size(VALUE self);
|
1566
|
+
static VALUE zipruby_file_comp_method(VALUE self);
|
1567
|
+
static VALUE zipruby_file_encryption_method(VALUE self);
|
1568
|
+
|
1569
|
+
extern VALUE Zip;
|
1570
|
+
extern VALUE Archive;
|
1571
|
+
VALUE File;
|
1572
|
+
extern VALUE Stat;
|
1573
|
+
extern VALUE Error;
|
1574
|
+
|
1575
|
+
void Init_zipruby_file() {
|
1576
|
+
File = rb_define_class_under(Zip, "File", rb_cObject);
|
1577
|
+
rb_define_alloc_func(File, zipruby_file_alloc);
|
1578
|
+
rb_define_method(File, "initialize", zipruby_file_initialize, -1);
|
1579
|
+
rb_define_method(File, "close", zipruby_file_close, 0);
|
1580
|
+
rb_define_method(File, "read", zipruby_file_read, -1);
|
1581
|
+
rb_define_method(File, "stat", zipruby_file_stat, 0);
|
1582
|
+
rb_define_method(File, "get_comment", zipruby_file_get_comment, -1);
|
1583
|
+
rb_define_method(File, "comment", zipruby_file_get_comment, -1);
|
1584
|
+
rb_define_method(File, "comment=", zipruby_file_set_comment, 1);
|
1585
|
+
rb_define_method(File, "delete", zipruby_file_delete, 0);
|
1586
|
+
rb_define_method(File, "rename", zipruby_file_rename, 1);
|
1587
|
+
rb_define_method(File, "unchange", zipruby_file_unchange, 1);
|
1588
|
+
rb_define_method(File, "revert", zipruby_file_unchange, 1);
|
1589
|
+
rb_define_method(File, "name", zipruby_file_name, 0);
|
1590
|
+
rb_define_method(File, "index", zipruby_file_index, 0);
|
1591
|
+
rb_define_method(File, "crc", zipruby_file_crc, 0);
|
1592
|
+
rb_define_method(File, "size", zipruby_file_size, 0);
|
1593
|
+
rb_define_method(File, "mtime", zipruby_file_mtime, 0);
|
1594
|
+
rb_define_method(File, "comp_size", zipruby_file_comp_size, 0);
|
1595
|
+
rb_define_method(File, "comp_method", zipruby_file_comp_method, 0);
|
1596
|
+
rb_define_method(File, "encryption_method", zipruby_file_encryption_method, 0);
|
1597
|
+
}
|
1598
|
+
|
1599
|
+
static VALUE zipruby_file_alloc(VALUE klass) {
|
1600
|
+
struct zipruby_file *p = ALLOC(struct zipruby_file);
|
1601
|
+
|
1602
|
+
p->archive = NULL;
|
1603
|
+
p->file = NULL;
|
1604
|
+
p->sb = NULL;
|
1605
|
+
|
1606
|
+
return Data_Wrap_Struct(klass, zipruby_file_mark, zipruby_file_free, p);
|
1607
|
+
}
|
1608
|
+
|
1609
|
+
static void zipruby_file_mark(struct zipruby_file *p) {
|
1610
|
+
if (p->archive) { rb_gc_mark(p->v_archive); }
|
1611
|
+
if (p->sb) { rb_gc_mark(p->v_sb); }
|
1612
|
+
}
|
1613
|
+
|
1614
|
+
static void zipruby_file_free(struct zipruby_file *p) {
|
1615
|
+
xfree(p);
|
1616
|
+
}
|
1617
|
+
|
1618
|
+
/* */
|
1619
|
+
static VALUE zipruby_file_initialize(int argc, VALUE *argv, VALUE self) {
|
1620
|
+
VALUE archive, index, flags, stat_flags;
|
1621
|
+
struct zipruby_archive *p_archive;
|
1622
|
+
struct zipruby_file *p_file;
|
1623
|
+
struct zipruby_stat *p_stat;
|
1624
|
+
struct zip_file *fzip;
|
1625
|
+
char *fname = NULL;
|
1626
|
+
int i_index = -1, i_flags = 0;
|
1627
|
+
|
1628
|
+
rb_scan_args(argc, argv, "22", &archive, &index, &flags, &stat_flags);
|
1629
|
+
|
1630
|
+
if (!rb_obj_is_instance_of(archive, Archive)) {
|
1631
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(archive)));
|
1632
|
+
}
|
1633
|
+
|
1634
|
+
switch (TYPE(index)) {
|
1635
|
+
case T_STRING: fname = RSTRING_PTR(index); break;
|
1636
|
+
case T_FIXNUM: i_index = NUM2INT(index); break;
|
1637
|
+
default:
|
1638
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
|
1639
|
+
}
|
1640
|
+
|
1641
|
+
if (!NIL_P(flags)) {
|
1642
|
+
i_flags = NUM2INT(flags);
|
1643
|
+
}
|
1644
|
+
|
1645
|
+
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
1646
|
+
Check_Archive(p_archive);
|
1647
|
+
|
1648
|
+
if (fname) {
|
1649
|
+
fzip = zip_fopen(p_archive->archive, fname, i_flags);
|
1650
|
+
|
1651
|
+
if (fzip == NULL) {
|
1652
|
+
rb_raise(Error, "Open file failed - %s: %s", fname, zip_strerror(p_archive->archive));
|
1653
|
+
}
|
1654
|
+
} else {
|
1655
|
+
fzip = zip_fopen_index(p_archive->archive, i_index, i_flags);
|
1656
|
+
|
1657
|
+
if (fzip == NULL) {
|
1658
|
+
rb_raise(Error, "Open file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
1659
|
+
}
|
1660
|
+
}
|
1661
|
+
|
1662
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1663
|
+
p_file->v_archive = archive;
|
1664
|
+
p_file->archive = p_archive->archive;
|
1665
|
+
p_file->file = fzip;
|
1666
|
+
p_file->v_sb = rb_funcall(Stat, rb_intern("new"), 3, archive, index, stat_flags);
|
1667
|
+
Data_Get_Struct(p_file->v_sb, struct zipruby_stat, p_stat);
|
1668
|
+
p_file->sb = p_stat->sb;
|
1669
|
+
|
1670
|
+
return Qnil;
|
1671
|
+
}
|
1672
|
+
|
1673
|
+
/* */
|
1674
|
+
static VALUE zipruby_file_close(VALUE self) {
|
1675
|
+
struct zipruby_file *p_file;
|
1676
|
+
int error;
|
1677
|
+
|
1678
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1679
|
+
Check_File(p_file);
|
1680
|
+
|
1681
|
+
if ((error = zip_fclose(p_file->file)) != 0) {
|
1682
|
+
char errstr[ERRSTR_BUFSIZE];
|
1683
|
+
zip_unchange(p_file->archive, p_file->sb->index);
|
1684
|
+
zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
|
1685
|
+
rb_raise(Error, "Close file failed: %s", errstr);
|
1686
|
+
}
|
1687
|
+
|
1688
|
+
p_file->archive = NULL;
|
1689
|
+
p_file->file = NULL;
|
1690
|
+
p_file->sb = NULL;
|
1691
|
+
|
1692
|
+
return Qnil;
|
1693
|
+
}
|
1694
|
+
|
1695
|
+
/* */
|
1696
|
+
static VALUE zipruby_file_read(int argc, VALUE *argv, VALUE self) {
|
1697
|
+
VALUE size, retval = Qnil;
|
1698
|
+
struct zipruby_file *p_file;
|
1699
|
+
struct zip_stat sb;
|
1700
|
+
int block_given;
|
1701
|
+
size_t bytes_left;
|
1702
|
+
char buf[DATA_BUFSIZE];
|
1703
|
+
ssize_t n;
|
1704
|
+
|
1705
|
+
rb_scan_args(argc, argv, "01", &size);
|
1706
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1707
|
+
Check_File(p_file);
|
1708
|
+
zip_stat_init(&sb);
|
1709
|
+
|
1710
|
+
if (p_file->archive->cdir->entry[0].bitflags & ZIP_GPBF_ENCRYPTED) {
|
1711
|
+
rb_raise(Error, "Read file failed: File encrypted");
|
1712
|
+
}
|
1713
|
+
|
1714
|
+
if (zip_stat_index(p_file->archive, p_file->sb->index, 0, &sb)) {
|
1715
|
+
rb_raise(Error, "Read file failed: %s", zip_strerror(p_file->archive));
|
1716
|
+
}
|
1717
|
+
|
1718
|
+
if (NIL_P(size)) {
|
1719
|
+
bytes_left = sb.size;
|
1720
|
+
} else {
|
1721
|
+
bytes_left = NUM2LONG(size);
|
1722
|
+
}
|
1723
|
+
|
1724
|
+
if (bytes_left <= 0) {
|
1725
|
+
return Qnil;
|
1726
|
+
}
|
1727
|
+
|
1728
|
+
block_given = rb_block_given_p();
|
1729
|
+
|
1730
|
+
while ((n = zip_fread(p_file->file, buf, MIN(bytes_left, sizeof(buf)))) > 0) {
|
1731
|
+
if (block_given) {
|
1732
|
+
rb_yield(rb_str_new(buf, n));
|
1733
|
+
} else {
|
1734
|
+
if (NIL_P(retval)) {
|
1735
|
+
retval = rb_str_new(buf, n);
|
1736
|
+
} else {
|
1737
|
+
rb_str_buf_cat(retval, buf, n);
|
1738
|
+
}
|
1739
|
+
}
|
1740
|
+
|
1741
|
+
bytes_left -= n;
|
1742
|
+
}
|
1743
|
+
|
1744
|
+
if (n == -1) {
|
1745
|
+
rb_raise(Error, "Read file failed: %s", zip_file_strerror(p_file->file));
|
1746
|
+
}
|
1747
|
+
|
1748
|
+
return retval;
|
1749
|
+
}
|
1750
|
+
|
1751
|
+
/* */
|
1752
|
+
static VALUE zipruby_file_stat(VALUE self) {
|
1753
|
+
struct zipruby_file *p_file;
|
1754
|
+
|
1755
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1756
|
+
Check_File(p_file);
|
1757
|
+
|
1758
|
+
return p_file->v_sb;
|
1759
|
+
}
|
1760
|
+
|
1761
|
+
/* */
|
1762
|
+
static VALUE zipruby_file_get_comment(int argc, VALUE *argv, VALUE self) {
|
1763
|
+
VALUE flags;
|
1764
|
+
struct zipruby_file *p_file;
|
1765
|
+
const char *comment;
|
1766
|
+
int lenp, i_flags = 0;
|
1767
|
+
|
1768
|
+
rb_scan_args(argc, argv, "01", &flags);
|
1769
|
+
|
1770
|
+
if (!NIL_P(flags)) {
|
1771
|
+
i_flags = NUM2INT(flags);
|
1772
|
+
}
|
1773
|
+
|
1774
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1775
|
+
Check_File(p_file);
|
1776
|
+
|
1777
|
+
// XXX: How is the error checked?
|
1778
|
+
comment = zip_get_file_comment(p_file->archive, p_file->sb->index, &lenp, i_flags);
|
1779
|
+
|
1780
|
+
return comment ? rb_str_new(comment, lenp) : Qnil;
|
1781
|
+
}
|
1782
|
+
|
1783
|
+
/* */
|
1784
|
+
static VALUE zipruby_file_set_comment(VALUE self, VALUE comment) {
|
1785
|
+
struct zipruby_file *p_file;
|
1786
|
+
char *s_comment = NULL;
|
1787
|
+
int len = 0;
|
1788
|
+
|
1789
|
+
if (!NIL_P(comment)) {
|
1790
|
+
Check_Type(comment, T_STRING);
|
1791
|
+
s_comment = RSTRING_PTR(comment);
|
1792
|
+
len = RSTRING_LEN(comment);
|
1793
|
+
}
|
1794
|
+
|
1795
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1796
|
+
Check_File(p_file);
|
1797
|
+
|
1798
|
+
if (zip_set_file_comment(p_file->archive, p_file->sb->index, s_comment, len) == -1) {
|
1799
|
+
zip_unchange_all(p_file->archive);
|
1800
|
+
zip_unchange_archive(p_file->archive);
|
1801
|
+
rb_raise(Error, "Comment file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1802
|
+
}
|
1803
|
+
|
1804
|
+
return Qnil;
|
1805
|
+
}
|
1806
|
+
|
1807
|
+
/* */
|
1808
|
+
static VALUE zipruby_file_delete(VALUE self) {
|
1809
|
+
struct zipruby_file *p_file;
|
1810
|
+
|
1811
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1812
|
+
Check_File(p_file);
|
1813
|
+
|
1814
|
+
if (zip_delete(p_file->archive, p_file->sb->index) == -1) {
|
1815
|
+
zip_unchange_all(p_file->archive);
|
1816
|
+
zip_unchange_archive(p_file->archive);
|
1817
|
+
rb_raise(Error, "Delete file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1818
|
+
}
|
1819
|
+
|
1820
|
+
return Qnil;
|
1821
|
+
}
|
1822
|
+
|
1823
|
+
/* */
|
1824
|
+
static VALUE zipruby_file_rename(VALUE self, VALUE name) {
|
1825
|
+
struct zipruby_file *p_file;
|
1826
|
+
|
1827
|
+
Check_Type(name, T_STRING);
|
1828
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1829
|
+
Check_File(p_file);
|
1830
|
+
|
1831
|
+
if (zip_rename(p_file->archive, p_file->sb->index, RSTRING_PTR(name)) == -1) {
|
1832
|
+
zip_unchange_all(p_file->archive);
|
1833
|
+
zip_unchange_archive(p_file->archive);
|
1834
|
+
rb_raise(Error, "Rename file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1835
|
+
}
|
1836
|
+
|
1837
|
+
return Qnil;
|
1838
|
+
}
|
1839
|
+
|
1840
|
+
/* */
|
1841
|
+
static VALUE zipruby_file_unchange(VALUE self) {
|
1842
|
+
struct zipruby_file *p_file;
|
1843
|
+
|
1844
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1845
|
+
Check_File(p_file);
|
1846
|
+
|
1847
|
+
if (zip_unchange(p_file->archive, p_file->sb->index) == -1) {
|
1848
|
+
rb_raise(Error, "Unchange file failed - %s: %s", p_file->sb->name, zip_strerror(p_file->archive));
|
1849
|
+
}
|
1850
|
+
|
1851
|
+
return Qnil;
|
1852
|
+
}
|
1853
|
+
|
1854
|
+
/* */
|
1855
|
+
static VALUE zipruby_file_name(VALUE self) {
|
1856
|
+
struct zipruby_file *p_file;
|
1857
|
+
|
1858
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1859
|
+
Check_File(p_file);
|
1860
|
+
|
1861
|
+
return rb_funcall(p_file->v_sb, rb_intern("name"), 0);
|
1862
|
+
}
|
1863
|
+
|
1864
|
+
/* */
|
1865
|
+
static VALUE zipruby_file_index(VALUE self) {
|
1866
|
+
struct zipruby_file *p_file;
|
1867
|
+
|
1868
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1869
|
+
Check_File(p_file);
|
1870
|
+
|
1871
|
+
return rb_funcall(p_file->v_sb, rb_intern("index"), 0);
|
1872
|
+
}
|
1873
|
+
|
1874
|
+
/* */
|
1875
|
+
static VALUE zipruby_file_crc(VALUE self) {
|
1876
|
+
struct zipruby_file *p_file;
|
1877
|
+
|
1878
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1879
|
+
Check_File(p_file);
|
1880
|
+
|
1881
|
+
return rb_funcall(p_file->v_sb, rb_intern("crc"), 0);
|
1882
|
+
}
|
1883
|
+
|
1884
|
+
/* */
|
1885
|
+
static VALUE zipruby_file_size(VALUE self) {
|
1886
|
+
struct zipruby_file *p_file;
|
1887
|
+
|
1888
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1889
|
+
Check_File(p_file);
|
1890
|
+
|
1891
|
+
return rb_funcall(p_file->v_sb, rb_intern("size"), 0);
|
1892
|
+
}
|
1893
|
+
|
1894
|
+
/* */
|
1895
|
+
static VALUE zipruby_file_mtime(VALUE self) {
|
1896
|
+
struct zipruby_file *p_file;
|
1897
|
+
|
1898
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1899
|
+
Check_File(p_file);
|
1900
|
+
|
1901
|
+
return rb_funcall(p_file->v_sb, rb_intern("mtime"), 0);
|
1902
|
+
}
|
1903
|
+
|
1904
|
+
/* */
|
1905
|
+
static VALUE zipruby_file_comp_size(VALUE self) {
|
1906
|
+
struct zipruby_file *p_file;
|
1907
|
+
|
1908
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1909
|
+
Check_File(p_file);
|
1910
|
+
|
1911
|
+
return rb_funcall(p_file->v_sb, rb_intern("comp_size"), 0);
|
1912
|
+
}
|
1913
|
+
|
1914
|
+
/* */
|
1915
|
+
static VALUE zipruby_file_comp_method(VALUE self) {
|
1916
|
+
struct zipruby_file *p_file;
|
1917
|
+
|
1918
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1919
|
+
Check_File(p_file);
|
1920
|
+
|
1921
|
+
return rb_funcall(p_file->v_sb, rb_intern("comp_method"), 0);
|
1922
|
+
}
|
1923
|
+
|
1924
|
+
/* */
|
1925
|
+
static VALUE zipruby_file_encryption_method(VALUE self) {
|
1926
|
+
struct zipruby_file *p_file;
|
1927
|
+
|
1928
|
+
Data_Get_Struct(self, struct zipruby_file, p_file);
|
1929
|
+
Check_File(p_file);
|
1930
|
+
|
1931
|
+
return rb_funcall(p_file->v_sb, rb_intern("encryption_method"), 0);
|
1932
|
+
}
|
1933
|
+
#include "zip.h"
|
1934
|
+
#include "zipruby.h"
|
1935
|
+
#include "zipruby_archive.h"
|
1936
|
+
#include "zipruby_stat.h"
|
1937
|
+
#include "ruby.h"
|
1938
|
+
|
1939
|
+
static VALUE zipruby_stat_alloc(VALUE klass);
|
1940
|
+
static void zipruby_stat_free(struct zipruby_stat *p);
|
1941
|
+
static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self);
|
1942
|
+
static VALUE zipruby_stat_name(VALUE self);
|
1943
|
+
static VALUE zipruby_stat_index(VALUE self);
|
1944
|
+
static VALUE zipruby_stat_crc(VALUE self);
|
1945
|
+
static VALUE zipruby_stat_size(VALUE self);
|
1946
|
+
static VALUE zipruby_stat_mtime(VALUE self);
|
1947
|
+
static VALUE zipruby_stat_comp_size(VALUE self);
|
1948
|
+
static VALUE zipruby_stat_comp_method(VALUE self);
|
1949
|
+
static VALUE zipruby_stat_encryption_method(VALUE self);
|
1950
|
+
|
1951
|
+
extern VALUE Zip;
|
1952
|
+
extern VALUE Archive;
|
1953
|
+
VALUE Stat;
|
1954
|
+
extern VALUE Error;
|
1955
|
+
|
1956
|
+
void Init_zipruby_stat() {
|
1957
|
+
Stat = rb_define_class_under(Zip, "Stat", rb_cObject);
|
1958
|
+
rb_define_alloc_func(Stat, zipruby_stat_alloc);
|
1959
|
+
rb_define_method(Stat, "initialize", zipruby_stat_initialize, -1);
|
1960
|
+
rb_define_method(Stat, "name", zipruby_stat_name, 0);
|
1961
|
+
rb_define_method(Stat, "index", zipruby_stat_index, 0);
|
1962
|
+
rb_define_method(Stat, "crc", zipruby_stat_crc, 0);
|
1963
|
+
rb_define_method(Stat, "size", zipruby_stat_size, 0);
|
1964
|
+
rb_define_method(Stat, "mtime", zipruby_stat_mtime, 0);
|
1965
|
+
rb_define_method(Stat, "comp_size", zipruby_stat_comp_size, 0);
|
1966
|
+
rb_define_method(Stat, "comp_method", zipruby_stat_comp_method, 0);
|
1967
|
+
rb_define_method(Stat, "encryption_method", zipruby_stat_encryption_method, 0);
|
1968
|
+
}
|
1969
|
+
|
1970
|
+
static VALUE zipruby_stat_alloc(VALUE klass) {
|
1971
|
+
struct zipruby_stat *p = ALLOC(struct zipruby_stat);
|
1972
|
+
|
1973
|
+
p->sb = ALLOC(struct zip_stat);
|
1974
|
+
zip_stat_init(p->sb);
|
1975
|
+
|
1976
|
+
return Data_Wrap_Struct(klass, 0, zipruby_stat_free, p);
|
1977
|
+
}
|
1978
|
+
|
1979
|
+
static void zipruby_stat_free(struct zipruby_stat *p) {
|
1980
|
+
xfree(p->sb);
|
1981
|
+
xfree(p);
|
1982
|
+
}
|
1983
|
+
|
1984
|
+
/* */
|
1985
|
+
static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
|
1986
|
+
VALUE archive, index, flags;
|
1987
|
+
struct zipruby_archive *p_archive;
|
1988
|
+
struct zipruby_stat *p_stat;
|
1989
|
+
char *fname = NULL;
|
1990
|
+
int i_index = -1, i_flags = 0;
|
1991
|
+
|
1992
|
+
rb_scan_args(argc, argv, "21", &archive, &index, &flags);
|
1993
|
+
|
1994
|
+
if (!rb_obj_is_instance_of(archive, Archive)) {
|
1995
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(archive)));
|
1996
|
+
}
|
1997
|
+
|
1998
|
+
switch (TYPE(index)) {
|
1999
|
+
case T_STRING: fname = RSTRING_PTR(index); break;
|
2000
|
+
case T_FIXNUM: i_index = NUM2INT(index); break;
|
2001
|
+
default:
|
2002
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
|
2003
|
+
}
|
2004
|
+
|
2005
|
+
if (!NIL_P(flags)) {
|
2006
|
+
i_flags = NUM2INT(flags);
|
2007
|
+
}
|
2008
|
+
|
2009
|
+
Data_Get_Struct(archive, struct zipruby_archive, p_archive);
|
2010
|
+
Check_Archive(p_archive);
|
2011
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2012
|
+
|
2013
|
+
if (fname) {
|
2014
|
+
if (zip_stat(p_archive->archive, fname, i_flags, p_stat->sb) != 0) {
|
2015
|
+
rb_raise(Error, "Obtain file status failed - %s: %s", fname, zip_strerror(p_archive->archive));
|
2016
|
+
}
|
2017
|
+
} else {
|
2018
|
+
if (zip_stat_index(p_archive->archive, i_index, i_flags, p_stat->sb) != 0) {
|
2019
|
+
rb_raise(Error, "Obtain file status failed at %d: %s", i_index, zip_strerror(p_archive->archive));
|
2020
|
+
}
|
2021
|
+
}
|
2022
|
+
|
2023
|
+
return Qnil;
|
2024
|
+
}
|
2025
|
+
|
2026
|
+
/* */
|
2027
|
+
static VALUE zipruby_stat_name(VALUE self) {
|
2028
|
+
struct zipruby_stat *p_stat;
|
2029
|
+
|
2030
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2031
|
+
|
2032
|
+
return p_stat->sb->name ? rb_str_new2(p_stat->sb->name) : Qnil;
|
2033
|
+
}
|
2034
|
+
|
2035
|
+
/* */
|
2036
|
+
static VALUE zipruby_stat_index(VALUE self) {
|
2037
|
+
struct zipruby_stat *p_stat;
|
2038
|
+
|
2039
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2040
|
+
|
2041
|
+
return INT2NUM(p_stat->sb->index);
|
2042
|
+
}
|
2043
|
+
|
2044
|
+
/* */
|
2045
|
+
static VALUE zipruby_stat_crc(VALUE self) {
|
2046
|
+
struct zipruby_stat *p_stat;
|
2047
|
+
|
2048
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2049
|
+
|
2050
|
+
return UINT2NUM(p_stat->sb->crc);
|
2051
|
+
}
|
2052
|
+
|
2053
|
+
/* */
|
2054
|
+
static VALUE zipruby_stat_size(VALUE self) {
|
2055
|
+
struct zipruby_stat *p_stat;
|
2056
|
+
|
2057
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2058
|
+
|
2059
|
+
return LONG2NUM(p_stat->sb->size);
|
2060
|
+
}
|
2061
|
+
|
2062
|
+
/* */
|
2063
|
+
static VALUE zipruby_stat_mtime(VALUE self) {
|
2064
|
+
struct zipruby_stat *p_stat;
|
2065
|
+
|
2066
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2067
|
+
|
2068
|
+
return rb_funcall(rb_cTime, rb_intern("at"), 1, LONG2NUM((long) p_stat->sb->mtime));
|
2069
|
+
}
|
2070
|
+
|
2071
|
+
/* */
|
2072
|
+
static VALUE zipruby_stat_comp_size(VALUE self) {
|
2073
|
+
struct zipruby_stat *p_stat;
|
2074
|
+
|
2075
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2076
|
+
|
2077
|
+
return LONG2NUM(p_stat->sb->comp_size);
|
2078
|
+
}
|
2079
|
+
|
2080
|
+
/* */
|
2081
|
+
static VALUE zipruby_stat_comp_method(VALUE self) {
|
2082
|
+
struct zipruby_stat *p_stat;
|
2083
|
+
|
2084
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2085
|
+
|
2086
|
+
return INT2NUM(p_stat->sb->comp_method);
|
2087
|
+
}
|
2088
|
+
|
2089
|
+
/* */
|
2090
|
+
static VALUE zipruby_stat_encryption_method(VALUE self) {
|
2091
|
+
struct zipruby_stat *p_stat;
|
2092
|
+
|
2093
|
+
Data_Get_Struct(self, struct zipruby_stat, p_stat);
|
2094
|
+
|
2095
|
+
return INT2NUM(p_stat->sb->encryption_method);
|
2096
|
+
}
|
2097
|
+
#include "ruby.h"
|
2098
|
+
#include "zip.h"
|
2099
|
+
#include "zipruby.h"
|
2100
|
+
#include "zipruby_zip.h"
|
2101
|
+
|
2102
|
+
VALUE Zip;
|
2103
|
+
|
2104
|
+
void Init_zipruby_zip() {
|
2105
|
+
Zip = rb_define_module("Zip");
|
2106
|
+
rb_define_const(Zip, "VERSION", rb_str_new2(VERSION));
|
2107
|
+
|
2108
|
+
rb_define_const(Zip, "CREATE", INT2NUM(ZIP_CREATE));
|
2109
|
+
rb_define_const(Zip, "EXCL", INT2NUM(ZIP_EXCL));
|
2110
|
+
rb_define_const(Zip, "CHECKCONS", INT2NUM(ZIP_CHECKCONS));
|
2111
|
+
rb_define_const(Zip, "TRUNC", INT2NUM(ZIP_TRUNC));
|
2112
|
+
|
2113
|
+
rb_define_const(Zip, "FL_NOCASE", INT2NUM(ZIP_FL_NOCASE));
|
2114
|
+
rb_define_const(Zip, "FL_NODIR", INT2NUM(ZIP_FL_NODIR));
|
2115
|
+
rb_define_const(Zip, "FL_COMPRESSED", INT2NUM(ZIP_FL_COMPRESSED));
|
2116
|
+
rb_define_const(Zip, "FL_UNCHANGED", INT2NUM(ZIP_FL_UNCHANGED));
|
2117
|
+
|
2118
|
+
rb_define_const(Zip, "CM_DEFAULT" , INT2NUM(ZIP_CM_DEFAULT));
|
2119
|
+
rb_define_const(Zip, "CM_STORE", INT2NUM(ZIP_CM_STORE));
|
2120
|
+
rb_define_const(Zip, "CM_SHRINK", INT2NUM(ZIP_CM_SHRINK));
|
2121
|
+
rb_define_const(Zip, "CM_REDUCE_1", INT2NUM(ZIP_CM_REDUCE_1));
|
2122
|
+
rb_define_const(Zip, "CM_REDUCE_2", INT2NUM(ZIP_CM_REDUCE_2));
|
2123
|
+
rb_define_const(Zip, "CM_REDUCE_3", INT2NUM(ZIP_CM_REDUCE_3));
|
2124
|
+
rb_define_const(Zip, "CM_REDUCE_4", INT2NUM(ZIP_CM_REDUCE_4));
|
2125
|
+
rb_define_const(Zip, "CM_IMPLODE", INT2NUM(ZIP_CM_IMPLODE));
|
2126
|
+
rb_define_const(Zip, "CM_DEFLATE", INT2NUM(ZIP_CM_DEFLATE));
|
2127
|
+
rb_define_const(Zip, "CM_DEFLATE64", INT2NUM(ZIP_CM_DEFLATE64));
|
2128
|
+
rb_define_const(Zip, "CM_PKWARE_IMPLODE", INT2NUM(ZIP_CM_PKWARE_IMPLODE));
|
2129
|
+
rb_define_const(Zip, "CM_BZIP2", INT2NUM(ZIP_CM_BZIP2));
|
2130
|
+
|
2131
|
+
rb_define_const(Zip, "EM_NONE", INT2NUM(ZIP_EM_NONE));
|
2132
|
+
rb_define_const(Zip, "EM_TRAD_PKWARE", INT2NUM(ZIP_EM_TRAD_PKWARE));
|
2133
|
+
// XXX: Strong Encryption Header not parsed yet
|
2134
|
+
}
|
2135
|
+
#include <string.h>
|
2136
|
+
|
2137
|
+
#include "zip.h"
|
2138
|
+
#include "zipint.h"
|
2139
|
+
#include "zipruby_zip_source_proc.h"
|
2140
|
+
#include "ruby.h"
|
2141
|
+
|
2142
|
+
static VALUE proc_call(VALUE proc) {
|
2143
|
+
return rb_funcall(proc, rb_intern("call"), 0);
|
2144
|
+
}
|
2145
|
+
|
2146
|
+
static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cmd cmd) {
|
2147
|
+
struct read_proc *z;
|
2148
|
+
VALUE src;
|
2149
|
+
char *buf;
|
2150
|
+
size_t n;
|
2151
|
+
|
2152
|
+
z = (struct read_proc *) state;
|
2153
|
+
buf = (char *) data;
|
2154
|
+
|
2155
|
+
switch (cmd) {
|
2156
|
+
case ZIP_SOURCE_OPEN:
|
2157
|
+
return 0;
|
2158
|
+
|
2159
|
+
case ZIP_SOURCE_READ:
|
2160
|
+
src = rb_protect(proc_call, z->proc, NULL);
|
2161
|
+
|
2162
|
+
if (TYPE(src) != T_STRING) {
|
2163
|
+
return 0;
|
2164
|
+
}
|
2165
|
+
|
2166
|
+
n = RSTRING_LEN(src);
|
2167
|
+
|
2168
|
+
if (n > 0) {
|
2169
|
+
n = (n > len) ? len : n;
|
2170
|
+
memcpy(buf, RSTRING_PTR(src), n);
|
2171
|
+
}
|
2172
|
+
|
2173
|
+
return n;
|
2174
|
+
|
2175
|
+
case ZIP_SOURCE_CLOSE:
|
2176
|
+
return 0;
|
2177
|
+
|
2178
|
+
case ZIP_SOURCE_STAT:
|
2179
|
+
{
|
2180
|
+
struct zip_stat *st = (struct zip_stat *)data;
|
2181
|
+
zip_stat_init(st);
|
2182
|
+
st->mtime = NUM2LONG(rb_funcall(z->mtime, rb_intern("tv_sec"), 0));
|
2183
|
+
return sizeof(*st);
|
2184
|
+
}
|
2185
|
+
|
2186
|
+
case ZIP_SOURCE_ERROR:
|
2187
|
+
return 0;
|
2188
|
+
|
2189
|
+
case ZIP_SOURCE_FREE:
|
2190
|
+
free(z);
|
2191
|
+
return 0;
|
2192
|
+
}
|
2193
|
+
|
2194
|
+
return -1;
|
2195
|
+
}
|
2196
|
+
|
2197
|
+
struct zip_source *zip_source_proc(struct zip *za, struct read_proc *z) {
|
2198
|
+
struct zip_source *zs;
|
2199
|
+
zs = zip_source_function(za, read_proc, z);
|
2200
|
+
return zs;
|
2201
|
+
}
|