This file is indexed.

/usr/share/perl5/File/Rsync.pm is in libfile-rsync-perl 0.48-3.

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
913
914
915
916
917
918
919
920
921
922
923
924
# -*- 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.008;    # it might work with older versions of 5 but not tested

use FileHandle;
use IPC::Run3 'run3';
use Carp 'carp';
use Scalar::Util qw(blessed);
use Data::Dumper;

use strict;
use vars qw($VERSION);

$VERSION = '0.48';

=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:remotedir' )
        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-3.1.1> but should perform properly with most recent
versions.

=head2 File::Rsync::new

    $obj = new File::Rsync;

        or

    $obj = File::Rsync->new;

        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 style of a hash (key/value pairs) and are the
same as the long options in I<rsync(1)> without the leading double-hyphen.
Any leading single or double-hyphens are removed, and you may use underscore
in place of hyphens in option names to simplify quoting and avoid possible
equation parsing (subtraction).

Although options are key/value pairs, as of version 0.46 the order is now
preserved.  Passing a hash reference is still supported for backwards
compatibility, but is deprecated as order cannot be preserved for this case.

An additional option of B<path-to-rsync> also exists which can be used to
override the using PATH environemt variable to find the rsync command binary,
and B<moddebug> 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: these are B<quote-src> and B<quote-dst>, which may be useful
in protecting the paths from shell expansion (particularly useful for paths
containing spaces).  This wraps all source and/or destination paths in
double-quotes to limit remote shell expansion.  It is similar but not
necessarily the same result as the B<protect-args> option in rsync itself.

The B<outfun> and B<errfun> options take a function reference, 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.

If options are passed as a hash reference (deprecated), the B<exclude>
needs an array reference as it's value since there cannot be duplicate keys
in a hash.  Since order cannot be preserved in a hash, this module currently
limits the use of B<exclude> or B<include> together.
They can be mixed together if options are in the form of a list or array ref.

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.
As of version 0.46 the value of B<files-from> may be an array reference.
The contents of the array are passed to B<files-from> the same as the
below method using B<infun> but implemented inside the module.

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> options, 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 added 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.

=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 = {
      # these are the boolean flags to rsync, all default off, including them
      # in the args list turns them on
      flag => {
         map { $_ => 0 }
            qw(8-bit-output acls append append-verify archive backup
            blocking-io checksum compress copy-dirlinks copy-links
            copy-unsafe-links crtimes cvs-exclude daemon del delay-updates
            delete delete-after delete-before delete-delay delete-during
            delete-excluded delete-missing-args devices dirs dry-run
            executability existing fake-super fileflags force force-change
            force-delete force-schange force-uchange from0 fuzzy group groups
            hard-links help hfs-compression ignore-errors ignore-existing
            ignore-missing-args ignore-non-existing ignore-times inc-recursive
            inplace ipv4 ipv6 keep-dirlinks links list-only msgs2stderr
            munge-links new-compress no-blocking-io no-detach no-devices
            no-dirs no-groups no-iconv no-implied-dirs no-inc-recursive
            no-links no-motd no-owner no-partial no-perms no-progress
            no-protect-args no-recursive no-relative no-specials no-super
            no-times no-whole-file numeric-ids old-compress old-dirs
            omit-dir-times omit-link-times owner partial perms preallocate
            progress protect-args protect-decmpfs prune-empty-dirs recursive
            relative remove-source-files safe-links size-only sparse specials
            stats super times update version whole-file xattrs)
      },
      # these have simple scalar args we cannot easily check
      # use 'string' so I don't forget and leave keyword scalar unqouted
      string => {
         map { $_ => '' }
            qw(address backup-dir block-size bwlimit checksum-seed chown
            compress-level config contimeout csum-length debug files-from
            groupmap iconv info log-file log-file-format log-format max-delete
            max-size min-size modify-window only-write-batch out-format outbuf
            partial-dir password-file port protocol read-batch rsh rsync-path
            skip-compress sockopts suffix temp-dir timeout usermap
            write-batch)
      },
      # 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 => {
         map { $_ => 0 }
            qw(human-readable itemize-changes one-file-system quiet verbose)
      },
      # 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
      list => {
         'chmod'         => [],
         'compare-dest'  => [],
         'copy-dest'     => [],
         'dparam'        => [],
         'exclude'       => [],
         'exclude-from'  => [],
         'filter'        => [],
         'include'       => [],
         'include-from'  => [],
         'link-dest'     => [],
         'literal'       => [],
         'remote-option' => [],
      },
      code => {    # input/output user functions
         'errfun' => undef,
         'outfun' => undef,
         # function to prvide --*-from=- data via pipe
         'infun' => undef,
      },
      _perlopts => {
         # the path name to the rsync binary (default is to use $PATH)
         'path-to-rsync' => 'rsync',
         # hostname of source, used if 'source' is an array reference
         'srchost' => '',
         # double-quote source and/or destination paths
         'quote-src' => 0,
         'quote-dst' => 0,
         # whether or not to print debug statements
         'moddebug' => 0,
      },
      # 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,
     # stderr from last exec in array format (messages from remote rsync proc)
      '_err' => 0,
      # stdout from last exec in array format (messages from local rsync proc)
      '_out' => 0,
      # this flag changes error checking in 'exec' when called by 'list'
      '_list_mode' => 0,
      # this array used to preserve arg order
      '_args' => [],
   };
   bless $self, $class;    # bless it first so defopts can find out the class
   if (@_) {
      &defopts($self, @_) or return;
   }
   return $self;
}

