2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 10:48:56 +03:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "package"
|
|
|
|
require_relative "installer"
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2008-10-26 02:58:43 +04:00
|
|
|
##
|
|
|
|
# Validator performs various gem file and gem database validation
|
|
|
|
|
|
|
|
class Gem::Validator
|
|
|
|
include Gem::UserInteraction
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2013-10-20 05:33:19 +04:00
|
|
|
def initialize # :nodoc:
|
2011-01-19 03:08:49 +03:00
|
|
|
require "find"
|
|
|
|
end
|
|
|
|
|
2008-10-26 02:58:43 +04:00
|
|
|
private
|
|
|
|
|
|
|
|
def find_files_for_gem(gem_directory)
|
|
|
|
installed_files = []
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
Find.find gem_directory do |file_name|
|
2019-02-14 15:59:03 +03:00
|
|
|
fn = file_name[gem_directory.size..file_name.size - 1].sub(/^\//, "")
|
2009-06-10 01:38:59 +04:00
|
|
|
installed_files << fn unless
|
2022-08-03 06:24:38 +03:00
|
|
|
fn.empty? || fn.include?("CVS") || File.directory?(file_name)
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2008-10-26 02:58:43 +04:00
|
|
|
installed_files
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
|
2013-10-20 05:33:19 +04:00
|
|
|
##
|
|
|
|
# Describes a problem with a file in a gem.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
ErrorData = Struct.new :path, :problem do
|
2018-11-21 13:20:47 +03:00
|
|
|
def <=>(other) # :nodoc:
|
2012-11-29 10:52:18 +04:00
|
|
|
return nil unless self.class === other
|
|
|
|
|
|
|
|
[path, problem] <=> [other.path, other.problem]
|
|
|
|
end
|
|
|
|
end
|
2008-10-26 02: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 10:52:18 +04:00
|
|
|
#--
|
|
|
|
# TODO needs further cleanup
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
def alien(gems=[])
|
2020-06-10 20:46:05 +03:00
|
|
|
errors = Hash.new {|h,k| h[k] = {} }
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
Gem::Specification.each do |spec|
|
|
|
|
next unless gems.include? spec.name unless gems.empty?
|
2013-12-01 03:27:52 +04:00
|
|
|
next if spec.default_gem?
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
gem_name = spec.file_name
|
|
|
|
gem_path = spec.cache_file
|
|
|
|
spec_path = spec.spec_file
|
|
|
|
gem_directory = spec.full_gem_path
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
unless File.directory? gem_directory
|
2012-11-29 10:52:18 +04:00
|
|
|
errors[gem_name][spec.full_name] =
|
2009-06-10 01:38:59 +04:00
|
|
|
"Gem registered but doesn't exist at #{gem_directory}"
|
|
|
|
next
|
|
|
|
end
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
unless File.exist? spec_path
|
2009-06-10 01:38:59 +04:00
|
|
|
errors[gem_name][spec_path] = "Spec file missing for installed gem"
|
2008-10-26 02:58:43 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2019-01-22 09:28:04 +03:00
|
|
|
unless File.readable?(gem_path)
|
|
|
|
raise Gem::VerificationError, "missing gem file #{gem_path}"
|
|
|
|
end
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
good, gone, unreadable = nil, nil, nil, nil
|
|
|
|
|
2018-02-06 05:58:35 +03:00
|
|
|
File.open gem_path, Gem.binary_mode do |file|
|
2012-11-29 10:52:18 +04:00
|
|
|
package = Gem::Package.new gem_path
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
good, gone = package.contents.partition do |file_name|
|
2012-11-29 10:52:18 +04:00
|
|
|
File.exist? File.join(gem_directory, file_name)
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
|
|
|
|
gone.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Missing file"
|
|
|
|
end
|
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
good, unreadable = good.partition do |file_name|
|
2012-11-29 10:52:18 +04:00
|
|
|
File.readable? File.join(gem_directory, file_name)
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
|
|
|
|
unreadable.sort.each do |path|
|
|
|
|
errors[gem_name][path] = "Unreadable file"
|
|
|
|
end
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
good.each do |entry, data|
|
|
|
|
begin
|
|
|
|
next unless data # HACK `gem check -a mkrf`
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
source = File.join gem_directory, entry["path"]
|
|
|
|
|
2018-02-06 05:58:35 +03:00
|
|
|
File.open source, Gem.binary_mode do |f|
|
2018-11-21 13:20:47 +03:00
|
|
|
unless f.read == data
|
2009-06-10 01:38:59 +04:00
|
|
|
errors[gem_name][entry["path"]] = "Modified from original"
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-10 01: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-26 02:58:43 +04:00
|
|
|
rescue Gem::VerificationError => e
|
2009-06-10 01:38:59 +04:00
|
|
|
errors[gem_name][gem_path] = e.message
|
2008-10-26 02:58:43 +04:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
end
|
2008-10-26 02:58:43 +04:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
errors.each do |name, subhash|
|
2012-12-23 04:35:09 +04:00
|
|
|
errors[name] = subhash.map do |path, msg|
|
|
|
|
ErrorData.new path, msg
|
|
|
|
end.sort
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2008-10-26 02:58:43 +04:00
|
|
|
errors
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|