Sha256: 9bb7e5b812e76f178c949f3f014b2c7d8aa8acaf62eac2c583d8ec705f3958fd

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require 'smart_proxy_openscap/storage'

module Proxy::OpenSCAP
  class StorageFS < Storage
    def store_archive(data)
      store(data, StoreReportError)
    end

    def store_spool(data)
      store(data, StoreSpoolError)
    end

    def store_failed(data)
      store(data, StoreFailedError)
    end

    def get_arf_xml(digest)
      get_arf_file(digest)[:xml]
    end

    def get_arf_html(digest)
      OpenSCAP.oscap_init
      xml = get_arf_file(digest)[:xml]
      size = get_arf_file(digest)[:size]
      arf_object = OpenSCAP::DS::Arf.new(:content => xml, :path => 'arf.xml.bz2', :length => size)
      arf_object.html.force_encoding('UTF-8')
    end

    private

    def store_arf(spool_arf_dir, data)
      filename = Digest::SHA256.hexdigest data
      target_path = spool_arf_dir + filename
      File.open(target_path,'w') { |f| f.write(data) }
      target_path
    end

    def create_directory
      begin
        FileUtils.mkdir_p @path
      rescue StandardError => e
        logger.error "Could not create '#{dir}' directory: #{e.message}"
        raise e
      end
      @path
    end

    def store(data, error_type)
      begin
        create_directory
      rescue StandardError => e
        raise error_type, "Could not fulfill request: #{e.message}"
      end

      begin
        target_path = store_arf(@path, data)
      rescue StandardError => e
        raise error_type, "Could not store file: #{e.message}"
      end

      logger.debug "File #{target_path} stored in reports dir."
    end

    def get_arf_file(digest)
      full_path = @path + digest
      raise FileNotFound, "Can't find path #{full_path}" if !File.file?(full_path) || File.zero?(full_path)
      file = File.open(full_path)
      { :size => file.size, :xml => file.read }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_proxy_openscap-0.5.0 lib/smart_proxy_openscap/storage_fs.rb