2005-10-20 16:46:35 -04:00
|
|
|
#!/bin/bash
|
2007-05-30 11:04:49 -04:00
|
|
|
#
|
2005-10-20 16:46:35 -04:00
|
|
|
# pacman-optimize
|
2007-05-30 11:04:49 -04:00
|
|
|
# @configure_input@
|
|
|
|
#
|
2007-04-11 16:40:33 -04:00
|
|
|
# Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
|
2007-05-30 11:04:49 -04:00
|
|
|
#
|
2005-10-20 16:46:35 -04:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2007-05-30 11:04:49 -04:00
|
|
|
#
|
2005-10-20 16:46:35 -04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2007-05-30 11:04:49 -04:00
|
|
|
#
|
2005-10-20 16:46:35 -04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-10-20 16:46:35 -04:00
|
|
|
#
|
|
|
|
|
2007-06-04 01:21:14 -04:00
|
|
|
# gettext initialization
|
|
|
|
export TEXTDOMAIN='pacman'
|
|
|
|
export TEXTDOMAINDIR='@localedir@'
|
|
|
|
|
2007-04-11 16:40:33 -04:00
|
|
|
myver='@PACKAGE_VERSION@'
|
2007-06-04 17:35:31 -04:00
|
|
|
dbroot='@localstatedir@/lib/pacman/'
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2007-06-04 01:21:14 -04:00
|
|
|
msg() {
|
|
|
|
local mesg=$1; shift
|
|
|
|
printf "==> ${mesg}\n" "$@" >&2
|
2007-01-03 00:45:18 -05:00
|
|
|
}
|
|
|
|
|
2007-06-04 01:21:14 -04:00
|
|
|
error () {
|
|
|
|
local mesg=$1; shift
|
|
|
|
printf "==> ERROR: ${mesg}\n" "$@" >&2
|
|
|
|
}
|
2007-01-03 00:45:18 -05:00
|
|
|
|
2005-10-20 16:46:35 -04:00
|
|
|
usage() {
|
2007-07-06 19:35:32 -04:00
|
|
|
printf "pacman-optimize (pacman) %s\n\n" "$myver"
|
|
|
|
printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0"
|
|
|
|
printf "$(gettext "\
|
|
|
|
pacman-optimize is a little hack that should improve the performance\n\
|
|
|
|
of pacman when reading/writing to its filesystem-based database.\n\n")"
|
|
|
|
printf "$(gettext "\
|
|
|
|
Because pacman uses many small files to keep track of packages,\n\
|
|
|
|
there is a tendency for these files to become fragmented over time.\n\
|
|
|
|
This script attempts to relocate these small files into one\n\
|
|
|
|
continuous location on your hard drive. The result is that the hard\n\
|
|
|
|
drive should be able to read them faster, since the hard drive head\n\
|
|
|
|
does not have to move around the disk as much.\n")"
|
2005-10-20 16:46:35 -04:00
|
|
|
}
|
|
|
|
|
2007-04-11 16:40:33 -04:00
|
|
|
version() {
|
|
|
|
printf "pacman-optimize (pacman) %s\n" "$myver"
|
2007-07-06 19:35:32 -04:00
|
|
|
printf "$(gettext "\
|
|
|
|
Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>.\n\n\
|
|
|
|
This is free software; see the source for copying conditions.\n\
|
|
|
|
There is NO WARRANTY, to the extent permitted by law.\n")"
|
2007-04-11 16:40:33 -04:00
|
|
|
}
|
|
|
|
|
2005-10-20 16:46:35 -04:00
|
|
|
die() {
|
2007-08-29 05:49:24 -04:00
|
|
|
error "$@"
|
2005-10-20 16:46:35 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
die_r() {
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -f "$lockfile"
|
|
|
|
die "$@"
|
2005-10-20 16:46:35 -04:00
|
|
|
}
|
|
|
|
|
2008-02-17 22:15:06 -05:00
|
|
|
# PROGRAM START
|
|
|
|
|
|
|
|
# determine whether we have gettext; make it a no-op if we do not
|
|
|
|
if [ ! $(type -t gettext) ]; then
|
|
|
|
gettext() {
|
|
|
|
echo "$@"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
2007-05-29 16:53:15 -04:00
|
|
|
if [ "$1" = "-h" -o "$1" = "--help" ]; then
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "-V" -o "$1" = "--version" ]; then
|
|
|
|
version
|
|
|
|
exit 0
|
|
|
|
fi
|
2005-10-20 16:46:35 -04:00
|
|
|
|
|
|
|
if [ "$1" != "" ]; then
|
2007-01-03 00:45:18 -05:00
|
|
|
dbroot="$1"
|
2005-10-20 16:46:35 -04:00
|
|
|
fi
|
|
|
|
|
2007-10-01 07:29:32 -04:00
|
|
|
# make sure diff is installed
|
|
|
|
if ! type diff >/dev/null 2>&1; then
|
|
|
|
die "$(gettext "diff tool was not found, please install diffutils.")"
|
|
|
|
fi
|
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
if [ ! -d "$dbroot" ]; then
|
2007-06-04 01:21:14 -04:00
|
|
|
die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
|
2005-10-20 16:46:35 -04:00
|
|
|
fi
|
|
|
|
|
2006-12-28 13:19:25 -05:00
|
|
|
if [ ! -w "$dbroot" ]; then
|
2007-06-04 01:21:14 -04:00
|
|
|
die "$(gettext "You must have correct permissions to optimize the database.")"
|
2006-12-28 13:19:25 -05:00
|
|
|
fi
|
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
# strip any trailing slash from our dbroot
|
|
|
|
dbroot="${dbroot%/}"
|
|
|
|
# form the path to our lockfile location
|
|
|
|
lockfile="${dbroot}/db.lck"
|
2007-08-29 05:49:24 -04:00
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
# make sure pacman isn't running
|
|
|
|
if [ -f "$lockfile" ]; then
|
|
|
|
die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
|
|
|
|
fi
|
2007-01-03 00:45:18 -05:00
|
|
|
# do not let pacman run while we do this
|
2007-08-29 05:49:24 -04:00
|
|
|
touch "$lockfile"
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
|
|
|
|
die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
|
|
|
|
|
2005-10-20 16:46:35 -04:00
|
|
|
# step 1: sum the old db
|
2007-06-04 01:21:14 -04:00
|
|
|
msg "$(gettext "MD5sum'ing the old database...")"
|
2007-08-29 05:49:24 -04:00
|
|
|
find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
|
2007-01-03 00:45:18 -05:00
|
|
|
|
|
|
|
# step 2: tar it up
|
2007-06-04 01:21:14 -04:00
|
|
|
msg "$(gettext "Tar'ing up %s...")" "$dbroot"
|
2007-08-29 05:49:24 -04:00
|
|
|
cd "$dbroot"
|
2008-10-16 20:47:54 -04:00
|
|
|
bsdtar -czf "$workdir/pacman-db.tar.gz" ./
|
2007-01-11 12:22:21 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -rf "$workdir"
|
2007-06-04 01:21:14 -04:00
|
|
|
die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
|
2007-01-11 12:22:21 -05:00
|
|
|
fi
|
2007-01-03 00:45:18 -05:00
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
# step 3: make and sum the new db side-by-side with the old
|
|
|
|
msg "$(gettext "Making and MD5sum'ing the new database...")"
|
2007-08-29 05:49:24 -04:00
|
|
|
mkdir "$dbroot.new"
|
2008-10-16 20:47:54 -04:00
|
|
|
bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
|
2007-01-11 12:22:21 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -rf "$workdir"
|
|
|
|
die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
|
2007-01-11 12:22:21 -05:00
|
|
|
fi
|
2008-10-16 20:47:54 -04:00
|
|
|
# immediate sync following extraction should get it written continuously on HDD
|
|
|
|
msg "$(gettext "Syncing database to disk...")"
|
|
|
|
sync
|
2007-08-29 05:49:24 -04:00
|
|
|
find "$dbroot.new" -type f | sort | \
|
2008-10-16 20:47:54 -04:00
|
|
|
xargs md5sum | sed 's#.new##' > "$workdir/pacsums.new"
|
2007-01-03 00:45:18 -05:00
|
|
|
|
|
|
|
# step 4: compare the sums
|
2007-06-04 01:21:14 -04:00
|
|
|
msg "$(gettext "Checking integrity...")"
|
2007-08-29 05:49:24 -04:00
|
|
|
diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
|
2005-10-20 16:46:35 -04:00
|
|
|
if [ $? -ne 0 ]; then
|
2007-01-03 00:45:18 -05:00
|
|
|
# failed
|
2008-10-16 20:47:54 -04:00
|
|
|
# leave our pacman-optimize tmpdir for checking to see what doesn't match up
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -rf "$dbroot.new"
|
|
|
|
die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
|
2005-10-20 16:46:35 -04:00
|
|
|
fi
|
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
# step 5: shuffle the newly extracted DB into the proper location
|
|
|
|
msg "$(gettext "Rotating database into place...")"
|
|
|
|
|
|
|
|
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"
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2008-10-16 20:47:54 -04:00
|
|
|
# remove the lock file and our working directory with sums and tarfile
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -f "$lockfile"
|
|
|
|
rm -rf "$workdir"
|
2005-10-20 16:46:35 -04:00
|
|
|
|
|
|
|
echo
|
2007-08-29 05:49:24 -04:00
|
|
|
msg "$(gettext "Finished. Your pacman database has been optimized.")"
|
2005-10-20 16:46:35 -04:00
|
|
|
echo
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
# vim: set ts=2 sw=2 noet:
|