File: Wrapper.pm

package info (click to toggle)
libarchive-tar-wrapper-perl 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 782; makefile: 2
file content (1333 lines) | stat: -rw-r--r-- 34,310 bytes parent folder | download
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
package Archive::Tar::Wrapper;

use strict;
use warnings;
use File::Temp    qw(tempdir);
use Log::Log4perl qw(:easy);
use File::Spec::Functions;
use File::Spec;
use File::Path;
use File::Copy;
use File::Find;
use File::Basename;
use File::Which qw(which);
use IPC::Run    qw(run);
use Cwd;
use Config;
use IPC::Open3;
use Symbol 'gensym';
use Carp;

our $VERSION = '0.42';
my $logger = get_logger();

sub new {
    my ( $class, %options ) = @_;

    my $self = {
        tar                   => delete $options{tar} || undef,
        tmpdir                => undef,
        tar_read_options      => '',
        tar_write_options     => '',
        tar_error_msg         => undef,
        tar_gnu_read_options  => [],
        tar_gnu_write_options => [],
        dirs                  => 0,
        max_cmd_line_args     => 512,
        ramdisk               => undef,
        _os_names             => {
            openbsd => 'openbsd',
            mswin   => 'MSWin32',
            solaris => 'solaris'
        },

        # hack used to enable unit testing
        osname      => delete $options{osname} || $Config{osname},
        bzip2_regex => qr/\.bz2$/ix,
        gzip_regex  => qr/\.t? # an optional t for matching tgz
                                    gz$ # ending with gz, which means compressed by gzip
                                    /ix,
        tar_error_msg => undef,
        version_info  => undef,
        tar_exit_code => undef,
        is_gnu        => undef,
        is_bsd        => undef,
        version_info  => undef,
        tar_exit_code => undef,
        %options,
    };

    bless $self, $class;

    unless ( defined $self->{tar} ) {

        if ( ( $self->_is_openbsd ) and ( $self->{tar_read_options} ) ) {
            $self->{tar_read_options} = '-' . $self->{tar_read_options};
        }

        if ( $self->{osname} eq 'MSWin32' ) {
            $self->_setup_mswin();
        }
        else {
            $self->{tar} = which('tar') || which('gtar');
        }

        unless ( defined $self->{tar} ) {

# this is specific for testing under MS Windows smokers without tar installed
# "OS unsupported" will mark the testing as NA instead of failure as convention.
            if ( $self->{osname} eq 'MSWin32' ) {
                LOGDIE 'tar not found in PATH, OS unsupported';
            }
            else {
                LOGDIE 'tar not found in PATH, please specify location';
            }
        }

    }

    $self->_acquire_tar_info();

    if ( defined $self->{ramdisk} ) {
        my $rc = $self->ramdisk_mount( %{ $self->{ramdisk} } );
        unless ($rc) {
            LOGDIE "Mounting ramdisk failed";
        }
        $self->{tmpdir} = $self->{ramdisk}->{tmpdir};
    }
    else {
        $self->{tmpdir} =
          tempdir( $self->{tmpdir} ? ( DIR => $self->{tmpdir} ) : () );
    }

    $self->{tardir} = File::Spec->catfile( $self->{tmpdir}, 'tar' );
    mkpath [ $self->{tardir} ], 0, oct(755)
      or LOGDIE 'Cannot create the path ' . $self->{tardir} . ": $!";
    $logger->debug( 'tardir location: ' . $self->{tardir} )
      if ( $logger->is_debug );
    $self->{objdir} = tempdir();

    return $self;
}

sub _is_openbsd {
    my $self = shift;
    return ( $self->{osname} eq $self->{_os_names}->{openbsd} );
}

sub _is_solaris {
    my $self = shift;
    return ( $self->{osname} eq $self->{_os_names}->{solaris} );
}

sub _read_solaris_opts {
    my ( $self, $compress_opt ) = @_;
    my @cmd;
    push( @cmd, $self->{tar} );
    push( @cmd, 'xp' );

    if ($compress_opt) {
        $cmd[-1] .= $compress_opt;
    }

    $cmd[-1] .= 'f';

    push( @cmd, $self->{tar_read_options} )
      if ( $self->{tar_read_options} ne '' );
    return \@cmd;
}