=head2 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.

=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 $href;
   my %OPT = ();    # this is the hash we will return a ref to

   # make sure we are passed the proper number of args
   if (@_ == 1) {
      if (my $reftype = ref $_[0]) {
         if ($reftype eq 'HASH') {
            carp "$pkgname: hash reference is deprecated, use array or list."
               if $^W;
            @_ = %{$_[0]};
            $href++;
         } elsif ($reftype eq 'ARRAY') {
            @_ = @{$_[0]};
         } else {
            carp "$pkgname: invalid reference type ($reftype) option.";
            return;
         }
      } else {
         carp "$pkgname: invalid option ($_[0]).";
         return;
      }
   }
   if (@_ % 2) {
      carp
         "$pkgname: invalid number of options passed (must be key/value pairs).";
      return;
   }

   # now process the options given, we handle debug first
   for (my $i = 0; $i < @_; $i += 2) {
      if ($_[$i] eq 'moddebug') {
         $OPT{moddebug} = $_[ $i + 1 ];
         warn "setting debug flag\n" if $OPT{moddebug};
         last;
      }
   }

   my @order;
   while (my ($inkey, $val) = splice @_, 0, 2) {
      (my $key = $inkey) =~ tr/_/-/;
      $key =~ s/^--?//;    # remove any leading hyphens if found
      $key = 'source' if $key eq 'src';
      $key = 'dest' if $key eq 'dst' or $key eq 'destination';
      next if $key eq 'moddebug';    # we did this one already
      warn "processing option: $inkey\n"
         if $OPT{moddebug}
         or $self->{_perlopts}{moddebug};
      if (  exists $self->{flag}{$key}
         or exists $self->{string}{$key}
         or exists $self->{counter}{$key}
         or exists $self->{_perlopts}{$key})
      {
         if ($key eq 'files-from' and ref $val eq 'ARRAY') {
            push @order, $key, '-', 'infun', $val;    # --files-from=- <\@
            $OPT{$key} = '-';
            $OPT{infun} = $val;

         } else {
            push @order, $key, $val;
            $OPT{$key} = $val;
         }
         next;
      }
      if (exists $self->{list}{$key} or $key eq 'source') {
         if (my $reftype = ref $val) {
            if ($reftype eq 'ARRAY') {
               push @order, $key, $val;
               $OPT{$key} = $val;
               next;
            } elsif ($key eq 'source' && blessed $val) {
               # if it's blessed, assume it returns a string
               $val = [$val];
               push @order, $key, $val;
               $OPT{$key} = $val;
               next;
            } else {
               carp "$pkgname: invalid reference type for $inkey option.";
               return;
            }
         } elsif ($key eq 'source') {
            $val = [$val];
            push @order, $key, $val;
            $OPT{$key} = $val;
            next;
         } else {
            carp "$pkgname: $inkey value is not a reference.";
            return;
         }
      }
      if ($key eq 'dest') {
         push @order, $key, $val;
         $OPT{$key} = $val;
         next;
      }
      if (exists $self->{code}{$key}) {
         if (ref $val eq 'CODE') {
            push @order, $key, $val;
            $OPT{$key} = $val;
            next;
         } elsif ($key eq 'infun' and ref $val eq 'ARRAY') {
            # IPC::Run3 lets us pass an array ref as stdin :)
            push @order, $key, $val;
            $OPT{$key} = $val;
            next;
         } else {
            carp "$pkgname: $inkey option is not a function reference.";
            return;
         }
      }

      carp "$pkgname: $inkey - unknown option.";
      return;
   }
   $OPT{_args} = \@order unless $href;
   return \%OPT;
}

