libarchive-ruby-swig 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,277 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #include <cstring>
12
+ #include <string>
13
+ #include <errno.h>
14
+ #include <sys/types.h>
15
+ #include <sys/stat.h>
16
+ #include <unistd.h>
17
+
18
+ #include <archive.h>
19
+ #include <archive_entry.h>
20
+ #include <ruby.h>
21
+ #include "entry.h"
22
+ #include "error.h"
23
+
24
+ Entry::Entry(struct archive_entry *entry)
25
+ {
26
+ _entry = entry;
27
+ }
28
+
29
+ Entry::~Entry()
30
+ {
31
+ if(_entry) {
32
+ archive_entry_free(_entry);
33
+ _entry = 0;
34
+ }
35
+ }
36
+
37
+ bool Entry::is_file()
38
+ {
39
+ return S_ISREG(archive_entry_filetype(_entry));
40
+ }
41
+
42
+ bool Entry::is_directory()
43
+ {
44
+ return S_ISDIR(archive_entry_filetype(_entry));
45
+ }
46
+
47
+ bool Entry::is_symbolic_link()
48
+ {
49
+ return S_ISLNK(archive_entry_filetype(_entry));
50
+ }
51
+
52
+ bool Entry::is_block_special()
53
+ {
54
+ return S_ISBLK(archive_entry_filetype(_entry));
55
+ }
56
+
57
+ bool Entry::is_character_special()
58
+ {
59
+ return S_ISCHR(archive_entry_filetype(_entry));
60
+ }
61
+
62
+ bool Entry::is_fifo()
63
+ {
64
+ return S_ISFIFO(archive_entry_filetype(_entry));
65
+ }
66
+
67
+ bool Entry::is_socket()
68
+ {
69
+ return S_ISSOCK(archive_entry_filetype(_entry));
70
+ }
71
+
72
+ void Entry::set_filetype(unsigned int filetype)
73
+ {
74
+ archive_entry_set_filetype(_entry, filetype);
75
+ }
76
+
77
+ unsigned int Entry::devmajor()
78
+ {
79
+ return archive_entry_devmajor(_entry);
80
+ }
81
+
82
+ void Entry::set_devmajor(unsigned int devmajor)
83
+ {
84
+ archive_entry_set_devmajor(_entry, devmajor);
85
+ }
86
+
87
+ unsigned int Entry::devminor()
88
+ {
89
+ return archive_entry_devminor(_entry);
90
+ }
91
+
92
+ void Entry::set_devminor(unsigned int devminor)
93
+ {
94
+ archive_entry_set_devminor(_entry, devminor);
95
+ }
96
+
97
+ unsigned long Entry::atime()
98
+ {
99
+ archive_entry_atime(_entry);
100
+ }
101
+
102
+ void Entry::set_atime(unsigned long atime)
103
+ {
104
+ archive_entry_set_atime(_entry, atime, 0);
105
+ }
106
+
107
+ void Entry::clear()
108
+ {
109
+ archive_entry_clear(_entry);
110
+ }
111
+
112
+ Entry *Entry::clone()
113
+ {
114
+ new Entry(archive_entry_clone(_entry));
115
+ }
116
+
117
+ unsigned long Entry::dev()
118
+ {
119
+ return archive_entry_dev(_entry);
120
+ }
121
+
122
+ void Entry::set_dev(unsigned long dev)
123
+ {
124
+ archive_entry_set_dev(_entry, dev);
125
+ }
126
+
127
+ unsigned long Entry::gid()
128
+ {
129
+ return archive_entry_gid(_entry);
130
+ }
131
+
132
+ void Entry::set_gid(unsigned long gid)
133
+ {
134
+ archive_entry_set_gid(_entry, gid);
135
+ }
136
+
137
+ const char *Entry::gname()
138
+ {
139
+ return archive_entry_gname(_entry);
140
+ }
141
+
142
+ void Entry::set_gname(const char *gname)
143
+ {
144
+ archive_entry_copy_gname(_entry, gname);
145
+ }
146
+
147
+ const char *Entry::hardlink()
148
+ {
149
+ return archive_entry_hardlink(_entry);
150
+ }
151
+
152
+ void Entry::set_hardlink(const char *hardlink)
153
+ {
154
+ archive_entry_copy_hardlink(_entry, hardlink);
155
+ }
156
+
157
+ unsigned long Entry::ino()
158
+ {
159
+ return archive_entry_ino(_entry);
160
+ }
161
+
162
+ void Entry::set_ino(unsigned long ino)
163
+ {
164
+ archive_entry_set_ino(_entry, ino);
165
+ }
166
+
167
+ int Entry::mode()
168
+ {
169
+ return archive_entry_mode(_entry);
170
+ }
171
+
172
+ void Entry::set_mode(int mode)
173
+ {
174
+ archive_entry_set_mode(_entry, mode);
175
+ }
176
+
177
+ unsigned long Entry::mtime()
178
+ {
179
+ return archive_entry_mtime(_entry);
180
+ }
181
+
182
+ void Entry::set_mtime(unsigned long mtime)
183
+ {
184
+ archive_entry_set_mtime(_entry, mtime, 0);
185
+ }
186
+
187
+ unsigned int Entry::nlink()
188
+ {
189
+ return archive_entry_nlink(_entry);
190
+ }
191
+
192
+ void Entry::set_nlink(unsigned int nlink)
193
+ {
194
+ archive_entry_set_nlink(_entry, nlink);
195
+ }
196
+
197
+ const char *Entry::pathname()
198
+ {
199
+ return archive_entry_pathname(_entry);
200
+ }
201
+
202
+ void Entry::set_pathname(const char *pathname)
203
+ {
204
+ archive_entry_copy_pathname(_entry, pathname);
205
+ }
206
+
207
+ unsigned int Entry::rdevmajor()
208
+ {
209
+ return archive_entry_rdevmajor(_entry);
210
+ }
211
+
212
+ void Entry::set_rdevmajor(unsigned int rdevmajor)
213
+ {
214
+ archive_entry_set_rdevmajor(_entry, rdevmajor);
215
+ }
216
+
217
+ unsigned int Entry::rdevminor()
218
+ {
219
+ return archive_entry_rdevminor(_entry);
220
+ }
221
+
222
+ void Entry::set_rdevminor(unsigned int rdevminor)
223
+ {
224
+ archive_entry_set_rdevminor(_entry, rdevminor);
225
+ }
226
+
227
+ unsigned long Entry::size()
228
+ {
229
+ return archive_entry_size(_entry);
230
+ }
231
+
232
+ void Entry::set_size(unsigned long size)
233
+ {
234
+ archive_entry_set_size(_entry, size);
235
+ }
236
+
237
+ const char *Entry::symlink()
238
+ {
239
+ return archive_entry_symlink(_entry);
240
+ }
241
+
242
+ void Entry::set_symlink(const char *symlink)
243
+ {
244
+ archive_entry_copy_symlink(_entry, symlink);
245
+ }
246
+
247
+ unsigned long Entry::uid()
248
+ {
249
+ return archive_entry_uid(_entry);
250
+ }
251
+
252
+ void Entry::set_uid(unsigned long uid)
253
+ {
254
+ archive_entry_set_uid(_entry, uid);
255
+ }
256
+
257
+ const char *Entry::uname()
258
+ {
259
+ return archive_entry_uname(_entry);
260
+ }
261
+
262
+ void Entry::set_uname(const char *uname)
263
+ {
264
+ archive_entry_copy_uname(_entry, uname);
265
+ }
266
+
267
+ void Entry::copy_stat_helper(const char *filename)
268
+ {
269
+ struct stat sb;
270
+ if(lstat(filename, &sb) == -1) {
271
+ std::string error_msg = strerror(errno);
272
+ throw Error(error_msg);
273
+ }
274
+
275
+ archive_entry_copy_stat(_entry, &sb);
276
+ }
277
+
@@ -0,0 +1,121 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #ifndef CLASS_ARCHIVE_ENTRY_H_INCLUDED
12
+ #define CLASS_ARCHIVE_ENTRY_H_INCLUDED
13
+
14
+ #include "writer.h"
15
+
16
+ struct archive_entry;
17
+
18
+ class Entry
19
+ {
20
+ friend void Writer::write_header(Entry *entry);
21
+
22
+ public:
23
+ Entry(struct archive_entry *entry);
24
+ virtual ~Entry();
25
+
26
+ bool is_file();
27
+ bool is_directory();
28
+ bool is_symbolic_link();
29
+ bool is_block_special();
30
+ bool is_character_special();
31
+ bool is_fifo();
32
+ bool is_socket();
33
+
34
+ void set_filetype(unsigned int filetype);
35
+
36
+ unsigned int devmajor();
37
+ void set_devmajor(unsigned int devmajor);
38
+
39
+ unsigned int devminor();
40
+ void set_devminor(unsigned int devminor);
41
+
42
+ unsigned long atime();
43
+ void set_atime(unsigned long atime);
44
+
45
+ void clear();
46
+
47
+ #ifdef SWIG
48
+ %newobject clone(Entry *entry);
49
+ #endif
50
+
51
+ Entry *clone();
52
+
53
+ unsigned long dev();
54
+ void set_dev(unsigned long dev);
55
+
56
+ unsigned long gid();
57
+ void set_gid(unsigned long gid);
58
+
59
+ const char *gname();
60
+ void set_gname(const char *gname);
61
+
62
+ const char *hardlink();
63
+ void set_hardlink(const char *hardlink);
64
+
65
+ unsigned long ino();
66
+ void set_ino(unsigned long ino);
67
+
68
+ int mode();
69
+ void set_mode(int mode);
70
+
71
+ unsigned long mtime();
72
+ void set_mtime(unsigned long mtime);
73
+
74
+ unsigned int nlink();
75
+ void set_nlink(unsigned int nlink);
76
+
77
+ const char *pathname();
78
+ void set_pathname(const char *pathname);
79
+
80
+ unsigned int rdevmajor();
81
+ void set_rdevmajor(unsigned int rdevmajor);
82
+
83
+ unsigned int rdevminor();
84
+ void set_rdevminor(unsigned int rdevminor);
85
+
86
+ unsigned long size();
87
+ void set_size(unsigned long size);
88
+
89
+ const char *symlink();
90
+ void set_symlink(const char *symlink);
91
+
92
+ unsigned long uid();
93
+ void set_uid(unsigned long uid);
94
+
95
+ const char *uname();
96
+ void set_uname(const char *uname);
97
+
98
+ #ifdef SWIG
99
+ %exception {
100
+ try {
101
+ $action
102
+ } catch(Error &err) {
103
+ static VALUE c_archive = rb_define_module("Archive");
104
+ static VALUE e_archive =
105
+ rb_define_class_under(c_archive, "Error", rb_eStandardError);
106
+ rb_raise(e_archive, err.what());
107
+ }
108
+ }
109
+ #endif
110
+
111
+ void copy_stat_helper(const char *filename);
112
+
113
+ #ifdef SWIG
114
+ %exception;
115
+ #endif
116
+
117
+ private:
118
+ struct archive_entry *_entry;
119
+ };
120
+
121
+ #endif
@@ -0,0 +1,35 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #ifndef CLASS_ARCHIVE_ERROR_H_INCLUDED
12
+ #define CLASS_ARCHIVE_ERROR_H_INCLUDED
13
+
14
+ #include <exception>
15
+
16
+ class Error: public std::exception
17
+ {
18
+ public:
19
+
20
+ Error(const std::string &error_msg)
21
+ : error_msg(error_msg) {};
22
+
23
+ virtual ~Error() throw()
24
+ {};
25
+
26
+ virtual const char *what() const throw() {
27
+ return error_msg.c_str();
28
+ }
29
+
30
+ private:
31
+
32
+ std::string error_msg;
33
+ };
34
+
35
+ #endif
@@ -0,0 +1,31 @@
1
+ #
2
+ # This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ # libarchive.
4
+ #
5
+ # Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+ #
7
+ # libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ # license text can be found in the file LICENSE.txt distributed with the source.
9
+ #
10
+
11
+ require 'mkmf'
12
+
13
+ unless have_header('archive.h') && \
14
+ have_library('archive', 'archive_read_open_filename')
15
+ $stderr.write "Libarchive development files not found.\n"
16
+ exit 1
17
+ end
18
+
19
+ if find_executable('g++')
20
+ $libs = append_library($libs, "stdc++")
21
+ end
22
+
23
+ if swig = find_executable('swig')
24
+ `#{swig} -c++ -ruby -features autodoc libarchive.i`
25
+ $distcleanfiles << 'libarchive_wrap.cxx'
26
+ else
27
+ $stderr.write "You need SWIG to compile this extension.\n"
28
+ exit 1
29
+ end
30
+
31
+ create_makefile('archive')
@@ -0,0 +1,30 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ %module archive
12
+
13
+ %{
14
+ #include <archive.h>
15
+ #include <archive_entry.h>
16
+ #include <ruby.h>
17
+ #include "reader.h"
18
+ #include "writer.h"
19
+ #include "entry.h"
20
+ #include "error.h"
21
+ %}
22
+
23
+ %wrapper %{
24
+ /* --- WRAPPER CODE START --- */
25
+ %}
26
+
27
+ %include "reader.h"
28
+ %include "writer.h"
29
+ %include "entry.h"
30
+
@@ -0,0 +1,119 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #include <string>
12
+ #include <archive.h>
13
+ #include <archive_entry.h>
14
+ #include <ruby.h>
15
+ #include "reader.h"
16
+ #include "entry.h"
17
+ #include "error.h"
18
+
19
+ Reader::Reader(struct archive *ar)
20
+ : _ar(ar),
21
+ _buf((char*) malloc(1024)),
22
+ _buf_size(1024)
23
+ {}
24
+
25
+
26
+ Reader::~Reader()
27
+ {
28
+ this->close();
29
+ free(_buf);
30
+ _buf = 0;
31
+ _buf_size = 0;
32
+ }
33
+
34
+
35
+ void Reader::close()
36
+ {
37
+ if(_ar) {
38
+ archive_read_finish(_ar);
39
+ _ar = 0;
40
+ }
41
+ }
42
+
43
+
44
+ Reader *Reader::read_open_filename(const char * filename)
45
+ {
46
+ struct archive *ar = archive_read_new();
47
+
48
+ try {
49
+ if(archive_read_support_compression_all(ar) != ARCHIVE_OK)
50
+ throw 0;
51
+
52
+ if(archive_read_support_format_all(ar) != ARCHIVE_OK)
53
+ throw 0;
54
+
55
+ if(archive_read_open_filename(ar, filename, 1024) != ARCHIVE_OK)
56
+ throw 0;
57
+
58
+ } catch(...) {
59
+ std::string error_msg = archive_error_string(ar);
60
+ archive_read_finish(ar);
61
+ throw Error(error_msg);
62
+ }
63
+
64
+ return new Reader(ar);
65
+ }
66
+
67
+
68
+ Entry *Reader::next_header()
69
+ {
70
+ Entry * result = 0;
71
+ std::string error_msg = "operation on uninitialized archive";
72
+
73
+ if(!_ar)
74
+ throw Error(error_msg);
75
+
76
+ struct archive_entry *entry = archive_entry_new();
77
+ int rval = archive_read_next_header2(_ar, entry);
78
+
79
+ switch(rval) {
80
+ case ARCHIVE_EOF:
81
+ archive_entry_free(entry);
82
+ close();
83
+ break;
84
+ case ARCHIVE_OK:
85
+ result = new Entry(entry);
86
+ break;
87
+ default:
88
+ archive_entry_free(entry);
89
+ error_msg = archive_error_string(_ar);
90
+ throw Error(error_msg);
91
+ }
92
+
93
+ return result;
94
+ }
95
+
96
+
97
+ VALUE Reader::read_data_helper(int len)
98
+ {
99
+ std::string error_msg = "error while reading from archive";
100
+
101
+ if(len > _buf_size) {
102
+ _buf = (char*) realloc(_buf, len);
103
+ _buf_size = len;
104
+ }
105
+
106
+ ssize_t rval = archive_read_data(_ar, (void*) _buf, len);
107
+
108
+ switch(rval) {
109
+ case ARCHIVE_FATAL:
110
+ case ARCHIVE_WARN:
111
+ case ARCHIVE_RETRY:
112
+ error_msg = archive_error_string(_ar);
113
+ throw Error(error_msg);
114
+ case 0:
115
+ return 0;
116
+ }
117
+
118
+ return rb_tainted_str_new(_buf, rval);
119
+ }
@@ -0,0 +1,60 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #ifndef CLASS_ARCHIVE_READER_H_INCLUDED
12
+ #define CLASS_ARCHIVE_READER_H_INCLUDED
13
+
14
+ struct archive;
15
+ class Entry;
16
+
17
+ #ifndef SWIG
18
+ #include <ruby.h>
19
+ #endif
20
+
21
+ class Reader
22
+ {
23
+ public:
24
+ Reader(struct archive *ar);
25
+ virtual ~Reader();
26
+ void close();
27
+
28
+ #ifdef SWIG
29
+ %exception {
30
+ try {
31
+ $action
32
+ } catch(Error &err) {
33
+ static VALUE c_archive = rb_define_module("Archive");
34
+ static VALUE e_archive =
35
+ rb_define_class_under(c_archive, "Error", rb_eStandardError);
36
+ rb_raise(e_archive, err.what());
37
+ }
38
+ }
39
+ #endif
40
+
41
+ #ifdef SWIG
42
+ %newobject read_open_filename(const char *filename);
43
+ %newobject next_header();
44
+ #endif
45
+
46
+ static Reader *read_open_filename(const char *filename);
47
+ Entry *next_header();
48
+ VALUE read_data_helper(int len);
49
+
50
+ #ifdef SWIG
51
+ %exception;
52
+ #endif
53
+
54
+ private:
55
+ struct archive *_ar;
56
+ char *_buf;
57
+ int _buf_size;
58
+ };
59
+
60
+ #endif
@@ -0,0 +1,141 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #include <string>
12
+ #include <archive.h>
13
+ #include <archive_entry.h>
14
+ #include <ruby.h>
15
+ #include "writer.h"
16
+ #include "entry.h"
17
+ #include "error.h"
18
+
19
+ Writer::Writer(struct archive *ar)
20
+ :_ar(ar),
21
+ _buf(0),
22
+ _buf_size(0)
23
+ {}
24
+
25
+
26
+ Writer::~Writer()
27
+ {
28
+ this->close();
29
+ free(_buf);
30
+ _buf = 0;
31
+ _buf_size = 0;
32
+ }
33
+
34
+
35
+ void Writer::close()
36
+ {
37
+ if(_ar) {
38
+ archive_write_finish(_ar);
39
+ _ar = 0;
40
+ }
41
+ }
42
+
43
+
44
+ Writer *Writer::write_open_filename(const char *filename,
45
+ int compression, int format)
46
+ {
47
+ std::string error_msg;
48
+ struct archive *ar = archive_write_new();
49
+
50
+ try {
51
+
52
+ switch(compression) {
53
+ case Archive::COMPRESSION_BZIP2:
54
+ archive_write_set_compression_bzip2(ar);
55
+ break;
56
+ case Archive::COMPRESSION_COMPRESS:
57
+ archive_write_set_compression_compress(ar);
58
+ break;
59
+ case Archive::COMPRESSION_GZIP:
60
+ archive_write_set_compression_gzip(ar);
61
+ break;
62
+ case Archive::COMPRESSION_LZMA:
63
+ archive_write_set_compression_lzma(ar);
64
+ break;
65
+ case Archive::COMPRESSION_NONE:
66
+ archive_write_set_compression_none(ar);
67
+ break;
68
+ case Archive::COMPRESSION_XZ:
69
+ archive_write_set_compression_xz(ar);
70
+ break;
71
+ default:
72
+ error_msg = "unknown or unsupported compression scheme";
73
+ throw Error(error_msg);
74
+ }
75
+
76
+ switch(format) {
77
+ case Archive::FORMAT_AR_BSD:
78
+ archive_write_set_format_ar_bsd(ar);
79
+ break;
80
+ case Archive::FORMAT_AR_SVR4:
81
+ archive_write_set_format_ar_svr4(ar);
82
+ break;
83
+ case Archive::FORMAT_CPIO:
84
+ archive_write_set_format_cpio(ar);
85
+ break;
86
+ case Archive::FORMAT_CPIO_NEWC:
87
+ archive_write_set_format_cpio_newc(ar);
88
+ break;
89
+ case Archive::FORMAT_MTREE:
90
+ archive_write_set_format_mtree(ar);
91
+ break;
92
+ case Archive::FORMAT_TAR:
93
+ case Archive::FORMAT_TAR_PAX_RESTRICTED:
94
+ archive_write_set_format_pax_restricted(ar);
95
+ break;
96
+ case Archive::FORMAT_TAR_PAX_INTERCHANGE:
97
+ archive_write_set_format_pax(ar);
98
+ break;
99
+ case Archive::FORMAT_TAR_USTAR:
100
+ archive_write_set_format_ustar(ar);
101
+ break;
102
+ default:
103
+ error_msg = "unknown or unsupported archive format";
104
+ throw Error(error_msg);
105
+ }
106
+
107
+ if(archive_write_open_filename(ar, filename) != ARCHIVE_OK) {
108
+ error_msg = archive_error_string(ar);
109
+ throw Error(error_msg);
110
+ }
111
+
112
+ } catch(...) {
113
+ archive_write_finish(ar);
114
+ throw;
115
+ }
116
+
117
+ return new Writer(ar);
118
+ }
119
+
120
+
121
+ Entry *Writer::new_entry_helper()
122
+ {
123
+ return new Entry(archive_entry_new());
124
+ }
125
+
126
+ void Writer::write_header(Entry *entry)
127
+ {
128
+ if(archive_write_header(_ar, entry->_entry) != ARCHIVE_OK) {
129
+ std::string error_msg = archive_error_string(_ar);
130
+ throw Error(error_msg);
131
+ }
132
+ }
133
+
134
+ void Writer::write_data_helper(const char *string, int length)
135
+ {
136
+ if(archive_write_data(_ar, (void*) string, length) == -1) {
137
+ std::string error_msg = archive_error_string(_ar);
138
+ throw Error(error_msg);
139
+ }
140
+ }
141
+
@@ -0,0 +1,103 @@
1
+ /*
2
+ This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ libarchive.
4
+
5
+ Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+
7
+ libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ license text can be found in the file LICENSE.txt distributed with the source.
9
+ */
10
+
11
+ #ifndef CLASS_ARCHIVE_WRITER_H_INCLUDED
12
+ #define CLASS_ARCHIVE_WRITER_H_INCLUDED
13
+
14
+ struct archive;
15
+ class Entry;
16
+
17
+ #ifndef SWIG
18
+ #include <ruby.h>
19
+ #endif
20
+
21
+ namespace Archive
22
+ {
23
+ enum {
24
+ FORMAT_AR = 1,
25
+ FORMAT_AR_BSD,
26
+ FORMAT_AR_SVR4,
27
+ FORMAT_CPIO,
28
+ FORMAT_CPIO_NEWC,
29
+ FORMAT_MTREE,
30
+ FORMAT_TAR,
31
+ FORMAT_TAR_GNUTAR,
32
+ FORMAT_TAR_PAX_INTERCHANGE,
33
+ FORMAT_TAR_PAX_RESTRICTED,
34
+ FORMAT_TAR_USTAR,
35
+ FORMAT_TAR_OLDTAR,
36
+ FORMAT_ZIP
37
+ };
38
+
39
+ enum {
40
+ COMPRESSION_BZIP2 = 1,
41
+ COMPRESSION_COMPRESS,
42
+ COMPRESSION_GZIP,
43
+ COMPRESSION_LZMA,
44
+ COMPRESSION_NONE,
45
+ COMPRESSION_XZ
46
+ };
47
+ };
48
+
49
+
50
+ class Writer
51
+ {
52
+ public:
53
+ Writer(struct archive *ar);
54
+ virtual ~Writer();
55
+ void close();
56
+
57
+ #ifdef SWIG
58
+ %exception {
59
+ try {
60
+ $action
61
+ } catch(Error &err) {
62
+ static VALUE c_archive = rb_define_module("Archive");
63
+ static VALUE e_archive =
64
+ rb_define_class_under(c_archive, "Error", rb_eStandardError);
65
+ rb_raise(e_archive, err.what());
66
+ }
67
+ }
68
+ #endif
69
+
70
+ #ifdef SWIG
71
+ %newobject write_open_filename(const char *filename,
72
+ int compression, int format);
73
+ #endif
74
+
75
+ static Writer *write_open_filename(const char *filename,
76
+ int compression, int format);
77
+
78
+ void write_header(Entry *entry);
79
+
80
+ #ifdef SWIG
81
+ %apply (char *STRING, int LENGTH) { (const char *string, int length) };
82
+ #endif
83
+
84
+ void write_data_helper(const char *string, int length);
85
+
86
+ #ifdef SWIG
87
+ %exception;
88
+ #endif
89
+
90
+ #ifdef SWIG
91
+ %newobject new_entry_helper();
92
+ #endif
93
+
94
+ Entry *new_entry_helper();
95
+
96
+ private:
97
+ struct archive *_ar;
98
+ char *_buf;
99
+ int _buf_size;
100
+
101
+ };
102
+
103
+ #endif
@@ -0,0 +1,114 @@
1
+ #
2
+ # This file is part of "libarchive-ruby-swig", a simple SWIG wrapper around
3
+ # libarchive.
4
+ #
5
+ # Copyright 2011, Tobias Koch <tobias.koch@gmail.com>
6
+ #
7
+ # libarchive-ruby-swig is licensed under a simplified BSD License. A copy of the
8
+ # license text can be found in the file LICENSE.txt distributed with the source.
9
+ #
10
+
11
+ require 'archive'
12
+
13
+ module Archive
14
+
15
+ class Entry
16
+ alias :file? :is_file
17
+ alias :directory? :is_directory
18
+ alias :symbolic_link? :is_symbolic_link
19
+ alias :block_special? :is_block_special
20
+ alias :character_special? :is_character_special
21
+ alias :fifo? :is_fifo
22
+ alias :socket? :is_socket
23
+
24
+ alias :filetype= :set_filetype
25
+ alias :devmajor= :set_devmajor
26
+ alias :devminor= :set_devminor
27
+ alias :atime= :set_atime
28
+ alias :dev= :set_dev
29
+ alias :gid= :set_gid
30
+ alias :gname= :set_gname
31
+ alias :hardlink= :set_hardlink
32
+ alias :ino= :set_ino
33
+ alias :mode= :set_mode
34
+ alias :mtime= :set_mtime
35
+ alias :nlink= :set_nlink
36
+ alias :pathname= :set_pathname
37
+ alias :rdevmajor= :set_rdevmajor
38
+ alias :rdevminor= :set_rdevminor
39
+ alias :size= :set_size
40
+ alias :symlink= :set_symlink
41
+ alias :uid= :set_uid
42
+ alias :uname= :set_uname
43
+
44
+ def copy_stat(path)
45
+ copy_stat_helper(path)
46
+ self.set_symlink(File.readlink(path)) if self.symbolic_link?
47
+ end
48
+
49
+ private_class_method :new
50
+ end
51
+
52
+ class Reader
53
+
54
+ def read_data(size)
55
+ if block_given?
56
+ while result = self.read_data_helper(size)
57
+ yield result
58
+ end
59
+ else
60
+ return self.read_data_helper(size)
61
+ end
62
+ end
63
+
64
+ private_class_method :new
65
+ end
66
+
67
+ class Writer
68
+
69
+ def new_entry()
70
+ entry = self.new_entry_helper
71
+ if block_given?
72
+ yield entry
73
+ else
74
+ return entry
75
+ end
76
+ end
77
+
78
+ def write_data(data = nil)
79
+ if block_given?
80
+ while result = yield
81
+ self.write_data_helper(result)
82
+ end
83
+ else
84
+ self.write_data_helper(data)
85
+ end
86
+ end
87
+
88
+ private_class_method :new
89
+ end
90
+
91
+ def self.read_open_filename(filename)
92
+ ar = Reader.read_open_filename(filename)
93
+
94
+ if block_given?
95
+ yield ar
96
+ ar.close()
97
+ else
98
+ return ar
99
+ end
100
+ end
101
+
102
+ def self.write_open_filename(filename, compression, format)
103
+ ar = Writer.write_open_filename(filename, compression, format)
104
+
105
+ if block_given?
106
+ yield ar
107
+ ar.close()
108
+ else
109
+ return ar
110
+ end
111
+ end
112
+
113
+ end
114
+
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libarchive-ruby-swig
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Tobias Koch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-30 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: libarchive-ruby-swig provides Ruby bindings to libarchive using SWIG. The gem allows you to read and write compressed archives in a variety of formats.
23
+ email: tobias.koch@gmail.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/libarchive-ruby-swig/extconf.rb
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/libarchive_rs.rb
32
+ - ext/libarchive-ruby-swig/entry.cpp
33
+ - ext/libarchive-ruby-swig/entry.h
34
+ - ext/libarchive-ruby-swig/error.h
35
+ - ext/libarchive-ruby-swig/libarchive.i
36
+ - ext/libarchive-ruby-swig/reader.cpp
37
+ - ext/libarchive-ruby-swig/reader.h
38
+ - ext/libarchive-ruby-swig/writer.cpp
39
+ - ext/libarchive-ruby-swig/writer.h
40
+ - ext/libarchive-ruby-swig/extconf.rb
41
+ has_rdoc: true
42
+ homepage: http://libarchive-rs.rubyforge.org
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: Libarchive/Ruby/SWIG
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Ruby bindings to libarchive
75
+ test_files: []
76
+