]> pd.if.org Git - zpackage/blob - zpm-update
fix uninitialized memory use
[zpackage] / zpm-update
1 #!/bin/sh
2
3 warn() {
4         echo $* 1>&2
5 }
6
7 run_ldconfig() {
8         if [ $(id -u) -eq 0 ]; then
9                 if [ ! -d $1/etc ]; then
10                         warn "no etc directory in $1, skipping ldconfig"
11                 elif [ -f $1/sbin/ldconfig ]; then
12                         $1/sbin/ldconfig -r ${1:-/}
13                 elif [ -f /sbin/ldconfig ]; then
14                         /sbin/ldconfig -r ${1:-/}
15                 else
16                         true
17                 fi
18         fi
19 }
20
21 changeall() {
22         for pkgid in $(zpm list -s $1); do
23                 zpm pkg $pkgid status=$2
24         done
25 }
26
27 die() {
28         if [ $# -gt 0 ]; then
29                 echo $* 1>&2
30         fi
31         zpm log -i -a 'aborted install' -t "$pkgid" "$*"
32         changeall installing
33         changeall removing installed
34         changeall updating installed
35         exit 1
36 }
37
38 current_version() (
39         zpm list -s installed $(zpm parse -n $1)
40 )
41
42 maybe_run() (
43         phase=$1
44         pkgid=$2
45         run=${3:-0}
46         current=$4
47
48         if [ $run -gt 0 ]; then
49                 zpm script -p $phase $pkgid $current
50                 rv=$?
51                 if [ $rv -ne 0 ]; then
52                         warn "$phase script for $pkgid failed"
53                         exit $rv
54                 fi
55         else
56                 if zpm script -Fql -p $phase $pkgid; then
57                         zpm note -e -p $pkgid -m "$phase script not run"
58                 fi
59         fi
60 )
61
62 validity_check() {
63         rv=0
64         # check if we're installing something already
65         var=$(zpm list -s installing | wc -l)
66         if [ $var -gt 0 ]; then
67                 zpm list -v -f $localdb -s installing 
68                 warn "already ($localdb) installing $var package(s)"
69                 rv=1
70         fi
71         var=$(zpm list -s removing | wc -l)
72         if [ $var -gt 0 ]; then
73                 zpm list -v -f $localdb -s removing 
74                 warn "already ($localdb) removing $var package(s)"
75                 rv=1
76         fi
77         var=$(zpm list -s updating | wc -l)
78         if [ $var -gt 0 ]; then
79                 zpm list -v -f $localdb -s updating 
80                 warn "already ($localdb) updating $var package(s)"
81                 rv=1
82         fi
83         if [ $rv -ne 0 ]; then
84                 die "aborting update"
85         fi
86 }
87
88 dryrun=0
89 verbose=0
90 runscripts=1
91 runconfigure=1
92 absorb=0
93 overwrite=0
94 syncopts=''
95
96 packagepath="/var/lib/zpm/packages"
97 to_install=
98 to_remove=
99 to_update=
100 install_for_libs=''
101
102 backup=0
103 ignorelibdeps=0
104 justlist=0
105 pullrepos=1
106
107 # treat un-adorned packages as uninstalls
108 uninstall=0
109
110 # TODO option to attempt to resume an ongoing install
111 # zpm-install [-SCn] [ -d localdb ] [ -f pkgfile ] [ -R installroot ] pkgstr ...
112 while getopts f:d:R:nSCvAObBLUzp opt; do
113         case $opt in
114                 f) pkgfile="$OPTARG" ;;
115                 d) localdb="$OPTARG" ;;
116                 P) packagepath="$OPTARG" ;;
117                 D) packagepath="$OPTARG:$packagepath" ;;
118                 R) rootdir="$OPTARG" ;;
119                 S) runscripts=0 ;;
120                 C) runconfigure=0 ;;
121                 A) absorb=1; syncopts="${syncopts} -A" ;;
122                 O) overwrite=1; syncopts="${syncopts} -O" ;;
123                 n) dryrun=1 ;;
124                 v) verbose=1 ;;
125                 b) backup=1 ;;
126                 B) backup=0 ;;
127                 L) ignorelibdeps=1 ;;
128                 l) justlist=1 ;;
129                 U) uninstall=1 ;;
130                 z) pullrepos=0 ;;
131                 p) syncopts="$syncopts -p" ;;
132                 *) die "usage ..." ;;
133         esac
134 done
135 shift $(( OPTIND - 1 ))
136
137 if [ -n "$rootdir" ]; then
138         : rootdir=${rootdir%%/}
139         ZPM_ROOT_DIR=$rootdir
140         export ZPM_ROOT_DIR
141 fi
142
143 if [ -n "$localdb" ]; then
144         ZPMDB=$localdb
145 elif [ -z "$ZPMDB" ]; then
146         ZPMDB="$ZPM_ROOT_DIR/var/lib/zpm/local.db"
147 fi
148
149 export ZPMDB
150
151 # create the localdb if needed
152 zpm init $ZPMDB
153
154 zpm test -v "$ZPMDB" || die "$ZPMDB is not a zpm database"
155
156 validity_check
157
158 for op in "$@"; do
159         case "$op" in
160                 -*) pkgid=$(zpm findpkg -I "${op#-}")
161                         if [ -n "$pkgid" ]; then
162                                 to_remove="$to_remove $pkgid"
163                         else
164                                 die "$op not installed"
165                         fi
166                         ;;
167                 +*) pkg=${op#+}
168                         search="$search $pkg"
169                         ;;
170                 *:*) pkg=${op%:*}
171                         file=${op#*:}
172                         pkgid=$(zpm findpkg -f $file $pkg)
173                         if [ -n $pkgid ]; then
174                                 merge="$merge $pkgid:$file"
175                         else
176                                 die "can't find $pkg in $file"
177                         fi
178                         ;;
179                 *)
180                         if [ $uninstall -eq 0 ]; then
181                                 search="$search $op"
182                         else
183                                 pkgid=$(zpm findpkg -I "${op}")
184                                 if [ -n "$pkgid" ]; then
185                                         to_remove="$to_remove $pkgid"
186                                 else
187                                         die "$op not installed"
188                                 fi
189                         fi
190                         ;;
191         esac
192 done
193
194 search=$(echo $search)
195
196 ebail=0
197
198 if [ $pullrepos -eq 1 ]; then
199         echo pulling repos
200         zpm repo pull
201 fi
202
203 if [ $ignorelibdeps -eq 1 ]; then
204         merge=$(zpm search -iIO $search)
205 else
206         merge=$(zpm search -iIO -l $search)
207 fi
208
209 if [ $? -ne 0 ]; then
210         warn "unmet packages or dependencies, or errors"
211         exit 1
212 fi
213
214 installset=
215 for mergepkg in $merge; do
216         pkgid=${mergepkg%%:*}
217         installset="$installset $pkgid"
218 done
219 installset=$(printf "%s\n" $installset | sort -u)
220
221 # TODO check for downgrade
222
223 # fixup package
224 nlist=
225 nfound=
226 baddl=
227 for mergepkg in $merge; do
228         pkgid=${mergepkg%:*}
229         repofile=${mergepkg#*:}
230         if [ ${repofile##*.} = 'repo' ]; then
231                 status=$(zpm pkg -f $repofile $pkgid status)
232                 reponame=$(basename $repofile .repo)
233                 if [ "$status" != 'merged' ]; then
234                         repodir=$(dirname $repofile)
235                         zpmfile=$repodir/$reponame/$pkgid.zpm
236                         if [ -f "$zpmfile" ]; then
237                                 nlist="$nlist $pkgid:$zpmfile"
238                                 continue
239                         else
240                                 echo downloading $zpmfile
241                                 zpm repo fetch $reponame $pkgid
242                                 if [ -f "$zpmfile" ]; then
243                                         nlist="$nlist $pkgid:$zpmfile"
244                                 else
245                                         baddl="$baddl $reponame:$pkgid"
246                                 fi
247                         fi
248                 fi
249         else
250                 nlist="$nlist $mergepkg"
251         fi
252 done
253 merge=${nlist#' '}
254 baddl=${baddl#' '}
255
256 if [ -n "$baddl" ]; then
257         printf 'unable to download: %s\n' $baddl
258         die
259 fi
260
261 for package in $merge; do
262         pkgid=${package%:*}
263         installlist="$installlist $pkgid"
264 done
265 installlist=${installlist#' '}
266
267 if [ $justlist -eq 1 ]; then
268         for package in $merge; do
269                 pkgid=${package%:*}
270                 printf 'install %s\n' $pkgid
271         done
272         for pkgid in $to_remove; do
273                 printf 'remove %s\n' $pkgid
274         done
275         exit 0
276 fi
277
278 if [ -z "$merge" ] && [ -z "$to_remove" ]; then
279         echo nothing to do
280         exit 0
281 fi
282
283 test -n "$merge" && echo "install: $installlist"
284 test -n "$to_remove" && echo "remove: $to_remove"
285
286 # finally, merge everything
287 for mergepkg in $merge; do
288         pkgid=${mergepkg%:*}
289         pkgfile=${mergepkg#*:}
290
291         # we could have found the package already in the localdb
292         # in which case, we don't need to merge it
293         # TODO check for symlinks?
294         if [ $pkgfile != $ZPMDB ]; then
295                 echo merging $pkgfile $pkgid
296                 zpm merge -Fuv -f "$pkgfile" $pkgid
297                 if [ $? -ne 0 ]; then
298                         die "unable to merge $pkgfile"
299                 fi
300         fi
301         to_install="$to_install $pkgid"
302 done
303
304 to_install=${to_install#' '}
305
306 if [ -n "$to_install" ]; then
307         echo "installing: $to_install"
308 fi
309
310 # absorb anything we're updating or removing, if absorb flag set
311
312 if [ -n "$to_install" ]; then
313         for pkgid in $to_install; do
314                 current=$(current_version $pkgid)
315                 maybe_run pre-install $pkgid $runscripts $current || die
316         done
317 fi
318
319 if [ -n "$to_remove" ]; then
320         for pkgid in $to_remove; do
321                 maybe_run pre-remove $pkgid $runscripts || die
322         done
323 fi
324
325 if [ -n "$to_install" ]; then
326         for pkgid in $to_install; do
327                 zpm pkg $pkgid status=installing
328         done
329         zpm shell "$ZPMDB" "update packages set status='updating' where status='installed' and package in (select package from packages where status = 'installing')"
330 fi
331
332 if [ -n "$to_remove" ]; then
333         for pkgid in $to_remove; do
334                 zpm pkg $pkgid status=removing
335         done
336 fi
337
338 if [ $dryrun -gt 0 ]; then
339         runscripts=0 # we don't want to run post scripts on a dry-run
340         syncopts="$syncopts -n"
341 fi
342
343 if [ $verbose -gt 0 ]; then
344         syncopts="${syncopts} -v"
345 else
346         # force -v during development
347         syncopts="${syncopts} -v"
348 fi
349
350 if [ $dryrun -eq 0 ] && [ $backup -eq 1 ]; then
351         true
352         #zpm absorb -s updating
353         #zpm absorb -s removing
354 fi
355
356 zpm syncfs $syncopts || die 'zpm-syncfs failed';
357
358 if [ -n "$to_remove" ]; then
359         for pkgid in $to_remove; do
360                 maybe_run post-remove $pkgid $runscripts
361         done
362 fi
363
364 changeall removing removed
365 changeall updating updated
366
367 echo running ldconfig
368 run_ldconfig $rootdir
369
370 if [ -n "$to_install" ]; then
371         for pkgid in $to_install; do
372                 package=$(zpm parse -n $pkgid)
373                 current=$(zpm list -s installed "$package")
374
375                 maybe_run post-install $pkgid $runscripts $current
376
377                 if [ -n "$current" ]; then
378                         zpm pkg $pkgid status=installed :$current status=updated
379                 else
380                         zpm pkg $pkgid status=installed
381                 fi
382
383                 maybe_run configure $pkgid $runconfigure
384         done
385 fi
386 echo install complete