Fix a comment.
[rsync/qnx.git] / support / munge-symlinks
blob2aa27f369c3001d5fec563a1472509f5a580fbce
1 #!/usr/bin/perl
2 # This script will either prefix all symlink values with the string
3 # "/rsyncd-munged/" or remove that prefix.
5 use strict;
6 use Getopt::Long;
8 my $SYMLINK_PREFIX = '/rsyncd-munged/';
10 my $munge_opt;
12 &GetOptions(
13 'munge' => sub { $munge_opt = 1 },
14 'unmunge' => sub { $munge_opt = 0 },
15 'all' => \( my $all_opt ),
16 'help|h' => \( my $help_opt ),
17 ) or &usage;
19 &usage if $help_opt || !defined $munge_opt;
21 my $munged_re = $all_opt ? qr/^($SYMLINK_PREFIX)+(?=.)/ : qr/^$SYMLINK_PREFIX(?=.)/;
23 push(@ARGV, '.') unless @ARGV;
25 open(PIPE, '-|', 'find', @ARGV, '-type', 'l') or die $!;
27 while (<PIPE>) {
28 chomp;
29 my $lnk = readlink($_) or next;
30 if ($munge_opt) {
31 next if !$all_opt && $lnk =~ /$munged_re/;
32 $lnk =~ s/^/$SYMLINK_PREFIX/;
33 } else {
34 next unless $lnk =~ s/$munged_re//;
36 if (!unlink($_)) {
37 warn "Unable to unlink symlink: $_ ($!)\n";
38 } elsif (!symlink($lnk, $_)) {
39 warn "Unable to recreate symlink: $_ -> $lnk ($!)\n";
40 } else {
41 print "$_ -> $lnk\n";
45 close PIPE;
46 exit;
48 sub usage
50 die <<EOT;
51 Usage: munge-symlinks --munge|--unmunge [--all] [DIR|SYMLINK...]
53 --munge Add the $SYMLINK_PREFIX prefix to symlinks if not already
54 present, or always when combined with --all.
55 --unmunge Remove one $SYMLINK_PREFIX prefix from symlinks or all
56 such prefixes with --all.
58 See the "munge symlinks" option in the rsyncd.conf manpage for more details.
59 EOT