Class: Archive_r::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_r.rb,
ext/archive_r/archive_r_ext.cc

Instance Method Summary collapse

Instance Method Details

#depthObject

Entry#depth -> Integer



818
819
820
821
# File 'ext/archive_r/archive_r_ext.cc', line 818

static VALUE entry_depth(VALUE self) {
  Entry *entry = entry_for_read(self);
  return INT2NUM(entry->depth());
}

#directory?Boolean

Entry#directory? -> Boolean

Returns:

  • (Boolean)


812
813
814
815
# File 'ext/archive_r/archive_r_ext.cc', line 812

static VALUE entry_is_directory(VALUE self) {
  Entry *entry = entry_for_read(self);
  return entry->is_directory() ? Qtrue : Qfalse;
}

#file?Boolean

Entry#file? -> Boolean

Returns:

  • (Boolean)


806
807
808
809
# File 'ext/archive_r/archive_r_ext.cc', line 806

static VALUE entry_is_file(VALUE self) {
  Entry *entry = entry_for_read(self);
  return entry->is_file() ? Qtrue : Qfalse;
}

#inspectObject



87
88
89
# File 'lib/archive_r.rb', line 87

def inspect
"#<Archive_r::Entry path=#{path.inspect} size=#{size} depth=#{depth}>"
end

#metadataObject

Entry#metadata -> Hash



936
937
938
939
940
941
942
943
944
# File 'ext/archive_r/archive_r_ext.cc', line 936

static VALUE entry_metadata(VALUE self) {
  Entry *entry = entry_for_read(self);
  VALUE hash = rb_hash_new();
  const EntryMetadataMap &metadata = entry->metadata();
  for (const auto &kv : metadata) {
    rb_hash_aset(hash, cpp_string_to_rb(kv.first), metadata_value_to_rb(kv.second));
  }
  return hash;
}

#metadata_value(key) ⇒ Object

Entry#metadata_value(key) -> Object or nil



947
948
949
950
951
952
953
954
955
# File 'ext/archive_r/archive_r_ext.cc', line 947

static VALUE entry_metadata_value(VALUE self, VALUE key) {
  Entry *entry = entry_for_read(self);
  std::string key_str = rb_string_to_cpp(StringValue(key));
  const EntryMetadataValue *value = entry->find_metadata(key_str);
  if (!value) {
    return Qnil;
  }
  return metadata_value_to_rb(*value);
}

#nameObject

Entry#name -> String



794
795
796
797
# File 'ext/archive_r/archive_r_ext.cc', line 794

static VALUE entry_name(VALUE self) {
  Entry *entry = entry_for_read(self);
  return cpp_string_to_rb(entry->name());
}

#pathObject

Entry#path -> String



781
782
783
784
785
# File 'ext/archive_r/archive_r_ext.cc', line 781

static VALUE entry_path(VALUE self) {
  Entry *entry = entry_for_read(self);
  const std::string entry_path_str = entry->path();
  return cpp_string_to_rb(entry->path());
}

#path_hierarchyObject

Entry#path_hierarchy -> Array



788
789
790
791
# File 'ext/archive_r/archive_r_ext.cc', line 788

static VALUE entry_path_hierarchy(VALUE self) {
  Entry *entry = entry_for_read(self);
  return path_hierarchy_to_rb(entry->path_hierarchy());
}

#read(*args) ⇒ Object

