class Dle::DlFile::Parser

Public Class Methods

new(file) click to toggle source
# File lib/dle/dl_file.rb, line 12
def initialize file
  @file = file
end

Public Instance Methods

parse() click to toggle source
# File lib/dle/dl_file.rb, line 16
def parse
  {}.tap do |fs|
    return fs unless File.readable?(@file)
    File.readlines(@file).each do |l|
      next if l.start_with?("#")
      if l.strip.start_with?("<HD-BASE>")
        fs[:HD_BASE] = l.strip.gsub("<HD-BASE>", "").gsub("</HD-BASE>", "")
      elsif l.strip.start_with?()
        fs[:HD_DOTFILES] = l.strip.gsub("<HD-DOTFILES>", "").gsub("</HD-DOTFILES>", "") == "true"
      elsif l.count("|") >= 4
        # parse line
        chunks = l.split("|")
        data = {}.tap do |r|
          r[:inode] = chunks.shift.strip
          r[:mode] = chunks.shift.strip
          r[:uid], r[:gid] = chunks.shift.split(":").map(&:strip)
          chunks.shift # ignore size
          r[:relative_path] = chunks.join("|").strip
          r[:path] = [fs[:HD_BASE], r[:relative_path]].join("/")
        end

        # skip headers
        next if data[:inode].downcase == "in"
        next if data[:mode].downcase == "mode"
        next if data[:uid].downcase == "owner"
        next if data[:relative_path].downcase == "file"

        # map node
        if fs.key? data[:inode]
          Thread.main.app_logger.warn "inode #{data[:inode]} already mapped, ignore..."
        else
          fs[data[:inode]] = Filesystem::Softnode.new(data)
        end
      end
    end
    Thread.main.app_logger.warn("DLFILE has no HD-BASE, deltaFS will fail!") unless fs[:HD_BASE].present?
  end
end