| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
# |
| 4 |
# quicker tar + better defaults |
| 5 |
# written by Jan Engelhardt, 2007-2010 |
| 6 |
# |
| 7 |
# Achieves higher compression by using dedicated file sorting |
| 8 |
# to put similar blocks next to each other. |
| 9 |
|
| 10 |
use File::Find; |
| 11 |
use Getopt::Long; |
| 12 |
use IPC::Open2; |
| 13 |
use strict; |
| 14 |
|
| 15 |
my @excludes = (); |
| 16 |
my $strategy = "by_normal"; |
| 17 |
my $packer = undef; |
| 18 |
my @result; |
| 19 |
&main(); |
| 20 |
|
| 21 |
sub main |
| 22 |
{ |
| 23 |
Getopt::Long::Configure(qw(bundling pass_through)); |
| 24 |
GetOptions( |
| 25 |
"x" => sub { push(@excludes, qr{(?:^|/)(?:\.git|\.svn|\.bzr|\.hg|CVS)(?:/|$)}); }, |
| 26 |
"svn" => sub { $strategy = "by_basename"; }, # lame alias I agree |
| 27 |
"ext" => sub { $strategy = "by_extension"; }, |
| 28 |
"use=s" => sub { $packer = $_[1]; }, |
| 29 |
); |
| 30 |
|
| 31 |
if (scalar(@ARGV) == 0) { |
| 32 |
die "No output file. What gives?\n"; |
| 33 |
} |
| 34 |
|
| 35 |
my $archive = shift @ARGV; |
| 36 |
if (!defined($packer)) { |
| 37 |
if (substr($archive, -3, 3) eq ".xz" || |
| 38 |
substr($archive, -4, 4) eq ".txz") { |
| 39 |
$packer = "xz"; |
| 40 |
} elsif (substr($archive, -4, 4) eq ".bz2" || |
| 41 |
substr($archive, -5, 5) eq ".tbz2") { |
| 42 |
$packer = "bzip2"; |
| 43 |
} elsif (substr($archive, -3, 3) eq ".gz" || |
| 44 |
substr($archive, -4, 4) eq ".tgz") { |
| 45 |
$packer = "gzip"; |
| 46 |
} elsif (substr($archive, -4, 4) eq ".tar") { |
| 47 |
$packer = undef; |
| 48 |
} elsif (substr($archive, -3, 3) eq ".lz") { |
| 49 |
$packer = "lzip"; |
| 50 |
} elsif (substr($archive, -4, 4) eq ".zst") { |
| 51 |
$packer = "zstd"; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
my @targets = grep(/^[^-]/, @ARGV); |
| 56 |
if (scalar(@targets) == 0) { |
| 57 |
die "No input directories. What gives?\n"; |
| 58 |
} |
| 59 |
foreach (@targets) { |
| 60 |
if (!-e $_) { |
| 61 |
warn "WARNING: Cannot find \"$_\": $!\n"; |
| 62 |
} |
| 63 |
} |
| 64 |
File::Find::find({ |
| 65 |
wanted => \&collect, |
| 66 |
no_chdir => 1, |
| 67 |
}, @targets); |
| 68 |
@ARGV = grep /^-/, @ARGV; |
| 69 |
@result = sort $strategy @result; |
| 70 |
|
| 71 |
&push_files($archive, \@result, \@ARGV); |
| 72 |
} |
| 73 |
|
| 74 |
sub by_normal |
| 75 |
{ |
| 76 |
return $a cmp $b; |
| 77 |
} |
| 78 |
|
| 79 |
sub by_basename |
| 80 |
{ |
| 81 |
my($p, $q) = ($a, $b); |
| 82 |
if (-d $p && !-d $q) { |
| 83 |
return -1; |
| 84 |
} |
| 85 |
if (!-d $p && -d $q) { |
| 86 |
return 1; |
| 87 |
} |
| 88 |
$p =~ s{.*/}{}g; |
| 89 |
$q =~ s{.*/}{}g; |
| 90 |
return $p cmp $q; |
| 91 |
} |
| 92 |
|
| 93 |
sub by_extension |
| 94 |
{ |
| 95 |
my($p, $q) = ($a, $b); |
| 96 |
if (-d $p && !-d $q) { |
| 97 |
return -1; |
| 98 |
} |
| 99 |
if (!-d $p && -d $q) { |
| 100 |
return 1; |
| 101 |
} |
| 102 |
if (-d $p && -d $q) { |
| 103 |
return &by_basename(); |
| 104 |
} |
| 105 |
# Both are files: |
| 106 |
$p =~ s{.*\.(\w+)$}{$1}; |
| 107 |
$q =~ s{.*\.(\w+)$}{$1}; |
| 108 |
if ($p ne $q) { |
| 109 |
return $p cmp $q; |
| 110 |
} |
| 111 |
# Same extension |
| 112 |
return &by_basename(); |
| 113 |
} |
| 114 |
|
| 115 |
sub collect |
| 116 |
{ |
| 117 |
foreach my $regex (@excludes) { |
| 118 |
if ($_ =~ $regex) { |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
push(@result, $_); |
| 123 |
} |
| 124 |
|
| 125 |
sub push_files |
| 126 |
{ |
| 127 |
local *COUT; |
| 128 |
my @args = ( |
| 129 |
"tar", "--no-recursion", "--null", "-T-", |
| 130 |
defined($packer) ? "--use=$packer" : (), |
| 131 |
"--owner=root", "--group=root", |
| 132 |
"-cvf", $_[0], @{$_[2]}, |
| 133 |
); |
| 134 |
|
| 135 |
if (!open(\*COUT, "|-", @args)) { |
| 136 |
die "Could not run tar: $!\n"; |
| 137 |
} |
| 138 |
print COUT join("\x00", @{$_[1]}); |
| 139 |
close COUT; |
| 140 |
return 0; |
| 141 |
} |