sub _read_openbsd_opts {
    my ( $self, $compress_opt ) = @_;
    my @cmd;
    push( @cmd, $self->{tar} );

    if ($compress_opt) {

        # actually, prepending '-' would work with any modern GNU tar
        $compress_opt = '-' . $compress_opt;
        push( @cmd, $compress_opt );
    }

    push( @cmd, '-x' );
    push( @cmd, $self->{tar_read_options} )
      if ( $self->{tar_read_options} ne '' );
    push( @cmd, @{ $self->{tar_gnu_read_options} } )
      if ( scalar( @{ $self->{tar_gnu_read_options} } ) > 0 );
    return \@cmd;

}

sub read {    ## no critic (ProhibitBuiltinHomonyms)
    my ( $self, $tarfile, @files ) = @_;

    my $cwd = getcwd();

    unless ( File::Spec::Functions::file_name_is_absolute($tarfile) ) {
        $tarfile = File::Spec::Functions::rel2abs( $tarfile, $cwd );
    }

    chdir $self->{tardir}
      or LOGDIE "Cannot chdir to $self->{tardir}";

    my $compr_opt = '';    # sane value
    $compr_opt = $self->is_compressed($tarfile);

    my @cmd;

    if ( $self->_is_openbsd ) {
        @cmd = @{ $self->_read_openbsd_opts($compr_opt) };
        push @cmd, '-f';
    }
    elsif ( $self->_is_solaris ) {
        @cmd = @{ $self->_read_solaris_opts($compr_opt) };
    }
    else {
        @cmd = (
            $self->{tar},
            "${compr_opt}x$self->{tar_read_options}",
            @{ $self->{tar_gnu_read_options} }, '-f'
        );
    }

    push( @cmd, $tarfile, @files );

    $logger->debug("Running @cmd") if ( $logger->is_debug );
    my $error_code = run( \@cmd, \my ( $in, $out, $err ) );

    unless ($error_code) {
        ERROR "@cmd failed: $err";
        chdir $cwd or LOGDIE "Cannot chdir to $cwd";
        return;
    }

    $logger->warn($err) if ( $logger->is_warn and $err );
    chdir $cwd or LOGDIE "Cannot chdir to $cwd: $!";
    return ( $error_code == 0 ) ? undef : $error_code;
}

sub list_reset {
    my ($self) = @_;

    #TODO: keep the file list as a fixed attribute of the instance
    my $list_file = File::Spec->catfile( $self->{objdir}, 'list' );
    my $cwd       = getcwd();
    chdir $self->{tardir} or LOGDIE "Can't chdir to $self->{tardir}: $!";
    open( my $fh, '>', $list_file ) or LOGDIE "Can't open $list_file: $!";

    if ( $logger->is_debug ) {
        $logger->debug('List of all files identified inside the tar file');
    }

    find(
        sub {
            my $entry = $File::Find::name;
            $entry =~ s#^\./##o;
            my $type = (
                  -d $_ ? 'd'
                : -l $_ ? 'l'
                :         'f'
            );
            print $fh "$type $entry\n";
            $logger->debug("$type $entry") if ( $logger->is_debug );
        },
        '.'
    );

    $logger->debug('All entries listed') if ( $logger->is_debug );
    close($fh);
    chdir $cwd or LOGDIE "Can't chdir to $cwd: $!";
    $self->_offset(0);
    return 1;
}

sub _read_from_tar {
    my $self = shift;
    my ( $wtr, $rdr, $err ) = ( gensym, gensym, gensym );
    my $pid = open3( $wtr, $rdr, $err, "$self->{tar} --version" );
    my ( $output, $error );

    {
        local $/ = undef;
        $output = <$rdr>;
        $error  = <$err>;
    }

    close($rdr);
    close($err);
    close($wtr);
    waitpid( $pid, 0 );
    chomp $error;
    chomp $output;
    $self->{tar_error_msg} = $error;
    $self->{version_info}  = $output;
    $self->{tar_exit_code} = $? >> 8;
    return 1;
}

sub _acquire_tar_info {
    my ( $self, $skip ) = @_;
    $self->_read_from_tar() unless ($skip);
    my $bsd_regex = qr/bsd/i;
    $self->{is_gnu} = 0;
    $self->{is_bsd} = 0;

    if ( $self->_is_openbsd() ) {

# there is no way to acquire version information from default tar program on OpenBSD
        $self->{version_info}  = "Information not available on $Config{osname}";
        $self->{tar_exit_code} = 0;
        $self->{is_bsd}        = 1;
    }
    elsif ( $self->_is_solaris() ) {
        $self->{version_info}  = "Information not available on $Config{osname}";
        $self->{tar_exit_code} = 0;
        $self->{is_bsd}        = 1;
    }
    elsif ( ( $self->{tar} =~ $bsd_regex ) and ( $self->{tar_exit_code} == 1 ) )
    {
# bsdtar exit code is 1 when asking for version, forcing to zero since is not an error
        $self->{tar_exit_code} = 0;
        $self->{is_bsd}        = 1;
    }

    $self->{version_info} = 'Information not available. Search for errors'
      unless ( $self->{tar_exit_code} == 0 );
    $self->{is_gnu} = 1 if ( $self->{version_info} =~ /GNU/ );
    return 1;
}

