This file is indexed.

/usr/share/perl5/File/Rsync.pm is in libfile-rsync-perl 0.43-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# -*- perl -*-
# vim:ft=perl foldlevel=1
#      __
#     /\ \ From the mind of
#    /  \ \
#   / /\ \ \_____ Lee Eakin  ( Leakin at dfw dot Nostrum dot com )
#  /  \ \ \______\       or  ( Leakin at cpan dot org )
# / /\ \ \/____  /       or  ( Leakin at japh dot net )
# \ \ \ \____\/ /        or  ( Lee at Eakin dot Org )
#  \ \ \/____  /  Wrapper module for the rsync program
#   \ \____\/ /   rsync can be found at http://rsync.samba.org/rsync/
#    \/______/

package File::Rsync;
require 5.004; # it might work with older versions of 5 but not tested

use FileHandle;
use IPC::Open3 qw(open3);
use IO::Select;
use POSIX ":sys_wait_h";
use Carp 'carp';
use File::Rsync::Config;
use Scalar::Util qw(blessed);

use strict;
use vars qw($VERSION);

$VERSION = '0.43';

=head1 NAME

File::Rsync - perl module interface to rsync(1) F<http://rsync.samba.org/rsync/>

=head1 SYNOPSIS

use File::Rsync;

$obj = File::Rsync->new( { archive => 1, compress => 1,
         rsh => '/usr/local/bin/ssh',
         'rsync-path' => '/usr/local/bin/rsync' } );

$obj->exec( { src => 'localdir', dest => 'rhost:remdir' } )
         or warn "rsync failed\n";

=head1 DESCRIPTION

Perl Convenience wrapper for the rsync(1) program.  Written for I<rsync-2.3.2>
and updated for I<rsync-2.6.0> but should perform properly with most recent
versions.

=over 4

=item File::Rsync::new

$obj = I<new> File::Rsync;

   or

$obj = File::Rsync->I<new>;

   or

$obj = File::Rsync->new(@options);

   or

$obj = File::Rsync->new(\%options);

Create a I<File::Rsync> object.  Any options passed at creation are stored
in the object as defaults for all future I<exec> calls on that object.
Options may be passed in the form of a hash and are the same as the long
options in I<rsync(1)> with the leading double-dash removed.  An additional
option of B<path-to-rsync> also exists which can be used to override the
hardcoded path to the rsync binary that is defined when the module is
installed, and B<debug> which causes the module methods to print some
debugging information to STDERR.  There are also 2 options to wrap the
source and/or destination paths in double-quotes.  They are B<quote-src>
and B<quote-dst>, and may be useful in protecting the paths from shell
expansion (particularly useful for paths containing spaces).  The
B<outfun> and B<errfun> options take a function reference.  The function
is called once for each line of output from the I<rsync> program with the
output line passed in as the first argument, the second arg is either
'out' or 'err' depending on the source.  This makes it possible to use the
same function for both and still determine where the output came from.
Options may also be passed as a reference to a hash.  The B<exclude>
option needs an array reference as its value, since there cannot be
duplicate keys in a hash.  There is an equivalent B<include> option.  Only
an B<exclude> or B<include> option should be used, not both.  Use the '+ '
or '- ' prefix trick to put includes in an B<exclude> array, or to put
excludes in an B<include> array (see I<rsync(1)> for details).
Include/exclude options form an ordered list.  The order must be retained
for proper execution.  There are also B<source> and B<dest> keys.  The key
B<src> is also accepted as an equivalent to B<source>, and B<dst> or
B<destination> may be used as equivalents to B<dest>.  The B<source>
option may take a scalar or an array reference.  If the source is the
local system then multiple B<source> paths are allowed.  In this case an
array reference should be used.  There is also a method for passing
multiple source paths to a remote system.  This method may be triggered in
this module by passing the remote hostname to the B<srchost> key and
passing an array reference to the B<source> key.  If the source host is
being accessed via an Rsync server, the remote hostname should have a
single trailing colon on the name.  When rsync is called, the B<srchost>
value and the values in the B<source> array will be joined with a colon
resulting in the double-colon required for server access.  The B<dest> key
only takes a scalar since I<rsync> only accepts a single destination path.

