2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
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
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
def initialize # :nodoc:
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'find'
|
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
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|
|
2019-02-14 07:59:03 -05:00
|
|
|
fn = file_name[gem_directory.size..file_name.size - 1].sub(/^\//, "")
|
2009-06-09 17:38:59 -04:00
|
|
|
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
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Describes a problem with a file in a gem.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
ErrorData = Struct.new :path, :problem do
|
2018-11-21 05:20:47 -05:00
|
|
|
def <=>(other) # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
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=[])
|
2020-06-10 13:46:05 -04:00
|
|
|
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?
|
2013-11-30 18:27:52 -05:00
|
|
|
next if spec.default_gem?
|
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
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless File.directory? gem_directory
|
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
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless File.exist? spec_path
|
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
|
2019-01-22 01:28:04 -05:00
|
|
|
unless File.readable?(gem_path)
|
|
|
|
raise Gem::VerificationError, "missing gem file #{gem_path}"
|
|
|
|
end
|
2008-10-25 18:58:43 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
good, gone, unreadable = nil, nil, nil, nil
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.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
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
good, gone = package.contents.partition do |file_name|
|
2012-11-29 01:52:18 -05:00
|
|
|
File.exist? File.join(gem_directory, file_name)
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
gone.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Missing file"
|
|
|
|
end
|
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
good, unreadable = good.partition do |file_name|
|
2012-11-29 01:52:18 -05:00
|
|
|
File.readable? File.join(gem_directory, file_name)
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
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']
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open source, Gem.binary_mode do |f|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless f.read == data
|
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
|