sub _setup_mswin {
    my $self = shift;

    # bsdtar is always preferred on Windows
    my $tar_path = which('bsdtar');
    $tar_path = which('tar') unless ( defined($tar_path) );

    if ( $tar_path =~ /\s/ ) {

        # double quoting will be required is there is a space
        $tar_path = qq($tar_path);
    }
    $self->{tar} = $tar_path;
}

sub tardir {
    my ($self) = @_;
    return $self->{tardir};
}

sub is_compressed {
    my ( $self, $tarfile ) = @_;

    return 'z' if $tarfile =~ $self->{gzip_regex};
    return 'j' if $tarfile =~ $self->{bzip2_regex};

    # Sloppy check for gzip files
    open( my $fh, '<', $tarfile ) or croak("Cannot open $tarfile: $!");
    binmode($fh);
    my $read = sysread( $fh, my $two, 2, 0 )
      or croak("Cannot sysread $tarfile: $!");
    close($fh);

    return 'z'
      if (  ( ( ord( substr( $two, 0, 1 ) ) ) == 0x1F )
        and ( ( ord( substr( $two, 1, 1 ) ) ) == 0x8B ) );

    return q{};
}

sub locate {
    my ( $self, $rel_path ) = @_;

    my $real_path = File::Spec->catfile( $self->{tardir}, $rel_path );

    if ( -e $real_path ) {
        $logger->debug("$real_path exists") if ( $logger->is_debug );
        return $real_path;
    }
    else {
        $logger->warn("$rel_path not found in tarball") if ( $logger->is_warn );
        return;
    }
}

sub add {
    my ( $self, $rel_path, $path_or_stringref, $opts ) = @_;

    if ($opts) {
        unless ( ( ref($opts) ) and ( ref($opts) eq 'HASH' ) ) {
            LOGDIE "Option parameter given to add() not a hashref.";
        }
    }

    my ( $perm, $uid, $gid, $binmode );
    $perm    = $opts->{perm}    if defined $opts->{perm};
    $uid     = $opts->{uid}     if defined $opts->{uid};
    $gid     = $opts->{gid}     if defined $opts->{gid};
    $binmode = $opts->{binmode} if defined $opts->{binmode};

    my $target     = File::Spec->catfile( $self->{tardir}, $rel_path );
    my $target_dir = dirname($target);

    unless ( -d $target_dir ) {
        if ( ref($path_or_stringref) ) {
            $self->add( dirname($rel_path), dirname($target_dir) );
        }
        else {
            $self->add( dirname($rel_path), dirname($path_or_stringref) );
        }
    }

    if ( ref($path_or_stringref) ) {
        open my $fh, '>', $target or LOGDIE "Can't open $target: $!";
        if ( defined $binmode ) {
            binmode $fh, $binmode;
        }
        print $fh $$path_or_stringref;
        close $fh;
    }
    elsif ( -d $path_or_stringref ) {

        # perms will be fixed further down
        mkpath( $target, 0, oct(755) ) unless -d $target;
    }
    else {
        copy $path_or_stringref, $target
          or LOGDIE "Can't copy $path_or_stringref to $target ($!)";
    }

    if ( defined $uid ) {
        chown $uid, -1, $target
          or LOGDIE "Can't chown $target uid to $uid ($!)";
    }

    if ( defined $gid ) {
        chown -1, $gid, $target
          or LOGDIE "Can't chown $target gid to $gid ($!)";
    }

    if ( defined $perm ) {
        chmod $perm, $target
          or LOGDIE "Can't chmod $target to $perm ($!)";
    }

    if (    not defined $uid
        and not defined $gid
        and not defined $perm
        and not ref($path_or_stringref) )
    {
        perm_cp( $path_or_stringref, $target )
          or LOGDIE "Can't perm_cp $path_or_stringref to $target ($!)";
    }

    return 1;
}

sub perm_cp {
    my ( $source, $target ) = @_;
    perm_set( $target, perm_get($source) );
    return 1;
}

sub perm_get {
    my ($filename) = @_;
    my @stats = ( stat $filename )[ 2, 4, 5 ]
      or LOGDIE "Cannot stat $filename ($!)";
    return \@stats;
}

