Merge branch '4562_mcedit_macros_paste'
[midnight-commander.git] / src / vfs / extfs / helpers / urar.in
blob71a4e82ad832563d96e7e537e0336b0203d936ee
1 #! /bin/sh
3 # Written by andrey joukov
4 # (C) 1996 2:5020/337.13@fidonet.org
5 # Updated by christian.gennerat@alcatel.fr 1999
6 # Andrew V. Samoilov <sav@bcs.zp.ua> 2000
8 # Andrew Borodin <aborodin@vmail.ru>
9 # David Haller <dnh@opensuse.org>
10 # 2013: support unrar5
12 # beta version 2.0
14 # rar and unrar can be found on http://www.rarlabs.com/
16 RAR=rar
18 # Prefer unrar (freeware).
19 UNRAR=`which unrar 2>/dev/null`
21 [ -z $UNRAR ] && UNRAR=$RAR
22 [ ! -x $UNRAR -a -x $RAR ] && UNRAR=$RAR
24 # Let the test framework hook in:
25 UNRAR=${MC_TEST_EXTFS_LIST_CMD:-$UNRAR}
27 # Determine the $UNRAR version
28 if [ -n "$MC_TEST_EXTFS_UNRAR_VERSION" ]; then
29 # Let the test framework fool us:
30 UNRAR_VERSION=$MC_TEST_EXTFS_UNRAR_VERSION
31 else
32 # Figure it out from rar itself:
33 UNRAR_VERSION=`$UNRAR -cfg- -? | grep "Copyright" | sed -e 's/.*\([0-9]\)\..*/\1/'`
36 mcrar4fs_list ()
38 $UNRAR v -c- -cfg- "$1" | @AWK@ -v uid=`id -u` -v gid=`id -g` '
39 BEGIN { flag=0 }
40 /^-------/ { flag++; if (flag > 1) exit 0; next }
41 flag==1 {
42 str = substr($0, 2)
43 getline
44 split($4, a, "-")
45 if (index($6, "D") != 0)
46 $6="drwxr-xr-x"
47 else
48 if (index($6, ".") != 0)
49 $6="-rw-r--r--"
50 printf "%s 1 %s %s %d %02d/%02d/%02d %s ./%s\n", $6, uid, gid, $1, a[2], a[1], a[3], $5, str
54 mcrar5fs_list ()
56 $UNRAR vt -c- -cfg- "$1" | @AWK@ -F ':' -v uid=`id -u` -v gid=`id -g` '
58 ### remove space after the ":" of the field name
59 sub ("^ ", "", $2);
62 $1 ~ /^ *Name$/ {
63 ### next file
64 name = mtime = size = attrs = "";
65 delete date;
66 name = $2;
67 ### if the name contains ":", append the rest of the fields
68 if (NF > 2) {
69 for (i = 3; i <= NF; i++) {
70 name = name ":" $i;
74 $1 ~ /^ *mtime$/ {
75 mtime = $2 ":" $3;
77 $1 ~ /^ *Size$/ {
78 size = $2;
80 $1 ~ /^ *Attributes$/ {
81 attrs = $2;
84 $1 ~ /^ *Compression$/ {
85 ### file done, using /^$/ is not so good you
86 ### would have to skip the version stuff first
88 ### get date and time
89 split (mtime, date, " ");
90 time = date[2];
91 ### cut off seconds from the time
92 sub (",[0-9]*$", "", time);
93 ### split for reordering of the date in the printf below
94 split (date[1], date, "-");
95 ### mc seems to be able to parse 4 digit years too, so remove if tested
96 # sub ("^..", "", date[1]); ### cut year to 2 digits only
98 ### check/adjust rights
99 if (index (attrs, "D") != 0) {
100 attrs = "drwxr-xr-x";
101 } else {
102 if (index (attrs, ".") != 0) {
103 attrs = "-rw-r--r--";
107 ### and finally
108 printf ("%s 1 %s %s %d %02d/%02d/%02d %s ./%s\n",
109 attrs, uid, gid, size, date[2], date[3], date[1], time, name);
114 mcrarfs_list ()
116 case x$UNRAR_VERSION in
117 x5 | x6 | x7) mcrar5fs_list "$@" ;;
118 *) mcrar4fs_list "$@" ;;
119 esac
122 mcrarfs_copyin ()
124 # copyin by christian.gennerat@alcatel.fr
125 # preserve pwd. It is clean, but is it necessary?
126 pwd=`pwd`
127 # Create a directory and copy in it the tmp file with the good name
128 mkdir "$3.dir"
129 cd "$3.dir"
130 di="${2%/*}"
131 # if file is to be written upper in the archive tree, make fake dir
132 if test x"$di" != x"${2##*/}" ; then
133 mkdir -p "$di"
135 cp -fp "$3" "$3.dir/$2"
136 $RAR a "$1" "$2" >/dev/null
137 cd "$pwd"
138 rm -rf "$3.dir"
141 mcrarfs_copyout ()
143 $UNRAR p -p- -c- -cfg- -inul "$1" "$2" > "$3"
146 mcrarfs_mkdir ()
148 # preserve pwd. It is clean, but is it necessary?
149 pwd=`pwd`
150 # Create a directory and create in it a tmp directory with the good name
151 dir=`mktemp -d "${MC_TMPDIR:-/tmp}/mctmpdir-urar.XXXXXX"` || exit 1
152 cd "$dir"
153 mkdir -p "$2"
154 # rar cannot create an empty directory
155 touch "$2"/.rarfs
156 $RAR a -r "$1" "$2" >/dev/null
157 $RAR d "$1" "$2/.rarfs" >/dev/null
158 cd "$pwd"
159 rm -rf "$dir"
162 mcrarfs_rm ()
164 $RAR d "$1" "$2" >/dev/null
167 umask 077
169 cmd="$1"
170 shift
172 case "$cmd" in
173 # Workaround for a bug in mc - directories must precede files to
174 # avoid duplicate entries, so we sort output by filenames
175 list) mcrarfs_list "$@" | sort -k 8 ;;
176 rm) mcrarfs_rm "$@" ;;
177 rmdir) mcrarfs_rm "$@" ;;
178 mkdir) mcrarfs_mkdir "$@" ;;
179 copyin) mcrarfs_copyin "$@" ;;
180 copyout) mcrarfs_copyout "$@" ;;
181 *) exit 1 ;;
182 esac
183 exit 0