Module: Archive_r

Defined in:
lib/archive_r.rb,
ext/archive_r/archive_r_ext.cc

Defined Under Namespace

Classes: Entry, Stream, Traverser

Constant Summary collapse

VERSION =
"0.1.8"
STANDARD_FORMATS =

Common archive formats excluding libarchive’s mtree/raw pseudo formats

%w[
  7zip ar cab cpio empty iso9660 lha rar tar warc xar zip
].freeze

Class Method Summary collapse

Class Method Details

.normalize_options(opts = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/archive_r.rb', line 44

def self.normalize_options(opts = nil)
  options =
    case opts
    when nil
      {}
    when Hash
      opts.dup
    else
      opts.to_hash.dup
    end

  options[:formats] = STANDARD_FORMATS unless options.key?(:formats)
  options
end

.on_fault(*args) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'ext/archive_r/archive_r_ext.cc', line 701

static VALUE archive_r_on_fault(int argc, VALUE *argv, VALUE self) {
  VALUE callback = Qnil;
  rb_scan_args(argc, argv, "01", &callback);

  if (!NIL_P(callback) && rb_block_given_p()) {
    rb_raise(rb_eArgError, "provide callable argument or block, not both");
  }

  if (NIL_P(callback) && rb_block_given_p()) {
    callback = rb_block_proc();
  }

  FaultCallback cb = make_ruby_fault_callback(callback);
  register_fault_callback(std::move(cb));
  return self;
}

.register_stream_factory(*args) ⇒ Object



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'ext/archive_r/archive_r_ext.cc', line 667

static VALUE archive_r_register_stream_factory(int argc, VALUE *argv, VALUE self) {
  VALUE callable = Qnil;
  rb_scan_args(argc, argv, "01", &callable);

  if (!NIL_P(callable) && rb_block_given_p()) {
    rb_raise(rb_eArgError, "provide callable argument or block, not both");
  }

  if (NIL_P(callable) && rb_block_given_p()) {
    callable = rb_block_proc();
  }

  if (NIL_P(callable)) {
    set_root_stream_factory(RootStreamFactory{});
    g_stream_factory_callback.reset();
    return Qnil;
  }

  if (!rb_respond_to(callable, rb_id_call_method)) {
    rb_raise(rb_eTypeError, "stream factory must respond to #call");
  }

  auto holder = std::make_shared<RubyCallbackHolder>(callable);

  RootStreamFactory factory = [holder](const PathHierarchy &hierarchy) -> std::shared_ptr<IDataStream> {
    VALUE result = invoke_ruby_stream_factory(holder, hierarchy);
    return stream_from_ruby_value(result, hierarchy);
  };

  set_root_stream_factory(factory);
  g_stream_factory_callback = holder;
  return Qnil;
}

.traverse(paths, **opts, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/archive_r.rb', line 59

def self.traverse(paths, **opts, &block)
  options = normalize_options(opts)

  if block
    Traverser.open(paths, options) { |traverser| traverser.each(&block) }
  else
    Traverser.new(paths, options).each
  end
end