file_unpack.pl untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 160 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #! /usr/bin/perl -w
2 #
3 # file_unpack.pl -- Demo of File::Unpack features.
4 #
5 # (C) 2010-2014, jnw@cpan.org, all rights reserved.
6 # Distribute under the same license as Perl itself.
7 #
8 # 2010-06-29, jw -- initial draught
9 # 2010-08-03, jw -- fixed -v.
10 # 2010-08-31, jw -- fixed -q with -m.
11 # 2010-09-01, jw -- added --list
12 # 2011-01-03, jw -- allow multiple arguments. Improved -m
13 # 2011-03-08, jw -- fixed usage of -l, added -p.
14 # 2011-04-21, jw -- better format error messages, and stop after error.
15 # 2011-05-12, jw -- added -n for no_op
16 # 2012-02-16, jw -- added -A for archive_name_as_dir.
17 # using {log_type} == 'PLAIN' unless -L
18 # 2013-01-25, jw -- added -f for follow_file_symlinks.
19 # 2014-07-21, jw -- default to one_shot unless named *deep*.
20  
21 use Data::Dumper;
22 $Data::Dumper::Terse = 1;
23 $Data::Dumper::Indent = 1;
24 use Getopt::Long qw(:config no_ignore_case);
25 use Pod::Usage;
26 use FindBin;
27 use lib "$FindBin::RealBin/blib/lib";
28 use File::Unpack;
29  
30 my $version = $File::Unpack::VERSION;
31 my @exclude;
32 my $exclude_vcs = 1;
33 my $help;
34 my $mime_only;
35 my $list_only;
36 my $list_perlish;
37 my @mime_helper_dirs;
38  
39 my %opt = ( verbose => 1, maxfilesize => '2.6G', one_shot => 1, no_op => 0, world_readable => 0, log_fullpath => 0, archive_name_as_dir => 0, follow_file_symlinks => 0, log_type => 'PLAIN');
40  
41 $opt{one_shot} = 0 if $0 =~ m{deep[^/]*$};
42  
43 push @mime_helper_dirs, "$FindBin::RealBin/helper" if -d "$FindBin::RealBin/helper";
44  
45 GetOptions(
46 "verbose|v+" => \$opt{verbose},
47 "version|V" => sub { print "$version\n"; exit },
48 "quiet" => sub { $opt{verbose} = 0; },
49 "destdir|D|C=s" => \$opt{destdir},
50 "exclude|E=s" => \@exclude,
51 "exclude-vcs!" => \$exclude_vcs,
52 "vcs|include-vcs!" => sub { $exclude_vcs = !$_[1]; },
53 "help|?" => \$help,
54 "logfile|L=s" => \$opt{logfile},
55 "fullpath-log|F" => \$opt{log_fullpath},
56 "one-shot|one_shot|1" => \$opt{one_shot},
57 "deep|recursive" => sub { $opt{one_shot} = 0; },
58 "mimetype|m+" => \$mime_only,
59 "no_op|no-op|noop|n+" => \$opt{no_op},
60 "list-helpers|l+" => \$list_only,
61 "print-helpers|p+" => \$list_perlish,
62 "params|P=s" => \%{$opt{log_params}},
63 "maxfilesize=s" => \$opt{maxfilesize},
64 "use-mime-helper-dir|I|u=s" => \@mime_helper_dirs,
65 "world-readable|world_readable|R+" => \$opt{world_readable},
66 "archive-dirs|archive_dirs|A" => \$opt{archive_name_as_dir},
67 "follow-file-symlinks|f+" => \$opt{follow_file_symlinks},
68 ) or $help++;
69  
70 @mime_helper_dirs = split(/,/,join(',',@mime_helper_dirs));
71 my $archive = shift or $list_perlish or $list_only or $help++;
72  
73 pod2usage(-verbose => 1, -msg => qq{
74 file_unpack V$version Usage:
75  
76 $0 [options] input.tar.gz ...
77 $0 [options] input/ ...
78 $0 -l
79 $0 -p
80  
81 Valid options are:
82 -v Be more verbose. Default: $opt{verbose}.
83 -q Be quiet, not verbose.
84  
85 -A --archive_dirs
86 Use exact archive names as subdirectories, modified by appending '._*'
87 to avoid collisions. Default: use truncated and/or modified archive
88 names. E.g. example.zip is per default unpacked into 'example/'. With
89 -A, it is unpacked into 'example.zip._/'. This does not apply for
90 single file archives. They are always unpacked without a directory.
91  
92 -C dir
93 -D --destdir dir
94 Directory, where to place the output file or directory.
95 A subdirectory is created, if there are more than one files to unpack.
96 Default: current dir.
97  
98 -E --exclude glob.pat
99 Specify files and directories that are not unpacked.
100 This option can be specified multiple times.
101  
102 -F --fullpath-log
103 Always use full path names in logfile. Default:
104 unpacked path names are written relative to destdir.
105  
106 --exclude-vcs --no-exclude-vcs
107 --include-vcs --no-include-vcs --vcs --no-vcs
108 Group switch for directory glob patterns of most version control systems.
109 This affects at least SCCS, RCS, CVS, .svn, .git, .hg, .osc .
110 The logfile has a {skipped}{exclude} counter.
111 Default: exclude-vcs=$exclude_vcs .
112  
113 -1 --one-shot
114 Make unpacker non-recursive. Perform one level of unpacking only.
115 This is the default unless the name of the unpacker contains the substring 'deep'.
116  
117 --deep
118 --recursive
119 Make unpacker recursive. Perform all possible levels of unpacking.
120 This is the default if the name of the unpacker contains the substring 'deep'.
121  
122 -h --help -?
123 Print this online help.
124
125 -L --logfile file.log
126 Specify a logfile, where freshly unpacked files are reported.
127 When a logfile is specified, its format is JSON;
128 default is STDOUT with format PLAIN.
129
130 -l --list-helpers
131 Overview of mime-type patterns and their helper commands.
132  
133 -p --print-helpers
134 List all builtin mime-helpers and all external mime-helpers as
135 a nested perl datastructure.
136  
137 -P --param KEY=VALUE
138 Place additional params into the log file.
139  
140 --maxfilesize size
141 Truncate an unpacked file, if it gets larger than the specified size.
142 Size can be specified as bytes (plain integer), kilo-, mega-, giga-, or
143 tera-bytes (suffix K,M,G,T). Default: $opt{maxfilesize}.
144  
145 -m --mimetype
146 Do not unpack, just report mimetype of the archive. Output format is
147 similar to '/usr/bin/file -i', unless -q or -v are given.
148 With -v, the unpacker command is also printed.
149  
150 -R --world-readable
151 Make the unpacked tree world readable. Default: user readable.
152  
153 -n --no-op
154 Do not unpack. Print the first unpack command only.
155  
156 -u --use-mime-helper-dir dir
157 Include an additonal directory of mime helpers.
158 Useable multiple times. Later additions take precedence.
159  
160 -f --follow-file-symlinks
161 Follow (and unpack) symlinks that point to files (or archives).
162 Used once, we follow only symlinks that are present before unpacking starts.
163 Used twice, we also follow symlinks that were unpacked from an archive.
164 Symlinks to directories or other (dangling) symlinks are always ignored.
165 The logfile has a {skipped}{symlink} counter.
166 Default: skip all symlinks.
167  
168 }) if $help;
169  
170 if (defined $opt{logfile})
171 {
172 $opt{log_type} = 'JSON';
173 $opt{logfile} = \*STDOUT if $opt{logfile} eq '-';
174 }
175  
176 $opt{logfile} ||= '/dev/null' if $list_only or $list_perlish or $mime_only or $opt{no_op};
177 my $u = File::Unpack->new(%opt);
178 my $list = $u->mime_helper_dir(@mime_helper_dirs);
179  
180 if ($list_perlish)
181 {
182 print Dumper $list;
183 exit 0;
184 }
185  
186 if ($list_only)
187 {
188 printf @$_ for $u->list();
189 exit 0;
190 }
191  
192 if ($mime_only)
193 {
194 $u->{verbose}-- if $u->{verbose};
195 if ($u->{verbose} > 1)
196 {
197 print "using File::LibMagic $File::LibMagic::VERSION\n" if defined $File::LibMagic::VERSION;
198 print "using File::MimeInfo::Magic $File::MimeInfo::Magic::VERSION\n" if defined $File::MimeInfo::Magic::VERSION;
199 print "using File::Unpack $File::Unpack::VERSION\n" if defined $File::Unpack::VERSION;
200 }
201  
202 while (defined $archive)
203 {
204 my $m = $u->mime($archive);
205 my ($h,$r) = $u->find_mime_helper($m);
206 if ($opt{verbose} > 1)
207 {
208 print "$archive: ", Dumper $m;
209 print File::Unpack::fmt_run_shellcmd($h) . "\n";
210 }
211 elsif ($opt{verbose} == 1)
212 {
213 print "$archive: $m->[0]; charset=$m->[1]\n";
214 }
215 else
216 {
217 print "$m->[0]\n";
218 }
219 $archive = shift;
220 }
221 exit 0;
222 }
223  
224 while (defined $archive and !$u->{error})
225 {
226 $u->exclude(vcs => $exclude_vcs);
227 $u->exclude(add => \@exclude) if @exclude;
228  
229 $u->unpack($archive);
230 map { print STDERR "ERROR: $_\n" } @{$u->{error}} if $u->{error};
231 $archive = shift;
232 if (defined($archive))
233 {
234 if (defined $opt{logfile} and -f $opt{logfile})
235 {
236 warn "File::Unpack($archive): overwriting previous logfile $opt{logfile} in 3 seconds. Press CTRL-C to abort.\n" if defined $archive;
237 sleep(3);
238 }
239 # reload, for the next round. (new() opens the logfile, unpack() closese it.)
240 $u = File::Unpack->new(%opt);
241 $u->mime_helper_dir(@mime_helper_dirs);
242 }
243 }
244  
245 # delete $u->{json};
246 # die "$0: " . Dumper $u;