|
| 1 | +#! @PERL@ |
| 2 | +# Midnight Commander - Brotli support |
| 3 | +# |
| 4 | +# Written by: |
| 5 | +# Vadim Kalinnikov < [email protected]> |
| 6 | +# |
| 7 | +# This file is part of the Midnight Commander. |
| 8 | +# |
| 9 | +# It requires brotli: https://github.com/google/brotli |
| 10 | +# On Debian/Ubuntu brotli can be installed via: |
| 11 | +# apt install brotli |
| 12 | + |
| 13 | +use strict; |
| 14 | +use warnings; |
| 15 | +use diagnostics; |
| 16 | + |
| 17 | +sub f_mode($) { |
| 18 | + my $mode = shift; |
| 19 | + |
| 20 | + my @perms = ('---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx'); |
| 21 | + |
| 22 | + my $ret = "-"; |
| 23 | + $ret .= $perms[($mode >> 6) & 7]; |
| 24 | + $ret .= $perms[($mode >> 3) & 7]; |
| 25 | + $ret .= $perms[$mode & 7]; |
| 26 | + |
| 27 | + return $ret; |
| 28 | +} |
| 29 | + |
| 30 | +sub f_time($) { |
| 31 | + my $t = shift; |
| 32 | + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t); |
| 33 | + |
| 34 | + my $ret = sprintf("%02d/%02d/%04d %02d:%02d:%02d", $mon, $mday, 1900+$year, $hour, $min, $sec); |
| 35 | + return $ret; |
| 36 | +} |
| 37 | + |
| 38 | +sub br_ls($) { |
| 39 | + my $filename = shift; |
| 40 | + |
| 41 | + exit 1 if ! -f $filename; |
| 42 | + |
| 43 | + # file information |
| 44 | + my @fileinfo = stat($filename); |
| 45 | + |
| 46 | + # strip extension |
| 47 | + $filename =~ s/\.br$//; |
| 48 | + $filename =~ s|^.*/||; |
| 49 | + |
| 50 | + printf "%s 1 %s %s %s %s %s\n", |
| 51 | + f_mode($fileinfo[2]), # attr |
| 52 | + getpwuid($fileinfo[4]) || $fileinfo[4], # uid |
| 53 | + getgrgid($fileinfo[5]) || $fileinfo[5], # gid |
| 54 | + $fileinfo[7], # size |
| 55 | + f_time($fileinfo[9]), # time |
| 56 | + $filename; |
| 57 | +} |
| 58 | + |
| 59 | +sub br_copyout($$) { |
| 60 | + my $srcfilename = shift; |
| 61 | + my $dstfilename = shift; |
| 62 | + |
| 63 | + system("brotli", "-dkfo", $dstfilename, $srcfilename) == 0 or exit(1); |
| 64 | +} |
| 65 | + |
| 66 | +if (@ARGV >= 2 && $ARGV[0] eq "list") { |
| 67 | + br_ls($ARGV[1]); |
| 68 | +} |
| 69 | +elsif (@ARGV >= 4 && $ARGV[0] eq "copyout") { |
| 70 | + br_copyout($ARGV[1], $ARGV[3]); |
| 71 | +} |
| 72 | +else { |
| 73 | + exit(1); |
| 74 | +} |
| 75 | + |
| 76 | + |
0 commit comments