| 1 | #! /usr/bin/perl |
| 2 | # |
| 3 | # Written by Chris Owen <chris@candu.co.uk> 2014 |
| 4 | # |
| 5 | # Deb file handling adapted from deb. |
| 6 | # |
| 7 | # Copyright (C) 2014 Free Software Foundation |
| 8 | # |
| 9 | |
| 10 | sub quote { |
| 11 | $_ = shift(@_); |
| 12 | s/([^\w\/.+-])/\\$1/g; |
| 13 | return($_); |
| 14 | } |
| 15 | |
| 16 | sub mcdebfs_list |
| 17 | { |
| 18 | # |
| 19 | # CAVEAT: Hard links are listed as if they were symlinks |
| 20 | # Empty directories do not appear at all |
| 21 | # |
| 22 | local($archivename)=@_; |
| 23 | local $qarchivename = quote($archivename); |
| 24 | chop($date=`LC_ALL=C date "+%b %d %H:%M"`); |
| 25 | chop($info_size=`dpkg -I $qarchivename | wc -c`); |
| 26 | |
| 27 | print "dr-xr-xr-x 1 root root 0 $date CONTENTS\n"; |
| 28 | print "dr-xr-xr-x 1 root root 0 $date DEBIAN\n"; |
| 29 | print "-r--r--r-- 1 root root $info_size $date INFO\n"; |
| 30 | |
| 31 | if ( open(PIPEIN, "LC_ALL=C dpkg-deb -c $qarchivename |") ) |
| 32 | { |
| 33 | while(<PIPEIN>) |
| 34 | { |
| 35 | @_ = split; |
| 36 | |
| 37 | $perm=$_[0]; $owgr=$_[1]; $size=$_[2]; |
| 38 | if($_[3] =~ /^\d\d\d\d\-/) { # New tar format |
| 39 | |
| 40 | ($year,$mon,$day) = split(/-/,$_[3]); |
| 41 | $month = ("Gee","Jan","Feb","Mar","Apr","May","Jun", |
| 42 | "Jul","Aug","Sep","Oct","Nov","Dec")[$mon] || "Gee"; |
| 43 | $time=$_[4]; |
| 44 | $pathindex=5; |
| 45 | } |
| 46 | else { |
| 47 | $mstring='GeeJanFebMarAprMayJunJulAugSepOctNovDec'; |
| 48 | $month=$_[3]; |
| 49 | $mon=index($mstring,$month) / 3; |
| 50 | $day=$_[4]; |
| 51 | $time=$_[5]; |
| 52 | $year=$_[6]; |
| 53 | $pathindex=7; |
| 54 | } |
| 55 | |
| 56 | $path=$_[$pathindex++]; |
| 57 | $arrow=$_[$pathindex++]; |
| 58 | $link=$_[$pathindex++]; |
| 59 | $link2=$_[$pathindex++]; |
| 60 | |
| 61 | $owgr=~s!/! !; |
| 62 | if($arrow eq 'link') |
| 63 | { |
| 64 | # report hard links as soft links |
| 65 | $arrow='->'; $link="/$link2"; |
| 66 | substr($perm, 0, 1) = "l"; |
| 67 | } |
| 68 | if($arrow ne '') |
| 69 | { |
| 70 | $arrow=' ' . $arrow; |
| 71 | $link= ' ' . $link; |
| 72 | } |
| 73 | $now=`date "+%Y %m"`; |
| 74 | ($thisyear, $thismon) = split(/ /, $now); |
| 75 | # show time for files younger than 6 months |
| 76 | # but not for files with dates in the future |
| 77 | if ($year * 12 + $mon > $thisyear * 12 + $thismon - 6 && |
| 78 | $year * 12 + $mon <= $thisyear * 12 + $thismon) { |
| 79 | print "$perm 1 $owgr $size $month $day $time CONTENTS/$path$arrow$link\n"; |
| 80 | } else { |
| 81 | print "$perm 1 $owgr $size $month $day $year CONTENTS/$path$arrow$link\n"; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if ( open(PIPEIN, "LC_ALL=C dpkg-deb -I $qarchivename |") ) |
| 86 | { |
| 87 | while(<PIPEIN>) |
| 88 | { |
| 89 | @_ = split; |
| 90 | $size=$_[0]; |
| 91 | last if $size =~ /:/; |
| 92 | next if $size !~ /\d+/; |
| 93 | if($_[4] eq '*') |
| 94 | { |
| 95 | $perm='-r-xr-xr-x'; |
| 96 | $name=$_[5]; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | $perm='-r--r--r--'; |
| 101 | $name=$_[4]; |
| 102 | } |
| 103 | print "$perm 1 root root $size $date DEBIAN/$name\n"; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | sub mcdebfs_copyout |
| 109 | { |
| 110 | local($archive,$filename,$destfile)=@_; |
| 111 | local $qarchive = quote($archive); |
| 112 | local $qfilename = quote($filename); |
| 113 | local $qdestfile = quote($destfile); |
| 114 | |
| 115 | if($filename eq "INFO") |
| 116 | { |
| 117 | system("dpkg-deb -I $qarchive > $qdestfile"); |
| 118 | } |
| 119 | elsif($filename =~ /^DEBIAN/) |
| 120 | { |
| 121 | $qfilename=~s!^DEBIAN/!!; |
| 122 | system("dpkg-deb -I $qarchive $qfilename > $qdestfile"); |
| 123 | } |
| 124 | elsif($filename eq "INSTALL") |
| 125 | { |
| 126 | if ( open(FILEOUT,">$destfile") ) |
| 127 | { |
| 128 | print FILEOUT $pressinstall; |
| 129 | close FILEOUT; |
| 130 | system("chmod a+x $qdestfile"); |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | # files can be prepended with ./ or not, depending on the version of tar |
| 136 | $qfilename=~s!^CONTENTS/!!; |
| 137 | system("dpkg-deb --fsys-tarfile $qarchive | tar xOf - $qfilename ./$qfilename > $qdestfile 2>/dev/null"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | sub mcdebfs_run |
| 142 | { |
| 143 | local($archive,$filename)=@_; |
| 144 | local $qarchive = quote($archive); |
| 145 | if($filename eq "INSTALL") |
| 146 | { |
| 147 | print "Installing $archive\n"; |
| 148 | system("dpkg -i $qarchive"); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | use File::Temp qw(mkdtemp); |
| 153 | my $template = "/tmp/mcdebfs.run.XXXXXX"; |
| 154 | $template="$ENV{MC_TMPDIR}/mcdebfs.XXXXXX" if ($ENV{MC_TMPDIR}); |
| 155 | $tmpdir = mkdtemp($template); |
| 156 | $tmpcmd="$tmpdir/run"; |
| 157 | &mcdebfs_copyout($archive, $filename, $tmpcmd); |
| 158 | system("chmod u+x $tmpcmd"); |
| 159 | system($tmpcmd); |
| 160 | unlink($tmpcmd); |
| 161 | rmdir($tmpdir); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | sub mcipkfs_list |
| 166 | { |
| 167 | local($archivename)=@_; |
| 168 | local($gzipfail)=system("gzip -t $archivename 2> /dev/null"); |
| 169 | if($gzipfail) |
| 170 | { |
| 171 | mcdebfs_list(@_) |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | system("tar ztvvf $archivename | sed -n \"s/^\\(..........\\) \\(\\w\\+\\)\\/\\(\\w\\+\\)\\s\\+\\(\\w\\+\\) \\(....\\)-\\(..\\)-\\(..\\)/\\1 1 \\2 \\3 \\4 \\6-\\7-\\5/p\"") |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | sub mcipkfs_copyout |
| 180 | { |
| 181 | local($archivename,$filename,$destfile)=@_; |
| 182 | local($gzipfail)=system("gzip -t $archivename 2> /dev/null"); |
| 183 | if($gzipfail) |
| 184 | { |
| 185 | mcdebfs_copyout(@_) |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | system("tar zxOf '$archivename' '$filename' > '$destfile'") |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | umask 077; |
| 194 | |
| 195 | if($ARGV[0] eq "list") { shift; &mcipkfs_list(@ARGV); exit 0; } |
| 196 | elsif($ARGV[0] eq "copyout") { shift; &mcipkfs_copyout(@ARGV); exit 0; } |
| 197 | |
| 198 | exit 1; |
| 199 | |