2011-06-30 11:07:54 -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@
|
|
|
|
#
|
2011-01-05 23:45:15 -05:00
|
|
|
# Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
# Copyright (c) 2002-2006 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
|
2011-06-23 22:21:51 -04:00
|
|
|
export TEXTDOMAIN='pacman-scripts'
|
2007-06-04 01:21:14 -04:00
|
|
|
export TEXTDOMAINDIR='@localedir@'
|
|
|
|
|
2011-12-06 16:29:33 -05:00
|
|
|
declare -r myver='@PACKAGE_VERSION@'
|
2010-12-12 22:40:24 -05:00
|
|
|
|
|
|
|
eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
|
|
|
|
dbroot="${DBPath:-@localstatedir@/lib/pacman/}"
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2011-06-27 14:29:13 -04:00
|
|
|
m4_include(library/output_format.sh)
|
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 "\
|
2011-01-05 23:45:15 -05:00
|
|
|
Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>.\n\
|
2009-07-01 03:08:33 -04:00
|
|
|
Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
|
2007-07-06 19:35:32 -04:00
|
|
|
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
|
2009-11-05 10:55:48 -05:00
|
|
|
if ! type gettext &>/dev/null; then
|
2008-02-17 22:15:06 -05:00
|
|
|
gettext() {
|
|
|
|
echo "$@"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
2009-11-05 10:55:48 -05:00
|
|
|
if [[ $1 = "-h" || $1 = "--help" ]]; then
|
2007-05-29 16:53:15 -04:00
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2009-11-05 10:55:48 -05:00
|
|
|
if [[ $1 = "-V" || $1 = "--version" ]]; then
|
2007-05-29 16:53:15 -04:00
|
|
|
version
|
|
|
|
exit 0
|
|
|
|
fi
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2009-11-05 10:55:48 -05:00
|
|
|
if [[ -n $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
|
|
|
|
|
2010-12-12 22:40:24 -05:00
|
|
|
if [[ ! -d $dbroot || ! -d $dbroot/local ]]; 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
|
|
|
|
|
2009-11-05 10:55:48 -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
|
2009-11-05 10:55:48 -05:00
|
|
|
if [[ -f $lockfile ]]; then
|
2008-10-16 20:47:54 -04:00
|
|
|
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
|
|
|
|
2011-10-13 11:23:21 -04:00
|
|
|
workdir=$(mktemp -d "${TMPDIR:-/tmp}/pacman-optimize.XXXXXXXXXX") ||
|
2011-06-27 14:29:13 -04:00
|
|
|
die_r "$(gettext "Can not create temp directory for database building.")\n" >&2
|
2008-10-16 20:47:54 -04:00
|
|
|
|
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" ./
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( $? )); 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"
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( $? )); 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
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( $? )); 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
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( fail )); then
|
2008-10-16 20:47:54 -04:00
|
|
|
# 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:
|