source: pkgtools/installpkg

Last change on this file was be5050d, checked in by Michał Andrzej Wróbel <maw@…>, 8 years ago

fix GNU tar 1.25 broken symlink behavior

  • Property mode set to 100755
File size: 6.0 KB
Line 
1#!/bin/sh
2#
3# installpkg
4# Simple tool which installs and upgrades Lintrack packages
5#
6# This file is licensed under GNU GPL 2.
7# Copyright (C) 2006-2009 ASN Sp. z o.o.
8#
9# Authors: Pawel Foremski <pjf@asn.pl>
10#          Michal Wrobel <xmxwx@asn.pl>
11#
12
13# paths
14if [ "$FAKEBOX" = "1" ]; then
15        ROOTDIR="$FB_ROOTFS"
16        VARDIR="$FB_ROOTFS/var/pkg"
17
18        sh() { sudo /usr/sbin/chroot $ROOTDIR /bin/sh "$@"; }
19else
20        ROOTDIR=/
21        VARDIR=/var/pkg
22fi
23
24# remove temporary files that this script could have created
25cleanup()
26{
27        rm -f $ROOTDIR/.* 2> /dev/null
28}
29
30run_ldconfig()
31{
32        echo "ldconfig -r $ROOTDIR"
33        ldconfig -r $ROOTDIR
34}
35
36run_depmod()
37{
38        ver=`ls -1t $ROOTDIR/lib/modules/ | sed -re '/^2\.6\./!d; q;'`
39
40        echo "depmod -b $ROOTDIR $ver"
41        depmod -b $ROOTDIR "$ver"
42}
43
44usage()
45{
46        cat <<- EOF
47        Usage: ${CALL}pkg [OPTIONS] [<package file>]
48       
49        $TITLE given package file.
50       
51        Options:
52                --rootdir <dir>       use <dir> as /
53                --nolibs              dont run ldconfig nor depmod
54                --ldconfig            run ldconfig and exit
55                --depmod              run depmod and exit
56                --verbose             show extracted files
57        EOF
58}
59
60# switch operation mode
61case "`basename $0`" in
62        installpkg) MODE="install"; TITLE="Installing"; CALL="install";;
63        upgradepkg) MODE="upgrade"; TITLE="Upgrading";  CALL="upgrade";;
64        *) echo "Call me as installpkg/upgradepkg"; exit 1;;
65esac
66
67# parse arguments
68while { [ -n "$1" ] && [ -z "${1##-*}" ]; }; do
69        case "$1" in
70                --nolibs)      OPT_NOLIBS=1;;
71                --ldconfig)    OPT_JUSTLD=1;;
72                --depmod)      OPT_JUSTDEPMOD=1;;
73                --verbose)     OPT_VERBOSE=1;;
74                --rootdir)
75                        ROOTDIR="`readlink -f $2`"
76                        VARDIR="$ROOTDIR/var/pkg"
77                        shift
78                        ;;
79                *) echo "Invalid option: $1" >&2; exit 1;;
80        esac
81        shift
82done
83
84[ "$OPT_JUSTLD" = "1" ] && run_ldconfig
85[ "$OPT_JUSTDEPMOD" = "1" ] && run_depmod
86[ -n "${OPT_JUSTLD}${OPT_JUSTDEPMOD}" ] && exit
87
88# check cl arguments
89PKG="$1"
90if [ -z "$PKG" ]; then
91        usage
92        exit 0
93elif [ ! -f "$PKG" ]; then
94        echo "$PKG: no such file" >&2
95        exit 1
96fi
97
98# get basic information about the package
99cd `dirname $PKG` || exit 1
100pkgdir=`pwd`
101pkglname="`basename $PKG`"
102pkg="$pkgdir/$pkglname"
103
104# start
105echo "$TITLE: $pkglname"
106mkdir -p $ROOTDIR && cd $ROOTDIR || exit 1
107
108# extract package information from filename
109set -- `echo "$pkglname" | sed -re 's/^(.*)-(.*)-(.*)-([0-9]+).*/\1 \2 \3 \4/g'`
110echo "$pkglname" | grep -q '\-big.pkg.tar.gz' && big="1"
111
112if { [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; }; then
113        echo "pkg: '$pkglname' is not a valid Lintrack 2.1+ package"
114        cleanup
115        exit 1
116fi >&2
117
118pkgname="$1"
119fixrel="$4"
120pkgver="$2-$3-$fixrel"
121
122# check if user is not dumb
123if [ "$MODE" = "upgrade" ]; then
124        if [ ! -f "$VARDIR/$pkgname/installed" ]; then # check if package is already installed
125                echo "pkg: package '$pkgname' is not installed: cannot upgrade"
126                cleanup
127                exit 1
128        elif [ $fixrel -gt 1 ]; then
129                # check if installed "bigness" matches this from the fixrel package
130                if [ -f "$VARDIR/$pkgname/big" ]; then
131                        [ "$big" != "1" ] && { echo "pkg: you should use a -big fixrel package"; cleanup; exit 1; }
132                else
133                        [ "$big" = "1" ] && { echo "pkg: you should NOT use a -big fixrel package"; cleanup; exit 1; }
134                fi
135
136                # check if all fixrels up to $fixrel have already been installed
137                ifixrel="`cat $VARDIR/$pkgname/installed | egrep -o '[0-9]+$'`"
138                if [ $((fixrel-1)) -ne $ifixrel ]; then
139                        echo "pkg: you need to install the fixrel=$((fixrel-1)) version of '$pkgname' package first"
140                        cleanup
141                        exit 1
142                fi
143        fi
144elif { [ "$MODE" = "install" ] && [ $fixrel -gt 1 ]; }; then
145        echo "pkg: '$pkglname' is a fixrel package for upgrades only: cannot install"
146        cleanup
147        exit 1
148fi >&2
149
150# run function preparing for operation to be carried
151if { [ $fixrel -eq 1 ] && tar -xzf $pkg .INSTALL 2>/dev/null; }; then
152        sh .INSTALL pre_$CALL $pkgver || {
153                echo "pkg: running 'pre_$CALL $pkgver' failed"
154                cleanup; exit 1
155        } >&2
156        runpost=1
157fi
158
159# if upgrading, store current permissions of all files from the target package
160if [ "$MODE" = "upgrade" ]; then
161        echo "Storing permissions"
162
163        if [ $fixrel -eq 1 ]; then
164                filelist="tar -Oxzf $pkg .FILELIST"
165        else
166                filelist="cat $VARDIR/$pkgname/FILELIST"
167        fi
168
169        $filelist 2>/dev/null | while read file; do
170                [ -e "$file" ] && stat -c "%a %n" $file
171        done > .PERMISSIONS
172
173        # and make sure that /etc/fc won't be bothered, except if it's a fixrel
174        [ $fixrel -eq 1 ] && exclude='etc/fc\netc/fc/*'
175        pkg_opt='--keepfc'
176fi
177
178# if package is already installed, remove the old one first
179if { [ -f "$VARDIR/$pkgname/installed" ] && [ $fixrel -eq 1 ]; }; then
180        # pkg is supposed to be in the same dir as installpkg
181        `dirname $0`/pkg remove $pkg_opt "$pkgname"
182fi
183
184# unpack new files
185echo -e "$exclude" | tar -X - -x${OPT_VERBOSE:+v}hzf $pkg || {
186        echo "pkg: couldn't extract package contents"
187        cleanup
188        exit 1
189} >&2
190
191# restore permissions, if needed
192if [ "$MODE" = "upgrade" ]; then
193        echo "Restoring permissions"
194        cat .PERMISSIONS | while read file; do
195                set -- $file
196                p="$1"; shift
197                chmod "$p" "$*"
198        done
199fi
200
201if [ "$OPT_NOLIBS" != "1" ]; then
202        if [ $fixrel -eq 1 ]; then
203                filelist=".FILELIST"
204        else
205                filelist="$VARDIR/$pkgname/FILELIST"
206        fi
207
208        egrep -q '^lib/modules' $filelist && run_depmod
209        egrep -v '^lib/modules' $filelist | egrep -q 'lib/.+' && run_ldconfig
210fi
211
212if [ $fixrel -eq 1 ]; then
213        # run post-function, if needed
214        if [ "$runpost" = 1 ]; then
215                sh .INSTALL post_$CALL $pkgver || {
216                        echo "pkg: running 'post_$CALL $pkgver' failed"
217                        echo "pkg: WARNING: igoring this error"
218                } >&2
219        fi
220
221        # register new package in database
222        mkdir -p "$VARDIR/$pkgname"
223        cd "$VARDIR/$pkgname" || { echo "pkg: couldn't register package in database"; cleanup; exit 1; }
224
225        echo "$pkgver" > installed
226        if [ -n "$big" ]; then touch big; else rm -f big; fi
227        mv -f $ROOTDIR/.PKGINFO PKGINFO
228        mv -f $ROOTDIR/.FILELIST FILELIST
229else
230        # run .FIX
231        if tar -xzf $pkg .FIX 2>/dev/null; then
232                sh .FIX || { echo "pkg: running .FIX script failed"; cleanup; exit 1; }
233        fi
234
235        # update version information in database
236        cd "$VARDIR/$pkgname" || { echo "pkg: couldn't update package information"; cleanup; exit 1; }
237        echo "$pkgver" > installed
238fi
239
240cleanup
241exit 0
242
243# vim: tw=120
Note: See TracBrowser for help on using the repository browser.