class Arachni::Support::Database::Base

Base class for Database data structures

Provides helper methods for data structures to be implemented related to objecting dumping, loading, unique filename generation, etc.

@author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>

@abstract

Public Class Methods

new( serializer = Marshal ) click to toggle source

@param [Object] serializer

Any object that responds to 'dump' and 'load'.
# File lib/arachni/support/database/base.rb, line 24
def initialize( serializer = Marshal )
    @serializer       = serializer
    @filename_counter = 0
end

Public Instance Methods

serialize( obj ) click to toggle source
# File lib/arachni/support/database/base.rb, line 29
def serialize( obj )
    compress( serializer.dump( obj ) )
end
unserialize( data ) click to toggle source
# File lib/arachni/support/database/base.rb, line 33
def unserialize( data )
    serializer.load( decompress( data ) )
end

Private Instance Methods

compress( string ) click to toggle source
# File lib/arachni/support/database/base.rb, line 90
def compress( string )
    Zlib::Deflate.deflate string
end
decompress( string ) click to toggle source
# File lib/arachni/support/database/base.rb, line 94
def decompress( string )
    Zlib::Inflate.inflate string
end
delete_file( filepath ) click to toggle source

Deletes a file.

@param [String] filepath

# File lib/arachni/support/database/base.rb, line 71
def delete_file( filepath )
    File.delete( filepath ) if File.exist?( filepath )
end
dump( obj, &block ) click to toggle source

Dumps the object to a unique file and returns its path.

The path can be used as a reference to the original value by way of passing it to load().

@param [Object] obj

@return [String]

Filepath
# File lib/arachni/support/database/base.rb, line 48
def dump( obj, &block )
    File.open( get_unique_filename, 'wb' ) do |f|
        serialized = serialize( obj )
        f.write( serialized )

        block.call( serialized ) if block_given?

        f.path
    end
end
generate_filename() click to toggle source
# File lib/arachni/support/database/base.rb, line 103
def generate_filename
    # Should be unique enough...
    "#{Options.paths.tmpdir}/#{self.class.name}_#{Process.pid}_#{object_id}_#{@filename_counter}".gsub( '::', '_' )
ensure
    @filename_counter += 1
end
get_unique_filename() click to toggle source
# File lib/arachni/support/database/base.rb, line 98
def get_unique_filename
    {} while File.exist?( path = generate_filename )
    path
end
load( filepath ) click to toggle source

Loads the object stored in filepath.

@param [String] filepath

@return [Object]

# File lib/arachni/support/database/base.rb, line 64
def load( filepath )
    unserialize( IO.binread( filepath ) )
end
load_and_delete_file( filepath ) click to toggle source

Loads the object in file and then removes it from the file-system.

@param [String] filepath

@return [Object]

# File lib/arachni/support/database/base.rb, line 80
def load_and_delete_file( filepath )
    obj = load( filepath )
    delete_file( filepath )
    obj
end
serializer() click to toggle source
# File lib/arachni/support/database/base.rb, line 86
def serializer
    @serializer
end