tarruby 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -48,11 +48,16 @@ gem install tarruby
48
48
 
49
49
  require 'tarruby'
50
50
 
51
+ buf = 'ABCDEFG'
52
+
51
53
  Tar.open('bar.tar', File::CREAT | File::WRONLY, 0644, Tar::GNU | Tar::VERBOSE) do |tar|
52
54
  Dir.glob('**/*.c').each do |filename|
53
55
  tar.append_file(filename)
54
56
  end
55
57
 
58
+ # append from buffer
59
+ tar.append_buffer('zoo.txt, buf)
60
+
56
61
  ##if append directory
57
62
  #tar.append_tree('dirname')
58
63
  end
@@ -21,6 +21,7 @@
21
21
  #include <fcntl.h>
22
22
  #include <sys/param.h>
23
23
  #include <sys/types.h>
24
+ #include <time.h>
24
25
 
25
26
  #ifdef STDC_HEADERS
26
27
  # include <stdlib.h>
@@ -266,4 +267,69 @@ tar_append_regfile(TAR *t, char *realname)
266
267
  return 0;
267
268
  }
268
269
 
270
+ static int
271
+ tar_append_function0(TAR *t, void *data, int (*f)(char *b, int l, void *d))
272
+ {
273
+ char block[T_BLOCKSIZE];
274
+ int filefd;
275
+ int i, j;
276
+ size_t size;
277
+
278
+ size = th_get_size(t);
279
+ for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
280
+ {
281
+ j = f(block, T_BLOCKSIZE, data);
282
+ if (j != T_BLOCKSIZE)
283
+ {
284
+ if (j != -1)
285
+ errno = EINVAL;
286
+ return -1;
287
+ }
288
+ if (tar_block_write(t, &block) == -1)
289
+ return -1;
290
+ }
291
+
292
+ if (i > 0)
293
+ {
294
+ j = f(block, i, data);
295
+ if (j == -1)
296
+ return -1;
297
+ memset(&(block[i]), 0, T_BLOCKSIZE - i);
298
+ if (tar_block_write(t, &block) == -1)
299
+ return -1;
300
+ }
301
+
302
+ return 0;
303
+ }
304
+
305
+ int
306
+ tar_append_function(TAR *t, char *savename, int size, void *data, int (*f)(char *b, int l, void *d))
307
+ {
308
+ /* set header block */
309
+ memset(&(t->th_buf), 0, sizeof(struct tar_header));
310
+ t->th_buf.typeflag = REGTYPE;
311
+ th_set_user(t, 0);
312
+ th_set_group(t, 0);
313
+ th_set_mode(t, 0644);
314
+ th_set_mtime(t, (int) time(NULL));
315
+ th_set_size(t, size);
316
+
317
+ /* set the header path */
318
+ th_set_path(t, savename);
319
+
320
+ /* print file info */
321
+ if (t->options & TAR_VERBOSE)
322
+ th_print_long_ls(t);
269
323
 
324
+ /* write header */
325
+ if (th_write(t) != 0)
326
+ {
327
+ return -1;
328
+ }
329
+
330
+ /* if it's a regular file, write the contents as well */
331
+ if (tar_append_function0(t, data, f) != 0)
332
+ return -1;
333
+
334
+ return 0;
335
+ }
@@ -149,6 +149,7 @@ int tar_append_eof(TAR *t);
149
149
  /* add file contents to a tarchive */
150
150
  int tar_append_regfile(TAR *t, char *realname);
151
151
 
152
+ int tar_append_function(TAR *t, char *savename, int size, void *data, int (*f)(char *b, int l, void *d));
152
153
 
153
154
  /***** block.c *************************************************************/
154
155
 
@@ -33,7 +33,7 @@
33
33
 
34
34
  #define INT2TIME(i) rb_funcall(rb_cTime, rb_intern("at"), 1, INT2NUM(i))
35
35
 
36
- #define VERSION "0.1.3"
36
+ #define VERSION "0.1.4"
37
37
 
38
38
  static VALUE Tar;
39
39
  static VALUE Error;
@@ -271,6 +271,35 @@ static VALUE tarruby_append_file(int argc, VALUE *argv, VALUE self) {
271
271
  return Qnil;
272
272
  }
273
273
 
274
+ static int tarruby_append_buffer0(char *buf, int len, void *data) {
275
+ char **p_buffer = (char **) data;
276
+ memcpy(buf, *p_buffer, len);
277
+ p_buffer += len;
278
+ return len;
279
+ }
280
+
281
+ /* */
282
+ static VALUE tarruby_append_buffer(VALUE self, VALUE savename, VALUE buffer) {
283
+ struct tarruby_tar *p_tar;
284
+ char *s_savename, *s_buffer;
285
+ int i_size;
286
+
287
+ Check_Type(savename, T_STRING);
288
+ Check_Type(buffer, T_STRING);
289
+ s_savename = RSTRING_PTR(savename);
290
+ strip_sep(s_savename);
291
+ s_buffer = RSTRING_PTR(buffer);
292
+ i_size = RSTRING_LEN(buffer);
293
+
294
+ Data_Get_Struct(self, struct tarruby_tar, p_tar);
295
+
296
+ if (tar_append_function(p_tar->tar, s_savename, i_size, (void *) &s_buffer, tarruby_append_buffer0) != 0) {
297
+ rb_raise(Error, "Append buffer failed: %s", strerror(errno));
298
+ }
299
+
300
+ return Qnil;
301
+ }
302
+
274
303
  /* */
275
304
  static VALUE tarruby_append_tree(int argc, VALUE *argv, VALUE self) {
276
305
  VALUE realdir, savedir;
@@ -323,7 +352,7 @@ static VALUE tarruby_extract_file(VALUE self, VALUE realname) {
323
352
  static int tarruby_extract_buffer0(char *buf, int len, void *data) {
324
353
  VALUE buffer = (VALUE) data;
325
354
  rb_str_buf_cat(buffer, buf, len);
326
- return 0;
355
+ return len;
327
356
  }
328
357
 
329
358
  /* */
@@ -650,6 +679,7 @@ void DLLEXPORT Init_tarruby() {
650
679
  #endif
651
680
  rb_define_method(Tar, "close", tarruby_close, 0);
652
681
  rb_define_method(Tar, "append_file", tarruby_append_file, -1);
682
+ rb_define_method(Tar, "append_buffer", tarruby_append_buffer, 2);
653
683
  rb_define_method(Tar, "append_tree", tarruby_append_tree, -1);
654
684
  rb_define_method(Tar, "extract_file", tarruby_extract_file, 1);
655
685
  rb_define_method(Tar, "extract_buffer", tarruby_extract_buffer, 0);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-14 00:00:00 +09:00
12
+ date: 2008-09-16 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15