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