| 1 | #!/bin/sh |
| 2 | # Midnight Commander - WIM suport |
| 3 | # |
| 4 | # Written by: |
| 5 | # Vadim Kalinnikov <moose@ylsoftware.com>, |
| 6 | # |
| 7 | # This file is part of the Midnight Commander. |
| 8 | # |
| 9 | # It required wimtools: https://wimlib.net/ |
| 10 | # On Debian/Ubuntu wimtools can be installed via: |
| 11 | # apt install wimtools |
| 12 | |
| 13 | which wimlib-imagex 2>/dev/null > /dev/null || exit 1 |
| 14 | WIM=`which wimlib-imagex` |
| 15 | AWK=`which awk` |
| 16 | |
| 17 | test -n "$2" || exit 1 |
| 18 | |
| 19 | ACTION=$1 |
| 20 | WIMFILENAME=$2 |
| 21 | |
| 22 | mcwim_list() { |
| 23 | # Here we can use "Image count" from output, |
| 24 | # but on some borken images we can get garbage, instead of number |
| 25 | IMAGECOUNT=`${WIM} info ${WIMFILENAME} | grep Index: | grep -v Boot | wc -l` |
| 26 | IMGNUM=1 |
| 27 | VUID=`id -nu` |
| 28 | VGID=`id -ng` |
| 29 | while [ ${IMGNUM} -le ${IMAGECOUNT} ]; do |
| 30 | ${WIM} dir ${WIMFILENAME} ${IMGNUM} --detailed | \ |
| 31 | ${AWK} -v uid=${VUID} -v gid=${VGID} -v imgnum=${IMGNUM} ' |
| 32 | /^----------------------------------------------------------------------------/,/^$/ { |
| 33 | if (match($0, /^Full Path/)) { |
| 34 | split($0, namesrc, "\""); |
| 35 | name=namesrc[2]; |
| 36 | } |
| 37 | if (match($0, /FILE_ATTRIBUTE_DIRECTORY is set/)) { |
| 38 | attr="drwxr-xr-x" |
| 39 | } |
| 40 | if (match($0, /^Uncompressed size/)) { |
| 41 | size=$4; |
| 42 | } |
| 43 | if (match($0, /^Last Write Time/)) { |
| 44 | months["Jan"] = "01"; |
| 45 | months["Feb"] = "02"; |
| 46 | months["Mar"] = "03"; |
| 47 | months["Apr"] = "04"; |
| 48 | months["May"] = "05"; |
| 49 | months["Jun"] = "06"; |
| 50 | months["Jul"] = "07"; |
| 51 | months["Aug"] = "08"; |
| 52 | months["Sep"] = "09"; |
| 53 | months["Oct"] = "10"; |
| 54 | months["Nov"] = "11"; |
| 55 | months["Dec"] = "12"; |
| 56 | split($0, mtimesrc, " "); |
| 57 | mtime=sprintf("%s/%s/%s %s", months[mtimesrc[6]], mtimesrc[7], mtimesrc[9], mtimesrc[8]); |
| 58 | } |
| 59 | |
| 60 | if (match($0, /^$/)) { |
| 61 | printf("%s 1 %s %s % 20s % 24s IMAGE%s%s\n", |
| 62 | attr, uid, gid, size, mtime, imgnum, name); |
| 63 | name = size = mtime = ""; |
| 64 | attr="-rw-r--r--"; |
| 65 | } |
| 66 | } |
| 67 | ' |
| 68 | |
| 69 | IMGNUM=$((IMGNUM+1)) |
| 70 | done |
| 71 | |
| 72 | # Virtual files |
| 73 | echo "-r-xr-xr-x 1 ${VUID} ${VGID} 1 01/01/2020 00:00:00 OPTIMIZE" |
| 74 | echo "-r-xr-xr-x 1 ${VUID} ${VGID} 1 01/01/2020 00:00:00 VERIFY" |
| 75 | } |
| 76 | |
| 77 | mcwim_copyout() { |
| 78 | # Virtual files |
| 79 | if [ "${FILENAMESRC}" = "OPTIMIZE" ]; then |
| 80 | echo "#/bin/sh" > ${FILENAMEDST} |
| 81 | echo "# Run this to optimize archive" >> ${FILENAMEDST} |
| 82 | echo "${WIM} optimize \"${WIMFILENAME}\"" >> ${FILENAMEDST} |
| 83 | chmod a+x ${FILENAMEDST} |
| 84 | exit 0; |
| 85 | elif [ "${FILENAMESRC}" = "VERIFY" ]; then |
| 86 | echo "#/bin/sh" > ${FILENAMEDST} |
| 87 | echo "# Run this to verify archive" >> ${FILENAMEDST} |
| 88 | echo "${WIM} verify \"${WIMFILENAME}\"" >> ${FILENAMEDST} |
| 89 | chmod a+x ${FILENAMEDST} |
| 90 | exit 0; |
| 91 | fi |
| 92 | |
| 93 | # Filename must contain imgnum |
| 94 | echo ${FILENAMESRC} | grep -E '^IMAGE[0-9]+/' || exit 1 |
| 95 | |
| 96 | IMGNUM=`echo ${FILENAMESRC} | cut -d '/' -f1 | sed "s/IMAGE//"` |
| 97 | REALFILENAME=`echo ${FILENAMESRC}|sed "s/IMAGE${IMGNUM}//"` |
| 98 | ${WIM} extract ${WIMFILENAME} ${IMGNUM} ${REALFILENAME} --to-stdout > ${FILENAMEDST} |
| 99 | } |
| 100 | |
| 101 | mcwim_copyin() { |
| 102 | # Skip virtual files |
| 103 | test "${FILENAMEDST}" = "OPTIMIZE" && exit 1; |
| 104 | test "${FILENAMEDST}" = "VERIFY" && exit 1; |
| 105 | |
| 106 | # Filename must contain imgnum |
| 107 | echo ${FILENAMEDST} | grep -E '^IMAGE[0-9]+/' || exit 1 |
| 108 | |
| 109 | IMGNUM=`echo ${FILENAMEDST} | cut -d '/' -f1 | sed "s/IMAGE//"` |
| 110 | REALFILENAME=`echo ${FILENAMEDST}|sed "s/IMAGE${IMGNUM}//"` |
| 111 | echo "add \"${FILENAMESRC}\" \"${REALFILENAME}\"" | ${WIM} update ${WIMFILENAME} ${IMGNUM} > /dev/null |
| 112 | } |
| 113 | |
| 114 | |
| 115 | mcwim_rm() { |
| 116 | # Skip virtual files |
| 117 | test "${FILENAMESRC}" = "OPTIMIZE" && exit 0; |
| 118 | test "${FILENAMESRC}" = "VERIFY" && exit 0; |
| 119 | |
| 120 | # Filename must contain imgnum |
| 121 | echo ${FILENAMESRC} | grep -E '^IMAGE[0-9]+/' || exit 1 |
| 122 | |
| 123 | IMGNUM=`echo ${FILENAMESRC} | cut -d '/' -f1 | sed "s/IMAGE//"` |
| 124 | REALFILENAME=`echo ${FILENAMESRC}|sed "s/IMAGE${IMGNUM}//"` |
| 125 | |
| 126 | if [ -z "${REALFILENAME}" ]; then |
| 127 | # If user want to remove image |
| 128 | ${WIM} delete ${WIMFILENAME} ${IMGNUM} |
| 129 | else |
| 130 | # remove regular file or directory |
| 131 | echo "delete \"${REALFILENAME}\"" | ${WIM} update ${WIMFILENAME} ${IMGNUM} --force --recursive > /dev/null |
| 132 | fi |
| 133 | } |
| 134 | |
| 135 | mcwim_run() { |
| 136 | case ${RUNFILENAME} in |
| 137 | OPTIMIZE) |
| 138 | ${WIM} optimize ${WIMFILENAME} |
| 139 | exit 0; |
| 140 | ;; |
| 141 | VERIFY) |
| 142 | ${WIM} verify ${WIMFILENAME} |
| 143 | exit 0; |
| 144 | ;; |
| 145 | esac |
| 146 | exit 1; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | mcwim_mkdir() { |
| 151 | # New dirname must contain imgnum |
| 152 | echo ${NEWDIRNAME} | grep -E '^IMAGE[0-9]+/' || exit 1 |
| 153 | IMGNUM=`echo ${NEWDIRNAME} | cut -d '/' -f1 | sed "s/IMAGE//"` |
| 154 | REALDIRNAME=`echo ${NEWDIRNAME}|sed "s/IMAGE${IMGNUM}//"` |
| 155 | test -z "${REALDIRNAME}" && exit 1 |
| 156 | |
| 157 | TMPDIR=`mktemp -d` |
| 158 | DSTBASENAME=`basename ${REALDIRNAME}` |
| 159 | SRCDIRNAME="${TMPDIR}/${DSTBASENAME}" |
| 160 | mkdir -p ${SRCDIRNAME} |
| 161 | echo "add \"${SRCDIRNAME}\" \"${REALDIRNAME}\"" | ${WIM} update ${WIMFILENAME} ${IMGNUM} > /dev/null |
| 162 | rm -rf ${TMPDIR} |
| 163 | } |
| 164 | |
| 165 | #echo "'$1' '$2' '$3' '$4' '$5'" >> /tmp/mcdebug |
| 166 | |
| 167 | case "$ACTION" in |
| 168 | list) |
| 169 | mcwim_list |
| 170 | ;; |
| 171 | |
| 172 | copyout) |
| 173 | test -n "$4" || exit 1 |
| 174 | FILENAMESRC="$3" |
| 175 | FILENAMEDST="$4" |
| 176 | mcwim_copyout |
| 177 | ;; |
| 178 | |
| 179 | copyin) |
| 180 | test -n "$4" || exit 1 |
| 181 | FILENAMEDST="$3" |
| 182 | FILENAMESRC="$4" |
| 183 | mcwim_copyin |
| 184 | ;; |
| 185 | |
| 186 | rm|rmdir) |
| 187 | test -n "$3" || exit 1 |
| 188 | FILENAMESRC="$3" |
| 189 | mcwim_rm |
| 190 | ;; |
| 191 | |
| 192 | run) |
| 193 | test -n "$3" || exit 1 |
| 194 | RUNFILENAME="$3" |
| 195 | mcwim_run |
| 196 | ;; |
| 197 | |
| 198 | mkdir) |
| 199 | test -n "$3" || exit 1 |
| 200 | NEWDIRNAME="$3" |
| 201 | mcwim_mkdir |
| 202 | ;; |
| 203 | |
| 204 | |
| 205 | *) |
| 206 | exit 1 |
| 207 | ;; |
| 208 | esac |