u7z 4.0 KB

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