sub perm_set {
    my ( $filename, $perms ) = @_;
    chown( $perms->[1], $perms->[2], $filename );
    chmod( $perms->[0] & oct(777), $filename )
      or LOGDIE "Cannot chmod $filename ($!)";
    return 1;
}

sub remove {
    my ( $self, $rel_path ) = @_;
    my $target = File::Spec->catfile( $self->{tardir}, $rel_path );
    rmtree($target) or LOGDIE "Can't rmtree $target: $!";
    return 1;
}

sub list_all {
    my ($self) = @_;
    my @entries = ();
    $self->list_reset();

    while ( my $entry = $self->list_next() ) {
        push @entries, $entry;
    }

    return \@entries;
}

sub list_next {
    my ($self)    = @_;
    my $offset    = $self->_offset();
    my $list_file = File::Spec->catfile( $self->{objdir}, 'list' );
    open my $fh, '<', $list_file or LOGDIE "Can't open $list_file: $!";
    seek $fh, $offset, 0;
    my $data;

  REDO: {
        my $line = <$fh>;

        unless ( defined($line) ) {
            close($fh);
        }
        else {
            chomp $line;
            my ( $type, $entry ) = split / /, $line, 2;
            redo if ( ( $type eq 'd' ) and ( not $self->{dirs} ) );
            $self->_offset( tell $fh );
            close($fh);
            $data =
              [ $entry, File::Spec->catfile( $self->{tardir}, $entry ), $type ];
        }
    }

    return $data;
}

sub _offset {
    my ( $self, $new_offset ) = @_;
    my $offset_file = File::Spec->catfile( $self->{objdir}, "offset" );

    if ( defined $new_offset ) {
        open my $fh, '>', $offset_file or LOGDIE "Can't open $offset_file: $!";
        print $fh "$new_offset\n";
        close $fh;
    }

    open my $fh, '<', $offset_file
      or LOGDIE
"Can't open $offset_file: $! (Did you call list_next() without a previous list_reset()?)";
    my $offset = <$fh>;
    chomp $offset;
    close $fh;
    return $offset;
}

sub write {    ## no critic (ProhibitBuiltinHomonyms)
    my ( $self, $tarfile, $compress ) = @_;

    my $cwd = getcwd();
    chdir $self->{tardir} or LOGDIE "Can't chdir to $self->{tardir}: $!";

    unless ( File::Spec::Functions::file_name_is_absolute($tarfile) ) {
        $tarfile = File::Spec::Functions::rel2abs( $tarfile, $cwd );
    }

    my $compr_opt = '';
    $compr_opt = 'z' if $compress;

    opendir( my $dir, '.' ) or LOGDIE "Cannot open $self->{tardir}: $!";
    my @top_entries = readdir($dir);
    closedir($dir);

    $self->_rem_dots( \@top_entries );

    my $cmd = [
        $self->{tar}, "${compr_opt}cf$self->{tar_write_options}",
        $tarfile,     @{ $self->{tar_gnu_write_options} }
    ];

    if ( @top_entries > $self->{max_cmd_line_args} ) {
        my $filelist_file = $self->{tmpdir} . "/file-list";
        open( my $fh, '>', $filelist_file )
          or LOGDIE "Cannot write to $filelist_file: $!";

        for my $entry (@top_entries) {
            print $fh "$entry\n";
        }

        close($fh);
        push @$cmd, "-T", $filelist_file;
    }
    else {
        push @$cmd, @top_entries;
    }

    $logger->debug("Running @$cmd") if ( $logger->is_debug );
    my $rc = run( $cmd, \my ( $in, $out, $err ) );

    unless ($rc) {
        ERROR "@$cmd failed: $err";
        chdir $cwd or LOGDIE "Cannot chdir to $cwd";
        return;
    }

    WARN $err if $err;
    chdir $cwd or LOGDIE "Cannot chdir to $cwd";
    return 1;
}

sub _rem_dots {
    my ( $self, $entries_ref ) = @_;
    my ( $first, $second ); ## no critic (NamingConventions::ProhibitAmbiguousNames)
    my $index = 0;
    my $found = 0;

    for ( @{$entries_ref} ) {

        if (    ( length($_) <= 2 )
            and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) )
        {
            if ( $found < 1 ) {
                $first = $index;
                $found++;
                $index++;
                next;
            }
            else {
                $second = $index;
                last;
            }

        }
        else {
            $index++;
        }
    }

    splice( @{$entries_ref}, $first, 1 );

    # array length is now shortened by one
    splice( @{$entries_ref}, ( $second - 1 ), 1 );
    return 1;

}

