tazpkg annotate modules/remove @ rev 914

remove: no local variables out of functions
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jul 23 10:15:28 2016 +0200 (2016-07-23)
parents 92509572ed28
children 07ef2e1b4273
rev   line source
al@840 1 #!/bin/sh
al@840 2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
al@840 3 # remove - TazPkg module
al@840 4 # Remove packages
al@840 5
al@840 6
al@840 7 # Connect function libraries
al@840 8 . /lib/libtaz.sh
al@840 9
al@840 10 # Get TazPkg working environment
al@840 11 . @@MODULES@@/getenv
al@840 12
al@840 13
al@840 14
al@840 15
al@840 16 # Log activity
al@840 17
al@840 18 log_pkg() {
al@840 19 [ -w "$LOG" ] &&
al@840 20 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)" >> "$LOG"
al@840 21 }
al@840 22
al@840 23
al@840 24 # Interactive mode
al@840 25
al@840 26 im() { tty -s; }
al@840 27
al@840 28
al@844 29 # Block of receipt function callers
al@844 30 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
al@844 31 # Few receipts function should be patched now.
al@844 32
al@844 33 # Input: $1 = path to the receipt to be processed
al@844 34
al@844 35 call_pre_remove() {
al@844 36 local tmp
al@844 37 if grep -q '^pre_remove()' "$1"; then
al@844 38 action 'Execute pre-remove commands...'
al@844 39 tmp="$(mktemp)"
al@844 40 cp "$1" "$tmp"
al@844 41 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 42 ( . "$tmp"; pre_remove "$root" )
al@844 43 status
al@844 44 rm "$tmp"
al@844 45 fi
al@844 46 }
al@844 47
al@844 48 call_post_remove() {
al@844 49 local tmp
al@844 50 if grep -q '^post_remove()' "$1"; then
al@844 51 action 'Execute post-remove commands...'
al@844 52 tmp="$(mktemp)"
al@844 53 cp "$1" "$tmp"
al@844 54 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 55 ( . "$tmp"; post_remove "$root" )
al@844 56 status
al@844 57 rm "$tmp"
al@844 58 fi
al@844 59 }
al@844 60
al@844 61
al@887 62 # return possible name for a virtual package name
al@840 63
al@887 64 virtual_pkg() {
al@887 65 # input: $1 virtual package name
al@887 66 # $2 repository db directory
al@887 67 # output: display possible package name
al@840 68
al@887 69 debug "\nvirtual_pkg('$1', '$2')"
al@887 70 local i
al@887 71 unset IFS
al@887 72 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
al@887 73 if echo $i | fgrep -q : ; then
al@887 74 # format 'alternative:newname'
al@887 75 # if alternative is installed then substitute newname
al@887 76 if [ -f $INSTALLED/${i%:*}/receipt ]; then
al@887 77 # substitute package dependency
al@887 78 echo ${i#*:}
al@887 79 return
al@887 80 fi
al@887 81 elif ! grep -q "^$1 " "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
al@887 82 # unconditional substitution
al@887 83 echo $i
al@887 84 return
al@887 85 fi
al@887 86 done
al@887 87 # the real package name
al@887 88 echo $1
al@887 89 }
al@887 90
al@887 91
al@887 92
al@887 93
al@887 94 for rep in $PRIORITY; do
al@887 95 [ ! -f "$rep/packages.info" ] && continue
al@887 96 PACKAGE="$(virtual_pkg "$1" "$rep")"
al@887 97 [ "$PACKAGE" != "$1" ] && break
al@887 98 done
al@840 99
al@840 100 if [ ! -f "$INSTALLED/$PACKAGE/receipt" ]; then
al@840 101 newline; _ 'Package "%s" is not installed.' "$PACKAGE"
al@840 102 exit 1
al@840 103 fi
al@840 104
al@840 105 . "$INSTALLED/$PACKAGE/receipt"
al@840 106
al@840 107 # Info #1: dependent packages (to be processed later)
al@840 108 ALTERED="$(awk -F$'\t' -vp=" $PACKAGE " 'index(" " $8 " ", p) { printf " %s\n", $1 }' "$PKGS_DB/installed.info")"
al@840 109
al@840 110 if [ -n "$ALTERED" ]; then
al@840 111 _ 'The following packages depend on package "%s":' "$PACKAGE"
al@840 112 echo "$ALTERED"
al@840 113 fi
al@840 114
al@840 115 # Info #2: changed packages (to be processed later)
al@840 116 REFRESH=$(cd "$INSTALLED"; grep -sl "^$PACKAGE$" */modifiers)
al@840 117
al@840 118 if [ -n "$REFRESH" ]; then
al@840 119 _ 'The following packages have been modified by package "%s":' "$PACKAGE"
al@840 120 for i in $REFRESH; do
al@840 121 echo " ${i%/modifiers}"
al@840 122 done
al@840 123 fi
al@840 124
al@840 125 # Confirmation
al@913 126 if [ -n "$noconfirm$yes" ] || im && [ -z "$auto" ]; then
al@840 127 confirm "$(_ 'Remove package "%s" (%s)? (y/N)' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 128 if [ "$?" -ne 0 ]; then
al@840 129 newline; _ 'Uninstallation of package "%s" cancelled.' "$PACKAGE"
al@840 130 exit 0
al@840 131 fi
al@840 132 fi
al@913 133 # We are here: non-interactive mode, or --auto, or --yes, or answer 'y'
al@840 134
al@840 135 # Removing package
al@840 136 title 'Removing package "%s"' "$PACKAGE"
al@840 137
al@887 138 # [1/5] Pre-remove commands
al@844 139 call_pre_remove "$INSTALLED/$PACKAGE/receipt"
al@844 140
al@840 141
al@887 142 # [2/5] Removing files
al@840 143 action 'Removing all files installed...'
al@857 144
al@857 145 # NOTE: package 'faenza-icon-theme' install time: 12s; removing time ~ 11min on my system o_O
al@860 146 # After optimization: 3s! (Long) for-loops are (big) evil ;)
al@857 147
al@857 148 # NOTE: many packages contains filenames with spaces:
al@857 149 # lzcat /var/lib/tazpkg/files.list.lzma | awk -F" " '{if(NF>2)print $1}' | sed 's|:$||' | uniq
al@857 150 # Redefine IFS to only-new-line field separator:
al@857 151 IFS=$'\n'
al@857 152
al@857 153 files2remove="$(mktemp)"
al@857 154
al@857 155 debug '\nDetermine which files to remove...'
al@840 156 if [ -f "$INSTALLED/$PACKAGE/modifiers" ]; then
al@857 157 debug ' (modifiers detected)'
al@857 158
al@857 159 mods="$(mktemp)"
al@857 160 for mod in $(cat "$INSTALLED/$PACKAGE/modifiers"); do
al@857 161 cat "$INSTALLED/$mod/files.list" >> "$mods" 2>/dev/null
al@840 162 done
al@857 163
al@857 164 awk -vroot="$root" -vfl="$INSTALLED/$PACKAGE/files.list" '
al@857 165 {
al@857 166 if (FILENAME == fl)
al@857 167 f[$0] = 1;
al@857 168 else
al@857 169 f[$0] = "";
al@857 170 }
al@857 171 END {
al@857 172 for (i in f) {
al@857 173 if (f[i] == 1) printf "%s%s\n", root, i;
al@857 174 }
al@857 175 }' "$INSTALLED/$PACKAGE/files.list" "$mods" > "$files2remove"
al@857 176 rm "$mods"
al@840 177 else
al@857 178 debug ' (modifiers not detected)'
al@857 179
al@857 180 awk -vroot="$root" '{ printf "%s%s\n", root, $0; }' \
al@857 181 "$INSTALLED/$PACKAGE/files.list" > "$files2remove"
al@857 182 fi
al@857 183
al@857 184 debug 'Removing files...'
al@857 185 xargs rm -f < "$files2remove"
al@857 186
al@860 187 debug 'Removing folders...'
al@857 188 awk '
al@857 189 BEGIN {
al@857 190 FS = "/"; OFS = "/";
al@857 191 }
al@857 192 {
al@857 193 # removing filename beyond the last "/"
al@857 194 $NF = "";
al@857 195 if (! a[$0]) {
al@857 196 a[$0] = 1; print;
al@857 197 }
al@860 198 }' "$files2remove" | xargs rmdir -p 2>/dev/null
al@857 199
al@860 200 rm "$files2remove"
al@857 201 unset IFS
al@857 202
al@840 203 status
al@840 204
al@887 205 # [3/5] Post-remove commands
al@844 206 call_post_remove "$INSTALLED/$PACKAGE/receipt"
al@840 207
al@887 208 # [4/5] Update system databases
pascal@914 209 fl="$INSTALLED/$PACKAGE/files.list" upd=0 udesk umime uicon uschm ukrnl
al@887 210
al@887 211 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
al@887 212 fgrep -q /usr/share/mime "$fl" && umime='yes'
al@887 213 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
al@887 214 fgrep -q /usr/share/glib-2.0/schemas "$fl" && uschm='yes'
al@887 215 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
al@887 216 fgrep -q /lib/modules "$fl" && ukrnl='yes'
al@887 217
al@887 218 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
al@887 219 action 'Update system databases...'
al@887 220 upd=1
al@887 221 fi
al@887 222
al@887 223 # package 'desktop-file-utils'
al@887 224 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
al@887 225 # package 'shared-mime-info'
al@887 226 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
al@887 227 # packages 'gtk+', 'gtk+3'
al@887 228 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
al@887 229 # package 'glib'
al@897 230 # hide messages like next because they are unresolved (we may to patch glib to hide them, almost the same)
al@897 231 # warning: Schema '*' has path '*'. Paths starting with '/apps/', '/desktop/' or '/system/' are deprecated.
al@897 232 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas 2>&1 | fgrep -v '/apps/'
al@887 233 # package 'gdk-pixbuf'
al@887 234 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
al@887 235 # packages 'busybox', 'kmod', 'depmod'
al@887 236 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
al@887 237
al@887 238 [ "$upd" -eq 1 ] && status
al@887 239
al@887 240
al@887 241 # [5/5] Remove package receipt and remove it from databases
al@840 242 action 'Removing package receipt...'
al@840 243 rm -rf "$INSTALLED/$PACKAGE"
al@840 244 sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION.tazpkg$/d" "$PKGS_DB/installed.$SUM"
al@840 245 sed -i "/^$PACKAGE /d" "$PKGS_DB/installed.info"
al@840 246 status
al@840 247
al@840 248 footer "$(_ 'Package "%s" (%s) removed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 249
al@840 250 # Log this activity
al@840 251 log_pkg Removed
al@840 252
al@840 253 # Stop if non-interactive mode and no --auto option
al@840 254 if ! im && [ -z "$auto" ]; then exit 0; fi
al@840 255
al@840 256 # Process dependent packages
al@840 257 if [ -n "$ALTERED" ]; then
al@840 258 if [ -n "$auto" ]; then
al@840 259 answer=0
al@840 260 else
al@840 261 confirm "$(_ 'Remove packages depending on package "%s"? (y/N)' "$PACKAGE")"
al@840 262 answer=$?
al@840 263 fi
al@840 264 if [ "$answer" -eq 0 ]; then
al@840 265 for i in $ALTERED; do
al@840 266 if [ -d "$INSTALLED/$i" ]; then
al@840 267 tazpkg remove $i
al@840 268 fi
al@840 269 done
al@840 270 fi
al@840 271 fi
al@840 272
al@840 273 # Process changed packages
al@840 274 if [ -n "$REFRESH" ]; then
al@840 275 if [ -n "$auto" ]; then
al@840 276 answer=0
al@840 277 else
al@840 278 confirm "$(_ 'Reinstall packages modified by package "%s"? (y/N)' "$PACKAGE")"
al@840 279 answer=$?
al@840 280 fi
al@840 281 if [ "$answer" -eq 0 ]; then
al@840 282 for i in $REFRESH; do
al@840 283 if [ "$(wc -l < "$INSTALLED/$i")" -gt 1 ]; then
al@849 284 _ 'Package "%s" was modified by "%s" and other packages. It will not be reinstalled.' \
al@849 285 "${i%/modifiers}" "$PACKAGE"
al@849 286 _ 'Check "%s" for reinstallation.' "$INSTALLED/$i"
al@849 287
al@840 288 continue
al@840 289 fi
al@840 290 rm -r "$INSTALLED/$i"
al@840 291 tazpkg get-install ${i%/modifiers} --forced
al@840 292 done
al@840 293 fi
al@840 294 fi
al@840 295