Version 2.6.0 of I<rsync(1)> provides a new B<files-from> option along with
a few other supporting options (B<from0>, B<no-relative>, and
B<no-implied-dirs>).  To support this wonderful new option at the level it
deserves, this module now has an additional parameter.  If B<files-from> is
set to '-' (meaning read from stdin) you can define B<infun> to be a
reference to a function that prints your file list to the default file handle.
The output from the function is attached to stdin of the rsync call during
exec.  If B<infun> is defined it will be called regardless of the value of
B<files-from>, so it can provide any data expected on stdin, but keep in mind
that stdin will not be attached to a tty so it is not very useful for sending
passwords (see the I<rsync(1)> and I<ssh(1)> man pages for ways to handle
authentication).  The I<rsync(1)> man page has a more complete description of
B<files-from>.  Also see L<File::Find> for ideas to use with B<files-from>
and B<infun>.  The B<infun> option may also be used with the B<include-from>
or B<exclude-from> settings, but this is generally more clumsy than using the
B<include> or B<exclude> arrays.

Version 2.6.3 of I<rsync(1)> provides new options B<partial-dir>,
B<checksum-seed>, B<keep-dirlinks>, B<inplace>, B<ipv4>, and B<ipv6>.
Version 2.6.4 of I<rsync(1)> provides new options B<del>, B<delete-before>
B<delete-during>, B<delay-updates>, B<dirs>, B<filter>, B<fuzzy>,
B<itemize-changes>, B<list-only>, B<omit-dir-times>, B<remove-sent-files>,
B<max-size>, and B<protocol>.

Version 0.38 of this module also adds support for the B<acls> option that
is not part of I<rsync(1)> unless the patch has been applied, but people do
use it.  It also includes a new B<literal> option that takes an array reference
similar to B<include>, B<exclude>, and B<filter>.  Any arguments in the array
are passed as literal arguments to rsync, and are passed first.  They should
have the proper single or double hyphen prefixes and the elements should be
split up the way you want them passed to exec.  The purpose of this option
is to allow the use of arbitrary options added by patches, and/or to allow
the use of new options in rsync without needing an imediate update to the
module in addtition to I<rsync(1)> itself.

=back

=cut