sub DESTROY {
    my ($self) = @_;
    $self->ramdisk_unmount()  if defined $self->{ramdisk};
    rmtree( $self->{objdir} ) if defined $self->{objdir};
    rmtree( $self->{tmpdir} ) if defined $self->{tmpdir};
    return 1;
}

sub is_gnu {
    return shift->{is_gnu};
}

sub is_bsd {
    return shift->{is_bsd};
}

sub ramdisk_mount {
    my ( $self, %options ) = @_;

    # mkdir -p /mnt/myramdisk
    # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk

    $self->{mount}  = which("mount")  unless $self->{mount};
    $self->{umount} = which("umount") unless $self->{umount};

    for (qw(mount umount)) {
        unless ( defined $self->{$_} ) {
            LOGWARN "No $_ command found in PATH";
            return;
        }
    }

    $self->{ramdisk} = {%options};
    $self->{ramdisk}->{size} = "100m"
      unless defined $self->{ramdisk}->{size};

    if ( !defined $self->{ramdisk}->{tmpdir} ) {
        $self->{ramdisk}->{tmpdir} = tempdir( CLEANUP => 1 );
    }

    my @cmd = (
        $self->{mount}, "-t", "tmpfs", "-o", "size=$self->{ramdisk}->{size}",
        "tmpfs",        $self->{ramdisk}->{tmpdir}
    );

    INFO "Mounting ramdisk: @cmd";
    my $rc = system(@cmd);

    if ($rc) {

        if ( $logger->is_info ) {
            $logger->info("Mount command '@cmd' failed: $?");
            $logger->info('Note that this only works on Linux and as root');
        }
        return;
    }

    $self->{ramdisk}->{mounted} = 1;
    return 1;
}

sub ramdisk_unmount {
    my ($self) = @_;

    return unless ( exists $self->{ramdisk}->{mounted} );
    my @cmd = ( $self->{umount}, $self->{ramdisk}->{tmpdir} );
    $logger->info("Unmounting ramdisk: @cmd") if ( $logger->is_info );
    my $rc = system(@cmd);

    if ($rc) {
        LOGWARN "Unmount command '@cmd' failed: $?";
        return;
    }

    delete $self->{ramdisk};
    return 1;
}

1;

__END__

=pod

=encoding UTF-8

=for :stopwords Mike Schilli Alceu Rodrigues de Freitas Junior MERCHANTABILITY Okopnik
Unmounts untarring tardir

=head1 NAME

Archive::Tar::Wrapper - API wrapper around the 'tar' utility

=head1 SYNOPSIS

    use Archive::Tar::Wrapper;

    my $arch = Archive::Tar::Wrapper->new();

    # Open a tarball, expand it into a temporary directory
    $arch->read("archive.tgz");

    # Iterate over all entries in the archive
    $arch->list_reset(); # Reset Iterator

    # Iterate through archive
    while(my $entry = $arch->list_next()) {
        my($tar_path, $phys_path) = @$entry;
        print "$tar_path\n";
    }

    # Get a huge list with all entries
    for my $entry (@{$arch->list_all()}) {
        my($tar_path, $real_path) = @$entry;
        print "Tarpath: $tar_path Tempfile: $real_path\n";
    }

    # Add a new entry
    $arch->add($logic_path, $file_or_stringref);

    # Remove an entry
    $arch->remove($logic_path);

    # Find the physical location of a temporary file
    my($tmp_path) = $arch->locate($tar_path);

    # Create a tarball
    $arch->write($tarfile, $compress);

=head1 DESCRIPTION

B<Archive::Tar::Wrapper> is an API wrapper around the C<tar> command line
program. It never stores anything in memory, but works on temporary
directory structures on disk instead. It provides a mapping between
the logical paths in the tarball and the 'real' files in the temporary
directory on disk.

It differs from L<Archive::Tar> in two ways:

=over 4

=item *

B<Archive::Tar::Wrapper> almost doesn't hold anything in memory (see C<write> method),
instead using disk as storage.

=item *

B<Archive::Tar::Wrapper> is 100% compliant with the platform's C<tar>
utility because it uses it internally.

=back

=head1 METHODS

=head2 new

    my $arch = Archive::Tar::Wrapper->new();

Constructor for the C<tar> wrapper class. Finds the C<tar> executable
by searching C<PATH> and returning the first hit. In case you want
to use a different tar executable, you can specify it as a parameter:

    my $arch = Archive::Tar::Wrapper->new(tar => '/path/to/tar');

