Class: Ar

Inherits:
Object
  • Object
show all
Defined in:
lib/ar.rb

Constant Summary collapse

VERSION =
"0.1"
ArMagic =
"!<arch>\n"
ArMagicPad =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Ar

Returns a new instance of Ar.



9
10
11
12
# File 'lib/ar.rb', line 9

def initialize(filename, options = {})
  @sourcefile = filename
  @files = Hash.new
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



7
8
9
# File 'lib/ar.rb', line 7

def files
  @files
end

#sourcefileObject (readonly)

Returns the value of attribute sourcefile.



7
8
9
# File 'lib/ar.rb', line 7

def sourcefile
  @sourcefile
end

Instance Method Details

#dump(filename, path = "./") ⇒ Object



49
50
51
52
53
54
# File 'lib/ar.rb', line 49

def dump(filename, path = "./")
  dump_path = File.join(path, filename)
  File.open(dump_path, "w+") do |f|
    f.write(@files[filename]["payload"])
  end
end

#get_payload(filename) ⇒ Object



60
61
62
63
# File 'lib/ar.rb', line 60

def get_payload(filename)
  raise "File not in archive" unless self.files.include?(filename)
  @files[filename]["payload"]
end

#loadObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ar.rb', line 14

def load()
  fp = open(@sourcefile, "r+")
  magic = fp.read(ArMagic.length)
  if magic != ArMagic
    raise "Magic not found !"
  end

  begin
    filename = ""
    while filename != nil
      filename = fp.read(16)
      if filename.nil?
        return
      end
      filename.gsub!(/\/\s+$/,"")
      filename.strip!
      @files[filename] = Hash.new
      @files[filename]["date"] = fp.read(12).to_i
      @files[filename]["uid"] = fp.read(6).to_i
      @files[filename]["gid"] = fp.read(6).to_i
      @files[filename]["filemode"] = fp.read(8).to_i
      @files[filename]["filesize"] = fp.read(10).to_i
      if @files[filename]["filesize"] != 0
        fp.read(2)
        @files[filename]["payload"] = fp.read(@files[filename]["filesize"])
      else
        @files.delete(filename)
      end
    end
  rescue Exception => e
    puts e.message
    return
  end
end