sub new {
   my $class = shift;

   # seed the options hash, booleans, scalars, excludes, source, dest, data,
   # status, stderr/stdout storage for last exec
   my $self = {
      # the full path name to the rsync binary
      'path-to-rsync' => $RsyncConfig{rsync_path},
      # these are the boolean flags to rsync, all default off, including them
      # in the args list turns them on
      'flag' => {qw(
         8-bit-output         0  fuzzy                0  no-specials          0
         acls                 0  group                0  no-super             0
         append               0  hard-links           0  no-times             0
         archive              0  help                 0  no-whole-file        0
         backup               0  ignore-errors        0  numeric-ids          0
         blocking-io          0  ignore-existing      0  omit-dir-times       0
         checksum             0  ignore-non-existing  0  one-file-system      0
         compress             0  ignore-times         0  whole-file           0
         copy-dirlinks        0  inplace              0  owner                0
         copy-links           0  ipv4                 0  partial              0
         copy-unsafe-links    0  ipv6                 0  perms                0
         cvs-exclude          0  keep-dirlinks        0  progress             0
         daemon               0  links                0  prune-empty-dirs     0
         del                  0  list-only            0  recursive            0
         delay-updates        0  no-blocking-io       0  relative             0
         delete               0  no-detach            0  remove-sent-files    0
         delete-after         0  no-devices           0  safe-links           0
         delete-before        0  no-dirs              0  size-only            0
         delete-during        0  no-groups            0  sparse               0
         delete-excluded      0  no-implied-dirs      0  specials             0
         devices              0  no-links             0  stats                0
         dirs                 0  no-owner             0  super                0
         dry-run              0  no-partial           0  times                0
         executability        0  no-perms             0  update               0
         existing             0  no-progress          0  version              0
         force                0  no-recursive         0  xattrs               0
         from0                0  no-relative          0  
      )},
      # these have simple scalar args we cannot easily check
      'scalar' => {qw(
         address              0  log-format           0  protocol             0
         backup-dir           0  max-delete           0  read-batch           0
         block-size           0  max-size             0  rsh                  0
         bwlimit              0  min-size             0  rsync-path           0
         checksum-seed        0  modify-window        0  sockopts             0
         compress-level       0  only-write-batch     0  suffix               0
         config               0  partial-dir          0  temp-dir             0
         csum-length          0  password-file        0  timeout              0
         files-from           0  port                 0  write-batch          0
      )},
      # these are not flags but counters, each time they appear it raises the
      # count, so we keep track and pass them the same number of times
      'counter' => {qw(
         human-readable       0  one-file-system      0  verbose              0
         itemize-changes      0  quiet                0
      )},
      # these can be specified multiple times and are additive, the doc also
      # specifies that it is an ordered list so we must preserve that order
      'chmod'        => [],
      'compare-dest' => [],
      'copy-dest'    => [],
      'exclude'      => [],
      'exclude-from' => [],
      'filter'       => [],
      'include'      => [],
      'include-from' => [],
      'link-dest'    => [],
      'literal'      => [],
      # hostname of source, used if 'source' is an array reference
      'srchost'     => '',
      # source host and/or path names
      'source'      => '',
      # destination host and/or path
      'dest'        => '',
      # return status from last exec
      'status'      => 0,
      'realstatus'  => 0,
      # last rsync command-line executed
      'lastcmd'     => undef,
      # whether or not to print debug statements
      'debug'       => 0,
      # double-quote source and/or destination paths
      'quote-src'   => 0,
      'quote-dst'   => 0,
      # stderr from last exec in array format (messages from remote rsync proc)
      'err'         => 0,
      'errfun'      => undef,
      # stdout from last exec in array format (messages from local rsync proc)
      'out'         => 0,
      'outfun'      => undef,
      # function to prvide --*-from=- data via pipe
      'infun'     => undef,
      # this flag changes error checking in 'exec' when called by 'list'
      'list'        => 0,
   };
   bless $self, $class; # bless it first so defopts can find out the class
   if (@_) {
      &defopts($self,@_) or return;
   }
   return $self;
}

=over 4

=item File::Rsync::defopts

$obj->defopts(@options);

   or

$obj->defopts(\%options);

Set default options for future exec calls for the object.  See I<rsync(1)>
for a complete list of valid options.  This is really the internal
method that I<new> calls but you can use it too.  The B<verbose> and B<quiet>
options to rsync are actually counters.  When assigning the perl hash-style
options you may specify the counter value directly and the module will pass
the proper number of options to rsync.

=back

=cut

sub defopts {
   # this method has now been split into 2 sub methods (parse and save)
   # _saveopts and _parseopts should only be used via defopts or exec
   my $self = shift;
   &_saveopts($self,&_parseopts($self,@_));
}

