Recipe 9.11. Program: symirror (Perl Cookbook)

Perl Cookbook

Perl CookbookSearch this book
Previous: 9.10. Splitting a Filename into Its Component PartsChapter 9
Directories
Next: 9.12. Program: lst
 

9.11. Program: symirror

The program in Example 9.6 recursively duplicates a directory tree, making a shadow forest full of symlinks pointing back at the real files.

Example 9.6: symirror

#!/usr/bin/perl -w
# symirror - build spectral forest of symlinks
use strict;
use File::Find;
use Cwd;

my ($srcdir, $dstdir);
my $cwd = getcwd();
die "usage: $0 realdir mirrordir" unless @ARGV == 2;

for (($srcdir, $dstdir) = @ARGV) {
    my $is_dir = -d;
    next if $is_dir;                        # cool
    if (defined ($is_dir)) {
        die "$0: $_ is not a directory\n";
    } else {                                # be forgiving
        mkdir($dstdir, 07777) or die "can't mkdir $dstdir: $!";
    }
} continue {
    s#^(?!/)#$cwd/#;                        # fix relative paths
}
                    
chdir $srcdir;
find(\&wanted, '.');

sub wanted {
    my($dev, $ino, $mode) = lstat($_);
    my $name = $File::Find::name;
    $mode &= 07777;                 # preserve directory permissions
    $name =~ s!^\./!!;              # correct name
    if (-d _) {                     # then make a real directory
        mkdir("$dstdir/$name", $mode)
            or die "can't mkdir $dstdir/$name: $!";
    } else {                        # shadow everything else
        symlink("$srcdir/$name", "$dstdir/$name")
            or die "can't symlink $srcdir/$name to $dstdir/$name: $!";
    }
}


Previous: 9.10. Splitting a Filename into Its Component PartsPerl CookbookNext: 9.12. Program: lst
9.10. Splitting a Filename into Its Component PartsBook Index9.12. Program: lst

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.