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@
|
|
|
|
#
|
2013-01-01 23:19:59 -05:00
|
|
|
# Copyright (c) 2006-2013 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@'
|
|
|
|
|
2013-03-04 03:26:31 -05:00
|
|
|
USE_COLOR='y'
|
|
|
|
|
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"
|
2013-03-04 03:26:31 -05:00
|
|
|
printf -- "$(gettext "Usage: %s [--nocolor] [pacman_db_root]")\n\n" "$0"
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "\
|
2007-07-06 19:35:32 -04:00
|
|
|
pacman-optimize is a little hack that should improve the performance\n\
|
|
|
|
of pacman when reading/writing to its filesystem-based database.\n\n")"
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "\
|
2007-07-06 19:35:32 -04:00
|
|
|
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"
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "\
|
2013-01-01 23:19:59 -05:00
|
|
|
Copyright (c) 2006-2013 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
|
|
|
|
2013-03-04 03:26:31 -05:00
|
|
|
if [[ $1 = "--nocolor" ]]; then
|
|
|
|
USE_COLOR='n'
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
m4_include(library/term_colors.sh)
|
|
|
|
|
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
|
|
|
|
|
2012-03-03 14:02:25 -05:00
|
|
|
if ! type -p openssl >/dev/null; then
|
|
|
|
die "$(gettext "Cannot find the %s binary required for verifying integrity.")" "openssl"
|
2007-10-01 07:29:32 -04:00
|
|
|
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%/}"
|
|
|
|
lockfile="${dbroot}/db.lck"
|
2012-03-03 14:02:25 -05:00
|
|
|
localdb="${dbroot}/local"
|
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") ||
|
2013-11-08 00:49:05 -05:00
|
|
|
die_r "$(gettext "Cannot create temporary 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...")"
|
2012-03-03 14:02:25 -05:00
|
|
|
(cd "$localdb" && find . -type f -print0 | \
|
|
|
|
xargs -0 openssl dgst -md5 | sort > "$workdir/pacsums.old")
|
2007-01-03 00:45:18 -05:00
|
|
|
|
|
|
|
# step 2: tar it up
|
2012-03-03 14:02:25 -05:00
|
|
|
msg "$(gettext "Tar'ing up %s...")" "$localdb"
|
|
|
|
bsdtar -czf "$workdir/pacman-db.tar.gz" -C "$localdb" ./
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( $? )); then
|
2007-08-29 05:49:24 -04:00
|
|
|
rm -rf "$workdir"
|
2012-03-03 14:02:25 -05:00
|
|
|
die_r "$(gettext "Tar'ing up %s failed.")" "$localdb"
|
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...")"
|
2012-03-03 14:02:25 -05:00
|
|
|
mkdir "$localdb.new"
|
|
|
|
bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$localdb.new"
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( $? )); then
|
2013-11-08 00:44:40 -05:00
|
|
|
rm -rf "$workdir"
|
|
|
|
die_r "$(gettext "Untar'ing %s failed.")" "$localdb"
|
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
|
2012-03-03 14:02:25 -05:00
|
|
|
(cd "$localdb.new" && find . -type f -print0 | \
|
|
|
|
xargs -0 openssl dgst -md5 | sort > "$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...")"
|
2012-03-03 14:02:25 -05:00
|
|
|
read -ra old_dgst < <(openssl dgst -md5 < "$workdir/pacsums.old")
|
|
|
|
read -ra new_dgst < <(openssl dgst -md5 < "$workdir/pacsums.new")
|
|
|
|
if [[ ${old_dgst[@]:(-1)} != ${new_dgst[@]:(-1)} ]]; 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
|
2012-03-03 14:02:25 -05:00
|
|
|
rm -rf "$localdb.new"
|
2007-08-29 05:49:24 -04:00
|
|
|
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
|
2012-03-03 14:02:25 -05:00
|
|
|
mv "$localdb" "$localdb.old" || fail=1
|
|
|
|
mv "$localdb.new" "$localdb" || fail=1
|
|
|
|
chmod --reference="$localdb.old" "$localdb" || fail=1
|
|
|
|
chown --reference="$localdb.old" "$localdb" || 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
|
2012-03-03 14:02:25 -05:00
|
|
|
die_r "$(gettext "New database substitution failed. Check for %s, %s, and %s directories.")" "$localdb" "$localdb.old" "$localdb.new"
|
2008-10-16 20:47:54 -04:00
|
|
|
fi
|
2012-03-03 14:02:25 -05:00
|
|
|
rm -rf "$localdb.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:
|