Ticket #44: u7z-9742

File u7z-9742, 3.7 KB (added by slavazanko, 16 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# beta version 0.3.2 (14 Nov 2004)
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
34SEVENZ=`which 7za`
35
36
37mc7zfs_list ()
38{
39    $SEVENZ l "$1" 2> /dev/null | gawk -v uid=${UID-0} '
40BEGIN { flag=0 }
41 /^-------/ { flag++; if (flag > 1) exit 0; next }
42{
43if (flag == 0) next
44
45year=substr($1, 1, 4)
46month=substr($1, 6, 2)
47day=substr($1, 9, 2)
48date=month "-" day "-" year
49
50time=substr($2, 1, 5)
51
52if (NF > 6)
53    name=substr($0, 54)
54else
55    name=$6
56
57gsub(/\\/, "/", name)
58
59if (index($3, "D") != 0)
60    $3="drwxr-xr-x"
61else
62if (index($3, ".") != 0)
63    $3="-rw-r--r--"
64
65printf "%s   1 %-8d %-8d %8d %s %s %s\n", $3, uid, 0, $4, date, time, name
66}'
67}
68
69mc7zfs_copyin ()
70{
71# preserve pwd.
72    pwd=`pwd`
73# Create a directory and copy in it the tmp file with the random name
74    mkdir "$3.dir"
75    cd "$3.dir"
76    di="${2%/*}"
77# if file is to be written upper in the archive tree, make fake dir
78    if test "$di" != "${2##*/}" ; then
79        mkdir -p "$di"
80    fi
81    cp -fp "$3" "$3.dir/$2"
82    $SEVENZ a "$1" "$2" >/dev/null
83    cd $pwd
84    rm -rf "$3.dir"
85}
86
87mc7zfs_copyout ()
88{
89    dir=tmpdir.${RANDOM}
90    mkdir /tmp/"$dir"
91#    echo "$1 $2 $3" > hers
92#       p7zip 0.91 don't understand filename in subdir without "./"
93#       but in top dir it understand only without "./"
94    $SEVENZ l "$1" | grep "[.][\\]" > /dev/null 2>&1 && echo "$2" | grep "\/" > /dev/null 2>&1 && EXFNAME=./"$2" || EXFNAME="$2"
95#    echo "$EXFNAME" > 11111
96#    echo Q is a workaround of p7zip bug, if the same file is in top dir of archive and in some
97#       subdir of archive, when we try to extract file from top level dir then after extracting,
98#       7za try to extract same file from subdirs, echo Q say it Quit.
99    echo Q | $SEVENZ e "$1" "$EXFNAME" -o/tmp/"$dir" > /dev/null
100    EXFN=`basename "$2"`
101    cat /tmp/"$dir"/"$EXFN" > "$3"
102    rm -rf /tmp/"$dir"
103}
104
105mc7zfs_mkdir ()
106{
107# Function not fully implemented, because 7z cannot keep empty directories
108# preserve pwd.
109    pwd=`pwd`
110# Create a directory and create in it a tmp directory with the good name     
111    dir=tmpdir.${RANDOM}
112    mkdir $dir
113    cd $dir
114    mkdir -p "$2"
115# 7z cannot create an empty directory   
116#    touch "$2"/.emptydir
117    $SEVENZ a -r "$1" "$2" >/dev/null
118#    echo "$1" "$2" >error34
119#    $SEVENZ d ../"$1" "$2/.7zfs" >/dev/null
120    cd $pwd
121    rm -rf $dir
122}
123
124mc7zfs_rm ()
125{
126    $SEVENZ d "$1" "$2" >/dev/null
127}
128
129umask 077
130
131cmd="$1"
132shift
133
134case "$cmd" in
135  list)    mc7zfs_list    "$@" ;;
136  rm)      mc7zfs_rm      "$@" ;;
137  rmdir)   mc7zfs_rm      "$@" ;;
138  mkdir)   mc7zfs_mkdir   "$@" ;;
139  copyin)  mc7zfs_copyin  "$@" ;;
140  copyout) mc7zfs_copyout "$@" ;;
141  *) exit 1 ;;
142esac
143exit 0