| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # Copyright 2003, Nathaniel J. Graham. All Rights Reserved. | ||
| 4 | # This program is free software. You may copy or redistribute | ||
| 5 | # it under the same terms as Perl itself. | ||
| 6 | |||
| 7 | use File::Version; | ||
| 8 | use Getopt::Std; | ||
| 9 | getopts('sp:r:'); | ||
| 10 | |||
| 11 | $opt_p = $opt_p && -d $opt_p ? $opt_p : '.'; | ||
| 12 | $opt_r ||= 1; | ||
| 13 | $opt_s ||= 0; | ||
| 14 | |||
| 15 | unless (@ARGV) { die <<'USAGE' } | ||
| 16 | cp_version.pl - simple versioning system. | ||
| 17 | Usage: cp_version.pl [OPTIONS] INPUT FILE(s) | ||
| 18 | -p search path | ||
| 19 | -r recursion depth | ||
| 20 | -s follow symbolic links (bool) | ||
| 21 | USAGE | ||
| 22 | |||
| 23 | shift(@ARGV) if $ARGV[0] eq 'SPECIAL'; | ||
| 24 | my @files = @ARGV; | ||
| 25 | @files = grep { -f && -r _ } @ARGV; | ||
| 26 | for(@files) { | ||
| 27 | my $info; | ||
| 28 | my %args = ( | ||
| 29 | FILE => $_, | ||
| 30 | SEARCH_PATHS => [ $opt_p ], | ||
| 31 | RECURSION_DEPTH => $opt_r, | ||
| 32 | FOLLOW_SYMBOLIC => $opt_s, | ||
| 33 | ); | ||
| 34 | $info = File::Version->new(%args) or next; | ||
| 35 | if($info->next_version && ©($info)) { | ||
| 36 | print "$info->{WHOLE} copied to $info->{NEXT_VERSION}\n"; | ||
| 37 | next; | ||
| 38 | } | ||
| 39 | print "$info->{WHOLE} copy failed.\n"; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub copy { | ||
| 43 | my $info = shift; | ||
| 44 | return unless ( -f $info->{WHOLE} && -r _ ); | ||
| 45 | if( -e $info->{NEXT_VERSION} ) { | ||
| 46 | print "$info->{NEXT_VERSION} already exists, skipping...\n"; | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | open(IN, "< $info->{WHOLE}") or die $!; | ||
| 50 | open(OUT, "> $info->{NEXT_VERSION}") or die $!; | ||
| 51 | my $size = (stat IN)[11] || 16384; | ||
| 52 | while ($len = sysread IN, $buf, $size) { | ||
| 53 | if(!defined $len) { | ||
| 54 | next if $! =~ /^Interrupted/; | ||
| 55 | die ("System read error: $!\n"); | ||
| 56 | } | ||
| 57 | $offset = 0; | ||
| 58 | while ($len) { | ||
| 59 | defined($written = syswrite OUT, $buf, $len, $offset) | ||
| 60 | or die "System write error $!\n"; | ||
| 61 | $len -= $written; | ||
| 62 | $offset += $written; | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | return 1; | ||
| 66 | } | ||
| 67 |