#!/bin/perl # Name: /usr/local/etc/dup.ln Author: Joe.Smith@MCI.com 13-Oct-98 # Purpose: Recursively links files from one directory to another $src = shift(@ARGV); # Source directory $dst = shift(@ARGV); # Destination directory die "Usage: $0 sourcedir destdir [subdir]\n" unless $dst; $start = $ARGV[0] ? $ARGV[0] : "."; # "." or a subset of the source dir # For each subdirectory in $src, make sure the corresponding directory exists # on $dst. For each file found, create hard link if not there already chop($startdir = `pwd`); $dst = "$startdir/$dst" unless $dst =~ m%^/%; $| = 1; sub syst { system(@_); exit(2) if ($? & 0x0002); # Abort on SIGINT (Control-C) $? == 0; # Return true if no error } if ($src eq "-") { # Read list of file names from standard input $src2 = ""; $olddir = $startdir; @dirs = (); } else { $src2 = "$src/"; chdir($src) || die "Invalid source directory $src - $!\n"; $cmd = "find $start -type d -print -o -type f -print -o -type l -print |"; $olddir=$src; open(STDIN,$cmd) || die "Can't open pipe to '$cmd' - $!\n"; } while() { chop; s%^\./%%; unless (($dirn,$filen) = m%^(.*/)(.*)$%) { ($dirn,$filen) = (".",$_) } if ($dirn ne $olddir) { chop($dir2 = "$dst/$dirn"); if (! -d $dir2) { mkdir($dir2,0775) || syst("mkdir -p $dir2"); warn("Directory $dir2 does not exist\n") unless -d $dir2; } } ($olddir = "$src2$dirn") =~ s%/\.$%%; ($file1,$file2) = ($_,"$dst/$_"); $file2 = $dst if $_ eq "."; next if -e $file2; # Something already exists there ($dev1,$ino1,$mode,$nli,$uid,$gid,$rdev,$size1,$at,$mt1,$ct) = lstat($file1); if (-l _) { $link1 = readlink($file1); printf("ln -s %-21s %s ",$link1,$file2); if (symlink($link1,$file2)) { print("\n"); } else { print(" (error:",0+$!,")\n"); syst("ln -s $link1 $file2"); # Let 'ln' report any error message } } elsif (-d _) { print("dir $src2$file1 -> $file2\n"); if (! -d $file2) { mkdir($file2,$mode) || syst("mkdir -p $file2"); chown($uid,$gid,$file2); chmod(($mode&07777),$file2); } } elsif (-f _) { printf("ln %-21s %s ",$file1,$file2); if (link($file1,$file2)) { print("\n"); } else { print(" (error:",0+$!,")\n"); syst("ln $file1 $file2"); # Let 'ln' report any error message } } else { warn "Unknown file type for $file1\n" if -e _; } }