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