2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/package'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/installer'
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
##
|
|
|
|
# Validator performs various gem file and gem database validation
|
|
|
|
|
|
|
|
class Gem::Validator
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def initialize
|
|
|
|
require 'find'
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
##
|
2008-10-25 18:58:43 -04:00
|
|
|
# Given a gem file's contents, validates against its own MD5 checksum
|
|
|
|
# gem_data:: [String] Contents of the gem file
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
def verify_gem(gem_data)
|
2012-11-29 01:52:18 -05:00
|
|
|
# TODO remove me? The code here only validate an MD5SUM that was
|
|
|
|
# in some old formatted gems, but hasn't been for a long time.
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
##
|
|
|
|
# Given the path to a gem file, validates against its own MD5 checksum
|
|
|
|
#
|
|
|
|
# gem_path:: [String] Path to gem file
|
|
|
|
|
|
|
|
def verify_gem_file(gem_path)
|
|
|
|
open gem_path, Gem.binary_mode do |file|
|
|
|
|
gem_data = file.read
|
|
|
|
verify_gem gem_data
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
rescue Errno::ENOENT, Errno::EINVAL
|
2008-10-25 18:58:43 -04:00
|
|
|
raise Gem::VerificationError, "missing gem file #{gem_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_files_for_gem(gem_directory)
|
|
|
|
installed_files = []
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
Find.find gem_directory do |file_name|
|
|
|
|
fn = file_name[gem_directory.size..file_name.size-1].sub(/^\//, "")
|
|
|
|
installed_files << fn unless
|
|
|
|
fn =~ /CVS/ || fn.empty? || File.directory?(file_name)
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
installed_files
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
ErrorData = Struct.new :path, :problem do
|
|
|
|
def <=> other
|
|
|
|
return nil unless self.class === other
|
|
|
|
|
|
|
|
[path, problem] <=> [other.path, other.problem]
|
|
|
|
end
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Checks the gem directory for the following potential
|
|
|
|
# inconsistencies/problems:
|
|
|
|
#
|
|
|
|
# * Checksum gem itself
|
|
|
|
# * For each file in each gem, check consistency of installed versions
|
|
|
|
# * Check for files that aren't part of the gem but are in the gems directory
|
|
|
|
# * 1 cache - 1 spec - 1 directory.
|
|
|
|
#
|
|
|
|
# returns a hash of ErrorData objects, keyed on the problem gem's name.
|
2012-11-29 01:52:18 -05:00
|
|
|
#--
|
|
|
|
# TODO needs further cleanup
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def alien(gems=[])
|
|
|
|
errors = Hash.new { |h,k| h[k] = {} }
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
Gem::Specification.each do |spec|
|
|
|
|
next unless gems.include? spec.name unless gems.empty?
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
gem_name = spec.file_name
|
|
|
|
gem_path = spec.cache_file
|
|
|
|
spec_path = spec.spec_file
|
|
|
|
gem_directory = spec.full_gem_path
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
unless File.directory? gem_directory then
|
2012-11-29 01:52:18 -05:00
|
|
|
errors[gem_name][spec.full_name] =
|
2009-06-09 17:38:59 -04:00
|
|
|
"Gem registered but doesn't exist at #{gem_directory}"
|
|
|
|
next
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
|
|
|
unless File.exist? spec_path then
|
2009-06-09 17:38:59 -04:00
|
|
|
errors[gem_name][spec_path] = "Spec file missing for installed gem"
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
verify_gem_file(gem_path)
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good, gone, unreadable = nil, nil, nil, nil
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
open gem_path, Gem.binary_mode do |file|
|
2012-11-29 01:52:18 -05:00
|
|
|
package = Gem::Package.new gem_path
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
good, gone = package.contents.partition { |file_name|
|
|
|
|
File.exist? File.join(gem_directory, file_name)
|
2009-06-09 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gone.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Missing file"
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
good, unreadable = good.partition { |file_name|
|
|
|
|
File.readable? File.join(gem_directory, file_name)
|
2009-06-09 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unreadable.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Unreadable file"
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good.each do |entry, data|
|
|
|
|
begin
|
|
|
|
next unless data # HACK `gem check -a mkrf`
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
source = File.join gem_directory, entry['path']
|
|
|
|
|
|
|
|
open source, Gem.binary_mode do |f|
|
|
|
|
unless f.read == data then
|
2009-06-09 17:38:59 -04:00
|
|
|
errors[gem_name][entry['path']] = "Modified from original"
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
installed_files = find_files_for_gem(gem_directory)
|
|
|
|
extras = installed_files - good - unreadable
|
|
|
|
|
|
|
|
extras.each do |extra|
|
|
|
|
errors[gem_name][extra] = "Extra file"
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
rescue Gem::VerificationError => e
|
2009-06-09 17:38:59 -04:00
|
|
|
errors[gem_name][gem_path] = e.message
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
errors.each do |name, subhash|
|
2012-12-22 19:35:09 -05:00
|
|
|
errors[name] = subhash.map do |path, msg|
|
|
|
|
ErrorData.new path, msg
|
|
|
|
end.sort
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
errors
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|