sub _parseopts {
   # this method checks and converts it's args into a reference to a hash
   # of valid options and returns it to the caller
   my $self = shift;
   my $pkgname = ref $self;
   my @opts = @_;
   my $opt;
   my %OPT = (); # this is the hash we will return a ref to

   # make sure we are passed the proper number of args
   if (@opts == 1) {
      $opt = shift;
      if (my $reftype = ref $opt) {
         unless ($reftype eq 'HASH') {
            carp "$pkgname: invalid reference type ($reftype) in options";
            return;
         }
      } else {
         carp "$pkgname: invalid option ($opt)";
         return;
      }
   } elsif (@opts % 2) {
      carp "$pkgname: invalid number of options passed (must be key/value pairs)";
      return;
   } else {
      $opt = {@opts};
   }

   # now process the options given, we handle debug first since hashes do not
   # have a specific order, and it would not be set first even if we sorted
   if (exists $opt->{'debug'}) {
      $OPT{'debug'} = $opt->{'debug'};
      print(STDERR "setting debug flag\n") if $OPT{'debug'};
   }
   foreach my $hashopt (keys %$opt) {
      my $savopt = $hashopt;
      $savopt =~ tr/_/-/;
      next if $hashopt eq 'debug'; # we did this one first (above)
      print STDERR "processing option: $hashopt\n"
         if $OPT{'debug'} or $self->{'debug'};
      if (exists $self->{'flag'}{$savopt}
            or exists $self->{'scalar'}{$savopt}
            or exists $self->{'counter'}{$savopt}) {
         $OPT{$savopt} = $opt->{$hashopt};
      } else {
         my $tag = '';
         if (     $hashopt eq 'chmod'
               or $hashopt eq 'compare-dest'
               or $hashopt eq 'copy-dest'
               or $hashopt eq 'exclude'
               or $hashopt eq 'exclude-from'
               or $hashopt eq 'filter'
               or $hashopt eq 'include'
               or $hashopt eq 'include-from'
               or $hashopt eq 'link-dest'
               or $hashopt eq 'literal') {
            $tag = $hashopt;
         } elsif ($hashopt eq 'source'
               or $hashopt eq 'src') {
            $tag = 'source';
         }
         if ($tag) {
            if (my $reftype = ref $opt->{$hashopt}) {
               if ($reftype eq 'ARRAY') {
                  $OPT{$tag} = $opt->{$hashopt};
               } elsif ($tag eq 'source' && blessed $opt->{$hashopt}) {
                  $OPT{$tag} = [ $opt->{$hashopt} ];
               } else {
                  carp "$pkgname: invalid reference type for $hashopt option";
                  return;
               }
            } elsif ($tag eq 'source') {
               $OPT{$tag} = [ $opt->{$hashopt} ];
            } else {
               carp "$pkgname: $hashopt is not a reference";
               return;
            }
         } elsif ($hashopt eq 'dest'
               or $hashopt eq 'destination'
               or $hashopt eq 'dst') {
            $OPT{'dest'} = $opt->{$hashopt};

         } elsif ($savopt eq 'path-to-rsync'
               or $savopt eq 'srchost'
               or $savopt eq 'quote-dst'
               or $savopt eq 'quote-src') {
            $OPT{$savopt} = $opt->{$hashopt};
         } elsif ($hashopt eq 'outfun' or $hashopt eq 'errfun'
               or $hashopt eq 'infun') {
            if (ref $opt->{$hashopt} eq 'CODE') {
               $OPT{$hashopt} = $opt->{$hashopt};
            } else {
               carp "$pkgname: $hashopt option is not a function reference";
               return;
            }
         } else {
            carp "$pkgname: $hashopt - unknown option";
            return;
         }
      }
   }
   return \%OPT;
}

