forked from pflanze/chj-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_uncompress-run
executable file
·131 lines (112 loc) · 3.09 KB
/
_uncompress-run
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
#!/usr/bin/perl -w
# Son Jun 3 22:12:16 CEST 2007
(my $email='pflanze%gmx,ch')=~ tr/%,/@./;
use strict;
our $filecmd= "/usr/bin/file";
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname originalprogram [options] possibly-gzipped-file
if one file is given and it is compressed, uncompress it into a tempfile
and call originalprogram with the original options.
See ${mydir}gv and ${mydir}xpdf wrappers, they are calling me.
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
@ARGV or usage;
usage if $ARGV[0]=~ /^--?h(elp)?$/;
our $origprog=shift @ARGV;
our @args= map {
# the argument, and whether it is a filearg
[ $_, not /^-/ ]
} @ARGV;
our @fileargs= grep {
$$_[1]
} @args;
use Chj::xperlfunc;
use Chj::Unix::SuperPATH;
sub normalexe {
# &{(do{ # really, I'm not joking, those *3* layers around the if are necessary.
&{ # ah no, this works, too. but once you decide (usually you have to,right???!) to use do, then you have to wrap it up in two additional layers. Funny, why is the do not necessary here?. (*because* it would take 3 layers otherwise?)
if ($origprog=~ m|/|) {
\&xexec
} else {
\&xsuperexec
}
}
# })}
($origprog,@ARGV);
}
use Chj::IO::Command;
use Chj::xtmpfile;
use Chj::xopen 'xopen_read';
use Chj::xpipeline 'xreceiverpipeline_with_out_to';
use Chj::xrealpath;
use Chj::xtmpdir;
use Chj::xperlfunc 'basename';
our $TMPDIR="/tmp";
sub uncompress {
my ($file)=@_;
$file= xrealpath $file;
my $c= Chj::IO::Command->new_sender($filecmd,"--",$file);
my $cnt=$c->xcontent;
$c->xxfinish;
my $decmd= do {
if ($cnt=~ /: gzip compressed/) {
["gunzip"]
} elsif ($cnt=~ /: bzip2 compressed/) {
["bunzip2"]
} else {
undef
}
};
$decmd and do {
#wiedermal pipelinenötig
my $f= xopen_read $file;
my $tdir= xtmpdir $TMPDIR."/"; # do not use lengthy program path. Well, I really meant to say this to xtmpfile; but there I'm now using the (exact) filename anyway (with xputback).
my $filename= basename $file;
##ugly, shouldn't this be done by the decompression detection routine.
$filename=~ s/\.(gz|bz2)\z//is;
##/ugly
my $t= xtmpfile "$tdir/$filename";
my $r= xreceiverpipeline_with_out_to($t, $decmd);
$f->xsendfile_to($r);
$r->xxfinish;
$t->xclose;#ps ich gebe $t hin aber in wahrheit tut es fd gell geben ?.
$t->xputback(0644);#hm forever--mask or perms?
## what with the cleanup??.switched off now ? I think. rite?.
## but the wrapper object will need to make some ordering anyway so don't bother.
bless [$tdir, $t->path], "CJ_uncompress_run::Tmpfile"
}
}
{
package CJ_uncompress_run::Tmpfile;
# fields [tdir, filepath]
sub path {
my $s=shift;
$$s[1]
}
sub DESTROY {
my $s=shift;
unlink $s->path;
# let the tmpdir clean itself up by itself.
}
}
if (@fileargs==1) {
my $file= $fileargs[0][0];
if (my $uncompressed= uncompress $file) {
xxsystem $origprog, map {
if ($$_[1]) {
$uncompressed->path
} else {
$$_[0]
}
} @args;
} else {
normalexe;
}
} else {
normalexe;
}