# # Ronin - A Ruby platform for exploit development and security research. # # Copyright (c) 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # require 'ronin/database' require 'ronin/cacheable' require 'ronin/extensions/kernel' module Ronin module Platform module ObjectCache # # Searches for object files within the specified _directory_. # # @param [String] directory # The directory to search for object files within. # # @return [Array] # All paths within the specified _directory_ pointing to object # files. # def ObjectCache.paths(directory) Dir[File.join(File.expand_path(directory),'**','*.rb')] end # # Finds all cached objects. # # @param [String] directory # Optional directory to search within for cached objects. # # @yield [obj] # The block that will receive all cached object. # # @yieldparam [Cacheable] obj # The cached object. # def ObjectCache.each(directory=nil,&block) attributes = {} if directory attributes.merge!(:cached_path.like => File.join(directory,'%')) end Cacheable.models.each do |base| base.all(attributes).each(&block) end return true end # # Cache all objects loaded from the paths within the specified # _directory_. # # @param [String] directory # The directory to cache all objects from. # def ObjectCache.cache(directory) Database.setup unless Database.setup? ObjectCache.paths(directory).each do |path| catch_all { Cacheable.cache_all(path) } end return true end # # Syncs all objects that were previously cached from paths within # the specified _directory_. Also cache objects which have yet to # be cached from the _directory_. # # @param [String] directory # The directory to sync all objects with. # def ObjectCache.sync(directory) new_paths = ObjectCache.paths(directory) Database.setup unless Database.setup? ObjectCache.each(directory) do |obj| new_paths.delete(obj.cached_path) catch_all { obj.sync! } end # cache the remaining new paths new_paths.each { |path| Cacheable.cache(path) } return true end # # Deletes all cached objects that existed in the specified # _directory_. # # @param [String] directory # Deletes all cached objects from the specified _directory_. # def ObjectCache.clean(directory) Database.setup unless Database.setup? ObjectCache.each(directory) { |obj| obj.destroy } return true end end end end