-
Notifications
You must be signed in to change notification settings - Fork 8
/
symlink-tool
executable file
·332 lines (305 loc) · 10.4 KB
/
symlink-tool
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2003 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
# By Jamie McCarthy, jamie@slashdot.org
# Based on template-tool, by Cliff Wood, Patrick Galbraith, Brian Aker
#
# STILL UNDER TESTING -- handles plugins now -- for now,
# be sure to use -T on any production site just in case this
# screws something up!!!
use strict;
use File::Basename;
use File::Path;
use File::Spec::Functions qw( :DEFAULT splitpath );
use Slash;
use Slash::Utility;
use Slash::DB;
use Slash::Install;
use Getopt::Std;
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my %opts;
my $symlink_exists = eval { symlink("",""); 1 };
if (!$symlink_exists) {
print STDERR "Sorry - symlink() not implemented on your system,\n";
print STDERR "so (duh!) symlink-tool cannot run.\n";
exit 1;
}
# Remember to doublecheck these match usage()!
usage('Options used incorrectly')
unless getopts('hvu:TUV', \%opts);
usage() if $opts{h} || !keys %opts;
version() if $opts{v};
usage('Right now only -U does anything') if !$opts{U};
my $testing = $opts{T} ? 1 : 0;
my $verbose = $opts{V} ? 1 : 0;
my $virtuser = $opts{u} || 'slash';
createEnvironment($virtuser);
my @dotfiles = ( );
my $slashdb = getCurrentDB();
my $constants = getCurrentStatic();
my $install = Slash::Install->new($virtuser);
# This will read all the themes and all the plugins available for this
# installation.
my $site_theme = $install->get("theme")->{value};
my $site_plugins = $install->get("plugin");
my @site_plugins = sort
map { $site_plugins->{$_}{value} }
keys %$site_plugins;
# What one theme, and what subset of the available plugins, is this
# site using?
my $theme_hr = $install->_getList(
$constants->{slashdir}, "themes", "THEME", 1
)->{$site_theme};
my $plugin_list = $install->_getList(
$constants->{slashdir}, "plugins", "PLUGIN", 1
);
############################################################
# Set up the %theme_subdir_files and %plugin_subdir_files hashes,
# to have a list of all the files that our theme references.
my %theme_subdir_files = ( );
my %plugin_subdir_files = ( );
# First, pull in the files referenced by the theme.
my @theme_htdocs_keys = grep /^(htdoc|image|topic)/, keys %$theme_hr;
for my $key (@theme_htdocs_keys) {
next unless $key && $theme_hr->{$key};
push @{$theme_subdir_files{htdocs}},
ref($theme_hr->{$key}) && ref($theme_hr->{$key}) eq 'ARRAY'
? @{$theme_hr->{$key}}
: $theme_hr->{$key};
}
for my $key (qw( tasks misc )) {
my $src_key = $key;
$src_key =~ s/s$//; # theme "task" becomes theme_subdir_files "tasks"
@{$theme_subdir_files{$key}} =
ref($theme_hr->{$src_key}) && ref($theme_hr->{$src_key}) eq 'ARRAY'
? @{$theme_hr->{$src_key}}
: $theme_hr->{$src_key};
}
# Then, pull in the files referenced by each plugin used.
my %plugin_htdocs_keys = ( );
my @plugin_htdocs_keys = ( );
for my $plugin (keys %$plugin_list) {
for my $key (grep /^(htdoc|image|topic)/, keys %{$plugin_list->{$plugin}}) {
$plugin_htdocs_keys{$key} = 1;
}
}
@plugin_htdocs_keys = sort keys %plugin_htdocs_keys;
for my $plugin (keys %$plugin_list) {
my $plugin_hr = $plugin_list->{$plugin};
for my $key (@plugin_htdocs_keys) {
next unless $key && $plugin_hr->{$key};
push @{$plugin_subdir_files{$plugin}{htdocs}},
ref($plugin_hr->{$key}) && ref($plugin_hr->{$key}) eq 'ARRAY'
? @{$plugin_hr->{$key}}
: $plugin_hr->{$key};
}
for my $key (qw( tasks misc )) {
my $src_key = $key;
$src_key =~ s/s$//; # plugin "task" becomes "tasks"
next unless $plugin_hr->{$src_key};
@{$plugin_subdir_files{$plugin}{$key}} =
ref($plugin_hr->{$src_key}) && ref($plugin_hr->{$src_key}) eq 'ARRAY'
? @{$plugin_hr->{$src_key}}
: $plugin_hr->{$src_key};
}
}
#use Data::Dumper;
#print "theme_hr: " . Dumper($theme_hr);
#print "plugin_list: " . Dumper($plugin_list);
#print "theme_subdir_files: " . Dumper(\%theme_subdir_files);
#print "plugin_subdir_files: " . Dumper(\%plugin_subdir_files);
my $theme_dir = $theme_hr->{dir};
my $site_dir = $constants->{datadir};
if (!$testing && !-w $site_dir) {
print STDERR "No write access to '$site_dir'\n";
print STDERR "for EUID $>, cannot write any files\n";
exit 1;
}
my %processed = ( );
my $num_changes = 0;
for my $subdir (sort keys %theme_subdir_files) {
my @files = sort @{$theme_subdir_files{$subdir}};
for my $target_file (@files) {
# Strip off "../slashcode/" from front to find
# where the symlink was installed in the
# "site/foo" tree.
my $theme_file_abs = canonpath(catfile($theme_dir, $target_file));
1 while $theme_file_abs =~ s{/[^/]+/\.\.}{};
my $site_rel_file = $target_file;
1 while $site_rel_file =~ s{^\.\.\/[^/]+/}{};
my $site_file_abs = canonpath(catfile($site_dir, $site_rel_file));
if ($processed{$site_file_abs}) {
if ($verbose) {
print "Skipping file in theme, already processed: '$site_file_abs'\n";
}
next;
}
my $site_file_abs_dot = dotfile($site_file_abs);
my $site_file_abs_dot_short = dotfile($site_rel_file);
# At this point, $site_file_abs is e.g.
# '/usr/local/slash/site/mysite/tasks/spamarmor.pl'
# $site_file_abs_dot is e.g.
# '/usr/local/slash/site/mysite/tasks/.spamarmor.pl'
# $theme_file_abs is e.g.
# '/usr/local/slash/theme/slashcode/tasks/spamarmor.pl'
$num_changes += handle_file($site_file_abs, $theme_file_abs,
$site_file_abs_dot, $site_file_abs_dot_short);
$processed{$site_file_abs} = 1;
}
}
for my $plugin (sort keys %plugin_subdir_files) {
my $plugin_hr = $plugin_subdir_files{$plugin};
my $plugin_dir = $plugin_list->{$plugin}{dir};
for my $subdir (sort keys %$plugin_hr) {
my @files = sort @{$plugin_hr->{$subdir}};
#print "plugin '$plugin' subdir '$subdir' files '@files'\n";
for my $target_file (@files) {
my $plugin_file_abs = canonpath(catfile($plugin_dir, $target_file));
1 while $plugin_file_abs =~ s{/[^/]+/\.\.}{};
my $site_rel_file = $target_file;
1 while $site_rel_file =~ s{^\.\.\/[^/]+/}{};
# Plugins don't get the type prepended so we need to
# do that here.
$site_rel_file =~ s{^(?:$subdir/)?}{$subdir/};
my $site_file_abs = canonpath(catfile($site_dir, $site_rel_file));
#print "plugin '$plugin' subdir '$subdir' site_rel_file '$site_rel_file' site_file_abs '$site_file_abs'\n";
if ($processed{$site_file_abs}) {
if ($verbose) {
print "Skipping file in '$plugin' plugin, already processed: '$site_file_abs'\n";
}
next;
}
my $site_file_abs_dot = dotfile($site_file_abs);
my $site_file_abs_dot_short = dotfile($site_rel_file);
# At this point, $site_file_abs is e.g.
# '/usr/local/slash/site/mysite/tasks/adminmail.pl'
# $site_file_abs_dot is e.g.
# '/usr/local/slash/site/mysite/tasks/.adminmail.pl
# $plugin_file_abs is e.g.
# '/usr/local/slash/plugin/Stats/adminmail.pl'
$num_changes += handle_file($site_file_abs, $plugin_file_abs,
$site_file_abs_dot, $site_file_abs_dot_short);
$processed{$site_file_abs} = 1;
}
}
}
if ($num_changes == 0) {
print "symlink-tool -U: no symlink changes necessary\n";
}
if (@dotfiles) {
print "symlink-tool: found dotfiles: @dotfiles\n";
}
###############################################
sub handle_file {
my($symlink_file, $target_file, $symlink_file_dot, $symlink_file_dot_short) = @_;
if (!-e $symlink_file && -e $symlink_file_dot) {
# The symlink file is missing and in its place is a
# dotfile. This site must have that file disabled,
# perhaps temporarily. Make sure it's correct; do
# what we were doing to do on the real file, on the
# dotfile instead.
$symlink_file = $symlink_file_dot;
push @dotfiles, $symlink_file_dot_short;
if ($verbose) {
print "Checking dotfile replacement at '$symlink_file'\n";
}
}
if (-e $symlink_file && !-l $symlink_file) {
# The file in place is not a symlink; compare it to
# what it should be and only replace it with a symlink
# if the two are identical.
my $diff;
if ($verbose) {
$diff = system("diff -U3 $target_file $symlink_file");
} else {
$diff = system("diff -U3 $target_file $symlink_file > /dev/null");
}
if ($diff) {
print "File should be symlink, but is not, and differs from intended target: '$symlink_file' '$target_file'; difference needs to be resolved or symlink-tool will not touch it\n";
return 1;
} else {
print "File at '$symlink_file' is not a symlink, but is identical to what it should be; ";
if ($testing) {
print "file would be unlinked and allowed to be recreated.\n";
return 1;
} else {
print "unlinking; will be created next.\n";
unlink $symlink_file;
}
}
}
lstat($symlink_file);
if (!-e _) {
print "Symlink at '$symlink_file' missing, ";
if ($testing) {
print "would create: '$target_file'\n";
return 1;
}
my $success = symlink($target_file, $symlink_file);
if (!$success) {
print "attempt to create FAILED: $!\n";
return 1;
}
print "successfully added link to '$target_file'\n";
return 1;
}
my $cur_target_file = readlink($symlink_file);
if ($cur_target_file eq $target_file) {
if ($verbose) {
print "Correct: $symlink_file -> $target_file\n";
}
return 0;
}
print "Symlink at '$symlink_file' mispointed at '$cur_target_file', ";
if ($testing) {
print "would repoint to '$target_file'\n";
return 1;
}
my $success = unlink $symlink_file;
if (!$success) {
print "attempt to unlink FAILED: $!\n";
return 1;
}
$success = symlink($target_file, $symlink_file);
if (!$success) {
print "original unlinked, attempt to link properly FAILED: $!\n";
return 1;
}
print "successfully relinked to '$target_file'\n";
return 1;
}
###############################################
sub dotfile {
my($file_abs) = @_;
my($volume, $dirs, $file) = splitpath($file_abs);
return "$dirs.$file";
}
###############################################
sub usage {
print "*** $_[0] \n" if $_[0];
print <<EOT;
Usage: $PROGNAME [OPTIONS] <template templateN>
Main options:
-h Help (this message)
-v Version
-u Virtual user (default is "slash")
-T Test run, report but don't do anything
-U Upgrade your symlinks to the latest for your theme and plugins
Debugging options:
-V Increases verbosity.
EOT
exit 0;
}
###############################################
sub version {
print <<EOT;
$PROGNAME $VERSION
This code is a part of Slash, and is released under the GPL.
Copyright 1997-2003 by Open Source Development Network. See README
and COPYING for more information, or see http://slashcode.com/.
EOT
exit 0;
}