sub _saveopts {
   # this method saves the data from the hash passed to it in the object's
   # hash
   my $self = shift;
   my $pkgname = ref $self;
   my $opts = shift;
   return unless ref $opts eq 'HASH';
   foreach my $opt (keys %$opts) {
      if (exists $self->{'flag'}{$opt}) {
         $self->{'flag'}{$opt} = $opts->{$opt};
      } elsif (exists $self->{'scalar'}{$opt}) {
         $self->{'scalar'}{$opt} = $opts->{$opt};
      } elsif (exists $self->{'counter'}{$opt}) {
         $self->{'counter'}{$opt} = $opts->{$opt};
      } elsif ($opt eq 'chmod' or $opt eq 'compare-dest'
            or $opt eq 'copy-dest' or $opt eq 'link-dest'
            or $opt eq 'exclude' or $opt eq 'exclude-from'
            or $opt eq 'include' or $opt eq 'include-from'
            or $opt eq 'filter' or $opt eq 'source' or $opt eq 'dest'
            or $opt eq 'debug' or $opt eq 'outfun' or $opt eq 'errfun'
            or $opt eq 'infun' or $opt eq 'path-to-rsync'
            or $opt eq 'srchost' or $opt eq 'quote-dst'
            or $opt eq 'quote-src' or $opt eq 'literal') {
         $self->{$opt} = $opts->{$opt};
      } else {
         carp "$pkgname: unknown option: $opt";
         return;
      }
   }
   return 1;
}

=over 4

=item File::Rsync::getcmd

my $cmd = $obj->getcmd(@options);

   or

my $cmd = $obj->getcmd(\%options);

   or

my ($cmd, $infun, $outfun, $errfun, $debug) = $obj->getcmd(\%options);

I<getcmd> returns a reference to an array containing the real rsync command
that would be called if the exec function were called.  The last example above
includes a reference to the optional stdin function, stdout function, stderr
function, and the debug setting.  This is the form used by the I<exec> method
to get the extra parameters it needs to do its job.  The function is exposed
to allow a user-defined exec function to be used, or for debugging purposes.

=back

=cut