Since B<Archive::Tar::Wrapper> creates temporary directories to store
C<tar> data, the location of the temporary directory can be specified:

    my $arch = Archive::Tar::Wrapper->new(tmpdir => '/path/to/tmpdir');

Tremendous performance increases can be achieved if the temporary
directory is located on a RAM disk. Check the L<Archive::Tar::Wrapper/Using RAM Disks>
section for details.

Additional options can be passed to the C<tar> command by using the
C<tar_read_options> and C<tar_write_options> parameters. Example:

     my $arch = Archive::Tar::Wrapper->new(
                   tar_read_options => 'p'
                );

will use C<tar xfp archive.tgz> to extract the tarball instead of just
C<tar xf archive.tgz>. GNU tar supports even more options, these can
be passed in via

     my $arch = Archive::Tar::Wrapper->new(
                    tar_gnu_read_options => ["--numeric-owner"],
                );

Similarly, C<tar_gnu_write_options> can be used to provide additional
options for GNU tar implementations. For example, the tar object

    my $tar = Archive::Tar::Wrapper->new(
                  tar_gnu_write_options => ["--exclude=foo"],
              );

will call the C<tar> utility internally like

    tar cf tarfile --exclude=foo ...

when the C<write> method gets called.

By default, the C<list_*()> functions will return only file entries:
directories will be suppressed. To have C<list_*()> return directories
as well, use

     my $arch = Archive::Tar::Wrapper->new(
                   dirs  => 1
                );

If more files are added to a tarball than the command line can handle,
B<Archive::Tar::Wrapper> will switch from using the command

    tar cfv tarfile file1 file2 file3 ...

to

    tar cfv tarfile -T filelist

where C<filelist> is a file containing all file to be added. The default
for this switch is 512, but it can be changed by setting the parameter
C<max_cmd_line_args>:

     my $arch = Archive::Tar::Wrapper->new(
         max_cmd_line_args  => 1024
     );

The allowable parameters are:

=over

=item *

C<tar>

=item *

C<tmpdir>

=item *

C<tar_read_options>

=item *

C<tar_write_options>

=item *

C<tar_gnu_read_options>

=item *

C<tar_gnu_write_options>

=item *

C<max_cmd_line_args>: defaults to 512

=item *

C<ramdisk>

=back

Returns a new instance of the class.

=head2 read

    $arch->read("archive.tgz");

C<read()> opens the given tarball, expands it into a temporary directory
and returns 1 on success or C<undef> on failure.
The temporary directory holding the tar data gets cleaned up when C<$arch>
goes out of scope.

C<read> handles both compressed and uncompressed files. To find out if
a file is compressed or uncompressed, it tries to guess by extension,
then by checking the first couple of bytes in the tar file.

If only a limited number of files is needed from a tarball, they
can be specified after the tarball name:

    $arch->read("archive.tgz", "path/file.dat", "path/sub/another.txt");

The file names are passed unmodified to the C<tar> command, make sure
that the file paths match exactly what's in the tarball, otherwise
C<read()> will fail.

=head2 list_reset

    $arch->list_reset()

Resets the list iterator. To be used before the first call to C<list_next()>.

=head2 tardir

    $arch->tardir();

Return the directory the tarball was unpacked in. This is sometimes useful
to play dirty tricks on B<Archive::Tar::Wrapper> by mass-manipulating
unpacked files before wrapping them back up into the tarball.

=head2 is_compressed

Returns a string to identify if the tarball is compressed or not.

Expect as parameter a string with the path to the tarball.

Returns:

=over

=item *

a "z" character if the file is compressed with C<gzip>.

=item *

a "j" character if the file is compressed with C<bzip2>.

=item *

a "" character if the file is not compressed at all.

=back

=head2 locate

    $arch->locate($logic_path);

Finds the physical location of a file, specified by C<$logic_path>, which
is the virtual path of the file within the tarball. Returns a path to
the temporary file B<Archive::Tar::Wrapper> created to manipulate the
tarball on disk.

=head2 add

    $arch->add($logic_path, $file_or_stringref, [$options]);

Add a new file to the tarball. C<$logic_path> is the virtual path
of the file within the tarball. C<$file_or_stringref> is either
a scalar, in which case it holds the physical path of a file
on disk to be transferred (i.e. copied) to the tarball, or it is
a reference to a scalar, in which case its content is interpreted
to be the data of the file.

If no additional parameters are given, permissions and user/group
id settings of a file to be added are copied. If you want different
settings, specify them in the options hash:

    $arch->add($logic_path, $stringref,
               { perm => 0755, uid => 123, gid => 10 });