Entry#read(length = nil) -> String



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'ext/archive_r/archive_r_ext.cc', line 876

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

  bool bounded_read = false;
  size_t requested_size = 0;
  if (!NIL_P(length_val)) {
    long long length_long = NUM2LL(length_val);
    if (length_long == 0) {
      return rb_str_new("", 0);
    } else if (length_long > 0) {
      const auto max_allowed = std::numeric_limits<size_t>::max();
      if (static_cast<unsigned long long>(length_long) > max_allowed) {
        rb_raise(rb_eRangeError, "requested length exceeds platform limits");
      }
      requested_size = static_cast<size_t>(length_long);
      bounded_read = true;
    }
    // Negative values fall through to the streaming path (full read)
  }

  Entry *entry = entry_for_read(self);
  const std::string entry_path_str = entry->path();

  try {
    if (bounded_read) {
      std::vector<uint8_t> buffer(requested_size);
      const ssize_t bytes_read = entry->read(buffer.data(), buffer.size());
      if (bytes_read < 0) {
        rb_raise(rb_eRuntimeError, "Failed to read entry payload at %s", entry_path_str.c_str());
      }
      return rb_str_new(reinterpret_cast<const char *>(buffer.data()), static_cast<long>(bytes_read));
    }

    std::string aggregate;
    const uint64_t reported_size = entry->size();
    if (reported_size > 0 && reported_size <= static_cast<uint64_t>(std::numeric_limits<size_t>::max())) {
      aggregate.reserve(static_cast<size_t>(reported_size));
    }

    std::vector<uint8_t> chunk(64 * 1024);
    while (true) {
      const ssize_t bytes_read = entry->read(chunk.data(), chunk.size());
      if (bytes_read < 0) {
        rb_raise(rb_eRuntimeError, "Failed to read entry payload at %s", entry_path_str.c_str());
      }
      if (bytes_read == 0) {
        break;
      }
      aggregate.append(reinterpret_cast<const char *>(chunk.data()), static_cast<size_t>(bytes_read));
    }

    return rb_str_new(aggregate.data(), static_cast<long>(aggregate.size()));
  } catch (const std::exception &e) {
    rb_raise(rb_eRuntimeError, "Failed to read entry at %s: %s", entry_path_str.c_str(), e.what());
    return Qnil;
  }
}

#set_descent(enabled) ⇒ Object

Entry#set_descent(enabled) -> self



824
825
826
827
828
# File 'ext/archive_r/archive_r_ext.cc', line 824

static VALUE entry_set_descent(VALUE self, VALUE enabled) {
  Entry *entry = entry_for_live(self);
  entry->set_descent(RTEST(enabled));
  return self;
}

#set_multi_volume_group(*args) ⇒ Object

Entry#set_multi_volume_group(base_name, order: :natural) -> nil



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
# File 'ext/archive_r/archive_r_ext.cc', line 831

static VALUE entry_set_multi_volume_group(int argc, VALUE *argv, VALUE self) {
  VALUE base_name_val;
  VALUE options_val = Qnil;

  rb_scan_args(argc, argv, "11", &base_name_val, &options_val);

  MultiVolumeGroupOptions options;
  if (!NIL_P(options_val)) {
    VALUE hash = rb_check_hash_type(options_val);
    if (NIL_P(hash)) {
      rb_raise(rb_eTypeError, "options must be a Hash");
    }

    static ID id_order = rb_intern("order");
    VALUE order_val = rb_hash_aref(hash, ID2SYM(id_order));
    if (!NIL_P(order_val)) {
      std::string order_str;
      if (SYMBOL_P(order_val)) {
        order_str = rb_id2name(SYM2ID(order_val));
      } else {
        order_str = rb_string_to_cpp(StringValue(order_val));
      }
      for (char &ch : order_str) {
        ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
      }
      if (order_str == "given") {
        options.ordering = PathEntry::Parts::Ordering::Given;
      } else if (order_str == "natural") {
        options.ordering = PathEntry::Parts::Ordering::Natural;
      } else {
        rb_raise(rb_eArgError, "order must be :natural or :given");
      }
    }
  }

  Entry *entry = entry_for_live(self);
  try {
    entry->set_multi_volume_group(rb_string_to_cpp(StringValue(base_name_val)), options);
  } catch (const std::exception &e) {
    rb_raise(rb_eRuntimeError, "Failed to set multi-volume group: %s", e.what());
  }
  return Qnil;
}

#sizeObject

Entry#size -> Integer



800
801
802
803
# File 'ext/archive_r/archive_r_ext.cc', line 800

static VALUE entry_size(VALUE self) {
  Entry *entry = entry_for_read(self);
  return LONG2NUM(entry->size());
}

#to_sObject

Additional helper methods can be added here



83
84
85
# File 'lib/archive_r.rb', line 83

def to_s
  path
end