-
Notifications
You must be signed in to change notification settings - Fork 7
/
atool
executable file
·1990 lines (1893 loc) · 76.7 KB
/
atool
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
#!/usr/bin/env perl
#
# atool - A script for managing file archives of various types.
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008,
# 2009, 2011, 2012 Oskar Liljeblad
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See the atool(1) manual page for usage details.
#
# This file uses tab stops with a length of two.
#
# XXX: We could use -CLSDA but 5.10.0 has a bug which prevents us from
# specifying this with shebang. Thanks to some helpful dude on #perl
# FreeNode.
if (${^UTF8LOCALE}) {
use Encode qw(decode_utf8);
binmode($_, ':encoding(UTF-8)') for \*STDIN, \*STDOUT, \*STDERR;
$_ = decode_utf8($_) for @ARGV, values %ENV;
}
use File::Basename;
use File::Spec;
use Getopt::Long;
use POSIX;
use locale;
use strict;
# Subroutine prototypes (needed for perl 5.6)
sub runcmds($$$;@);
sub getmode();
sub multiarchivecmd($$$$@);
sub singlearchivecmd($$$$$@);
sub maketarcmd($$$$@);
sub cmdexec($@);
sub parsefmt($$);
sub makeoutdir();
sub makeoutfile($);
sub explain($);
sub extract(@);
sub shquotemeta($);
sub tailslash($);
sub de($);
sub makespec(@);
sub backticks(@);
sub readconfig($$);
sub formatext($);
sub stripext($);
sub findformat($$);
sub unlink_directory($);
sub find_comparable_file($);
sub makeabsolute($);
sub quote($);
sub shell_execute(@);
sub save_outdir($);
sub handle_empty_add(@);
sub issingleformat($);
sub repack_archive($$$$);
sub set_config_option($$$);
$::SYSCONFDIR = '/usr/local/etc'; # This line is automatically updated by make
$::PACKAGE = 'atool'; # This line is automatically updated by make
$::VERSION = '0.39.0-epihax'; # This line is automatically updated by make
$::BUG_EMAIL = '[email protected]'; # This line is automatically updated by make
$::PROGRAM = $::PACKAGE;
# Configuration options and their built-in defaults
$::cfg_args_diff = '-ru'; # arguments to pass to diff program
$::cfg_decompress_to_cwd = 1; # decompress to current directory
$::cfg_default_verbosity = 1; # default verbosity level
$::cfg_extract_deb_control = 1; # extract DEBIAN control dir from .deb packages?
$::cfg_keep_compressed = 1; # keep compressed file after pack/unpack
$::cfg_path_7z = '7z'; # 7z program
$::cfg_path_ar = 'ar'; # ar program
$::cfg_path_arc = 'arc'; # arc program
$::cfg_path_arj = 'arj'; # arj program
$::cfg_path_bzip = 'bzip'; # bzip program
$::cfg_path_bzip2 = 'bzip2'; # bzip2 program
$::cfg_path_cabextract = 'cabextract'; # cabextract program
$::cfg_path_cat = 'cat'; # cat program
$::cfg_path_compress = 'compress'; # compress program
$::cfg_path_cpio = 'cpio'; # cpio program
$::cfg_path_diff = 'diff'; # diff program
$::cfg_path_dpkg_deb = 'dpkg-deb'; # dpkg-deb program
$::cfg_path_file = 'file'; # file program
$::cfg_path_find = 'find'; # find program
$::cfg_path_gzip = 'gzip'; # gzip program
$::cfg_path_jar = 'jar'; # jar program
$::cfg_path_lbzip2 = 'lbzip2'; # lbzip2 program
$::cfg_path_lha = 'lha'; # lha program
$::cfg_path_lrzip = 'lrzip'; # lrzip program
$::cfg_path_lzip = 'lzip'; # lzip program
$::cfg_path_lzma = 'lzma'; # lzma program
$::cfg_path_lzop = 'lzop'; # lzop program
$::cfg_path_nomarch = 'nomarch'; # nomarch program
$::cfg_path_pager = 'pager'; # pager program
$::cfg_path_pbzip2 = 'pbzip2'; # pbzip2 program
$::cfg_path_pigz = 'pigz'; # pigz program
$::cfg_path_plzip = 'plzip'; # plzip program
$::cfg_path_rar = 'rar'; # rar program
$::cfg_path_rpm = 'rpm'; # rpm program
$::cfg_path_rpm2cpio = 'rpm2cpio'; # rpm2cpio program
$::cfg_path_rzip = 'rzip'; # rzip program
$::cfg_path_syscfg = File::Spec->catfile($::SYSCONFDIR, $::PROGRAM.'.conf'); # system-wide configuration file
$::cfg_path_tar = 'bsdtar'; # tar program
$::cfg_path_unace = 'unace'; # unace program
$::cfg_path_unalz = 'unalz'; # unalz program
$::cfg_path_unarj = 'unarj'; # unarj program
$::cfg_path_unrar = 'unrar'; # unrar program
$::cfg_path_unzip = 'unzip'; # unzip program
$::cfg_path_usercfg = '.'.$::PROGRAM.'rc'; # user configuration file
$::cfg_path_xargs = 'xargs'; # xargs program
$::cfg_path_xz = 'xz'; # xz program
$::cfg_path_zip = 'zip'; # zip program
$::cfg_show_extracted = 1; # always show extracted file/directory
$::cfg_strip_unknown_ext = 1; # strip unknown extensions
$::cfg_tmpdir_name = 'Unpack-%04d'; # extraction directory name
$::cfg_tmpfile_name = 'Pack-%04d'; # temporary file used during packing
$::cfg_use_arc_for_unpack = 0; # use arc to unpack arc files?
$::cfg_use_arj_for_unpack = 0; # use arj to unpack arj files?
$::cfg_use_file = 1; # use file(1) for unknown extensions?
$::cfg_use_file_always = 0; # always use file to identify archives (ignore extension)
$::cfg_use_find_cpio_print0 = 1; # use -print0/-0 find/cpio options?
$::cfg_use_gzip_for_z = 1; # use gzip to decompress .Z files?
$::cfg_use_jar = 0; # use jar or zip for .jar archives?
$::cfg_use_lbzip2 = 1; # use lbzip2 instead of bzip2
$::cfg_use_pbzip2 = 1; # use pbzip2 instead of bzip2
$::cfg_use_pigz = 1; # use pigz instead of gzip
$::cfg_use_plzip = 0; # use plzip instead of lzip
$::cfg_use_rar_for_unpack = 0; # use rar to unpack rar files?
$::cfg_use_tar_bzip2_option = 1; # does tar support --bzip2?
$::cfg_use_tar_lzma_option = 1; # does tar support --lzma?
$::cfg_use_tar_lzip_option = 0; # does tar support --lzip?
$::cfg_use_tar_lzop_option = 0; # does tar support --lzop?
$::cfg_use_tar_xz_option = 0; # does tar support --xz?
$::cfg_use_tar_z_option = 1; # does tar support -z?
# Global variables
$::basename = quote(File::Basename::basename($0));
@::rmdirs = ();
$::up = File::Spec->updir();
$::cur = File::Spec->curdir();
@::opt_options = ();
@::opt_format_options = ();
# Parse arguments
Getopt::Long::config('bundling');
Getopt::Long::GetOptions(
'l|list' => \$::opt_cmd_list,
'x|extract' => \$::opt_cmd_extract,
'X|extract-to=s' => \$::opt_cmd_extract_to,
'a|add' => \$::opt_cmd_add,
'c|cat' => \$::opt_cmd_cat,
'd|diff' => \$::opt_cmd_diff,
'r|repack' => \$::opt_cmd_repack,
'q|quiet' => sub { $::opt_verbosity--; },
'v|verbose' => sub { $::opt_verbosity++; },
'V|verbosity=i' => \$::opt_verbosity,
'config=s' => \$::opt_config,
'o|option=s' => sub { push @::opt_options, $_[1] },
'help' => \$::opt_cmd_help,
'version' => \$::opt_cmd_version,
'F|format=s' => \$::opt_format,
'O|format-option=s' => sub { push @::opt_format_options, $_[1] },
'f|force' => \$::opt_force,
'p|page' => \$::opt_use_pager,
'e|each' => \$::opt_each,
'E|explain' => \$::opt_explain,
'S|simulate' => \$::opt_simulate,
'save-outdir=s' => \$::opt_save_outdir,
'D|subdir' => \$::opt_extract_subdir,
'0|null' => \$::opt_null,
) or exit 1;
# Display --version
if ($::opt_cmd_version) {
print $::PACKAGE.' '.$::VERSION."\
Copyright (C) 2011 Oskar Liljeblad\
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
Written by Oskar Liljeblad.\n";
exit;
}
# Display --help
if ($::opt_cmd_help) {
print <<_END_;
Usage: $::PROGRAM [OPTION]... ARCHIVE [FILE]...
$::PROGRAM -e [OPTION]... [ARCHIVE]...
Manage file archives of various types.
Commands:
-l, --list list files in archive (als)
-x, --extract extract files from archive (aunpack)
-X, --extract-to=PATH extract archive to specified directory
-a, --add create archive (apack)
-c, --cat extract file to standard out (acat)
-d, --diff generate a diff between two archives (adiff)
-r, --repack repack archives to a different format (arepack)
--help display this help and exit
--version output version information and exit
Options:
-e, --each execute command above for each file specified
-F, --format=EXT override archive format (see below)
-O, --format-option=OPT give specific options to the archiver
-D, --subdir always create subdirectory when extracting
-f, --force allow overwriting of local files
-q, --quiet decrease verbosity level by one
-v, --verbose increase verbosity level by one
-V, --verbosity=LEVEL specify verbosity (0, 1 or 2)
-p, --page send output through pager
-0, --null filenames from standard in are null-byte separated
-E, --explain explain what is being done by $::PROGRAM
-S, --simulate simulation mode - no filesystem changes are made
-o, --option=KEY=VALUE override a configuration option
--config=FILE load configuration defaults from file
Archive format (for --format) may be specified either as a
file extension ("tar.gz") or as "tar+gzip".
Report bugs to Oskar Liljeblad <$::BUG_EMAIL>.
_END_
exit;
}
# Read configuration files
if (defined $::opt_config) {
readconfig($::opt_config, 0);
} else {
readconfig($::cfg_path_syscfg, 1);
if ($::cfg_path_usercfg !~ /^\//) {
readconfig(File::Spec->catfile($ENV{HOME}, $::cfg_path_usercfg), 1);
} else {
readconfig($::cfg_path_usercfg, 1);
}
}
foreach my $opt (@::opt_options) {
my ($var,$val) = ($opt =~ /^([^=]+)=(.*)$/);
die "$::basename: invalid value for --option: $opt\n" if !defined $val;
set_config_option($var, $val, '');
}
# Verify option integrity
$::opt_verbosity += $::cfg_default_verbosity;
if ($::opt_explain && $::opt_simulate) {
die "$::basename: --explain and --simulate options are mutually exclusive\n"; #OK
}
my $mode = getmode();
if (defined $::opt_save_outdir && $mode eq 'extract-to') {
die "$::basename: --save-outdir cannot be used in extract-to mode\n";
}
if ($::opt_extract_subdir && $mode ne 'extract') {
die "$::basename: --subdir can only be used in extract mode\n";
}
if ($mode eq 'diff') {
die "$::basename: missing archive argument\n" if (@ARGV < 2); #OK
my $use_pager = $::opt_use_pager;
$::opt_verbosity--;
$::opt_use_pager = 0;
my $outfile1 = makeoutdir() || exit 1;
my $outfile2 = makeoutdir() || exit 1;
$::opt_cmd_extract_to = $outfile1;
$::opt_cmd_extract_to_type = 'f';
exit 1 if (!runcmds('extract-to', undef, $ARGV[0]));
$::opt_cmd_extract_to = $outfile2;
$::opt_cmd_extract_to_type = 'f';
exit 1 if (!runcmds('extract-to', undef, $ARGV[1]));
my $match1 = find_comparable_file($outfile1);
my $match2 = find_comparable_file($outfile2);
my @cmd = ($::cfg_path_diff, split(/ /, $::cfg_args_diff), $match1, $match2);
push @cmd, ['|'], get_pager_program() if $use_pager;
my $allok = cmdexec(1, @cmd);
foreach my $file ($outfile1,$outfile2) {
warn 'rm -r ',quote($file),"\n" if $::opt_simulate;
if (-e $file && -d $file) {
#if (-e $file) {
#print "$::basename: remove `$file'? ";
#select((select(STDOUT), $| = 1)[0]);
#my $line = <STDIN>;
#if (defined $line && $line =~ /^y/) {
#if (-d $file) {
warn 'rm -r ',quote($file),"\n" if $::opt_explain;
unlink_directory($file) if !$::opt_simulate;
#} else {
#unlink $file;
#}
#}
}
}
exit ($allok ? 0 : 1);
}
elsif ($mode eq 'repack') {
if ($::opt_each) {
my $totaldiff = 0;
if (!defined $::opt_format) {
die "$::basename: specify a format with -F when using --each in repack mode\n";
}
my $fmt2 = findformat($::opt_format, 1);
exit 1 if !defined $fmt2; # OK
for (my $c = 0; $c < @ARGV; $c++) {
my $fmt1 = findformat($ARGV[$c], 0);
next if !defined $fmt1;
if (!issingleformat($fmt1) && issingleformat($fmt2)) {
warn "$::basename: format $fmt1 is cannot be repacked into format $fmt2\n";
warn "skipping ", quote($ARGV[$c]), "\n";
next;
}
if ($fmt1 eq $fmt2) {
warn "$::basename: will not repack to same archive type\n";
warn "skipping ", quote($ARGV[$c]), "\n";
next;
}
my $newname = stripext($ARGV[$c]).formatext($fmt2);
if (-e $newname) {
warn "$::basename: ".quote($newname).": destination file exists\n";
warn "skipping ", quote($ARGV[$c]), "\n";
next;
}
repack_archive($ARGV[$c], $newname, $fmt1, $fmt2);
my $diff = $::opt_simulate ? 0 : (-s $ARGV[$c]) - (-s $newname);
$totaldiff += $diff;
if ($::opt_verbosity >= 1) {
print quote($newname), ': ',
($diff >= 0 ? 'saved '.$diff : 'grew '.-$diff),' ',
($diff == 1 ? 'byte':'bytes'), "\n";
}
}
if ($::opt_verbosity >= 1) {
print $totaldiff >= 0 ? 'saved '.$totaldiff : 'grew '.-$totaldiff, ' ',
$totaldiff == 1 ? 'byte':'bytes', " in total\n";
}
} else {
die "$::basename: missing archive arguments\n" if @ARGV < 1; #OK
die "$::basename: missing archive argument\n" if @ARGV < 2; #OK
die "$::basename: will not repack to same archive file\n"
if ($ARGV[0] eq $ARGV[1] || File::Spec->canonpath($ARGV[0]) eq File::Spec->canonpath($ARGV[1]));
die "$::basename: ".quote($ARGV[1]).": destination file exists\n" if -e $ARGV[1];
my $fmt1 = findformat($ARGV[0], 0);
my $fmt2 = findformat($ARGV[1], 0);
exit 1 if !defined $fmt1 || !defined $fmt2; # OK
die "$::basename: format $fmt1 is cannot be repacked into format $fmt1\n"
if (!issingleformat($fmt1) && issingleformat($fmt2));
die "$::basename: will not repack to same archive type\n" if $fmt1 eq $fmt2;
repack_archive($ARGV[0], $ARGV[1], $fmt1, $fmt2);
my $diff = ($::opt_simulate ? 0 : (-s $ARGV[0]) - (-s $ARGV[1]));
if ($::opt_verbosity >= 1) {
print quote($ARGV[1]), ': ',
($diff >= 0 ? 'saved '.$diff : 'grew '.-$diff),' ',
($diff == 1 ? 'byte':'bytes'), "\n";
}
}
}
elsif ($::opt_each) {
my $allok = 1;
if ($mode eq 'cat') {
die "$::basename: --each can not be used with cat or add command\n"; #OK
}
if ($mode eq 'add') {
if (!defined $::opt_format) {
die "$::basename: specify a format with -F when using --each in add mode\n";
}
my $format = findformat($::opt_format, 1);
exit 1 if !defined $format;
for (my $c = 0; $c < @ARGV; $c++) {
my $archive = File::Spec->canonpath($ARGV[$c]) . formatext($format);
warn quote($archive).":\n" if $::opt_verbosity > 1;
runcmds('add', $format, $archive, $ARGV[$c]) or $allok = 0;
}
} else {
for (my $c = 0; $c < @ARGV; $c++) {
warn quote($ARGV[$c]).":\n" if $::opt_verbosity > 1;
runcmds($mode, undef, $ARGV[$c]) or $allok = 0;
}
}
exit ($allok ? 0 : 1);
}
else {
die "$::basename: missing archive argument\n" if (@ARGV == 0); #OK
runcmds($mode, undef, shift @ARGV, @ARGV) || exit 1;
}
# runcmds(mode, format, archive, args)
# Execute an atool command. This is where it all happens.
# If mode is 'extract', returns the directory (or only file)
# which was extracted.
# If forceformat is undef, the format will be detected from
# $::opt_format or the filename.
sub runcmds($$$;@) {
my ($mode, $format, $archive, @args) = @_;
if (!defined $format) {
if (defined $::opt_format) {
$format = findformat($::opt_format, 1);
} else {
$format = findformat($archive, 0);
}
return undef if !defined $format;
}
my @cmd;
my $outdir;
if ($format eq 'tar+bzip2') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_bzip2_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'f', '--bzip2'), @args;
} elsif ($::cfg_use_pbzip2) {
push @cmd, $::cfg_path_pbzip2, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_pbzip2, '-c', ['>'], $archive if $mode eq 'add';
#if ($mode eq 'add') {
# Unfortunately pbzip2 cannot read from standard in
# 2012-03-15: It seems now it does.
# my $tmpname = makeoutfile($::cfg_tmpfile_name);
# push @cmd, maketarcmd($tmpname, $outdir, $mode, 'f'), @args;
# push @cmd, [';'], $::cfg_path_pbzip2, '-c', $tmpname, ['>'], $archive;
# push @cmd, [';'], 'rm', $tmpname;
#} else {
# push @cmd, $::cfg_path_pbzip2, '-cd', $archive, ['|'];
# push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
#}
} elsif ($::cfg_use_lbzip2) {
push @cmd, $::cfg_path_lbzip2, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_lbzip2, '-c', ['>'], $archive if $mode eq 'add';
} else {
push @cmd, $::cfg_path_bzip2, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_bzip2, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+gzip') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_z_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'zf'), @args;
} elsif ($::cfg_use_pigz) {
push @cmd, $::cfg_path_pigz, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_pigz, '-c', ['>'], $archive if $mode eq 'add';
} else {
push @cmd, $::cfg_path_gzip, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_gzip, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+bzip') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, $::cfg_path_bzip, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_bzip, '-c', ['>'], $archive if $mode eq 'add';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+compress') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_gzip_for_z) {
push @cmd, $::cfg_path_gzip, '-cd', $archive, ['|'] if $mode ne 'add';
} else {
push @cmd, $::cfg_path_compress, '-cd', $archive, ['|'] if $mode ne 'add';
}
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_compress, '-c', ['>'], $archive if $mode eq 'add';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+lzop') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_lzop_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'f', '--lzop'), @args;
} else {
push @cmd, $::cfg_path_lzop, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_lzop, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+lzip') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_lzip_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'f', '--lzip'), @args;
} elsif ($::cfg_use_plzip) {
push @cmd, $::cfg_path_plzip, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_plzip, '-c', ['>'], $archive if $mode eq 'add';
} else {
push @cmd, $::cfg_path_lzip, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_lzip, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+xz') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_xz_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'f', '--xz'), @args;
} else {
push @cmd, $::cfg_path_xz, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_xz, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+7z') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, $::cfg_path_7z, 'x', '-so', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_7z, 'a', '-si', $archive if $mode eq 'add';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar+lzma') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($::cfg_use_tar_lzma_option) {
push @cmd, maketarcmd($archive, $outdir, $mode, 'f', '--lzma'), @args;
} else {
push @cmd, $::cfg_path_lzma, '-cd', $archive, ['|'] if $mode ne 'add';
push @cmd, maketarcmd('-', $outdir, $mode, 'f'), @args;
push @cmd, ['|'], $::cfg_path_lzma, '-c', ['>'], $archive if $mode eq 'add';
}
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'tar') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, maketarcmd($archive, $outdir, $mode, 'f'), @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'jar' && $::cfg_use_jar) {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
my $opts = '';
if ($mode eq 'add') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
$opts .= 'v' if $::opt_verbosity >= 1;
push @cmd, $::cfg_path_jar;
push @cmd, "x$opts", '-C', $outdir if $mode eq 'extract';
push @cmd, "x$opts", '-C', $::opt_cmd_extract_to if $mode eq 'extract-to';
push @cmd, "t$opts" if $mode eq 'list';
push @cmd, "c$opts" if $mode eq 'add';
push @cmd, $archive, @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'jar' || $format eq 'zip') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'add') {
push @cmd, $::cfg_path_zip, '-r';
} else {
push @cmd, $::cfg_path_unzip;
push @cmd, '-p' if $mode eq 'cat';
push @cmd, '-l' if $mode eq 'list';
push @cmd, '-d', $outdir if $mode eq 'extract';
push @cmd, '-d', $::opt_cmd_extract_to if $mode eq 'extract-to';
}
push @cmd, '-v' if $::opt_verbosity > 1;
push @cmd, '-qq' if $::opt_verbosity < 0;
push @cmd, '-q' if $::opt_verbosity == 0;
push @cmd, $archive, @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'rar') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'add' || $::cfg_use_rar_for_unpack) {
push @cmd, $::cfg_path_rar;
} else {
push @cmd, $::cfg_path_unrar;
}
push @cmd, 'a' if $mode eq 'add';
push @cmd, 'vt' if $mode eq 'list' && $::opt_verbosity >= 3;
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, '-ierr', 'p' if $mode eq 'cat';
push @cmd, '-r0' if ($mode eq 'add');
push @cmd, $archive, @args;
push @cmd, tailslash($outdir) if $mode eq 'extract';
push @cmd, tailslash($::opt_cmd_extract_to) if $mode eq 'extract-to';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq '7z') {
# 7z has the -so option for writing data to stdout, but it doesn't
# write data to terminal even if the file is designed to be
# read in a terminal...
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
#if ($mode eq 'cat') {
# warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
# return undef;
#}
push @cmd, $::cfg_path_7z;
push @cmd, 'a' if $mode eq 'add';
push @cmd, 'l' if $mode eq 'list';
push @cmd, 'x', '-so' if $mode eq 'cat';
push @cmd, 'x', '-o'.$outdir if $mode eq 'extract';
push @cmd, 'x', '-o'.$::opt_cmd_extract_to if $mode eq 'extract-to';
push @cmd, @::opt_format_options, $archive, @args;
return multiarchivecmd($archive, $outdir, $mode, 1, 0, \@args, @cmd);
}
elsif ($format eq 'cab') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'add') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
push @cmd, $::cfg_path_cabextract;
push @cmd, '--single';
push @cmd, '--directory', $outdir if $mode eq 'extract';
push @cmd, '--directory', $::opt_cmd_extract_to if $mode eq 'extract-to';
push @cmd, '--pipe' if $mode eq 'cat';
push @cmd, '--list' if $mode eq 'list';
push @cmd, $archive;
push @cmd, '--filter';
push @cmd, @args;
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'alzip') {
if ($mode eq 'cat' || $mode eq 'add' || $mode eq 'list') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, $::cfg_path_unalz;
push @cmd, $archive;
push @cmd, $outdir if $mode eq 'extract';
push @cmd, $::opt_cmd_extract_to if $mode eq 'extract-to';
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'lha') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, $::cfg_path_lha;
push @cmd, 'a' if $mode eq 'add';
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity >= 3;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'lq' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'xw='.tailslash($outdir) if $mode eq 'extract';
push @cmd, 'xw='.tailslash($::opt_cmd_extract_to) if $mode eq 'extract-to';
push @cmd, 'p' if $mode eq 'cat';
push @cmd, $archive, @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'ace') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
push @cmd, $::cfg_path_unace;
if ($mode eq 'add' || $mode eq 'cat') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
push @cmd, 'v', '-c' if $mode eq 'list' && $::opt_verbosity >= 3;
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, $archive, @args;
push @cmd, tailslash($outdir) if $mode eq 'extract';
push @cmd, tailslash($::opt_cmd_extract_to) if $mode eq 'extract-to';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'arj') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'cat') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
if ($mode eq 'add' || $::cfg_use_arj_for_unpack) {
push @cmd, $::cfg_path_arj;
push @cmd, 'a' if $mode eq 'add';
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, $archive, @args;
push @cmd, tailslash($outdir) if $mode eq 'extract';
push @cmd, tailslash($::opt_cmd_extract_to) if $mode eq 'extract-to';
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
} else {
push @cmd, $::cfg_path_unarj;
# XXX: cat mode might work for arj archives, but it extract to stderr!
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, $archive if ($mode ne 'extract' && $mode ne 'extract-to');;
# we call makeabsolute here because needcwd=1 to the multiarchivecmd call
push @cmd, makeabsolute($archive) if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 1, \@args, @cmd);
}
}
elsif ($format eq 'arc') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'add' || $::cfg_use_arc_for_unpack) {
push @cmd, $::cfg_path_arc;
push @cmd, 'a' if $mode eq 'add';
push @cmd, 'v' if $mode eq 'list' && $::opt_verbosity >= 3;
push @cmd, 'l' if $mode eq 'list' && $::opt_verbosity == 2;
push @cmd, 'ln' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, 'x' if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, 'p' if $mode eq 'cat';
} else {
push @cmd, $::cfg_path_nomarch;
push @cmd, '-lvU' if $mode eq 'list' && $::opt_verbosity >= 2;
push @cmd, '-lU' if $mode eq 'list' && $::opt_verbosity <= 1;
push @cmd, '-p' if $mode eq 'cat';
}
push @cmd, $archive if ($mode ne 'extract' && $mode ne 'extract-to');
# we call makeabsolute here because needcwd=1 to the multiarchivecmd call
push @cmd, makeabsolute($archive) if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, @args;
@cmd = handle_empty_add(@cmd) if ($mode eq 'add' && @args == 0);
return multiarchivecmd($archive, $outdir, $mode, 0, 1, \@args, @cmd);
}
elsif ($format eq 'rpm') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'list') {
# push @cmd, $::cfg_path_rpm;
# push @cmd, '-qlp';
# push @cmd, '-v' if $::opt_verbosity >= 1;
# push @cmd, $archive, @args;
# return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
push @cmd, $::cfg_path_rpm2cpio;
push @cmd, makeabsolute($archive);
push @cmd, ['|'];
push @cmd, $::cfg_path_tar, 'tf', '-', @args;
return multiarchivecmd($archive, $outdir, $mode, 0, 1, \@args, @cmd);
}
elsif ($mode eq 'extract' || $mode eq 'extract-to') {
push @cmd, $::cfg_path_rpm2cpio;
push @cmd, makeabsolute($archive);
push @cmd, ['|'];
push @cmd, $::cfg_path_tar, 'xvf', '-', @args;
# push @cmd, $::cfg_path_cpio, '-imd', '--quiet', @args;
return multiarchivecmd($archive, $outdir, $mode, 0, 1, \@args, @cmd);
}
else { # add and cat
# FIXME: I guess cat could work too, but it would require that we
# extracted to a temporary dir, read and printed it, then removed it.
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
}
elsif ($format eq 'deb') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'cat') {
push @cmd, $::cfg_path_dpkg_deb, '--fsys-tarfile', makeabsolute($archive), ['|'];
push @cmd, $::cfg_path_tar, '-xO', @args;
} elsif ($mode eq 'list' || $mode eq 'extract' || $mode eq 'extract-to') {
push @cmd, $::cfg_path_dpkg_deb;
push @cmd, '--contents' if $mode eq 'list';
if ($mode eq 'extract' || $mode eq 'extract-to') {
push @cmd, '--extract' if $::opt_verbosity <= 0;
push @cmd, '--vextract' if $::opt_verbosity > 0;
}
push @cmd, $archive;
push @cmd, $outdir if $mode eq 'extract';
push @cmd, $::opt_cmd_extract_to if $mode eq 'extract-to';
push @cmd, @args;
if ($::cfg_extract_deb_control && ($mode eq 'extract' || $mode eq 'extract-to')) {
push @cmd, [';'];
push @cmd, $::cfg_path_dpkg_deb;
push @cmd, '--control';
push @cmd, $archive;
push @cmd, File::Spec->catdir($outdir, 'DEBIAN') if $mode eq 'extract';
push @cmd, File::Spec->catdir($::opt_cmd_extract_to, 'DEBIAN') if $mode eq 'extract-to';
}
}
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($format eq 'ar') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
my $v = ($::opt_verbosity >= 1 ? 'v' : '');
push @cmd, $::cfg_path_ar;
push @cmd, 'rc'.$v if $mode eq 'add';
push @cmd, 'x'.$v if ($mode eq 'extract' || $mode eq 'extract-to');
push @cmd, 't'.$v if $mode eq 'list';
# Don't use v(erbose) with cat command because ar would add "\n<member data>\n\n" to output
push @cmd, 'p' if $mode eq 'cat';
push @cmd, makeabsolute($archive), @args;
return multiarchivecmd($archive, $outdir, $mode, 1, 1, \@args, @cmd);
}
elsif ($format eq 'cpio') {
return undef if ($mode eq 'extract' && !defined ($outdir = makeoutdir()));
if ($mode eq 'list') {
push @cmd, $::cfg_path_cat, $archive, ['|'];
push @cmd, $::cfg_path_cpio, '-t';
push @cmd, '-v' if $::opt_verbosity >= 1;
return multiarchivecmd($archive, $outdir, $mode, 0, 0, \@args, @cmd);
}
elsif ($mode eq 'extract' || $mode eq 'extract-to') {
push @cmd, $::cfg_path_cat, makeabsolute($archive), ['|'];
push @cmd, $::cfg_path_cpio, '-i';
push @cmd, '-v' if $::opt_verbosity >= 1;
return multiarchivecmd($archive, $outdir, $mode, 0, 1, \@args, @cmd);
}
elsif ($mode eq 'add') {
if (@args == 0) {
push @cmd, $::cfg_path_cpio;
push @cmd, '-0' if $::opt_null;
push @cmd, '-o';
push @cmd, '-v' if $::opt_verbosity >= 1;
push @cmd, ['>'], $archive;
} else {
push @cmd, $::cfg_path_find, @args;
push @cmd, '-print0' if $::cfg_use_find_cpio_print0;
push @cmd, ['|'], $::cfg_path_cpio;
push @cmd, '-0' if $::cfg_use_find_cpio_print0;
push @cmd, '-o';
push @cmd, '-v' if $::opt_verbosity >= 1;
push @cmd, ['>'], $archive;
}
return multiarchivecmd($archive, $outdir, $mode, 1, 1, \@args, @cmd);
}
else { # cat
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
}
elsif ($format eq 'bzip2') {
return singlearchivecmd($archive, $::cfg_path_pbzip2, $format, $mode, 1, @args) if $::cfg_use_pbzip2;
return singlearchivecmd($archive, $::cfg_path_lbzip2, $format, $mode, 1, @args) if $::cfg_use_lbzip2;
return singlearchivecmd($archive, $::cfg_path_bzip2, $format, $mode, 1, @args);
}
elsif ($format eq 'bzip') {
return singlearchivecmd($archive, $::cfg_path_bzip, $format, $mode, 1, @args);
}
elsif ($format eq 'gzip') {
return singlearchivecmd($archive, $::cfg_use_pigz ? $::cfg_path_pigz : $::cfg_path_gzip, $format, $mode, 1, @args);
}
elsif ($format eq 'compress') {
if ($::cfg_use_gzip_for_z && $mode ne 'add') {
return singlearchivecmd($archive, $::cfg_path_gzip, $format, $mode, 1, @args);
} else {
return singlearchivecmd($archive, $::cfg_path_compress, $format, $mode, 1, @args);
}
}
elsif ($format eq 'lzma') {
return singlearchivecmd($archive, $::cfg_path_lzma, $format, $mode, 1, @args);
}
elsif ($format eq 'lzop') {
return singlearchivecmd($archive, $::cfg_path_lzop, $format, $mode, 0, @args);
}
elsif ($format eq 'lzip') {
return singlearchivecmd($archive, $::cfg_use_plzip ? $::cfg_path_plzip : $::cfg_path_lzip, $format, $mode, 1, @args);
}
elsif ($format eq 'xz') {
return singlearchivecmd($archive, $::cfg_path_xz, $format, $mode, 1, @args);
}
elsif ($format eq 'rzip') {
return singlearchivecmd($archive, $::cfg_path_rzip, $format, $mode, 0, @args);
}
elsif ($format eq 'lrzip') {
return singlearchivecmd($archive, $::cfg_path_lrzip, $format, $mode, 0, @args);
}
return undef;
}
# de(value):
# Return 1 if value defined and is non-zero, 0 otherwise.
sub de($) {
my ($value) = @_;
return defined $value && $value ? 1 : 0;
}
# getmode()
# Identify the execution mode, and return it.
# Possible modes are 'cat', 'extract', 'list', 'add' or 'extract-to'.
sub getmode() {
my $mode;
if (de($::opt_cmd_list)
+ de($::opt_cmd_cat)
+ de($::opt_cmd_extract)
+ de($::opt_cmd_add)
+ de($::opt_cmd_extract_to)
+ de($::opt_cmd_diff)
+ de($::opt_cmd_repack) > 1) {
die "$::basename: only one command may be specified\n"; #OK
}
$mode = 'cat' if ($::basename eq 'acat');
$mode = 'extract' if ($::basename eq 'aunpack');
$mode = 'list' if ($::basename eq 'als');
$mode = 'add' if ($::basename eq 'apack');
$mode = 'diff' if ($::basename eq 'adiff');
$mode = 'repack' if ($::basename eq 'arepack');
$mode = 'add' if ($::opt_cmd_add);
$mode = 'cat' if ($::opt_cmd_cat);
$mode = 'list' if ($::opt_cmd_list);
$mode = 'extract' if ($::opt_cmd_extract);
$mode = 'extract-to' if ($::opt_cmd_extract_to);
$mode = 'diff' if ($::opt_cmd_diff);
$mode = 'repack' if ($::opt_cmd_repack);
if (!defined $mode) {
die "$::basename: no command specified\nTry `$::basename --help' for more information.\n"; #OK
}
return $mode;
}
# singlearchivecmd(archive, command, format, mode, args)
# Execute a command for single-file archives.
# The command parameter specifies what command to execute.
# If mode is 'extract-to', returns the directory (or only file)
# which was extracted.
sub singlearchivecmd($$$$$@) {
my ($archive, $cmd, $format, $mode, $can_do_c, @args) = @_;
my $outfile;
my $reason;
my @cmd;
push @cmd, $cmd;
push @cmd, '-v' if $::opt_verbosity > 1;
if ($mode eq 'list') {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
elsif ($mode eq 'cat') {
if (!$can_do_c) {
warn "$::basename: ".quote($archive).": $mode command not supported for $format archives\n";
return undef;
}
push @cmd, '-c', '-d', $archive, @args;
$outfile = $archive; # Just so that we don't return undef
}
elsif ($mode eq 'add') {
if (@args > 1) {
warn "$::basename: cannot add more than one file with this format\n";
return undef;
}
if (!$::opt_force && (-e $archive || -l $archive)) {
warn "$::basename: ".quote($archive).": refusing to overwrite existing file\n";
return undef;
}
#if (!$::cfg_keep_compressed && stripext($archive) ne $args[0]) {
# warn "$::basename: ".quote($archive).": cannot create a $format archive with this name (use -X)\n";
# return;
#}
if ($can_do_c) {
push @cmd, '-c', @args, ['>'], $archive;
} else {
push @cmd, '-o', $archive, @args;
}
$outfile = $archive; # Just so that we don't return undef
}
elsif ($mode eq 'extract') {
$outfile = stripext($archive);
if ($::cfg_decompress_to_cwd) {
$outfile = basename($outfile);
}
if (-e $outfile) {
$outfile = makeoutfile($::cfg_tmpdir_name);
$reason = 'local file exists';
}
if ($can_do_c) {
push @cmd, '-c', '-d', $archive, @args, ['>'], $outfile;
} else {
push @cmd, '-o', $outfile, '-d', $archive, @args;
}
}
elsif ($mode eq 'extract-to') {
$outfile = $::opt_cmd_extract_to;
if ($::opt_simulate ? $::opt_cmd_extract_to_type eq 'd' : -d $outfile) {
my $base = File::Basename::basename($archive);
$outfile = File::Spec->catfile($outfile, stripext($base));
}
if ($can_do_c) {
push @cmd, '-c', '-d', $archive, @args, ['>'], $outfile;
} else {