#!/opt/bin/perl # ############################################################################### # # # Program to update one directory based on another (also recursively) # # # ############################################################################### # # # Version 1.0 - Written 15.11.95 by Steffen Beyer # # # ############################################################################### # # # Usage: distrib [-r] # # # ############################################################################### # # # Copyright (C) 1995 by software design & management GmbH & Co. KG # # # ############################################################################### # # Some defaults: # $version = 'version 1.0'; # $self = $0; $self =~ s!^.*/!!; # $argc = @ARGV; $argx = 2; # # Issue "about" info if no arguments are given: # if ($argc == 0) { print "$self $version\n"; print "Usage: $self [-r] \n"; print " -r : apply [r]ecursively to all subdirectories\n"; exit; } # # Check for options: # while ($ARGV[0] =~ /^-(.*)$/) { $option = $1; if (($option eq 'r') || ($option eq 'R')) { shift; $recursive = true; $argx++; next; } die "$self error: Unknown option '$ARGV[0]'!\n"; } # # Check number of arguments: # if ($argc != $argx) { die "$self error: Wrong number of arguments ($argc instead of $argx)!\n"; } # # Get and check the two directory names: # $directory1 = shift; $directory2 = shift; # # Remove trailing '/': # $directory1 =~ s!/$!!; $directory2 =~ s!/$!!; # unless (-d $directory1) { die "$self error: '$directory1' is not a directory!\n"; } # unless (-d $directory2) { die "$self error: '$directory2' is not a directory!\n"; } # unless ((-r $directory1) && (-w _) && (-x _)) { die "$self error: '$directory1' is not readable, writable or executable!\n"; } # unless ((-r $directory2) && (-w _) && (-x _)) { die "$self error: '$directory2' is not readable, writable or executable!\n"; } # # Determine owner: # unless (($uid,$gid) = (stat($directory2))[4,5]) { die "$self error: Can't stat directory '$directory2': $!\n"; } # # Update directory2 using directory1: # &update($directory1, $directory2); # # Done: # exit; # # Subroutine that does it all: # sub update { local($source,$target) = @_; local($sourcefile,$targetfile,$copy,$rc); local($mode,$time1,$time2); # # Get the list of all entries in the source directory: # unless (opendir(DIR,"$source")) { warn "$self warning: Can't open directory '$source': $!\n"; return; } # # Slurp them all into an array: # local(@entries) = readdir(DIR); # closedir(DIR); # # Cycle through all directory entries: # for (@entries) { next if ($_ eq '.'); next if ($_ eq '..'); $sourcefile = "$source/$_"; $targetfile = "$target/$_"; print "source = '$sourcefile'\n"; if ((-d $sourcefile) && ($recursive)) { if (-f $targetfile) { warn "$self warning: Can't copy directory '$sourcefile' over file '$targetfile'!\n"; next; } unless (-d $targetfile) { if (-e $targetfile) { warn "$self warning: Can't copy directory '$sourcefile' over special file '$targetfile'!\n"; next; } unless (($mode) = (stat($sourcefile))[2]) { warn "$self warning: Can't stat directory '$sourcefile': $!\n"; next; } unless (mkdir($targetfile, 0777)) { warn "$self warning: Can't create directory '$targetfile': $!\n"; next; } print "created '$targetfile'\n"; chown($uid, $gid, $targetfile); chmod($mode, $targetfile); } &update($sourcefile, $targetfile); } if (-f $sourcefile) { if (-d $targetfile) { warn "$self warning: Can't copy file '$sourcefile' over directory '$targetfile'!\n"; next; } $copy = 0; if (-f $targetfile) { unless (($time1) = (stat($sourcefile))[9]) { warn "$self warning: Can't stat file '$sourcefile': $!\n"; next; } unless (($time2) = (stat($targetfile))[9]) { warn "$self warning: Can't stat file '$targetfile': $!\n"; next; } # if ($time1 > $time2) { $copy = 1; } } else { if (-e $targetfile) { warn "$self warning: Can't copy file '$sourcefile' over special file '$targetfile'!\n"; next; } $copy = 1; } if ($copy) { unless (($mode) = (stat($sourcefile))[2]) { warn "$self warning: Can't stat file '$sourcefile': $!\n"; next; } system("cp -p '$sourcefile' '$targetfile'"); $rc = ($? >> 8); if ($rc) { warn "$self warning: Can't copy '$sourcefile' to '$targetfile': $rc\n"; next; } print "copied '$targetfile'\n"; chown($uid, $gid, $targetfile); chmod($mode, $targetfile); } } } } # # The End. #