libarchive-ruby-gvalmon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/entry.cpp ADDED
@@ -0,0 +1,369 @@
1
+ /****************************************************************************
2
+ This file is part of libarchive-ruby.
3
+
4
+ libarchive-ruby is a Ruby binding for the C library libarchive.
5
+
6
+ Copyright (C) 2011 Hans Mackowiak
7
+
8
+ libarchive-ruby is free software; you can redistribute it and/or modify
9
+ it under the terms of the GNU General Public License as published by
10
+ the Free Software Foundation; either version 2 of the License, or
11
+ (at your option) any later version.
12
+
13
+ libarchive-ruby is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU General Public License for more details.
17
+
18
+ You should have received a copy of the GNU General Public License along
19
+ with libarchive-ruby; if not, write to the Free Software Foundation, Inc.,
20
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
+ ****************************************************************************/
22
+
23
+ #include "main.hpp"
24
+
25
+ #define _self wrap<archive_entry*>(self)
26
+
27
+ VALUE rb_cArchiveEntry;
28
+
29
+
30
+
31
+ void rb_define_attr_method(VALUE klass,const std::string &name, VALUE get(VALUE),VALUE set(VALUE,VALUE))
32
+ {
33
+ rb_define_method(klass,name.c_str(),RUBY_METHOD_FUNC(get),0);
34
+ rb_define_method(klass,(name + "=").c_str(),RUBY_METHOD_FUNC(set),1);
35
+ }
36
+
37
+
38
+ #define macro_attr(attr,get,set) \
39
+ VALUE _get_##attr(VALUE self) { return get(archive_entry_##attr(_self)); } \
40
+ VALUE _set_##attr(VALUE self,VALUE val) { archive_entry_set_##attr(_self,set(val));return val; }
41
+
42
+ #define is_type(type,flag) \
43
+ VALUE _is_##type(VALUE self) { return archive_entry_filetype(_self) == flag ? Qtrue : Qfalse; }
44
+
45
+
46
+
47
+ namespace ArchiveEntry {
48
+
49
+ VALUE _alloc(VALUE self)
50
+ {
51
+ return wrap(archive_entry_new());
52
+ }
53
+
54
+ VALUE _initialize_copy(VALUE self,VALUE source)
55
+ {
56
+ rarchive_entry *file;
57
+ Data_Get_Struct( self, rarchive_entry, file);
58
+ file->entry = archive_entry_clone(wrap<archive_entry*>(source));
59
+ return self;
60
+ }
61
+
62
+
63
+ macro_attr(dev,INT2NUM,NUM2INT)
64
+ macro_attr(devminor,INT2NUM,NUM2INT)
65
+ macro_attr(devmajor,INT2NUM,NUM2INT)
66
+
67
+ macro_attr(rdev,INT2NUM,NUM2INT)
68
+ macro_attr(rdevminor,INT2NUM,NUM2INT)
69
+ macro_attr(rdevmajor,INT2NUM,NUM2INT)
70
+
71
+ macro_attr(uid,INT2NUM,NUM2INT)
72
+ macro_attr(gid,INT2NUM,NUM2INT)
73
+
74
+ macro_attr(uname,wrap,wrap<const char*>)
75
+ macro_attr(gname,wrap,wrap<const char*>)
76
+
77
+ macro_attr(pathname,wrap,wrap<const char*>)
78
+ macro_attr(symlink,wrap,wrap<const char*>)
79
+ macro_attr(hardlink,wrap,wrap<const char*>)
80
+ //macro_attr(sourcepath,wrap,wrap<const char*>)
81
+
82
+ is_type(file,AE_IFREG)
83
+ is_type(symlink,AE_IFLNK)
84
+ is_type(directory,AE_IFDIR)
85
+ is_type(chardev,AE_IFCHR)
86
+ is_type(blockdev,AE_IFBLK)
87
+ is_type(pipe,AE_IFIFO)
88
+ is_type(socket,AE_IFSOCK)
89
+
90
+
91
+
92
+
93
+ VALUE _get_atime(VALUE self)
94
+ {
95
+ if(archive_entry_atime_is_set(_self))
96
+ return rb_time_new(archive_entry_atime(_self),archive_entry_atime_nsec(_self));
97
+ else
98
+ return Qnil;
99
+ }
100
+
101
+ VALUE _get_ctime(VALUE self)
102
+ {
103
+ if(archive_entry_ctime_is_set(_self))
104
+ return rb_time_new(archive_entry_ctime(_self),archive_entry_ctime_nsec(_self));
105
+ else
106
+ return Qnil;
107
+ }
108
+
109
+ VALUE _get_mtime(VALUE self)
110
+ {
111
+ if(archive_entry_mtime_is_set(_self))
112
+ return rb_time_new(archive_entry_mtime(_self),archive_entry_mtime_nsec(_self));
113
+ else
114
+ return Qnil;
115
+ }
116
+
117
+ VALUE _get_birthtime(VALUE self)
118
+ {
119
+ if(archive_entry_birthtime_is_set(_self))
120
+ return rb_time_new(archive_entry_birthtime(_self),archive_entry_birthtime_nsec(_self));
121
+ else
122
+ return Qnil;
123
+ }
124
+
125
+ VALUE _set_atime(VALUE self,VALUE value)
126
+ {
127
+ if(NIL_P(value))
128
+ archive_entry_unset_atime(_self);
129
+ else
130
+ archive_entry_set_atime(_self,NUM2INT(rb_funcall(value,rb_intern("to_i"),0)),NUM2INT(rb_funcall(value,rb_intern("usec"),0)));
131
+ return value;
132
+ }
133
+
134
+ VALUE _set_ctime(VALUE self,VALUE value)
135
+ {
136
+ if(NIL_P(value))
137
+ archive_entry_unset_ctime(_self);
138
+ else
139
+ archive_entry_set_ctime(_self,NUM2INT(rb_funcall(value,rb_intern("to_i"),0)),NUM2INT(rb_funcall(value,rb_intern("usec"),0)));
140
+ return value;
141
+ }
142
+
143
+ VALUE _set_mtime(VALUE self,VALUE value)
144
+ {
145
+ if(NIL_P(value))
146
+ archive_entry_unset_mtime(_self);
147
+ else
148
+ archive_entry_set_mtime(_self,NUM2INT(rb_funcall(value,rb_intern("to_i"),0)),NUM2INT(rb_funcall(value,rb_intern("usec"),0)));
149
+ return value;
150
+ }
151
+
152
+ VALUE _set_birthtime(VALUE self,VALUE value)
153
+ {
154
+ if(NIL_P(value))
155
+ archive_entry_unset_birthtime(_self);
156
+ else
157
+ archive_entry_set_birthtime(_self,NUM2INT(rb_funcall(value,rb_intern("to_i"),0)),NUM2INT(rb_funcall(value,rb_intern("usec"),0)));
158
+ return value;
159
+ }
160
+
161
+
162
+ /*
163
+ * call-seq:
164
+ * entry <=> other -> -1,0,1 or nil
165
+ *
166
+ * compares two entries
167
+ */
168
+ VALUE _compare(VALUE self,VALUE other)
169
+ {
170
+ if(rb_obj_is_kind_of(other,rb_cArchiveEntry) != Qtrue)
171
+ return Qnil;
172
+ else {
173
+ return rb_funcall(_get_mtime(self),rb_intern("<=>"),1,_get_mtime(other));
174
+ }
175
+ }
176
+
177
+
178
+ /*
179
+ * call-seq:
180
+ * entry.inspect -> String
181
+ *
182
+ * returns readable string.
183
+ */
184
+ VALUE _inspect(VALUE self){
185
+ VALUE array[3];
186
+ array[0]=rb_str_new2("#<%s:%s>");
187
+ array[1]=rb_class_of(self);
188
+ array[2]=_get_pathname(self);
189
+ return rb_f_sprintf(3,array);
190
+ }
191
+
192
+ }
193
+
194
+ //__LA_DECL __LA_MODE_T archive_entry_filetype(struct archive_entry *);
195
+
196
+
197
+
198
+ /*
199
+ * call-seq:
200
+ * entry.sourcepath -> String or nil
201
+ *
202
+ * returns the hardlink
203
+ */
204
+ VALUE ArchiveEntry_sourcepath(VALUE self)
205
+ {
206
+ return wrap(archive_entry_sourcepath(_self));
207
+ }
208
+
209
+ /*
210
+ * call-seq:
211
+ * entry.strmode -> String or nil
212
+ *
213
+ * returns the mode as string
214
+ */
215
+ VALUE ArchiveEntry_strmode(VALUE self)
216
+ {
217
+ return wrap(archive_entry_strmode(_self));
218
+ }
219
+
220
+ //ACL added later with acl gem
221
+
222
+ VALUE ArchiveEntry_access_acl(VALUE self){
223
+ if(rb_const_defined(rb_cObject,rb_intern("ACL"))){
224
+ VALUE rb_cAcl = rb_const_get(rb_cObject,rb_intern("ACL"));
225
+ VALUE rb_cAclEntry = rb_const_get(rb_cObject,rb_intern("Entry"));
226
+ VALUE result = rb_class_new_instance(0,NULL,rb_cAcl);
227
+ archive_entry_acl_reset(_self,ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
228
+ int type,permset,tag,qual;
229
+ const char* name;
230
+ while(archive_entry_acl_next(_self, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,&type, &permset, &tag,&qual, &name) == 0){
231
+ VALUE entry;
232
+ VALUE temp[3];
233
+ switch(tag){
234
+ case ARCHIVE_ENTRY_ACL_USER:
235
+ case ARCHIVE_ENTRY_ACL_USER_OBJ:
236
+ temp[0] = ID2SYM(rb_intern("user"));
237
+ break;
238
+ case ARCHIVE_ENTRY_ACL_GROUP:
239
+ case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
240
+ temp[0] = ID2SYM(rb_intern("group"));
241
+ break;
242
+ case ARCHIVE_ENTRY_ACL_MASK:
243
+ temp[0] = ID2SYM(rb_intern("mask"));
244
+ break;
245
+ case ARCHIVE_ENTRY_ACL_OTHER:
246
+ temp[0] = ID2SYM(rb_intern("other"));
247
+ break;
248
+ }
249
+ temp[1] = INT2NUM(permset);
250
+ switch(tag){
251
+ case ARCHIVE_ENTRY_ACL_USER:
252
+ case ARCHIVE_ENTRY_ACL_GROUP:
253
+ temp[2] = INT2NUM(qual);
254
+ entry=rb_class_new_instance(3,temp,rb_cAclEntry);
255
+ break;
256
+ default:
257
+ entry=rb_class_new_instance(2,temp,rb_cAclEntry);
258
+ break;
259
+ }
260
+ rb_funcall(result,rb_intern("<<"),1,entry);
261
+ }
262
+ return result;
263
+ }else
264
+ rb_raise(rb_eNotImpError,"this function require the libacl-ruby gem!");
265
+ return Qnil;
266
+ }
267
+
268
+ VALUE ArchiveEntry_acl_add(VALUE self){
269
+ archive_entry_acl_add_entry(_self,ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, ARCHIVE_ENTRY_ACL_GROUP,
270
+ 101, "abc");
271
+ return self;
272
+ }
273
+
274
+
275
+
276
+ /* Document-attr: atime
277
+ *
278
+ * Encapsulate the writing and reading of the configuration
279
+ * file. ...
280
+ */
281
+ /* Document-attr: ctime
282
+ *
283
+ * Encapsulate the writing and reading of the configuration
284
+ * file. ...
285
+ */
286
+ /* Document-attr: mtime
287
+ *
288
+ * Encapsulate the writing and reading of the configuration
289
+ * file. ...
290
+ */
291
+
292
+ void Init_archive_entry(VALUE rb_cArchive){
293
+ #if 0
294
+ rb_cArchive = rb_define_class("Archive",rb_cObject);
295
+
296
+ rb_define_attr(rb_cArchiveEntry,"path",1,1);
297
+ rb_define_attr(rb_cArchiveEntry,"symlink",1,1);
298
+ rb_define_attr(rb_cArchiveEntry,"hardlink",1,1);
299
+
300
+ rb_define_attr(rb_cArchiveEntry,"uid",1,1);
301
+ rb_define_attr(rb_cArchiveEntry,"uname",1,1);
302
+ rb_define_attr(rb_cArchiveEntry,"gid",1,1);
303
+ rb_define_attr(rb_cArchiveEntry,"gname",1,1);
304
+
305
+ rb_define_attr(rb_cArchiveEntry,"atime",1,1);
306
+ rb_define_attr(rb_cArchiveEntry,"ctime",1,1);
307
+ rb_define_attr(rb_cArchiveEntry,"mtime",1,1);
308
+ rb_define_attr(rb_cArchiveEntry,"birthtime",1,1);
309
+
310
+ rb_define_attr(rb_cArchiveEntry,"dev",1,1);
311
+ rb_define_attr(rb_cArchiveEntry,"devmajor",1,1);
312
+ rb_define_attr(rb_cArchiveEntry,"devminor",1,1);
313
+
314
+ rb_define_attr(rb_cArchiveEntry,"rdev",1,1);
315
+ rb_define_attr(rb_cArchiveEntry,"rdevmajor",1,1);
316
+ rb_define_attr(rb_cArchiveEntry,"rdevminor",1,1);
317
+
318
+
319
+ #endif
320
+
321
+ using namespace ArchiveEntry;
322
+ rb_cArchiveEntry = rb_define_class_under(rb_cArchive,"Entry",rb_cObject);
323
+ rb_define_alloc_func(rb_cArchiveEntry,_alloc);
324
+ rb_define_private_method(rb_cArchiveEntry,"initialize_copy",RUBY_METHOD_FUNC(_initialize_copy),1);
325
+ rb_define_method(rb_cArchiveEntry,"inspect",RUBY_METHOD_FUNC(_inspect),0);
326
+
327
+ rb_define_attr_method(rb_cArchiveEntry,"path",_get_pathname,_set_pathname);
328
+ rb_define_attr_method(rb_cArchiveEntry,"symlink",_get_symlink,_set_symlink);
329
+ rb_define_attr_method(rb_cArchiveEntry,"hardlink",_get_hardlink,_set_hardlink);
330
+
331
+ rb_define_method(rb_cArchiveEntry,"sourcepath",RUBY_METHOD_FUNC(ArchiveEntry_sourcepath),0);
332
+
333
+ rb_define_attr_method(rb_cArchiveEntry,"uid",_get_uid,_set_uid);
334
+ rb_define_attr_method(rb_cArchiveEntry,"gid",_get_gid,_set_gid);
335
+ rb_define_attr_method(rb_cArchiveEntry,"uname",_get_uname,_set_uname);
336
+ rb_define_attr_method(rb_cArchiveEntry,"gname",_get_gname,_set_gname);
337
+
338
+ rb_define_attr_method(rb_cArchiveEntry,"atime",_get_atime,_set_atime);
339
+ rb_define_attr_method(rb_cArchiveEntry,"ctime",_get_ctime,_set_ctime);
340
+ rb_define_attr_method(rb_cArchiveEntry,"mtime",_get_mtime,_set_mtime);
341
+ rb_define_attr_method(rb_cArchiveEntry,"birthtime",_get_birthtime,_set_birthtime);
342
+
343
+ rb_define_attr_method(rb_cArchiveEntry,"dev",_get_dev,_set_dev);
344
+ rb_define_attr_method(rb_cArchiveEntry,"dev_major",_get_devmajor,_set_devmajor);
345
+ rb_define_attr_method(rb_cArchiveEntry,"dev_minor",_get_devminor,_set_devminor);
346
+
347
+ rb_define_attr_method(rb_cArchiveEntry,"rdev",_get_rdev,_set_rdev);
348
+ rb_define_attr_method(rb_cArchiveEntry,"rdev_major",_get_rdevmajor,_set_rdevmajor);
349
+ rb_define_attr_method(rb_cArchiveEntry,"rdev_minor",_get_rdevminor,_set_rdevminor);
350
+
351
+ rb_define_method(rb_cArchiveEntry,"file?",RUBY_METHOD_FUNC(_is_file),0);
352
+ rb_define_method(rb_cArchiveEntry,"directory?",RUBY_METHOD_FUNC(_is_directory),0);
353
+ rb_define_method(rb_cArchiveEntry,"chardev?",RUBY_METHOD_FUNC(_is_chardev),0);
354
+ rb_define_method(rb_cArchiveEntry,"blockdev?",RUBY_METHOD_FUNC(_is_blockdev),0);
355
+ rb_define_method(rb_cArchiveEntry,"symlink?",RUBY_METHOD_FUNC(_is_symlink),0);
356
+ rb_define_method(rb_cArchiveEntry,"pipe?",RUBY_METHOD_FUNC(_is_pipe),0);
357
+ rb_define_method(rb_cArchiveEntry,"socket?",RUBY_METHOD_FUNC(_is_socket),0);
358
+
359
+
360
+ rb_include_module(rb_cArchiveEntry,rb_mComparable);
361
+ rb_define_method(rb_cArchiveEntry,"<=>",RUBY_METHOD_FUNC(_compare),1);
362
+
363
+ rb_define_alias(rb_cArchiveEntry,"to_s","path");
364
+ //*
365
+ rb_define_method(rb_cArchiveEntry,"access_acl",RUBY_METHOD_FUNC(ArchiveEntry_access_acl),0);
366
+ //*/
367
+
368
+
369
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,49 @@
1
+ #Encoding: UTF-8
2
+ =begin
3
+ This file is part of libarchive-ruby.
4
+
5
+ libarchive-ruby is a Ruby binding for the C library libarchive.
6
+
7
+ Copyright © 2011 Hans Mackowiak
8
+
9
+ libarchive-ruby is free software; you can redistribute it and/or modify
10
+ it under the terms of the GNU General Public License as published by
11
+ the Free Software Foundation; either version 2 of the License, or
12
+ (at your option) any later version.
13
+
14
+ libarchive-ruby is distributed in the hope that it will be useful,
15
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ GNU General Public License for more details.
18
+
19
+ You should have received a copy of the GNU General Public License along
20
+ with libarchive-ruby; if not, write to the Free Software Foundation, Inc.,
21
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
+ =end
23
+
24
+ require 'mkmf'
25
+
26
+
27
+ dir_config("archive")
28
+
29
+ pkg_config("libarchive")
30
+ unless(find_library("archive","main") && find_header("archive.h"))
31
+ abort("libarchive dev files")
32
+ end
33
+ CONFIG["warnflags"] = RbConfig::CONFIG["warnflags"] = " -Wall"
34
+
35
+ unless have_func("rb_string_value_cstr","ruby.h")
36
+ abort("missing VALUE to char* convert!")
37
+ end
38
+ unless have_macro("RETURN_ENUMERATOR","ruby.h")
39
+ abort("missing the return Enumerator macro.")
40
+ end
41
+ have_func("rb_proc_arity","ruby.h")
42
+ have_func("archive_read_support_format_raw","archive.h")
43
+
44
+ $CFLAGS += "-x c++ -Wall"
45
+
46
+
47
+ create_header
48
+
49
+ create_makefile("archive")
data/ext/main.cpp ADDED
@@ -0,0 +1,68 @@
1
+ /****************************************************************************
2
+ This file is part of libarchive-ruby.
3
+
4
+ libarchive-ruby is a Ruby binding for the C library libarchive.
5
+
6
+ Copyright (C) 2011,2012,2013 Hans Mackowiak
7
+
8
+ libarchive-ruby is free software; you can redistribute it and/or modify
9
+ it under the terms of the GNU General Public License as published by
10
+ the Free Software Foundation; either version 2 of the License, or
11
+ (at your option) any later version.
12
+
13
+ libarchive-ruby is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU General Public License for more details.
17
+
18
+ You should have received a copy of the GNU General Public License along
19
+ with libarchive-ruby; if not, write to the Free Software Foundation, Inc.,
20
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
+ ****************************************************************************/
22
+
23
+ #include "main.hpp"
24
+
25
+ template <>
26
+ VALUE wrap< rarchive >(rarchive *file )
27
+ {
28
+ return Data_Wrap_Struct(rb_cArchive, NULL, free, file);
29
+ }
30
+
31
+
32
+ template <>
33
+ rarchive* wrap< rarchive* >(const VALUE &vfile)
34
+ {
35
+ if ( ! rb_obj_is_kind_of(vfile, rb_cArchive) )
36
+ return NULL;
37
+ rarchive *file;
38
+ Data_Get_Struct( vfile, rarchive, file);
39
+ return file;
40
+ }
41
+ template <>
42
+ VALUE wrap< archive_entry >(struct archive_entry *entry )
43
+ {
44
+ rarchive_entry *temp = new rarchive_entry;
45
+ //archive_entry other = archive_entry_clone(entry);
46
+ temp->entry = archive_entry_clone(entry);
47
+ return Data_Wrap_Struct(rb_cArchiveEntry, NULL, free, temp);
48
+ }
49
+
50
+ template <>
51
+ archive_entry* wrap< archive_entry* >(const VALUE &vfile)
52
+ {
53
+ if ( ! rb_obj_is_kind_of(vfile, rb_cArchiveEntry) )
54
+ return NULL;
55
+ rarchive_entry *file;
56
+ Data_Get_Struct( vfile, rarchive_entry, file);
57
+ return file->entry;
58
+ }
59
+ template <>
60
+ VALUE wrap< const char >(const char *str )
61
+ {
62
+ return str == NULL? Qnil : rb_str_new2(str);
63
+ }
64
+ template <>
65
+ const char* wrap< const char* >(const VALUE &vfile)
66
+ {
67
+ return NIL_P(vfile) ? NULL : StringValueCStr((volatile VALUE&)vfile);
68
+ }