sub getcmd {
   my $self = shift;
   my $pkgname = ref $self;
   my $merged = $self;
   my $list = $self->{list};
   $self->{list} = 0 if $self->{list};
   if (@_) { # If args are passed to exec then we have to merge the saved
      # (default) options with those passed, for any conflicts those passed
      # directly to exec take precidence
      my $execopts = &_parseopts($self,@_);
      return unless ref $execopts eq 'HASH';
      my %runopts = ();
      # first copy the default info from $self
      foreach my $type (qw(flag scalar counter)) {
         foreach my $opt (keys %{$self->{$type}}) {
            $runopts{$type}{$opt} = $self->{$type}{$opt};
         }
      }
      foreach my $opt (qw(path-to-rsync chmod compare-dest copy-dest
               exclude exclude-from filter include include-from
               link-dest source srchost debug dest outfun
               errfun infun quote-dst quote-src literal)) {
         $runopts{$opt} = $self->{$opt};
      }
      # now allow any args passed directly to exec to override
      foreach my $opt (keys %$execopts) {
         if (exists $runopts{'flag'}{$opt}) {
            $runopts{'flag'}{$opt} = $execopts->{$opt};
         } elsif (exists $runopts{'scalar'}{$opt}) {
            $runopts{'scalar'}{$opt} = $execopts->{$opt};
         } elsif (exists $runopts{'counter'}{$opt}) {
            $runopts{'counter'}{$opt} = $execopts->{$opt};
         } elsif ($opt eq 'chmod' or $opt eq 'compare-dest'
               or $opt eq 'copy-dest' or $opt eq 'link-dest'
               or $opt eq 'exclude' or $opt eq 'exclude-from'
               or $opt eq 'include' or $opt eq 'include-from'
               or $opt eq 'filter' or $opt eq 'source' or $opt eq 'dest'
               or $opt eq 'debug' or $opt eq 'outfun' or $opt eq 'errfun'
               or $opt eq 'infun' or $opt eq 'path-to-rsync'
               or $opt eq 'srchost' or $opt eq 'quote-dst'
               or $opt eq 'quote-src' or $opt eq 'literal') {
            $runopts{$opt} = $execopts->{$opt};
         } else {
            carp "$pkgname: unknown option: $opt";
            return;
         }
      }
      $merged = \%runopts;
   }

   my @cmd = ($merged->{'path-to-rsync'});

   # put any literal options first
   push @cmd,@{$merged->{'literal'}} if @{$merged->{'literal'}};

   foreach my $opt (sort keys %{$merged->{'flag'}}) {
      push @cmd,"--$opt" if $merged->{'flag'}{$opt};
   }
   foreach my $opt (sort keys %{$merged->{'scalar'}}) {
      push @cmd,"--$opt=$merged->{'scalar'}{$opt}" if $merged->{'scalar'}{$opt};
   }
   foreach my $opt (sort keys %{$merged->{'counter'}}) {
      for (my $i = 0;$i<$merged->{'counter'}{$opt};$i++) {
         push @cmd,"--$opt";
      }
   }
   if ((@{$merged->{'exclude'}} != 0) + (@{$merged->{'include'}} != 0)
           + (@{$merged->{'filter'}} != 0) > 1) {
      carp "$pkgname: 'exclude' and/or 'include' and/or 'filter' options specified, only one allowed";
      return;
   }
   foreach my $opt (@{$merged->{'chmod'}}) {
      push @cmd,"--chmod=$opt";
   }
   foreach my $opt (@{$merged->{'compare-dest'}}) {
      push @cmd,"--compare-dest=$opt";
   }
   foreach my $opt (@{$merged->{'copy-dest'}}) {
      push @cmd,"--copy-dest=$opt";
   }
   foreach my $opt (@{$merged->{'exclude'}}) {
      push @cmd,"--exclude=$opt";
   }
   foreach my $opt (@{$merged->{'exclude-from'}}) {
      push @cmd,"--exclude-from=$opt";
   }
   foreach my $opt (@{$merged->{'filter'}}) {
      push @cmd,"--filter=$opt";
   }
   foreach my $opt (@{$merged->{'include'}}) {
      push @cmd,"--include=$opt";
   }
   foreach my $opt (@{$merged->{'include-from'}}) {
      push @cmd,"--include-from=$opt";
   }
   foreach my $opt (@{$merged->{'link-dest'}}) {
      push @cmd,"--link-dest=$opt";
   }
   if ($merged->{'source'}) {
      if ($merged->{'srchost'}) {
         push @cmd, "$merged->{'srchost'}:" . join ' ',
            $merged->{'quote-src'} ? map { "\"$_\"" } @{$merged->{'source'}}
                                    : @{$merged->{'source'}};
      } else {
         push @cmd,
            $merged->{'quote-src'} ? map { "\"$_\"" } @{$merged->{'source'}}
                                    : @{$merged->{'source'}};
      }
   } elsif ($merged->{'srchost'} and $list) {
      push @cmd, "$merged->{'srchost'}:";
   } else {
      if ($list) {
         carp "$pkgname: no 'source' specified";
         return;
      } elsif ($merged->{'dest'}) {
         carp "$pkgname: option 'dest' specified without 'source' option";
         return;
      } else {
         carp "$pkgname: no source or destination specified";
         return;
      }
   }
   unless ($list) {
      if ($merged->{'dest'}) {
         push @cmd,
            $merged->{'quote-dst'} ? "\"$merged->{'dest'}\""
                                   : $merged->{'dest'};
      } else {
         carp "$pkgname: option 'source' specified without 'dest' option";
         return;
      }
   }
   return(wantarray
         ? (\@cmd,
            $merged->{'infun'},
            $merged->{'outfun'},
            $merged->{'errfun'},
            $merged->{'debug'})
         : \@cmd);
}

=over 4

=item File::Rsync::exec

$obj->exec(@options) or warn "rsync failed\n";

   or

$obj->exec(\%options) or warn "rsync failed\n";

This is the method that does the real work.  Any options passed to this
routine are appended to any pre-set options and are not saved.  They effect
the current execution of I<rsync> only.  In the case of conflicts, the options
passed directly to I<exec> take precedence.  It returns B<1> if the return
status was zero (or true), if the I<rsync> return status was non-zero it
returns B<0> and stores the return status.  You can examine the return status
from I<rsync> and any output to stdout and stderr with the methods listed below.

