1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 00:08:50 -05:00

Give pacman-optimize a refresher

This patch addresses quite a few lingering issues in the pacman-optimize
script. FS#11767 provoked this look-over and the following issues were
noticed and fixed:

* If an alternate dbroot was specified, then the lockfile location was never
  updated to reflect it. The lockfile location is now set after all dbpath
  initialization.
* The inclusion of a trailing slash on dbroot was problematic and led to the
  following command being executed:
    bsdtar -xpf /tmp/pacman-optimize.p12Q4vAUWY/pacman-db.tar.gz \
	  -C /var/lib/pacman/.new/
  It is doubtful we meant to create a hidden directory like this below our
  database root, only to go and delete it a second later and then
  re-extract. Fix the whole thing by ensuring our dbpath has its trailing
  slash stripped and then appending it when necessary.
* The DB extraction was performed twice for no real apparent reason. This
  opens the door for extraction problems the second time around, leaving you
  with no original database to fall back to. Change the behavior so we only
  extract once, and then perform a directory shuffle once we verify the
  checksums are correct.
* Perform an explicit sync after we drop the new database on the disk. It
  should work better this way.
* Tighten up our check for a pacman lockfile and the time we create one.
  There is still a possible race condition but the window is shorter.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-10-16 19:47:54 -05:00
parent 2f5d792725
commit a63aeed562

View File

@ -25,7 +25,6 @@ export TEXTDOMAINDIR='@localedir@'
myver='@PACKAGE_VERSION@' myver='@PACKAGE_VERSION@'
dbroot='@localstatedir@/lib/pacman/' dbroot='@localstatedir@/lib/pacman/'
lockfile="${dbroot}db.lck"
msg() { msg() {
local mesg=$1; shift local mesg=$1; shift
@ -98,11 +97,6 @@ if ! type diff >/dev/null 2>&1; then
die "$(gettext "diff tool was not found, please install diffutils.")" die "$(gettext "diff tool was not found, please install diffutils.")"
fi fi
# make sure pacman isn't running
if [ -f "$lockfile" ]; then
die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
fi
if [ ! -d "$dbroot" ]; then if [ ! -d "$dbroot" ]; then
die "$(gettext "%s does not exist or is not a directory.")" "$dbroot" die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
fi fi
@ -111,12 +105,21 @@ if [ ! -w "$dbroot" ]; then
die "$(gettext "You must have correct permissions to optimize the database.")" die "$(gettext "You must have correct permissions to optimize the database.")"
fi fi
workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) || # strip any trailing slash from our dbroot
die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2 dbroot="${dbroot%/}"
# form the path to our lockfile location
lockfile="${dbroot}/db.lck"
# make sure pacman isn't running
if [ -f "$lockfile" ]; then
die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
fi
# do not let pacman run while we do this # do not let pacman run while we do this
touch "$lockfile" touch "$lockfile"
workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
# step 1: sum the old db # step 1: sum the old db
msg "$(gettext "MD5sum'ing the old database...")" msg "$(gettext "MD5sum'ing the old database...")"
find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old" find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
@ -124,46 +127,56 @@ find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
# step 2: tar it up # step 2: tar it up
msg "$(gettext "Tar'ing up %s...")" "$dbroot" msg "$(gettext "Tar'ing up %s...")" "$dbroot"
cd "$dbroot" cd "$dbroot"
bsdtar -czf "$workdir/pacmanDB.tgz" ./ bsdtar -czf "$workdir/pacman-db.tar.gz" ./
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
rm -rf "$workdir" rm -rf "$workdir"
die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot" die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
fi fi
# step 3: make and sum the new db # step 3: make and sum the new db side-by-side with the old
msg "$(gettext "Making and MD5sum'ing the new db...")" msg "$(gettext "Making and MD5sum'ing the new database...")"
mkdir "$dbroot.new" mkdir "$dbroot.new"
bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot.new/" bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
rm -rf "$workdir" rm -rf "$workdir"
die_r "$(gettext "Untar'ing %s failed.")" "$dbroot" die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
fi fi
# immediate sync following extraction should get it written continuously on HDD
msg "$(gettext "Syncing database to disk...")"
sync
find "$dbroot.new" -type f | sort | \ find "$dbroot.new" -type f | sort | \
xargs md5sum | sed 's#.new/##' > "$workdir/pacsums.new" xargs md5sum | sed 's#.new##' > "$workdir/pacsums.new"
# step 4: compare the sums # step 4: compare the sums
msg "$(gettext "Checking integrity...")" msg "$(gettext "Checking integrity...")"
diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1 diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# failed # failed
# leave /tmp/pacsums.old and .new for checking to see what doesn't match up # leave our pacman-optimize tmpdir for checking to see what doesn't match up
rm -rf "$dbroot.new" rm -rf "$dbroot.new"
die_r "$(gettext "Integrity check FAILED, reverting to old database.")" die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
fi fi
# step 5: remove the new temporary database and the old one # step 5: shuffle the newly extracted DB into the proper location
# and use the .tgz to replace the old one msg "$(gettext "Rotating database into place...")"
msg "$(gettext "Putting the new database in place...")"
rm -rf "$dbroot.new" "$dbroot"/*
bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot/"
# remove the lock file, sum files, and .tgz of database fail=0
mv "$dbroot" "$dbroot.old" || fail=1
mv "$dbroot.new" "$dbroot" || fail=1
chmod --reference="$dbroot.old" "$dbroot" || fail=1
chown --reference="$dbroot.old" "$dbroot" || fail=1
if [ $fail -ne 0 ]; then
# failure with our directory shuffle
die_r "$(gettext "New database substitution failed. Check for $dbroot,\n$dbroot.old, and $dbroot.new directories.")"
fi
rm -rf "$dbroot.old"
# remove the lock file and our working directory with sums and tarfile
rm -f "$lockfile" rm -f "$lockfile"
rm -rf "$workdir" rm -rf "$workdir"
echo echo
msg "$(gettext "Finished. Your pacman database has been optimized.")" msg "$(gettext "Finished. Your pacman database has been optimized.")"
msg "$(gettext "For full benefits of pacman-optimize, run 'sync' now.")"
echo echo
exit 0 exit 0