Ticket #44: u7z-9743

File u7z-9743, 3.3 KB (added by slavazanko, 11 years ago)

added by sgh

Line 
1#! /bin/sh
2#
3# u7z - 7zip file archive Virtual File System for Midnight Commander ( ftp://ftp.ibiblio.org/pub/Linux/utils/file/managers/mc/ )
4#
5# Copyright (C) 2004 Sergiy Niskorodov (sgh at ukrpost dot net)
6
7# Written by Sergiy Niskorodov aka SGh
8#
9# version 4.29 (12 Nov 2005)
10#
11# 7z for linux can be found on http://sourceforge.net/projects/p7zip/
12
13
14# Thanks to urar VFS authors andrey joukov 2:5020/337.13@fidonet.org,
15# christian.gennerat@alcatel.fr, Andrew V. Samoilov <sav@bcs.zp.ua>
16# I use this script like example
17
18
19# This program is free software; you can redistribute it and/or modify
20# it under the terms of the GNU General Public License as published by
21# the Free Software Foundation; either version 2 of the License, or
22# (at your option) any later version.
23#
24# This program is distributed in the hope that it will be useful,
25# but WITHOUT ANY WARRANTY; without even the implied warranty of
26# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27# GNU General Public License for more details.
28#
29# You should have received a copy of the GNU General Public License
30# along with this program; if not, write to the Free Software
31# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32
33
34
35SEVENZ=`which 7z` || SEVENZ=`which 7za`
36
37
38mc7zfs_list ()
39{
40    $SEVENZ l "$1" 2> /dev/null | gawk -v uid=${UID-0} '
41BEGIN { flag=0; arr_of_month="JanFebMarAprMayJunJulAugSepOctNovDec" }
42 /^-------/ { flag++; if (flag > 1) exit 0; next }
43{
44if (flag == 0) next
45
46year=substr($1, 1, 4)
47month=substr($1, 6, 2)
48day=substr($1, 9, 2)
49
50month_name=substr(arr_of_month, (month-1)*3+1, 3)
51
52time=substr($2, 1, 5)
53
54if (index($3, "D") != 0)
55    attr="drwxr-xr-x"
56else
57if (index($3, ".") != 0)
58    attr="-rw-r--r--"
59
60size=$4
61
62$0=substr($0, 54)
63if (NF > 1)
64    name=$0
65else
66    name=$1
67
68gsub(/\\/, "/", name)
69
70printf "%s   1 %-8d %-8d %8d %3s %2d %4d %s %s\n", attr, uid, 0, size, month_name, day, year, time, name
71}'
72}
73
74mc7zfs_copyin ()
75{
76# preserve pwd.
77    pwd=`pwd`
78# Create a directory and copy in it the tmp file with the random name
79    dir="$3".dir
80    mkdir "$dir"
81    cd "$dir"
82    di="${2%/*}"
83# if file is to be written upper in the archive tree, make fake dir
84    if test "$di" != "${2##*/}" ; then
85        mkdir -p "$di"
86    fi
87    cp -fp "$3" "$dir/$2"
88    $SEVENZ a "$1" "$2" >/dev/null 2> /dev/null
89    cd $pwd
90    rm -rf "$3.dir"
91}
92
93mc7zfs_copyout ()
94{
95    $SEVENZ l "$1" | grep -q "[.][/].*$2" &> /dev/null && EXFNAME=*./"$2" || EXFNAME="$2"
96    $SEVENZ e -r- -so "$1" "$EXFNAME" > "$3" 2> /dev/null
97}
98
99mc7zfs_mkdir ()
100{
101# preserve pwd.
102    pwd=`pwd`
103# Create a directory and create in it a tmp directory with the good name     
104    dir=tmpdir.${RANDOM}
105    mkdir $dir
106    cd $dir
107    mkdir -p "$2"
108    $SEVENZ a -r "$1" "$2" >/dev/null 2>/dev/null
109    cd $pwd
110    rm -rf $dir
111}
112
113mc7zfs_rm ()
114{
115    $SEVENZ l "$1" | grep -q "[.][/].*$2" &> /dev/null && EXFNAME=*./"$2" || EXFNAME="$2"
116    $SEVENZ d "$1" "$EXFNAME" 2>&1 | grep -q E_NOTIMPL &> /dev/null && { echo -e "Function not implemented...\n7z cannot delete files from solid archive." >&2 ; exit 1 ; }
117}
118
119umask 077
120
121cmd="$1"
122shift
123
124case "$cmd" in
125  list)    mc7zfs_list    "$@" ;;
126  rm)      mc7zfs_rm      "$@" ;;
127  rmdir)   mc7zfs_rm      "$@" ;;
128  mkdir)   mc7zfs_mkdir   "$@" ;;
129  copyin)  mc7zfs_copyin  "$@" ;;
130  copyout) mc7zfs_copyout "$@" ;;
131  *) exit 1 ;;
132esac
133exit 0