zipruby 0.3.0 → 0.3.1

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/zipruby_stat.c CHANGED
@@ -1,3 +1,5 @@
1
+ #include <string.h>
2
+
1
3
  #include "zip.h"
2
4
  #include "zipruby.h"
3
5
  #include "zipruby_archive.h"
@@ -7,14 +9,6 @@
7
9
  static VALUE zipruby_stat_alloc(VALUE klass);
8
10
  static void zipruby_stat_free(struct zipruby_stat *p);
9
11
  static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self);
10
- static VALUE zipruby_stat_name(VALUE self);
11
- static VALUE zipruby_stat_index(VALUE self);
12
- static VALUE zipruby_stat_crc(VALUE self);
13
- static VALUE zipruby_stat_size(VALUE self);
14
- static VALUE zipruby_stat_mtime(VALUE self);
15
- static VALUE zipruby_stat_comp_size(VALUE self);
16
- static VALUE zipruby_stat_comp_method(VALUE self);
17
- static VALUE zipruby_stat_encryption_method(VALUE self);
18
12
 
19
13
  extern VALUE Zip;
20
14
  extern VALUE Archive;
@@ -33,6 +27,7 @@ void Init_zipruby_stat() {
33
27
  rb_define_method(Stat, "comp_size", zipruby_stat_comp_size, 0);
34
28
  rb_define_method(Stat, "comp_method", zipruby_stat_comp_method, 0);
35
29
  rb_define_method(Stat, "encryption_method", zipruby_stat_encryption_method, 0);
30
+ rb_define_method(Stat, "directory?", zipruby_stat_is_directory, 0);
36
31
  }
37
32
 
38
33
  static VALUE zipruby_stat_alloc(VALUE klass) {
@@ -92,7 +87,7 @@ static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
92
87
  }
93
88
 
94
89
  /* */