If $file_or_stringref is a reference to a Unicode string, the C<binmode>
option has to be set to make sure the string gets written as proper UTF-8
into the tar file:

    $arch->add($logic_path, $stringref, { binmode => ":utf8" });

=head2 perm_cp

Copies the permissions from a file to another.

Expects as parameters:

=over

=item 1.

string of the path to the file which permissions will be copied from.

=item 2.

string of the path to the file which permissions will be copied to.

=back

Returns 1 if everything works as expected.

=head2 perm_get

Gets the permissions from a file.

Expects as parameter the path to the source file.

Returns an array reference with only the permissions values, as returned by C<stat>.

=head2 perm_set

Sets the permission on a file.

Expects as parameters:

=over

=item 1.

The path to the file where the permissions should be applied to.

=item 2.

An array reference with the permissions (see C<perm_set>)

=back

Returns 1 if everything goes fine.

Ignore errors here, as we can't change uid/gid unless we're the superuser (see LIMITATIONS section).

=head2 remove

    $arch->remove($logic_path);

Removes a file from the tarball. C<$logic_path> is the virtual path
of the file within the tarball.

=head2 list_all

    my $items = $arch->list_all();

Returns a reference to a (possibly huge) array of items in the
tar file. Each item is a reference to an array, containing two
elements: the relative path of the item in the tar file and the
physical path to the unpacked file or directory on disk.

To iterate over the list, the following construct can be used:

    # Get a huge list with all entries
    for my $entry (@{$arch->list_all()}) {
        my($tar_path, $real_path) = @$entry;
        print "Tarpath: $tar_path Tempfile: $real_path\n";
    }

If the list of items in the tar file is big, use C<list_reset()> and
C<list_next()> instead of C<list_all>.

=head2 list_next

    my ($tar_path, $phys_path, $type) = $arch->list_next();

Returns the next item in the tar file. It returns a list of three scalars:
the relative path of the item in the tar file, the physical path
to the unpacked file or directory on disk, and the type of the entry
(f=file, d=directory, l=symlink). Note that by default,
B<Archive::Tar::Wrapper> won't display directories, unless the C<dirs>
parameter is set when running the constructor.

=head2 write

    $arch->write($tarfile, $compress);

Write out the tarball by tarring up all temporary files and directories
and store it in C<$tarfile> on disk. If C<$compress> holds a true value,
compression is used.

=head2 is_gnu

    $arch->is_gnu();

Checks if the tar executable is a GNU tar by running 'tar --version'
and parsing the output for "GNU".

Returns true or false (in Perl terms).

=head2 is_bsd

    $arch->is_bsd();

Same as C<is_gnu()>, but for BSD.

=head2 ramdisk_mount

Mounts a RAM disk.

It executes the C<mount> program under the hood to mount a RAM disk.

Expects as parameter a hash with options to mount the RAM disk, like:

=over

=item *

C<size>

=item *

C<type> (most probably C<tmpfs>)

=item *

C<tmpdir>

=back

Returns 1 if everything goes fine.

Be sure to check the L<Archive::Tar::Wrapper/Using RAM Disks> for full details on using RAM disks.

=head2 ramdisk_unmount

Unmounts the RAM disk already mounted with C<ramdisk_mount>.

Don't expect parameters and returns 1 if everything goes fine.

Be sure to check the L<Archive::Tar::Wrapper/Using RAM Disks> for full details on using RAM disks.

=head1 Using RAM Disks

On Linux, it's quite easy to create a RAM disk and achieve tremendous
speedups while untarring or modifying a tarball. You can either
create the RAM disk by hand by running

   # mkdir -p /mnt/myramdisk
   # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk

and then feeding the RAM disk as a temporary directory to
B<Archive::Tar::Wrapper>, like

   my $tar = Archive::Tar::Wrapper->new( tmpdir => '/mnt/myramdisk' );

or using B<Archive::Tar::Wrapper>'s built-in option C<ramdisk>:

   my $tar = Archive::Tar::Wrapper->new(
       ramdisk => {
           type => 'tmpfs',
           size => '20m',   # 20 MB
       },
   );

Only drawback with the latter option is that creating the RAM disk needs
to be performed as root, which often isn't desirable for security reasons.
For this reason, B<Archive::Tar::Wrapper> offers a utility functions that
mounts the RAM disk and returns the temporary directory it's located in:

    # Create new ramdisk (as root):
    my $tmpdir = Archive::Tar::Wrapper->ramdisk_mount(
        type => 'tmpfs',
        size => '20m',   # 20 MB
    );

    # Delete a ramdisk (as root):
    Archive::Tar::Wrapper->ramdisk_unmount();

