Class: Ruar::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/ruar/access.rb,
lib/ruar/core_ext/kernel_require.rb,
ext/ruar/ruar.c

Defined Under Namespace

Modules: CoreExt, Native

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive) ⇒ Access

Returns a new instance of Access.



25
26
27
28
# File 'lib/ruar/access.rb', line 25

def initialize(archive)
  @archive = archive
  rebuild
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



5
6
7
# File 'lib/ruar/access.rb', line 5

def archive
  @archive
end

#headerObject (readonly)

Returns the value of attribute header.



5
6
7
# File 'lib/ruar/access.rb', line 5

def header
  @header
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/ruar/access.rb', line 5

def index
  @index
end

Class Method Details

.abs_path(path) ⇒ Object



77
78
79
# File 'lib/ruar/access.rb', line 77

def self.abs_path(path)
  Pathname.new(path).cleanpath.to_s
end

.clean_path(path) ⇒ Object

Array



82
83
84
85
86
87
# File 'lib/ruar/access.rb', line 82

def self.clean_path(path)
  cleaned = Ruar::Access.abs_path(path).split(File::SEPARATOR)
  cleaned.delete('')

  cleaned
end

.make_failed_to_eval_error(path) ⇒ Object



11
12
13
# File 'lib/ruar/access.rb', line 11

def self.make_failed_to_eval_error(path)
  Ruar::Error::FailedToEval.new(path)
end

.make_not_exist_error(path) ⇒ Object



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

def self.make_not_exist_error(path)
  Ruar::Error::FileNotFound.new(path)
end

.warn_autoload(name_error) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ruar/access.rb', line 15

def self.warn_autoload(name_error)
  location = name_error.backtrace_locations.first
  message = <<~MSG
    #{location}
    Kernel.autoload and Module.autoload are not supported by ruar,
    if you are using them, try `require` instead
  MSG
  warn message.yellow
end

Instance Method Details

#eval(path, eval_bind = TOPLEVEL_BINDING) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruar/access.rb', line 60

def eval(path, eval_bind = TOPLEVEL_BINDING)
  pseudo_filename = File.join(Ruar.path_prefix.to_s, Ruar::Access.abs_path(path))
  pseudo_lineno = 1
  file = read(path)
  # FIXME: need to test
  begin
    Kernel.eval(file, eval_bind, pseudo_filename, pseudo_lineno)
  rescue NameError => e # FIXME: to warn autoload pitfall
    begin
      Ruar::Access.warn_autoload(e)
      raise
    rescue StandardError
      raise Ruar::Access.make_failed_to_eval_error(path)
    end
  end
end

#lookup(path) ⇒ Object

TODO: Need to deliberately test this part



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruar/access.rb', line 31

def lookup(path)
  paths = Ruar::Access.clean_path(path)
  filename = paths.pop

  pwd = @index['files']

  begin
    paths.each { |dir| pwd = pwd[dir]['files'] }
  rescue StandardError
    raise Ruar::Access.make_not_exist_error(path)
  end

  raise Ruar::Access.make_not_exist_error(path) if pwd[filename].nil?

  offset = pwd[filename]['offset'] + @file_start
  size = pwd[filename]['size']
  executable = pwd[filename]['executable']

  [offset, size, executable]
end

#read(path) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ruar/access.rb', line 52

def read(path)
  offset, size, _executable = lookup(path)
  file = Ruar::Access::Native.file(@archive, offset.to_i, size.to_i)
  file = Ruar.cipher.decrypt(file)[:decrypted] if Ruar.cipher.enable?

  file
end