Mon, 28 Jan 2013 17:37:18 +0100
Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.
michael@13 | 1 | #!@l_prefix@/lib/openpkg/bash |
michael@13 | 2 | ## |
michael@13 | 3 | ## rpmdb -- OpenPKG RPM Database Administration Utility |
michael@428 | 4 | ## Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/> |
michael@13 | 5 | ## |
michael@428 | 6 | ## This software is property of the OpenPKG GmbH, DE MUC HRB 160208. |
michael@428 | 7 | ## All rights reserved. Licenses which grant limited permission to use, |
michael@428 | 8 | ## copy, modify and distribute this software are available from the |
michael@428 | 9 | ## OpenPKG GmbH. |
michael@13 | 10 | ## |
michael@428 | 11 | ## THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
michael@13 | 12 | ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
michael@13 | 13 | ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@13 | 14 | ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR |
michael@13 | 15 | ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@13 | 16 | ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@13 | 17 | ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
michael@13 | 18 | ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
michael@13 | 19 | ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
michael@13 | 20 | ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
michael@13 | 21 | ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@13 | 22 | ## SUCH DAMAGE. |
michael@13 | 23 | ## |
michael@13 | 24 | |
michael@13 | 25 | # program information |
michael@13 | 26 | progname="rpmdb" |
michael@13 | 27 | |
michael@13 | 28 | # configuration defaults |
michael@13 | 29 | help="" |
michael@13 | 30 | prefix="@l_prefix@" |
michael@13 | 31 | dbpath="" |
michael@428 | 32 | dbapi="" |
michael@428 | 33 | tmpdir="" |
michael@13 | 34 | rpm="" |
michael@13 | 35 | musr="@l_musr@" |
michael@13 | 36 | mgrp="@l_mgrp@" |
michael@13 | 37 | mode="" |
michael@13 | 38 | force=no |
michael@13 | 39 | verbose=2 |
michael@13 | 40 | |
michael@13 | 41 | ## |
michael@13 | 42 | ## PARSE COMMAND LINE |
michael@13 | 43 | ## |
michael@13 | 44 | |
michael@13 | 45 | # iterate over argument line |
michael@428 | 46 | args="" |
michael@13 | 47 | for opt |
michael@13 | 48 | do |
michael@13 | 49 | case $opt in |
michael@13 | 50 | -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;; |
michael@13 | 51 | *) arg='' ;; |
michael@13 | 52 | esac |
michael@13 | 53 | case $opt in |
michael@13 | 54 | -h|--help ) help="Usage" ;; |
michael@13 | 55 | -B|--build ) mode=build ;; |
michael@428 | 56 | -M|--migrate ) mode=migrate ;; |
michael@13 | 57 | -R|--rebuild ) mode=rebuild ;; |
michael@13 | 58 | -C|--cleanup ) mode=cleanup ;; |
michael@13 | 59 | -F|--fixate ) mode=fixate ;; |
michael@13 | 60 | -L|--list ) mode=list ;; |
michael@13 | 61 | -f|--force ) force=yes ;; |
michael@13 | 62 | -q|--quiet ) verbose=0 ;; |
michael@13 | 63 | -v|--verbose ) verbose=`expr $verbose + 1` ;; |
michael@13 | 64 | --prefix=* ) prefix=$arg ;; |
michael@13 | 65 | --dbpath=* ) dbpath=$arg ;; |
michael@428 | 66 | --dblib=* ) dblib=$arg ;; |
michael@428 | 67 | --tmpdir=* ) tmpdir=$arg ;; |
michael@13 | 68 | --rpm=* ) rpm=$arg ;; |
michael@13 | 69 | --musr=* ) musr=$arg ;; |
michael@13 | 70 | --mgrp=* ) mgrp=$arg ;; |
michael@428 | 71 | -- ) ;; |
michael@428 | 72 | -* ) help="Invalid option \`$opt'"; break ;; |
michael@428 | 73 | * ) args="$args \"$opt\"" ;; |
michael@13 | 74 | esac |
michael@13 | 75 | done |
michael@428 | 76 | eval "set -- $args" |
michael@13 | 77 | |
michael@428 | 78 | # make sure exactly one run-time mode is specified |
michael@13 | 79 | if [ ".$mode" = . ]; then |
michael@13 | 80 | help="No or invalid run-time mode specified" |
michael@13 | 81 | fi |
michael@13 | 82 | |
michael@13 | 83 | # error or usage message |
michael@13 | 84 | if [ ".$help" != . ]; then |
michael@13 | 85 | if [ ".$help" != ".Usage" ]; then |
michael@13 | 86 | echo "$progname:ERROR: $help" 1>&2 |
michael@13 | 87 | fi |
michael@13 | 88 | cat 1>&2 <<EOT |
michael@13 | 89 | $progname:USAGE: $progname [options] |
michael@13 | 90 | |
michael@13 | 91 | -h, --help print usage message (this one) |
michael@13 | 92 | -B, --build build new RPM database from scratch |
michael@428 | 93 | -M, --migrate migrate RPM database to new format |
michael@13 | 94 | -R, --rebuild rebuild new from old RPM database |
michael@13 | 95 | -C, --cleanup cleanup existing RPM database |
michael@13 | 96 | -F, --fixate fixate existing RPM database |
michael@13 | 97 | -L, --list list all RPM database files |
michael@13 | 98 | -f, --force operate in force mode (no save operations) |
michael@13 | 99 | -q, --quiet operate in quiet mode (no verbose messages at all) |
michael@13 | 100 | -v, --verbose operate in more verbose mode (increase verbose level) |
michael@13 | 101 | --prefix=PREFIX use OpenPKG instance under PREFIX |
michael@13 | 102 | --dbpath=PATH use OpenPKG RPM database under PATH |
michael@428 | 103 | --dblib=LIB use OpenPKG RPM database backend library ("db" or "sqlite") |
michael@428 | 104 | --tmpdir=PATH use OpenPKG temporary directory under PATH |
michael@13 | 105 | --rpm=PROG use OpenPKG RPM executable PROG |
michael@13 | 106 | --musr=USERNAME use OpenPKG management user USERNAME |
michael@13 | 107 | --mgrp=GROUPNAME use OpenPKG management group GROUPNAME |
michael@13 | 108 | |
michael@13 | 109 | This is OpenPKG rpmdb, an RPM database administration utility, providing |
michael@428 | 110 | lower-level maintainance functions for the Berkeley-DB based RPM |
michael@13 | 111 | database. It allows building new RPM databases from scratch, rebuilding |
michael@13 | 112 | a new from an old RPM database (content dumping and reloading), |
michael@13 | 113 | cleaning up problems on an existing RPM database (removal of DB region |
michael@13 | 114 | files, etc) and fixating the files in an existing RPM database (file |
michael@13 | 115 | attributes). |
michael@13 | 116 | |
michael@13 | 117 | EOT |
michael@13 | 118 | if [ ".$help" != ".Usage" ]; then |
michael@13 | 119 | exit 2 |
michael@13 | 120 | else |
michael@13 | 121 | exit 0 |
michael@13 | 122 | fi |
michael@13 | 123 | fi |
michael@13 | 124 | |
michael@13 | 125 | # delayed determination of variables |
michael@13 | 126 | if [ ".$dbpath" = . ]; then |
michael@13 | 127 | dbpath="$prefix/RPM/DB" |
michael@13 | 128 | fi |
michael@428 | 129 | if [ ".$dblib" = . ]; then |
michael@428 | 130 | dblib="${1-db}" |
michael@428 | 131 | fi |
michael@428 | 132 | if [ ".$tmpdir" = . ]; then |
michael@428 | 133 | tmpdir="$prefix/RPM/TMP" |
michael@428 | 134 | fi |
michael@13 | 135 | if [ ".$rpm" = . ]; then |
michael@13 | 136 | rpm="$prefix/bin/openpkg rpm" |
michael@13 | 137 | fi |
michael@13 | 138 | |
michael@13 | 139 | ## |
michael@13 | 140 | ## DATABASE FILE INFORMATION |
michael@13 | 141 | ## |
michael@13 | 142 | |
michael@13 | 143 | dbfiles=" |
michael@428 | 144 | 3:btree:Basenames |
michael@428 | 145 | 3:btree:Conflictname |
michael@428 | 146 | 3:temporary:Depends |
michael@428 | 147 | 3:btree:Dirnames |
michael@428 | 148 | 3:btree:Filedigests |
michael@428 | 149 | 3:btree:Filepaths |
michael@428 | 150 | 3:btree:Group |
michael@428 | 151 | 3:btree:Installtid |
michael@428 | 152 | 3:btree:Name |
michael@428 | 153 | 3:hash:Packagecolor |
michael@428 | 154 | 3:btree:Packages |
michael@428 | 155 | 3:btree:Providename |
michael@428 | 156 | 3:btree:Provideversion |
michael@428 | 157 | 3:hash:Pubkeys |
michael@428 | 158 | 3:btree:Requirename |
michael@428 | 159 | 3:btree:Requireversion |
michael@428 | 160 | 3:hash:Sha1header |
michael@428 | 161 | 3:hash:Sigmd5 |
michael@428 | 162 | 3:btree:Triggername |
michael@428 | 163 | 3:btree:Obsoletename |
michael@428 | 164 | 3:btree:Nvra |
michael@428 | 165 | 3:btree:Sourcepkgid |
michael@428 | 166 | 3:btree:BuildEnvironment |
michael@428 | 167 | 3:region:__db.001 |
michael@428 | 168 | 3:region:__db.002 |
michael@428 | 169 | 3:region:__db.003 |
michael@428 | 170 | 3:region:__db.004 |
michael@428 | 171 | 3:region:__db.005 |
michael@428 | 172 | 3:region:__db.006 |
michael@428 | 173 | 3:region:__db.007 |
michael@428 | 174 | 3:region:__db.008 |
michael@428 | 175 | 3:region:__db.009 |
michael@428 | 176 | 4:*:Basenames |
michael@428 | 177 | 4:*:Conflictname |
michael@428 | 178 | 4:temporary:Depends |
michael@428 | 179 | 4:*:Dirnames |
michael@428 | 180 | 4:*:Filedigests |
michael@428 | 181 | 4:*:Filepaths |
michael@428 | 182 | 4:*:Group |
michael@428 | 183 | 4:*:Installtid |
michael@428 | 184 | 4:*:Name |
michael@428 | 185 | 4:*:Packagecolor |
michael@428 | 186 | 4:*:Packages |
michael@428 | 187 | 4:*:Providename |
michael@428 | 188 | 4:*:Provideversion |
michael@428 | 189 | 4:*:Pubkeys |
michael@428 | 190 | 4:*:Requirename |
michael@428 | 191 | 4:*:Requireversion |
michael@428 | 192 | 4:*:Sha1header |
michael@428 | 193 | 4:*:Sigmd5 |
michael@428 | 194 | 4:*:Triggername |
michael@428 | 195 | 4:*:Obsoletename |
michael@428 | 196 | 4:*:Nvra |
michael@428 | 197 | 4:*:Sourcepkgid |
michael@428 | 198 | 4:*:BuildEnvironment |
michael@13 | 199 | " |
michael@13 | 200 | |
michael@13 | 201 | ## |
michael@13 | 202 | ## HELPER FUNCTIONS |
michael@13 | 203 | ## |
michael@13 | 204 | |
michael@13 | 205 | error () { |
michael@13 | 206 | echo "$progname:ERROR: $*" 1>&2 |
michael@13 | 207 | exit 1 |
michael@13 | 208 | } |
michael@13 | 209 | |
michael@13 | 210 | warning () { |
michael@13 | 211 | echo "$progname:WARNING: $*" 1>&2 |
michael@13 | 212 | } |
michael@13 | 213 | |
michael@13 | 214 | verbose () { |
michael@13 | 215 | local level=$1 |
michael@13 | 216 | shift |
michael@13 | 217 | if [ $level -le $verbose ]; then |
michael@13 | 218 | local lead="" |
michael@13 | 219 | case "$level" in |
michael@13 | 220 | 1 ) lead="" ;; |
michael@13 | 221 | 2 ) lead="" ;; |
michael@13 | 222 | 3 ) lead=" " ;; |
michael@13 | 223 | * ) lead=" " ;; |
michael@13 | 224 | esac |
michael@13 | 225 | echo "$progname: $lead$*" 1>&2 |
michael@13 | 226 | fi |
michael@13 | 227 | } |
michael@13 | 228 | |
michael@428 | 229 | rpmdb_version_load () { |
michael@428 | 230 | if [ -f $dbpath/VERSION ]; then |
michael@428 | 231 | eval `(. $dbpath/VERSION || exit $?; echo "DBAPI=\"$DBAPI\"; DBLIB=\"$DBLIB\"; DBVER=\"$DBVER\"")` |
michael@428 | 232 | fi |
michael@428 | 233 | if [ ".$DBAPI" = . ]; then DBAPI="3"; fi |
michael@428 | 234 | if [ ".$DBLIB" = . ]; then DBLIB="db"; fi |
michael@428 | 235 | if [ ".$DBVER" = . ]; then DBVER="4.1.2"; fi |
michael@428 | 236 | } |
michael@428 | 237 | |
michael@428 | 238 | rpmdb_version_save () { |
michael@428 | 239 | if [ ".$DBAPI" = .3 ]; then |
michael@428 | 240 | DBLIB="db" |
michael@428 | 241 | DBVER=`$rpmdb_load -V 2>&1 |\ |
michael@428 | 242 | grep 'Berkeley DB [0-9][0-9]*\.[0-9][0-9]*' |\ |
michael@428 | 243 | sed -e 's;^;X;' \ |
michael@428 | 244 | -e 's;^X[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$;\1;' \ |
michael@428 | 245 | -e 's;^X[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$;\1.0;' \ |
michael@428 | 246 | -e 's;^X.*$;0.0.0;'` |
michael@428 | 247 | elif [ ".$DBAPI" = .4 ]; then |
michael@428 | 248 | DBLIB="sqlite" |
michael@428 | 249 | DBVER=`$rpmdb_sqlite --version 2>&1 |\ |
michael@428 | 250 | grep '^[0-9][0-9]*\.[0-9][0-9]*' |\ |
michael@428 | 251 | sed -e 's;^;X;' \ |
michael@428 | 252 | -e 's;^X[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$;\1;' \ |
michael@428 | 253 | -e 's;^X[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$;\1.0;' \ |
michael@428 | 254 | -e 's;^X.*$;0.0.0;'` |
michael@428 | 255 | fi |
michael@428 | 256 | ( echo "DBAPI=$DBAPI" |
michael@428 | 257 | echo "DBLIB=$DBLIB" |
michael@428 | 258 | echo "DBVER=$DBVER" |
michael@428 | 259 | ) >$dbpath/VERSION.new || exit $? |
michael@428 | 260 | mv $dbpath/VERSION.new $dbpath/VERSION || exit $? |
michael@428 | 261 | chown $musr:$mgrp $dbpath/VERSION 2>/dev/null || true |
michael@428 | 262 | chmod 644 $dbpath/VERSION 2>/dev/null || true |
michael@428 | 263 | } |
michael@428 | 264 | |
michael@13 | 265 | rpm () { |
michael@428 | 266 | local opts="--define '_dbpath `echo $dbpath | sed -e 's;/*$;;' -e 's;$;/;'`'" |
michael@428 | 267 | opts="$opts --define '_repackage_all_erasures 0'" |
michael@13 | 268 | if [ ".$force" = .yes ]; then |
michael@13 | 269 | opts="$opts --define '__dbi_private yes'" |
michael@13 | 270 | fi |
michael@13 | 271 | verbose 3 "run: $rpm $opts $@" |
michael@13 | 272 | eval "$rpm $opts \"\$@\"" |
michael@13 | 273 | } |
michael@13 | 274 | |
michael@428 | 275 | rpmdb_load="$prefix/lib/openpkg/db_tool load" |
michael@428 | 276 | rpmdb_dump="$prefix/lib/openpkg/db_tool dump" |
michael@428 | 277 | rpmdb_sqlite="$prefix/lib/openpkg/sqlite3" |
michael@13 | 278 | |
michael@13 | 279 | ## |
michael@13 | 280 | ## RPM DATABASE OPERATIONS |
michael@13 | 281 | ## |
michael@13 | 282 | |
michael@13 | 283 | db_wait () { |
michael@13 | 284 | # wait until RPM has released the database in case we are called |
michael@13 | 285 | # asynchronously to RPM (especially important when upgrading from |
michael@13 | 286 | # RPM 4.0 where concurrent access is still not possible) |
michael@13 | 287 | verbose 2 "waiting for RPM database to be available" |
michael@13 | 288 | local i=0 |
michael@13 | 289 | while [ $i -lt 10 ]; do |
michael@13 | 290 | if $prefix/libexec/openpkg/rpm -q openpkg >/dev/null 2>&1; then |
michael@13 | 291 | break |
michael@13 | 292 | fi |
michael@13 | 293 | sleep 1 |
michael@13 | 294 | i=`expr $i + 1` |
michael@13 | 295 | done |
michael@13 | 296 | if [ $i -eq 10 ]; then |
michael@13 | 297 | exit 1 |
michael@13 | 298 | else |
michael@13 | 299 | exit 0 |
michael@13 | 300 | fi |
michael@13 | 301 | } |
michael@13 | 302 | |
michael@13 | 303 | db_remove () { |
michael@13 | 304 | # remove all known files |
michael@13 | 305 | verbose 2 "removing (possibly existing) old RPM database DB files" |
michael@13 | 306 | for dbfile in $dbfiles; do |
michael@428 | 307 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 308 | verbose 3 "removing database file: $dbpath/$dbfile ($dbapi:$dbtype)" |
michael@13 | 309 | rm -f $dbpath/$dbfile |
michael@13 | 310 | done |
michael@13 | 311 | } |
michael@13 | 312 | |
michael@13 | 313 | db_init () { |
michael@13 | 314 | # perform official "initdb" operation |
michael@13 | 315 | verbose 2 "creating new RPM database (built-in RPM procedure)" |
michael@13 | 316 | rpm --initdb |
michael@428 | 317 | rpmdb_version_save |
michael@13 | 318 | } |
michael@13 | 319 | |
michael@13 | 320 | db_unbreak () { |
michael@428 | 321 | # cleanup DB files |
michael@428 | 322 | verbose 2 "cleaning up RPM database DB files" |
michael@13 | 323 | for dbfile in $dbfiles; do |
michael@428 | 324 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 325 | if [ -f $dbpath/$dbfile ]; then |
michael@428 | 326 | if [ ".$dbapi" = .3 -a ".$dbtype" = .region ]; then |
michael@428 | 327 | verbose 3 "cleaning up DB file: $dbpath/$dbfile ($dbapi:$dbtype)" |
michael@428 | 328 | rm -f $dbpath/$dbfile || true |
michael@428 | 329 | elif [ ".$dbtype" = .temporary ]; then |
michael@428 | 330 | verbose 3 "cleaning up DB file: $dbpath/$dbfile ($dbapi:$dbtype)" |
michael@428 | 331 | rm -f $dbpath/$dbfile || true |
michael@428 | 332 | fi |
michael@13 | 333 | fi |
michael@13 | 334 | done |
michael@13 | 335 | } |
michael@13 | 336 | |
michael@428 | 337 | db_convert () { |
michael@428 | 338 | # make sure all RPM database DB files have the correct type |
michael@428 | 339 | # (as the type can be different during upgrading from RPM 4 to RPM 5) |
michael@428 | 340 | verbose 2 "making sure RPM database are of the correct type" |
michael@13 | 341 | for dbfile in $dbfiles; do |
michael@428 | 342 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 343 | if [ ".$dbapi" != ".$DBAPI" ]; then |
michael@428 | 344 | continue |
michael@428 | 345 | fi |
michael@428 | 346 | if [ -f $dbpath/$dbfile ]; then |
michael@428 | 347 | if [ ".$dbapi" = .3 ] && [ ".$dbtype" = .hash -o ".$dbtype" = .btree ]; then |
michael@428 | 348 | dbtype_old="`$rpmdb_dump -h $tmpdir -r $dbpath/$dbfile | grep '^type=' | sed -e 'q' | sed -e 's/^type=//'`" |
michael@428 | 349 | if [ ".$dbtype_old" != ".$dbtype" ]; then |
michael@428 | 350 | verbose 3 "converting DB file: $dbpath/$dbfile ($dbtype_old -> $dbtype)" |
michael@428 | 351 | rm -f $dbpath/$dbfile.new |
michael@428 | 352 | $rpmdb_dump -h $tmpdir -r $dbpath/$dbfile |\ |
michael@428 | 353 | sed -e "s/^type=.*/type=$dbtype/" -e '/^h_nelem=.*/d' |\ |
michael@428 | 354 | $rpmdb_load -h $tmpdir $dbpath/$dbfile.new |
michael@428 | 355 | if $rpmdb_dump -h $tmpdir -r $dbpath/$dbfile.new >/dev/null 2>&1; then |
michael@428 | 356 | rm -f $dbpath/$dbfile |
michael@428 | 357 | mv $dbpath/$dbfile.new $dbpath/$dbfile |
michael@428 | 358 | else |
michael@428 | 359 | warning "failed to convert RPM DB file \"$dbfile\"" |
michael@428 | 360 | fi |
michael@428 | 361 | fi |
michael@13 | 362 | fi |
michael@13 | 363 | fi |
michael@13 | 364 | done |
michael@13 | 365 | } |
michael@13 | 366 | |
michael@13 | 367 | db_reload () { |
michael@13 | 368 | # rebuilding new from old RPM database DB files by dumping and |
michael@13 | 369 | # reloading their entire content |
michael@13 | 370 | verbose 2 "dumping and reloading RPM database DB file contents" |
michael@13 | 371 | for dbfile in $dbfiles; do |
michael@428 | 372 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 373 | if [ ".$dbapi" != ".$DBAPI" ]; then |
michael@428 | 374 | continue |
michael@428 | 375 | fi |
michael@13 | 376 | if [ -f $dbpath/$dbfile ]; then |
michael@428 | 377 | if [ ".$dbapi" = .3 ]; then |
michael@428 | 378 | verbose 3 "dumping and reloading DB file: $dbpath/$dbfile ($dbtype)" |
michael@428 | 379 | if [ ".$dbtype" = .hash -o ".$dbtype" = .btree ]; then |
michael@428 | 380 | rm -f $dbpath/$dbfile.new |
michael@428 | 381 | $rpmdb_dump -h $tmpdir -r $dbpath/$dbfile |\ |
michael@428 | 382 | $rpmdb_load -h $tmpdir $dbpath/$dbfile.new |
michael@428 | 383 | if $rpmdb_dump -h $tmpdir -r $dbpath/$dbfile.new >/dev/null 2>&1; then |
michael@428 | 384 | rm -f $dbpath/$dbfile |
michael@428 | 385 | mv $dbpath/$dbfile.new $dbpath/$dbfile |
michael@428 | 386 | else |
michael@428 | 387 | warning "failed to reload RPM DB file \"$dbfile\"" |
michael@428 | 388 | fi |
michael@428 | 389 | elif [ ".$dbtype" = .region ]; then |
michael@428 | 390 | rm -f $dbpath/$dbfile || true |
michael@428 | 391 | elif [ ".$dbtype" = .temporary ]; then |
michael@428 | 392 | rm -f $dbpath/$dbfile || true |
michael@428 | 393 | fi |
michael@428 | 394 | elif [ ".$dbapi" = .4 ]; then |
michael@428 | 395 | verbose 3 "dumping and reloading DB file: $dbpath/$dbfile ($dbtype)" |
michael@428 | 396 | rm -f $dbpath/$dbfile.new |
michael@428 | 397 | $rpmdb_sqlite $dbpath/$dbfile .dump |\ |
michael@428 | 398 | $rpmdb_sqlite $dbpath/$dbfile.new |
michael@428 | 399 | if $rpmdb_sqlite $dbpath/$dbfile.new >/dev/null 2>&1; then |
michael@428 | 400 | rm -f $dbpath/$dbfile |
michael@428 | 401 | mv $dbpath/$dbfile.new $dbpath/$dbfile |
michael@428 | 402 | else |
michael@428 | 403 | warning "failed to reload RPM DB file \"$dbfile\"" |
michael@428 | 404 | fi |
michael@13 | 405 | fi |
michael@13 | 406 | fi |
michael@13 | 407 | done |
michael@428 | 408 | rpmdb_version_save |
michael@428 | 409 | } |
michael@428 | 410 | |
michael@428 | 411 | db_migrate () { |
michael@428 | 412 | # perform database migration |
michael@428 | 413 | if [ ".$1" != . ]; then |
michael@428 | 414 | dblib="$1" |
michael@428 | 415 | fi |
michael@428 | 416 | dbapi_old="$DBAPI" |
michael@428 | 417 | if [ ".$dblib" = .db ]; then |
michael@428 | 418 | dbapi_new="3" |
michael@428 | 419 | elif [ ".$dblib" = .sqlite ]; then |
michael@428 | 420 | dbapi_new="4" |
michael@428 | 421 | else |
michael@428 | 422 | error "unknown RPM database backend library \"$dblib\"" |
michael@428 | 423 | fi |
michael@428 | 424 | if [ ".$dbapi_new" = ".$dbapi_old" ]; then |
michael@428 | 425 | error "RPM database already uses requested backend ($DBAPI:$DBLIB:$DBVER)" |
michael@428 | 426 | fi |
michael@428 | 427 | verbose 2 "migrating RPM database" |
michael@428 | 428 | rpm --rebuilddb --dbapi "$dbapi_old" --rebuilddbapi "$dbapi_new" |
michael@428 | 429 | verbose 3 "old RPM database format: $DBAPI:$DBLIB:$DBVER" |
michael@428 | 430 | DBAPI="$dbapi_new" |
michael@428 | 431 | rpmdb_version_save |
michael@428 | 432 | rpmdb_version_load |
michael@428 | 433 | verbose 3 "new RPM database format: $DBAPI:$DBLIB:$DBVER" |
michael@13 | 434 | } |
michael@13 | 435 | |
michael@13 | 436 | db_rebuild () { |
michael@13 | 437 | # perform official "rebuilddb" operation |
michael@13 | 438 | verbose 2 "rebuilding RPM database (built-in RPM procedure)" |
michael@13 | 439 | rpm --rebuilddb |
michael@428 | 440 | rpmdb_version_save |
michael@13 | 441 | } |
michael@13 | 442 | |
michael@13 | 443 | db_operate () { |
michael@13 | 444 | # perform some read/write operation on RPM database |
michael@13 | 445 | # (we have no package available, but removing and reimporting |
michael@13 | 446 | # the OpenPKG OpenPGP keys is a harmless thing and always possible) |
michael@13 | 447 | verbose 2 "performing read/write operation on RPM database" |
michael@13 | 448 | for spec in \ |
michael@13 | 449 | openpkg.org.pgp:gpg-pubkey-63c4cb9f-3c591eda \ |
michael@13 | 450 | openpkg.com.pgp:gpg-pubkey-61b7ae34-4544a6af \ |
michael@13 | 451 | openpkg.net.pgp:gpg-pubkey-52197903-4544a74d \ |
michael@13 | 452 | ; do |
michael@13 | 453 | eval `echo "$spec" | sed -e 's/^\(.*\):\(.*\)$/file="\1"; package="\2"/'` |
michael@13 | 454 | rpm -q $package >/dev/null 2>&1 && rpm -e $package --allmatches || true |
michael@13 | 455 | rpm -q $package >/dev/null 2>&1 || rpm --import $prefix/etc/openpkg/$file || true |
michael@13 | 456 | done |
michael@13 | 457 | rpm -qa >/dev/null 2>&1 |
michael@13 | 458 | } |
michael@13 | 459 | |
michael@13 | 460 | db_fixate () { |
michael@13 | 461 | # fix ownership and permissions of (especially newly created) |
michael@13 | 462 | # RPM database files to make sure they are consistent |
michael@13 | 463 | verbose 2 "making sure RPM database files have consistent attributes" |
michael@13 | 464 | for dbfile in $dbfiles; do |
michael@428 | 465 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 466 | if [ ".$dbapi" != ".$DBAPI" ]; then |
michael@428 | 467 | continue |
michael@428 | 468 | fi |
michael@13 | 469 | verbose 3 "fixating DB file: $dbpath/$dbfile ($dbtype)" |
michael@428 | 470 | if [ -f $dbpath/$dbfile ]; then |
michael@428 | 471 | chown $musr:$mgrp $dbpath/$dbfile 2>/dev/null || true |
michael@428 | 472 | chmod 644 $dbpath/$dbfile 2>/dev/null || true |
michael@428 | 473 | fi |
michael@13 | 474 | done |
michael@428 | 475 | chown $musr:$mgrp $dbpath/VERSION 2>/dev/null || true |
michael@428 | 476 | chmod 644 $dbpath/VERSION 2>/dev/null || true |
michael@13 | 477 | } |
michael@13 | 478 | |
michael@13 | 479 | db_list () { |
michael@13 | 480 | # list all database files |
michael@13 | 481 | for dbfile in $dbfiles; do |
michael@428 | 482 | eval `echo $dbfile | sed -e 's/^\(.*\):\(.*\):\(.*\)$/dbapi="\1"; dbtype="\2"; dbfile="\3";/'` |
michael@428 | 483 | if [ ".$dbapi" != ".$DBAPI" ]; then |
michael@428 | 484 | continue |
michael@428 | 485 | fi |
michael@428 | 486 | if [ ! -f "$dbpath/$dbfile" ]; then |
michael@428 | 487 | continue |
michael@428 | 488 | fi |
michael@13 | 489 | if [ $verbose -eq 0 ]; then |
michael@13 | 490 | echo "$dbfile" |
michael@13 | 491 | elif [ $verbose -eq 1 ]; then |
michael@13 | 492 | echo "$dbpath/$dbfile" |
michael@13 | 493 | elif [ $verbose -ge 2 ]; then |
michael@13 | 494 | echo "$dbpath/$dbfile ($dbtype)" |
michael@13 | 495 | fi |
michael@13 | 496 | done |
michael@13 | 497 | } |
michael@13 | 498 | |
michael@13 | 499 | ## |
michael@13 | 500 | ## ENVIRONMENT CONSISTENCY CHECKS |
michael@13 | 501 | ## |
michael@13 | 502 | |
michael@13 | 503 | # sanity check OpenPKG instance |
michael@13 | 504 | if [ ".$prefix" = . ]; then |
michael@13 | 505 | error "OpenPKG instance directory is empty" |
michael@13 | 506 | fi |
michael@13 | 507 | if [ ! -d $prefix ]; then |
michael@13 | 508 | error "OpenPKG instance directory \"$prefix\" not found" |
michael@13 | 509 | fi |
michael@13 | 510 | if [ ! -x $prefix/bin/openpkg ]; then |
michael@13 | 511 | error "OpenPKG instance directory \"$prefix\" not valid" |
michael@13 | 512 | fi |
michael@13 | 513 | |
michael@13 | 514 | # sanity check OpenPKG RPM database |
michael@13 | 515 | if [ ".$dbpath" = . ]; then |
michael@13 | 516 | error "OpenPKG RPM database directory is empty" |
michael@13 | 517 | fi |
michael@13 | 518 | if [ ! -d $dbpath ]; then |
michael@13 | 519 | error "OpenPKG RPM database directory \"$dbpath\" not found" |
michael@13 | 520 | fi |
michael@13 | 521 | if [ ! -w $dbpath ]; then |
michael@13 | 522 | error "OpenPKG RPM database directory \"$dbpath\" not writable" |
michael@13 | 523 | fi |
michael@13 | 524 | |
michael@428 | 525 | # load database information |
michael@428 | 526 | rpmdb_version_load |
michael@428 | 527 | |
michael@13 | 528 | ## |
michael@13 | 529 | ## DISPATCH INTO COMMANDS |
michael@13 | 530 | ## |
michael@13 | 531 | |
michael@13 | 532 | case "$mode" in |
michael@13 | 533 | build ) |
michael@13 | 534 | verbose 1 "BUILDING NEW RPM DATABASE FROM SCRATCH ($dbpath)" |
michael@13 | 535 | db_remove |
michael@13 | 536 | db_init |
michael@428 | 537 | db_fixate |
michael@428 | 538 | db_operate |
michael@428 | 539 | ;; |
michael@428 | 540 | |
michael@428 | 541 | migrate ) |
michael@428 | 542 | verbose 1 "MIGRATING RPM DATABASE FORMAT ($dbpath)" |
michael@428 | 543 | db_unbreak |
michael@428 | 544 | db_convert |
michael@428 | 545 | db_migrate |
michael@13 | 546 | db_rebuild |
michael@13 | 547 | db_fixate |
michael@13 | 548 | db_operate |
michael@13 | 549 | ;; |
michael@13 | 550 | |
michael@13 | 551 | rebuild ) |
michael@13 | 552 | verbose 1 "REBUILDING NEW FROM OLD RPM DATABASE ($dbpath)" |
michael@13 | 553 | db_unbreak |
michael@428 | 554 | db_convert |
michael@13 | 555 | db_reload |
michael@13 | 556 | db_rebuild |
michael@13 | 557 | db_fixate |
michael@13 | 558 | db_operate |
michael@13 | 559 | ;; |
michael@13 | 560 | |
michael@13 | 561 | cleanup ) |
michael@13 | 562 | verbose 1 "CLEANING UP EXISTING RPM DATABASE ($dbpath)" |
michael@13 | 563 | db_unbreak |
michael@428 | 564 | db_convert |
michael@13 | 565 | db_rebuild |
michael@13 | 566 | db_fixate |
michael@13 | 567 | db_operate |
michael@13 | 568 | ;; |
michael@13 | 569 | |
michael@13 | 570 | fixate ) |
michael@13 | 571 | verbose 1 "FIXATING EXISTING RPM DATABASE ($dbpath)" |
michael@13 | 572 | db_fixate |
michael@13 | 573 | ;; |
michael@13 | 574 | |
michael@13 | 575 | list ) |
michael@13 | 576 | db_list |
michael@13 | 577 | ;; |
michael@13 | 578 | esac |
michael@13 | 579 | |
michael@13 | 580 | exit 0 |
michael@13 | 581 |