Optionally, the C<ramdisk_mount()> command accepts a C<tmpdir> parameter
pointing to a temporary directory for the RAM disk if you wish to set it
yourself instead of letting B<Archive::Tar::Wrapper> create it automatically.

=head1 KNOWN LIMITATIONS

=over

=item *

Currently, only C<tar> programs supporting the C<z> option (for
compressing/decompressing) are supported. Future version will use
C<gzip> alternatively.

=item *

Currently, you can't add empty directories to a tarball directly.
You could add a temporary file within a directory, and then
C<remove()> the file.

=item *

If you delete a file, the empty directories it was located in
stay in the tarball. You could try to C<locate()> them and delete
them. This will be fixed, though.

=item *

Filenames containing newlines are causing problems with the list
iterators. To be fixed.

=item *

If you ask B<Archive::Tar::Wrapper> to add a file to a tarball, it copies it
into a temporary directory and then calls the system tar to wrap up that
directory into a tarball.

This approach has limitations when it comes to file permissions: If the file to
be added belongs to a different user/group, B<Archive::Tar::Wrapper> will adjust
the uid/gid/permissions of the target file in the temporary directory to
reflect the original file's settings, to make sure the system tar will add it
like that to the tarball, just like a regular tar run on the original file
would. But this will fail of course if the original file's uid is different
from the current user's, unless the script is running with superuser rights.
The tar program by itself (without B<Archive::Tar::Wrapper>) works differently:
It'll just make a note of a file's uid/gid/permissions in the tarball (which it
can do without superuser rights) and upon extraction, it'll adjust the
permissions of newly generated files if the -p option is given (default for
superuser).

=back

=head1 BUGS

B<Archive::Tar::Wrapper> doesn't currently handle filenames with embedded
newlines.

=head2 Microsoft Windows support

Support on Microsoft Windows is limited.

Versions below Windows 10 will not be supported for desktops, and for servers
only Windows 2012 and above.

The GNU C<tar.exe> program doesn't work properly with the current interface of
B<Archive::Tar::Wrapper>.

You must use the C<bsdtar.exe> and make sure it appears first in the C<PATH>
environment variable than the GNU tar (if it is installed). See
L<http://libarchive.org/> for details about how to download and
install C<bsdtar.exe>, or go to L<http://gnuwin32.sourceforge.net/packages.html>
for a direct download. Be sure to look for the C<bzip2> program package to
install it as well.

Windows 10 might come already with C<bsdtar> program already installed. Please
search for that on the appropriate page (Microsoft keeps changing the link to
keep track of it here).

Having spaces in the path string to the tar program might be an issue too.
Although there is some effort in terms of workaround it, you best might avoid it
completely by installing in a different path than C<C:\Program Files>.
Installing both C<bsdtar> and C<bzip2> in C<C:\GnuWin32> will probably be enough
when running the installers.

=head1 SEE ALSO

=over

=item *

Linux Gazette article from Ben Okopnik, L<issue 87|https://linuxgazette.net/87/okopnik.html>.

=back

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/haarg/Archive-Tar-Wrapper/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHORS

=over 4

=item *

Mike Schilli <cpan@perlmeister.com>

=item *

Alceu Rodrigues de Freitas Junior <glasswalk3r@yahoo.com.br>

=back

=head1 CONTRIBUTORS

=for stopwords Chris Weyl David Cantrell Precious Graham Knop intrigeri Kent Fredric Mark Gardner Mike Schilli Mohammad S Anwar Paulo Custodio Randy Stauner Sanko Robinson Shoichi Kaji

=over 4

=item *

Chris Weyl <cweyl@alumni.drew.edu>

=item *

David Cantrell <david@cantrell.org.uk>

=item *

David Precious <davidp@preshweb.co.uk>

=item *

Graham Knop <haarg@haarg.org>

=item *

intrigeri <intrigeri@boum.org>

=item *

Kent Fredric <kentfredric@gmail.com>

=item *

Mark Gardner <mjg+github@phoenixtrap.com>

=item *

Mike Schilli <github@perlmeister.com>

=item *

Mohammad S Anwar <mohammad.anwar@yahoo.com>

=item *

Paulo Custodio <pauloscustodio@gmail.com>

=item *

Randy Stauner <randy@magnificent-tears.com>

=item *

Sanko Robinson <sanko@cpan.org>

=item *

Shoichi Kaji <skaji@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2024 by Mike Schilli.

This is free software, licensed under:

  The GNU General Public License, Version 3, June 2007

=cut