/
repair
executable file
·183 lines (142 loc) · 3.97 KB
/
repair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env bash
source "$rvm_scripts_path/base"
usage()
{
printf "%b" "
Usage:
rvm repair [option]
Options:
wrappers - Repair wrappers
symlinks - Repair symlinks
environments - Repair environments
archives - Repair archives
gemsets - Repair gemsets
all - Repair all of the above
"
}
repair_gemsets()
{
typeset directory directories
rvm_log "Removing gemsets missing names or interpreters."
(
builtin cd "${rvm_gems_path:-"rvm_path/gems"}"
directories=(
$( find . -mindepth 1 -maxdepth 1 -type d | GREP_OPTIONS="" \grep '@$' )
$( find . -mindepth 1 -maxdepth 1 -type d | GREP_OPTIONS="" \grep '^./@')
)
for directory in "${directories[@]//.\/}"
do
__rvm_rm_rf "./$directory/"
done
)
rvm_log "Gemsets repaired."
return 0
}
repair_wrappers()
{
typeset wrapper_ruby_name
rvm_log "Regenerating all wrappers..."
while read -r wrapper_ruby_name
do
rvm_log "Regenerating wrappers for $wrapper_ruby_name"
__rvm_run "wrappers.regenerate" "\"$rvm_scripts_path/wrapper\" '$wrapper_ruby_name'"
done < <("$rvm_scripts_path/list" gemsets strings)
rvm_log "Wrappers regenerated"
return 0
}
# Removes stale symlinks in $rvm_bin_path, likely
# related to wrappers.
repair_symlinks()
{
rvm_log "Repairing symlinks..."
(
builtin cd "${rvm_bin_path}"
for executable_name in $(\find \. -type l)
do
if [[ -e "$executable_name" || \
"$(readlink "$executable_name")" != "$rvm_wrappers_path/"* ]]
then
continue
fi
if [[ -f "$executable_name" ]]
then
rvm_log "removing stale symlink from $(basename "$executable_name")"
\rm -f "$executable_name"
fi
done
)
rvm_log "Symlinks repaired"
}
# Regenerates each symlink file.
repair_environments()
{
typeset environment_name environments
rvm_log "Regenerating environments..."
environments=($(builtin cd "$rvm_environments_path" ; find . -maxdepth 1 -mindepth 1 -type f))
for environment_name in "${environments[@]//.\/}"
do
[[ -L "$rvm_environments_path/$environment_name" ]] && continue
rvm_log "Regenerating environment file for '$environment_name'"
[[ -f "$rvm_environments_path/$environment_name" ]] && \rm -f "$rvm_environments_path/$environment_name"
(
source "$rvm_scripts_path/base"
__rvm_become "$environment_name"
__rvm_ensure_has_environment_files
)
done
rvm_log "Environments regenerated"
}
# Removes archives that have incorrect md5 sums.
repair_archives()
{
typeset archive_file archives stored_md5sum
rvm_log "Repairing archives..."
archives=($(builtin cd "${rvm_archives_path}" ; find . -maxdepth 1 -mindepth 1 -type f))
for archive_file in "${archives[@]//.\/}"
do
[[ -f "${rvm_archives_path}/$archive_file" ]] || continue
stored_md5sum="$("$rvm_scripts_path/db" "$rvm_path/config/md5" "$archive_file" | head -n1)"
if [[ -z "$stored_md5sum" ]]
then
stored_md5sum="$("$rvm_scripts_path/db" "$rvm_user_path/md5" "$archive_file" | head -n1)"
fi
if [[ -n "$stored_md5sum" ]]
then
if ! "$rvm_scripts_path/md5" "${rvm_archives_path}/$archive_file" "$stored_md5sum"
then
rvm_log "Removing archive for '$archive_file' - Incorrect md5 checksum."
__rvm_rm_rf "${rvm_archives_path}/$archive_file"
fi
fi
done
rvm_log "Archives repaired"
return 0
}
repair_all()
{
repair_symlinks
repair_archives
repair_environments
repair_wrappers
return 0
}
args=($*)
action="${args[$__array_start]}"
args[$__array_start]=""
args=(${args[@]})
if [[ -z "$action" ]]
then
usage
exit $?
fi
case "$action" in
all) repair_all ;;
symlinks) repair_symlinks ;;
gemsets) repair_gemsets ;;
environments) repair_environments ;;
archives) repair_archives ;;
wrappers) repair_wrappers ;;
help) usage ;;
*) usage >&2 ; exit 1 ;;
esac
exit $?