#! /usr/bin/perl # CPIF -- CoPy IF Different # # usage: cpif source destination # # Cpif is used when updating collections of files, some of which may # have been changed, when it is desirable to copy only those files # which have actually been modified. It is, in essence, a local # rdist. # # Cpif compares the date of the last modification of the two files # and proceeds as follows: # # Source Destination Action # ======= =========== ============================== # Newer Older Copy source to destination. # Same Same No action; report "files identical". # Older Newer No action; warning "destination newer." # $from = $ARGV[0]; $to = $ARGV[1]; # Confirm that the input file... if (!(-e $from)) { # exists, die("Input file $from does not exist"); } if (!(-f $from)) { # is a plain data file, die("Input file $from is not a plain file"); } if (!(-r $from)) { # which is readable by me. die("Input file $from is not readable"); } # Okay, now let's obtain the last modification time # for the input and output files. It's OK if the # output file doesn't exist yet; in that case we # make up a modification time for it one second older # than that of the input file. @s = stat($from); $fromtime = $s[9]; if (-f $to) { @s = stat($to); $totime = $s[9]; } else { $totime = $fromtime - 1; } #$ft = join(":", localtime($fromtime)); #$tt = join(":", localtime($totime)); #print(STDERR "From $from at $ft to $to at $tt.\n"); # Now see if the destination needs to be updated. if ($totime < $fromtime) { $c = "cp -p $from $to\n"; # print(STDERR "$c"); system($c); print(STDERR "$0: $to updated.\n"); } elsif ($totime > $fromtime) { print(STDERR "$0: source $from older than destination $to.\n"); } else { print(STDERR "$0: source and destination identical.\n"); }