=back

=cut

sub exec {
   my $self = shift;

   my ($cmd, $infun, $outfun, $errfun, $debug) = $self->getcmd(@_);
   return unless $cmd;
   print STDERR "exec: @$cmd\n" if $debug;
   my $out = FileHandle->new; my $err = FileHandle->new;
   $err->autoflush(1);
   $out->autoflush(1);
   local $SIG{CHLD}='DEFAULT';
   my $pid;
   {
      my $in = FileHandle->new;
      $in->autoflush(1);
      $pid = eval{ open3 $in,$out,$err,@$cmd };
      $self->{lastcmd} = $cmd;
      if ($@) {
         $self->{'realstatus'} = 0;
         $self->{'status'} = 255;
         $self->{'err'} = [$@,"Execution of rsync failed.\n"];
         return 0;
      }
      if ($infun) {
         select((select($in),&{$infun})[0]);
      }
      $in->close;
   }
   my $odata = my $edata = '';

   my $stream = { 
     $out->fileno => {
        name         => 'out',
        data         => \$odata,
        buffer_tail  => '',
        block_size   => ($out->stat)[11] || 1024,
        handler      => $outfun
     },         
     $err->fileno => {
        name         => 'err',
        data         => \$edata,
        buffer_tail  => '',
        block_size   => ($err->stat)[11] || 1024,
        handler      => $errfun
     }
   };

   my $select = IO::Select->new;
   $select->add($out,$err);

   while ($out->opened or $err->opened) {
      foreach my $fd ( $select->can_read(1) ) {
         my $str = $stream->{$fd->fileno};
         warn("stream not found") unless $str;

         my $buffer;
         if ( $fd->sysread($buffer, $str->{block_size}) ) {
            ${$str->{data}} .= $buffer;
            if ( $str->{handler} ) {
               my $tail = '';
               $tail = $1 if $buffer =~ s/([^\n]+)\z//s;
               foreach my $line ( split /^/m, $str->{buffer_tail}.$buffer ) {
                  &{$str->{handler}}($line, $str->{name});
               }
               $str->{buffer_tail} = $tail;
            }
         } else {
            $select->remove($fd);
            $fd->close;
         }
      }
   }

   $self->{'out'} = $odata ? [ split /^/m,$odata ] : '';
   $self->{'err'} = $edata ? [ split /^/m,$edata ] : '';
   $out->close;
   $err->close;
   waitpid $pid,0;
   $self->{'realstatus'} = $?;
   $self->{'status'} = $?>>8;
   return($self->{'status'} ? 0 : 1);
}

=over 4

=item File::Rsync::list

$out = $obj->list(@options);

   or

$out = $obj->list(\%options);

   or

@out = $obj->list(\%options);

This is a wrapper for I<exec> called without a destination to get a listing.
It returns the output of stdout like the I<out> function below.  When
no destination is given rsync returns the equivalent of 'ls -l' or 'ls -lr'
modified by any include/exclude/filter parameters you specify.  This is useful
for manual comparison without actual changes to the destination or for
comparing against another listing taken at a different point in time.

(As of rsync version 2.6.4-pre1 this can also be accomplished with the
'list-only' option regardless of whether a destination is given.)

=back

=cut

sub list {
   my $self = shift;
   $self->{list}++;
   $self->exec(@_);
   if ($self->{'out'}) {
      return(wantarray ? @{$self->{'out'}} : $self->{'out'});
   } else {
      return;
   }
}

=over 4

=item File::Rsync::status

$rval = $obj->I<status>;

Returns the status from last I<exec> call right shifted 8 bits.

=back

=cut

sub status {
   my $self = shift;
   return $self->{'status'};
}

