Class: MooTool::Models::FileIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/mootool/models/file_index.rb

Constant Summary collapse

PATHS =
%w[
  /System/Volumes/Hardware/FactoryData/System/Library/Caches/com.apple.factorydata
].freeze
MOUNTS_PATTERNS =
%w[
  iSCPreboot
  Preboot
  Update
  Recovery
  Hardware
].freeze
FILE_KINDS =
%w[
  kernelcache* kernel sep-patches* imutablekernel *.der *.im4m *.aea.* ft*.bin
  *.im4p *.dmg *.der *.img4 *.pem *request.txt *response.txt dmg.*
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index = nil) ⇒ FileIndex

Returns a new instance of FileIndex.



23
24
25
# File 'lib/mootool/models/file_index.rb', line 23

def initialize(index = nil)
  @index = index || []
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



27
28
29
# File 'lib/mootool/models/file_index.rb', line 27

def index
  @index
end

Class Method Details

.currentObject



29
30
31
# File 'lib/mootool/models/file_index.rb', line 29

def self.current
  @current ||= load('/Users/rickmark/Desktop/index.json')
end

.load(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mootool/models/file_index.rb', line 33

def self.load(path)
  json_data = JSON.parse(File.read(path))

  index_data = json_data.map do |entry|
    FileLocation.from_hash entry
  end

  @index = new(index_data)
rescue StandardError
  new([])
end

Instance Method Details

#as_json(options = {}) ⇒ Object



75
76
77
# File 'lib/mootool/models/file_index.rb', line 75

def as_json(options = {})
  @index.map { |e| e.as_json(options) }
end

#files_with_hash(hash) ⇒ Object



89
90
91
# File 'lib/mootool/models/file_index.rb', line 89

def files_with_hash(hash)
  @index.select { |entry| entry.hash? hash }
end

#generate_hashesObject



93
94
95
# File 'lib/mootool/models/file_index.rb', line 93

def generate_hashes
  @index.each(&:hashes)
end

#has_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/mootool/models/file_index.rb', line 83

def has_hash?(hash)
  @index.any? do |entry|
    entry.hash? hash
  end
end

#mountsObject



45
46
47
48
49
50
# File 'lib/mootool/models/file_index.rb', line 45

def mounts
  MOUNTS_PATTERNS.flat_map do |pattern|
    volume_mounts = Dir.glob("/Volumes/#{pattern}*")
    volume_mounts + ["/System/Volumes/#{pattern}"]
  end
end

#performObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mootool/models/file_index.rb', line 52

def perform
  flat_names = PATHS.flat_map do |path|
    Dir.glob('**/*', base: path).map do |file|
      MooTool::Models::FileLocation.new(path, file)
    end
  end

  search_names = mounts.flat_map do |path|
    Dir.glob("**/{#{FILE_KINDS.join(',')}}", base: path).map do |file|
      MooTool::Models::FileLocation.new(path, file)
    rescue StandardError
      next
    end
  end

  @index = flat_names + search_names
  @index.reject! do |e|
    File.directory? e.fullname
  rescue Errno::EACCES, Errno::EPERM
    true
  end
end

#to_json(*options) ⇒ Object



79
80
81
# File 'lib/mootool/models/file_index.rb', line 79

def to_json(*options)
  as_json(*options).to_json(*options)
end