sub _saveopts {
   # save the data from the hash passed in the object
   my $self    = shift;
   my $pkgname = ref $self;
   my $opts    = shift;
   return unless ref $opts eq 'HASH';
SO: for my $opt (keys %$opts) {
      for my $type (qw(flag string counter list code _perlopts)) {
         if (exists $self->{$type}{$opt}) {
            $self->{$type}{$opt} = $opts->{$opt};
            next SO;
         }
      }
      if (  $opt eq 'source'
         or $opt eq 'dest'
         or $opt eq '_args')
      {
         $self->{$opt} = $opts->{$opt};
      } else {
         carp "$pkgname: unknown option: $opt.";
         return;
      }
   }    # end SO
   return 1;
}

=head2 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.

=cut

sub getcmd {
   my $self    = shift;
   my $pkgname = ref $self;
   my $merged  = $self;
   my $list    = $self->{_list_mode};
   $self->{_list_mode} = 0;
   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
      for my $type (qw(flag string counter list code _perlopts)) {
         for my $opt (keys %{$self->{$type}}) {
            $runopts{$type}{$opt} = $self->{$type}{$opt};
         }
      }
      for my $opt (qw(source dest)) {
         $runopts{$opt} = $self->{$opt};
      }
      @{$runopts{_args}} = @{$self->{_args}};
      # now allow any args passed directly to exec to override
   OPT: for my $opt (keys %$execopts) {
         for my $type (qw(flag string counter list code _perlopts)) {
            if (exists $runopts{$type}{$opt}) {
               $runopts{$type}{$opt} = $execopts->{$opt};
               next OPT;
            }
         }
         if ($opt eq '_args') {
            # only preserve order if we already have order
            push @{$runopts{$opt}}, @{$execopts->{$opt}}
               if @{$runopts{$opt}};
         } elsif ($opt eq 'source' or $opt eq 'dest') {
            $runopts{$opt} = $execopts->{$opt};
         } else {
            carp "$pkgname: unknown option: $opt.";
            return;
         }
      }
      $merged = \%runopts;
   }

   if (
      !@{$merged->{_args}}    # include and exclude allowed if ordered args
      && ( (@{$merged->{list}{exclude}} != 0)
         + (@{$merged->{list}{include}} != 0)
         + (@{$merged->{list}{filter}} != 0) > 1)
      )
   {
      carp "$pkgname: 'exclude' and/or 'include' and/or 'filter' "
         . "options specified, only one allowed.";
      return;
   }

   my $srchost = $merged->{srchost};
   $srchost .= ':' if $srchost and substr($srchost, 0, 8) ne 'rsync://';

   # build the real command
   my @cmd = ($merged->{_perlopts}{'path-to-rsync'});

   if (@{$merged->{_args}}) {    # prefer ordered args if we have them
      my $gotsrc;
      for (my $e = 0; $e < @{$merged->{_args}}; $e += 2) {
         my $key = $merged->{_args}[$e];
         my $val = $merged->{_args}[ $e + 1 ];
         if ($key eq 'literal') {
            push @cmd, ref $val eq 'ARRAY' ? @$val : $val;
         } elsif (exists $merged->{flag}{$key}) {
            push @cmd, "--$key" if $val;
         } elsif (exists $merged->{string}{$key}) {
            push @cmd, "--$key=$val" if $val;
         } elsif (exists $merged->{counter}{$key}) {
            for (my $i = 0; $i < $val; $i++) {
               push @cmd, "--$key";
            }
         } elsif (exists $merged->{list}{$key}) {
            push @cmd, ref $val eq 'ARRAY'
               ? map "--$key=$_", @$val
               : "--$key=$val";
         } elsif ($key eq 'source') {
            if ($merged->{srchost}) {
               push @cmd, $srchost . join ' ',
                  $merged->{'quote-src'}
                  ? map ("\"$_\"", ref $val eq 'ARRAY' ? @$val : $val)
                  : ref $val eq 'ARRAY' ? @$val
                  :                       $val;
            } else {
               push @cmd,
                  $merged->{'quote-src'}
                  ? map ("\"$_\"", ref $val eq 'ARRAY' ? @$val : $val)
                  : ref $val eq 'ARRAY' ? @$val
                  :                       $val;
            }
            $gotsrc++;
         } elsif ($key eq 'dest') {
            if ($list) {
               if (not $gotsrc) {
                  if ($merged->{srchost}) {
                     push @cmd, $srchost;
                  } else {
                     carp "$pkgname: no 'source' specified.";
                     return;
                  }
               }
            } elsif (not $gotsrc) {
               carp
                  "$pkgname: option 'dest' specified without 'source' option.";
               return;
            } else {
               push @cmd, $merged->{'quote-dst'} ? "\"$val\"" : $val;
            }
         }
      }
   } else {
      # we do a bunch of extra work here to support hash refs,
      # they don't work well here, no order, we do what we can
      # put any literal options first
      push @cmd, @{$merged->{list}{literal}} if @{$merged->{list}{literal}};

      for my $opt (sort keys %{$merged->{flag}}) {
         push @cmd, "--$opt" if $merged->{flag}{$opt};
      }
      for my $opt (sort keys %{$merged->{string}}) {
         push @cmd, "--$opt=$merged->{string}{$opt}"
            if $merged->{string}{$opt};
      }
      for my $opt (sort keys %{$merged->{counter}}) {
         for (my $i = 0; $i < $merged->{counter}{$opt}; $i++) {
            push @cmd, "--$opt";
         }
      }
      for my $opt (sort keys %{$merged->{list}}) {
         next if $opt eq 'literal';
         for my $val (@{$merged->{list}{$opt}}) {
            push @cmd, "--$opt=$val";
         }
      }

      if ($merged->{source}) {
         if ($merged->{srchost}) {
            push @cmd, $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, $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->{code}{infun},
         $merged->{code}{outfun}, $merged->{code}{errfun},
         $merged->{_perlopts}{moddebug}
         )
      : \@cmd
   );
}