=over 4

=item File::Rsync::realstatus

$rval = $obj->I<realstatus>;

Returns the real status from last I<exec> call (not right shifted).

=back

=cut

sub realstatus {
   my $self = shift;
   return $self->{'realstatus'};
}

=over 4

=item File::Rsync::err

$aref = $obj->I<err>;

In a scalar context this method will return a reference to an array containing
all output to stderr from the last I<exec> call, or zero (false) if there
was no output.  In an array context it will return an array of all output to
stderr or an empty list.  The scalar context can be used to efficiently test
for the existance of output.  I<rsync> sends all messages from the remote
I<rsync> process and any error messages to stderr.  This method's purpose is
to make it easier for you to parse that output for appropriate information.

=back

=cut

sub err {
   my $self = shift;
   if ($self->{'err'}) {
      return(wantarray ? @{$self->{'err'}} : $self->{'err'});
   } else {
      return;
   }
}

=over 4

=item File::Rsync::out

$aref = $obj->I<out>;

Similar to the I<err> method, in a scalar context it returns a reference to an
array containing all output to stdout from the last I<exec> call, or zero
(false) if there was no output.  In an array context it returns an array of all
output to stdout or an empty list.  I<rsync> sends all informational messages
(B<verbose> option) from the local I<rsync> process to stdout.

=back

=cut

sub out {
   my $self = shift;
   if ($self->{'out'}) {
      return(wantarray ? @{$self->{'out'}} : $self->{'out'});
   } else {
      return;
   }
}

=over 4

=item File::Rsync::lastcmd

$aref = $obj->I<lastcmd>;

Returns the actual system command used by the last I<exec> call, or '' before
any calls to I<exec> for the object.  This can be useful in the case of an
error condition to give a more informative message or for debugging purposes.
In an array context it return an array of args as passed to the system, in
a scalar context it returns a space-seperated string.  See I<getcmd> for access
to the command before execution.

=back

=cut

sub lastcmd {
   my $self = shift;
   if ($self->{lastcmd}) {
      return wantarray ? @{$self->{lastcmd}} : join ' ',@{$self->{lastcmd}};
   } else {
      return;
   }
}

=head1 Author

Lee Eakin E<lt>leakin@dfw.nostrum.comE<gt>

=head1 Credits

The following people have contributed ideas, bug fixes, code or helped out
by reporting or tracking down bugs in order to improve this module since
it's initial release.  See the Changelog for details:

Greg Ward

Boris Goldowsky

James Mello

Andreas Koenig

Joe Smith

Jonathan Pelletier

Heiko Jansen

Tong Zhu

Paul Egan

Ronald J Kimball

James CE Johnson

Bill Uhl

Peter teStrake

Harald Flaucher

Simon Myers

Gavin Carr

Petya Kohts

=head1 Inspiration and Assistance

Gerard Hickey                             C<PGP::Pipe>

Russ Allbery                              C<PGP::Sign>

Graham Barr                               C<Net::*>

Andrew Tridgell and Paul Mackerras        rsync(1)

John Steele   E<lt>steele@nostrum.comE<gt>

Philip Kizer  E<lt>pckizer@nostrum.comE<gt>

Larry Wall                                perl(1)

I borrowed many clues on wrapping an external program from the PGP modules,
and I would not have had such a useful tool to wrap except for the great work
of the B<rsync> authors.  Thanks also to Graham Barr, the author of the libnet
modules and many others, for looking over this code.  Of course I must mention
the other half of my brain, John Steele, and his good friend Philip Kizer for
finding B<rsync> and bringing it to my attention.  And I would not have been
able to enjoy writing useful tools if not for the creator of the B<perl>
language.

=head1 Copyrights

      Copyright (c) 1999-2005 Lee Eakin.  All rights reserved.
 
      This program is free software; you can redistribute it and/or modify
      it under the same terms as Perl itself. 

=cut

1;