95
- static VALUE zipruby_stat_name(VALUE self) {
90
+ VALUE zipruby_stat_name(VALUE self) {
96
91
  struct zipruby_stat *p_stat;
97
92
 
98
93
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -101,7 +96,7 @@ static VALUE zipruby_stat_name(VALUE self) {
101
96
  }
102
97
 
103
98
  /* */
104
- static VALUE zipruby_stat_index(VALUE self) {
99
+ VALUE zipruby_stat_index(VALUE self) {
105
100
  struct zipruby_stat *p_stat;
106
101
 
107
102
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -110,7 +105,7 @@ static VALUE zipruby_stat_index(VALUE self) {
110
105
  }
111
106
 
112
107
  /* */
113
- static VALUE zipruby_stat_crc(VALUE self) {
108
+ VALUE zipruby_stat_crc(VALUE self) {
114
109
  struct zipruby_stat *p_stat;
115
110
 
116
111
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -119,7 +114,7 @@ static VALUE zipruby_stat_crc(VALUE self) {
119
114
  }
120
115
 
121
116
  /* */
122
- static VALUE zipruby_stat_size(VALUE self) {
117
+ VALUE zipruby_stat_size(VALUE self) {
123
118
  struct zipruby_stat *p_stat;
124
119
 
125
120
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -128,7 +123,7 @@ static VALUE zipruby_stat_size(VALUE self) {
128
123
  }
129
124
 
130
125
  /* */
131
- static VALUE zipruby_stat_mtime(VALUE self) {
126
+ VALUE zipruby_stat_mtime(VALUE self) {
132
127
  struct zipruby_stat *p_stat;
133
128
 
134
129
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -137,7 +132,7 @@ static VALUE zipruby_stat_mtime(VALUE self) {
137
132
  }
138
133
 
139
134
  /* */
140
- static VALUE zipruby_stat_comp_size(VALUE self) {
135
+ VALUE zipruby_stat_comp_size(VALUE self) {
141
136
  struct zipruby_stat *p_stat;
142
137
 
143
138
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -146,7 +141,7 @@ static VALUE zipruby_stat_comp_size(VALUE self) {
146
141
  }
147
142
 
148
143
  /* */
149
- static VALUE zipruby_stat_comp_method(VALUE self) {
144
+ VALUE zipruby_stat_comp_method(VALUE self) {
150
145
  struct zipruby_stat *p_stat;
151
146
 
152
147
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
@@ -155,10 +150,34 @@ static VALUE zipruby_stat_comp_method(VALUE self) {
155
150
  }
156
151
 
157
152
  /* */
158
- static VALUE zipruby_stat_encryption_method(VALUE self) {
153
+ VALUE zipruby_stat_encryption_method(VALUE self) {
159
154
  struct zipruby_stat *p_stat;
160
155
 
161
156
  Data_Get_Struct(self, struct zipruby_stat, p_stat);
162
157
 
163
158
  return INT2NUM(p_stat->sb->encryption_method);
164
159
  }
160
+
161
+ /* */
162
+ VALUE zipruby_stat_is_directory(VALUE self) {
163
+ struct zipruby_stat *p_stat;
164
+ const char *name;
165
+ size_t name_len;
166
+ off_t size;
167
+
168
+ Data_Get_Struct(self, struct zipruby_stat, p_stat);
169
+ name = p_stat->sb->name;
170
+ size = p_stat->sb->size;
171
+
172
+ if (!name || size != 0) {
173
+ return Qfalse;
174
+ }
175
+
176
+ name_len = strlen(name);
177
+
178
+ if (name_len > 0 && name[name_len - 1] == '/') {
179
+ return Qtrue;
180
+ } else {
181
+ return Qfalse;
182
+ }
183
+ }
data/ext/zipruby_stat.h CHANGED
@@ -2,6 +2,7 @@
2
2
  #define __ZIPRUBY_STAT_H__
3
3
 
4
4
  #include "zip.h"
5
+ #include "ruby.h"
5
6
 
6
7
  struct zipruby_stat {
7
8
  struct zip_stat *sb;
@@ -9,4 +10,14 @@ struct zipruby_stat {
9
10
 
10
11
  void Init_zipruby_stat();
11
12
 
13
+ VALUE zipruby_stat_name(VALUE self);
14
+ VALUE zipruby_stat_index(VALUE self);
15
+ VALUE zipruby_stat_crc(VALUE self);
16
+ VALUE zipruby_stat_size(VALUE self);
17
+ VALUE zipruby_stat_mtime(VALUE self);
18
+ VALUE zipruby_stat_comp_size(VALUE self);
19
+ VALUE zipruby_stat_comp_method(VALUE self);
20
+ VALUE zipruby_stat_encryption_method(VALUE self);
21
+ VALUE zipruby_stat_is_directory(VALUE self);
22
+
12
23
  #endif
@@ -0,0 +1,85 @@
1
+ #include <string.h>
2
+
3
+ #include "zip.h"
4
+ #include "zipint.h"
5
+ #include "zipruby_zip_source_io.h"
6
+ #include "ruby.h"
7
+
8
+ #define IO_READ_BUFSIZE 8192
9
+
10
+ static VALUE io_read(VALUE io) {
11
+ return rb_funcall(io, rb_intern("read"), 1, INT2FIX(IO_READ_BUFSIZE));
12
+ }
13
+
14
+ static ssize_t read_io(void *state, void *data, size_t len, enum zip_source_cmd cmd) {
15
+ struct read_io *z;
16
+ VALUE src;
17
+ char *buf;
18
+ size_t n;
19
+ int status = 0;
20
+
21
+ z = (struct read_io *) state;
22
+ buf = (char *) data;
23
+
24
+ switch (cmd) {
25
+ case ZIP_SOURCE_OPEN:
26
+ return 0;
27
+
28
+ case ZIP_SOURCE_READ:
29
+ src = rb_protect(io_read, z->io, NULL);
30
+
31
+ if (status != 0) {
32
+ VALUE message, clazz;
33
+
34
+ #if defined(RUBY_VM)
35
+ message = rb_funcall(rb_errinfo(), rb_intern("message"), 0);
36
+ clazz = CLASS_OF(rb_errinfo());
37
+ #else
38
+ message = rb_funcall(ruby_errinfo, rb_intern("message"), 0);
39
+ clazz = CLASS_OF(ruby_errinfo);
40
+ #endif
41
+
42
+ rb_warn("Error in IO: %s (%s)", RSTRING_PTR(message), rb_class2name(clazz));
43
+ return -1;
44
+ }
45
+
46
+ if (TYPE(src) != T_STRING) {
47
+ return 0;
48
+ }
49
+
50
+ n = RSTRING_LEN(src);
51
+
52
+ if (n > 0) {
53
+ n = (n > len) ? len : n;
54
+ memcpy(buf, RSTRING_PTR(src), n);
55
+ }
56
+
57
+ return n;
58
+
59
+ case ZIP_SOURCE_CLOSE:
60
+ return 0;
61
+
62
+ case ZIP_SOURCE_STAT:
63
+ {
64
+ struct zip_stat *st = (struct zip_stat *)data;
65
+ zip_stat_init(st);
66
+ st->mtime = z->mtime;
67
+ return sizeof(*st);
68
+ }
69
+
70
+ case ZIP_SOURCE_ERROR:
71
+ return 0;
72
+
73
+ case ZIP_SOURCE_FREE:
74
+ free(z);
75
+ return 0;
76
+ }
77
+
78
+ return -1;
79
+ }
80
+
81
+ struct zip_source *zip_source_io(struct zip *za, struct read_io *z) {
82
+ struct zip_source *zs;
83
+ zs = zip_source_function(za, read_io, z);
84
+ return zs;
85
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef __ZIPRUBY_ZIP_SOURCE_IO_H__
2
+ #define __ZIPRUBY_ZIP_SOURCE_IO_H__
3
+
4
+ #include "zip.h"
5
+ #include "ruby.h"
6
+
7
+ struct read_io {
8
+ VALUE io;
9
+ long mtime;
10
+ };
11
+
12
+ struct zip_source *zip_source_io(struct zip *za, struct read_io *z);
13
+
14
+ #endif
@@ -14,6 +14,7 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
14
14
  VALUE src;
15
15
  char *buf;
16
16
  size_t n;
17
+ int status = 0;
17
18
 
18
19
  z = (struct read_proc *) state;
19
20
  buf = (char *) data;
@@ -23,7 +24,22 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
23
24
  return 0;
24
25
 
25
26
  case ZIP_SOURCE_READ:
26
- src = rb_protect(proc_call, z->proc, NULL);
27
+ src = rb_protect(proc_call, z->proc, &status);
28
+
29
+ if (status != 0) {
30
+ VALUE message, clazz;
31
+
32
+ #if defined(RUBY_VM)
33
+ message = rb_funcall(rb_errinfo(), rb_intern("message"), 0);
34
+ clazz = CLASS_OF(rb_errinfo());
35
+ #else
36
+ message = rb_funcall(ruby_errinfo, rb_intern("message"), 0);
37
+ clazz = CLASS_OF(ruby_errinfo);
38
+ #endif
39
+
40
+ rb_warn("Error in Proc: %s (%s)", RSTRING_PTR(message), rb_class2name(clazz));
41
+ return -1;
42
+ }
27
43
 
28
44
  if (TYPE(src) != T_STRING) {
29
45
  return 0;
@@ -45,7 +61,7 @@ static ssize_t read_proc(void *state, void *data, size_t len, enum zip_source_cm
45
61
  {
46
62
  struct zip_stat *st = (struct zip_stat *)data;
47
63
  zip_stat_init(st);
48
- st->mtime = NUM2LONG(rb_funcall(z->mtime, rb_intern("tv_sec"), 0));
64
+ st->mtime = z->mtime;
49
65
  return sizeof(*st);
50
66
  }
51
67
 
@@ -6,7 +6,7 @@
6
6
 
7
7
  struct read_proc {
8
8
  VALUE proc;
9
- VALUE mtime;
9
+ long mtime;
10
10
  };
11
11
 
12
12
  struct zip_source *zip_source_proc(struct zip *za, struct read_proc *z);