=head2 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.

=cut

sub exec {
   my $self = shift;

   my ($cmd, $infun, $outfun, $errfun, $debug) = $self->getcmd(@_);
   return unless $cmd;
   warn "exec: @$cmd\n" if $debug;
   run3($cmd, $infun, \my $stdout, \my $stderr);
   $self->{_lastcmd}    = $cmd;
   $self->{_realstatus} = $?;
   $self->{_status}     = $? & 127 ? $? & 127 : $? >> 8;
   $self->{_out}        = $stdout ? [ split /^/m, $stdout ] : '';
   $self->{_err}        = $stderr ? [ split /^/m, $stderr ] : '';
   if ($outfun and $self->{_out}) {
      for (@{$self->{_out}}) { $outfun->($_, 'out') }
   }
   if ($errfun and $self->{_err}) {
      for (@{$self->{_err}}) { $errfun->($_, 'err') }
   }
   return ($self->{_status} ? 0 : 1);
}

=head2 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.)

=cut

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

=head2 File::Rsync::status

    $rval = $obj->status;

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

=cut

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

=head2 File::Rsync::realstatus

    $rval = $obj->realstatus;

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

=cut

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

=head2 File::Rsync::err

    $aref = $obj->err;

In 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.

=cut

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

=head2 File::Rsync::out

    $aref = $obj->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.

=cut

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

=head2 File::Rsync::lastcmd

    $aref = $obj->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.

=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

Neil Hooey

Erez Schatz

Max Maischein

=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-2015 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;