| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require "rbconfig" |
| 4 |
require "shellwords" |
| 5 |
require "fiddle" |
| 6 |
|
| 7 |
module Bundler |
| 8 |
class CLI::Doctor |
| 9 |
DARWIN_REGEX = /\s+(.+) \(compatibility / |
| 10 |
LDD_REGEX = /\t\S+ => (\S+) \(\S+\)/ |
| 11 |
|
| 12 |
attr_reader :options |
| 13 |
|
| 14 |
def initialize(options) |
| 15 |
@options = options |
| 16 |
end |
| 17 |
|
| 18 |
def otool_available? |
| 19 |
Bundler.which("otool") |
| 20 |
end |
| 21 |
|
| 22 |
def ldd_available? |
| 23 |
Bundler.which("ldd") |
| 24 |
end |
| 25 |
|
| 26 |
def dylibs_darwin(path) |
| 27 |
output = `/usr/bin/otool -L #{path.shellescape}`.chomp |
| 28 |
dylibs = output.split("\n")[1..-1].map {|l| l.match(DARWIN_REGEX).captures[0] }.uniq |
| 29 |
# ignore @rpath and friends |
| 30 |
dylibs.reject {|dylib| dylib.start_with? "@" } |
| 31 |
end |
| 32 |
|
| 33 |
def dylibs_ldd(path) |
| 34 |
output = `/usr/bin/ldd #{path.shellescape}`.chomp |
| 35 |
output.split("\n").map do |l| |
| 36 |
match = l.match(LDD_REGEX) |
| 37 |
next if match.nil? |
| 38 |
match.captures[0] |
| 39 |
end.compact |
| 40 |
end |
| 41 |
|
| 42 |
def dylibs(path) |
| 43 |
case RbConfig::CONFIG["host_os"] |
| 44 |
when /darwin/ |
| 45 |
return [] unless otool_available? |
| 46 |
dylibs_darwin(path) |
| 47 |
when /(linux|solaris|bsd)/ |
| 48 |
return [] unless ldd_available? |
| 49 |
dylibs_ldd(path) |
| 50 |
else # Windows, etc. |
| 51 |
Bundler.ui.warn("Dynamic library check not supported on this platform.") |
| 52 |
[] |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
def bundles_for_gem(spec) |
| 57 |
Dir.glob("#{spec.full_gem_path}/**/*.bundle") |
| 58 |
end |
| 59 |
|
| 60 |
def check! |
| 61 |
require_relative "check" |
| 62 |
Bundler::CLI::Check.new({}).run |
| 63 |
end |
| 64 |
|
| 65 |
def run |
| 66 |
Bundler.ui.level = "warn" if options[:quiet] |
| 67 |
Bundler.settings.validate! |
| 68 |
check! |
| 69 |
|
| 70 |
definition = Bundler.definition |
| 71 |
broken_links = {} |
| 72 |
|
| 73 |
definition.specs.each do |spec| |
| 74 |
bundles_for_gem(spec).each do |bundle| |
| 75 |
bad_paths = dylibs(bundle).select do |f| |
| 76 |
Fiddle.dlopen(f) |
| 77 |
false |
| 78 |
rescue Fiddle::DLError |
| 79 |
true |
| 80 |
end |
| 81 |
if bad_paths.any? |
| 82 |
broken_links[spec] ||= [] |
| 83 |
broken_links[spec].concat(bad_paths) |
| 84 |
end |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
permissions_valid = check_home_permissions |
| 89 |
|
| 90 |
if broken_links.any? |
| 91 |
message = "The following gems are missing OS dependencies:" |
| 92 |
broken_links.map do |spec, paths| |
| 93 |
paths.uniq.map do |path| |
| 94 |
"\n * #{spec.name}: #{path}" |
| 95 |
end |
| 96 |
end.flatten.sort.each {|m| message += m } |
| 97 |
raise ProductionError, message |
| 98 |
elsif !permissions_valid |
| 99 |
Bundler.ui.info "No issues found with the installed bundle" |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
private |
| 104 |
|
| 105 |
def check_home_permissions |
| 106 |
require "find" |
| 107 |
files_not_readable_or_writable = [] |
| 108 |
files_not_rw_and_owned_by_different_user = [] |
| 109 |
files_not_owned_by_current_user_but_still_rw = [] |
| 110 |
broken_symlinks = [] |
| 111 |
Find.find(Bundler.bundle_path.to_s).each do |f| |
| 112 |
if !File.exist?(f) |
| 113 |
broken_symlinks << f |
| 114 |
elsif !File.writable?(f) || !File.readable?(f) |
| 115 |
if File.stat(f).uid != Process.uid |
| 116 |
files_not_rw_and_owned_by_different_user << f |
| 117 |
else |
| 118 |
files_not_readable_or_writable << f |
| 119 |
end |
| 120 |
elsif File.stat(f).uid != Process.uid |
| 121 |
files_not_owned_by_current_user_but_still_rw << f |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
ok = true |
| 126 |
|
| 127 |
if broken_symlinks.any? |
| 128 |
Bundler.ui.warn "Broken links exist in the Bundler home. Please report them to the offending gem's upstream repo. These files are:\n - #{broken_symlinks.join("\n - ")}" |
| 129 |
|
| 130 |
ok = false |
| 131 |
end |
| 132 |
|
| 133 |
if files_not_owned_by_current_user_but_still_rw.any? |
| 134 |
Bundler.ui.warn "Files exist in the Bundler home that are owned by another " \ |
| 135 |
"user, but are still readable/writable. These files are:\n - #{files_not_owned_by_current_user_but_still_rw.join("\n - ")}" |
| 136 |
|
| 137 |
ok = false |
| 138 |
end |
| 139 |
|
| 140 |
if files_not_rw_and_owned_by_different_user.any? |
| 141 |
Bundler.ui.warn "Files exist in the Bundler home that are owned by another " \ |
| 142 |
"user, and are not readable/writable. These files are:\n - #{files_not_rw_and_owned_by_different_user.join("\n - ")}" |
| 143 |
|
| 144 |
ok = false |
| 145 |
end |
| 146 |
|
| 147 |
if files_not_readable_or_writable.any? |
| 148 |
Bundler.ui.warn "Files exist in the Bundler home that are not " \ |
| 149 |
"readable/writable by the current user. These files are:\n - #{files_not_readable_or_writable.join("\n - ")}" |
| 150 |
|
| 151 |
ok = false |
| 152 |
end |
| 153 |
|
| 154 |
ok |
| 155 |
end |
| 156 |
end |
| 157 |
end |