Sha256: 4b2f95ef884901394a89f2ce5323a6eb15f4bb713959c478dc4b241a3d65dfe1

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

require_relative "cpio/cpio"
require_relative "cpio/cpio_old_format"

module Excavate
  module Extractors
    class CpioExtractor < Extractor
      def extract(target)
        extract_cpio_new_format(target)
      rescue RuntimeError => e
        raise unless e.message.start_with?("Invalid magic")

        extract_cpio_old_format(target)
      end

      private

      def extract_cpio_inner_new(entry, file, target)
        path = File.join(target, entry.name)
        if entry.directory?
          FileUtils.mkdir_p(path)
        else
          FileUtils.mkdir_p(File.dirname(path))
          File.write(path, file.read, mode: "wb")
        end
      end

      def extract_cpio_new_format(target)
        File.open(@archive, "rb") do |archive_file|
          CPIO::ASCIIReader.new(archive_file).each do |entry, file|
            extract_cpio_inner_new(entry, file, target)
          end
        end
      end

      def extract_cpio_inner_old(entry, target)
        path = File.expand_path(entry.filename, target)
        if entry.directory?
          FileUtils.mkdir_p(path)
        else
          FileUtils.mkdir_p(File.dirname(path))
          File.write(path, entry.data, mode: "wb")
        end
      end

      def extract_cpio_old_format(target)
        File.open(@archive, "rb") do |archive_file|
          CPIO::ArchiveReader.new(archive_file).each_entry do |entry|
            extract_cpio_inner_old(entry, target)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
excavate-0.3.7 lib/excavate/extractors/cpio_extractor.rb
excavate-0.3.6 lib/excavate/extractors/cpio_extractor.rb
excavate-0.3.5 lib/excavate/extractors/cpio_extractor.rb
excavate-0.3.4 lib/excavate/extractors/cpio_extractor.rb
excavate-0.3.3 lib/excavate/extractors/cpio_extractor.rb
excavate-0.3.2 lib/excavate/extractors/cpio_extractor.rb