| 1 | #!/bin/sh |
| 2 | |
| 3 | # This is a parser for Dar archives in Midnight Commander. You need |
| 4 | # the GPL dar program (version >= 2.3.0) written by Denis Corbin. |
| 5 | |
| 6 | # Author: Guus Jansman |
| 7 | |
| 8 | # Limitations: |
| 9 | # - The archive file can not be changed |
| 10 | # - Symbolic and hard links are not handled properly |
| 11 | # - Block and character special files are not handled |
| 12 | # - Files not stored in (differential) backups are not handled |
| 13 | # - Dar files in archives are not handled (due filename restriction) |
| 14 | |
| 15 | DAR=dar |
| 16 | |
| 17 | # dar expects the basename (without number and extension) |
| 18 | BASENAME="`echo "$2" | sed -e 's/\.[0-9]*\.[Dd][Aa][Rr]$//'`" |
| 19 | |
| 20 | mcdarfs_list () |
| 21 | { |
| 22 | $DAR -l "$BASENAME" -N -Q -as 2>/dev/null | gawk -v uuid=${UID-0} ' |
| 23 | BEGIN { flag=0 } |
| 24 | /^-------/ { flag++; if (flag > 1) exit 0; next } |
| 25 | /^$/ { next } |
| 26 | { |
| 27 | if (flag == 0) next |
| 28 | line=$0 |
| 29 | split(line, record, " ") |
| 30 | |
| 31 | # Do not display removed files |
| 32 | if (record[1] == "[" && record[2] == "REMOVED") |
| 33 | { |
| 34 | next |
| 35 | } |
| 36 | |
| 37 | # We want "line" to start with permutation |
| 38 | # TODO: better algorithm |
| 39 | while (length(record[1]) != 10 || match(substr(record[1], 2, 1), "[r-]") == 0) |
| 40 | { |
| 41 | # line without real contents |
| 42 | if (length(line) == 0) { |
| 43 | next |
| 44 | } |
| 45 | line=substr(line, length(record[1])+1) |
| 46 | while (length(line) != 0 && substr(line, 1, 1) != " ") |
| 47 | { |
| 48 | line=substr(line, 2) |
| 49 | } |
| 50 | split(line, record, " ") |
| 51 | } |
| 52 | |
| 53 | perm=record[1] |
| 54 | # Block and character special files not supported |
| 55 | # Change [bc] to [bcl] if symbolic links should not show up either |
| 56 | if (match(substr(perm, 1, 1), "[bc]") != 0) |
| 57 | { |
| 58 | next |
| 59 | } |
| 60 | uid=record[2] |
| 61 | if (match(uid, "^[0-9]*$") != 0) |
| 62 | { |
| 63 | uid=sprintf("%-8d", uid) |
| 64 | } |
| 65 | gid=record[3] |
| 66 | if (match(gid, "^[0-9]*$") != 0) |
| 67 | { |
| 68 | gid=sprintf("%-8d", gid) |
| 69 | } |
| 70 | size=record[4] |
| 71 | month=record[6] |
| 72 | day=record[7] |
| 73 | tm=substr(record[8], 1, 5) |
| 74 | year=record[9] |
| 75 | name=substr(line, index(line, sprintf("%s:", tm))+14) |
| 76 | # TODO: find symbolic link target (probably the link has to be extracted) |
| 77 | printf "%s 1 %s %s %8d %3s %02d %04d %s %s\n", perm, uid, gid, size, month, day, year, tm, name |
| 78 | }' |
| 79 | } |
| 80 | |
| 81 | mcdarfs_copyout () |
| 82 | { |
| 83 | # Dummy directory necessary since dar cannot output to stdout or named file |
| 84 | mkdir "$3.dir.tmp" |
| 85 | chmod 700 "$3.dir.tmp" |
| 86 | if [ ! -d "$3.dir.tmp" ]; then exit 1; fi |
| 87 | $DAR -x "$BASENAME" -N -O -Q -wa -g "$2" -R "$3.dir.tmp" >/dev/null 2>&1 |
| 88 | if [ -e "$3.dir.tmp/$2" ]; then |
| 89 | mv "$3.dir.tmp/$2" "$3" |
| 90 | rm -rf "$3.dir.tmp" |
| 91 | else |
| 92 | rm -rf "$3.dir.tmp" |
| 93 | exit 1 |
| 94 | fi |
| 95 | } |
| 96 | |
| 97 | umask 077 |
| 98 | cmd="$1" |
| 99 | shift |
| 100 | case "$cmd" in |
| 101 | list) mcdarfs_list "$@" ;; |
| 102 | copyout) mcdarfs_copyout "$@" ;; |
| 103 | *) exit 1 ;; |
| 104 | esac |
| 105 | exit 0 |