package textbender::a::b::FileSync; # Copyright 2004-2005, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. use strict; use warnings; =pod =head1 DESCRIPTION File synchronization. =head1 EXPORTS =over 4 =cut use File::Basename (); use File::Copy (); use File::Find (); use File::Path (); use File::stat (); BEGIN { use Exporter (); our @ISA; @ISA = qw( Exporter ); our @EXPORT_OK; @EXPORT_OK = qw( $from_dir sync_found sync_to_dir sync_to_file $to_dir ); } our @EXPORT_OK; =pod =item B File::Find "wanted" sub that copies the file to $to_dir, if out-of-date or non-existent there. $from_dir must contain the root directory exactly as passed to find. So, given: $from_dir = /var/from/somewhere $_ = /var/from/somewhere/specific/file $to_dir = /var/to/elsewhere then you have: $to_file = /var/to/elsewhere/specific/file Example: File::Find::find ( {follow_fast=>1, no_chdir=>1, wanted=>\&textbender::a::b::FileSync::sync_found}, $textbender::a::b::FileSync::from_dir ); =cut sub sync_found() { our $from_dir; our $to_dir; use textbender::a::b::Console qw( $verbosity ); my $from_file = $File::Find::name; # /var/from/somewhere/specific/file $from_file =~ m'/\.#[^/]+$' and return; # ignore emacs lock files my $rel_from_file = substr( $from_file, length($from_dir) ); # /specific/file my $to_file = $to_dir . $rel_from_file; if( -d $from_file ) { -d $to_file and return; $verbosity && print " $to_file/\n"; File::Path::mkpath $to_file or die; } else # file { my $mtime_from = File::stat::stat($from_file)->mtime; if( -e $to_file ) { my $mtime_to = File::stat::stat($to_file)->mtime; $mtime_to += 1; # allow for rounding errors of SMB and other remote mounts $mtime_from > $mtime_to or return; } $verbosity && print " $to_file\n"; File::Copy::copy( $from_file, $to_file ) or die $! . ": $from_file --> $to_file\n"; utime( time, $mtime_from, $to_file ) or warn; # force sync, even across hosts with out-of-sync clocks } } =pod =item B( $I, $I ) If the namesake file in $ is older or non-existent, then: copies I to I; sets its modification time to match; and returns the basename of the file. Otherwise returns 0. =cut sub sync_to_dir( $$ ) { my $from_file = shift; my $to_dir = shift; my $basename = File::Basename::basename( $from_file ); if( sync_to_file( $from_file, $to_dir . '/' . $basename )) { return $basename; } return 0; } =pod =item B( $I, $I ) If is older or non-existent, then: copies I to I; sets its modification time to match; and returns I. Otherwise returns 0. =cut sub sync_to_file( $$ ) { use textbender::a::b::Console qw( $verbosity ); my $from_file = shift; my $to_file = shift; my $mtime_from = File::stat::stat($from_file)->mtime; if( -e $to_file ) { my $mtime_to = File::stat::stat($to_file)->mtime; $mtime_from > $mtime_to or return 0; } $verbosity && print " $to_file\n"; File::Copy::copy( $from_file, $to_file ) or die $! . ": $from_file -> $to_file"; utime( time, $mtime_from, $to_file ) or warn; # force sync, even across hosts with out-of-sync clocks return $to_file; } =pod =back =cut 1;