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
|
|
|
|
# along with this program; if not, write to the Free Software
|
2007-05-30 11:04:49 -04:00
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
2005-10-20 16:46:35 -04:00
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
|
2007-04-11 16:40:33 -04:00
|
|
|
myver='@PACKAGE_VERSION@'
|
2007-05-29 17:46:20 -04:00
|
|
|
dbroot='@ROOTDIR@@DBPATH@'
|
|
|
|
lockfile='@ROOTDIR@@LOCKFILE@'
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
error() {
|
2007-01-11 12:22:21 -05:00
|
|
|
if [ "$USECOLOR" = "YES" -o "$USECOLOR" = "yes" ]; then
|
|
|
|
echo -e "\033[1;31m:: ERROR:\033[1;0m \033[1;1m$@\033[1;0m" >&2
|
|
|
|
else
|
|
|
|
echo ":: ERROR: $@" >&2
|
|
|
|
fi
|
2007-01-03 00:45:18 -05:00
|
|
|
}
|
|
|
|
|
2007-05-29 16:53:15 -04:00
|
|
|
# determine current USECOLOR setting
|
2007-05-29 17:46:20 -04:00
|
|
|
[ -f "/etc/rc.conf" ] && source /etc/rc.conf
|
|
|
|
[ -f "/etc/rc.d/functions" ] && source /etc/rc.d/functions
|
2007-01-03 00:45:18 -05:00
|
|
|
|
2005-10-20 16:46:35 -04:00
|
|
|
usage() {
|
2007-05-29 16:53:15 -04:00
|
|
|
echo "pacman-optimize (pacman) $myver"
|
2005-10-20 16:46:35 -04:00
|
|
|
echo "usage: $0 [pacman_db_root]"
|
2007-05-30 11:04:49 -04:00
|
|
|
echo
|
2005-10-20 16:46:35 -04:00
|
|
|
echo "pacman-optimize is a little hack that should improve the performance"
|
|
|
|
echo "of pacman when reading/writing to its filesystem-based database."
|
|
|
|
echo
|
|
|
|
echo "Because pacman uses many small files to keep track of packages,"
|
|
|
|
echo "there is a tendency for these files to become fragmented over time."
|
|
|
|
echo "This script attempts to relocate these small files into one"
|
2007-01-03 00:45:18 -05:00
|
|
|
echo "continuous location on your hard drive. The result is that the hard"
|
2005-10-20 16:46:35 -04:00
|
|
|
echo "drive should be able to read them faster, since the hard drive head"
|
|
|
|
echo "does not have to move around the disk as much."
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2007-04-11 16:40:33 -04:00
|
|
|
version() {
|
|
|
|
printf "pacman-optimize (pacman) %s\n" "$myver"
|
|
|
|
printf "Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>.\n"
|
|
|
|
echo
|
|
|
|
printf "This is free software; see the source for copying conditions.\n"
|
|
|
|
printf "There is NO WARRANTY, to the extent permitted by law.\n"
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2005-10-20 16:46:35 -04:00
|
|
|
die() {
|
2007-01-03 00:45:18 -05:00
|
|
|
error $@
|
2005-10-20 16:46:35 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
die_r() {
|
2007-05-29 16:53:15 -04:00
|
|
|
rm -f $lockfile
|
2007-01-03 00:45:18 -05:00
|
|
|
die $@
|
2005-10-20 16:46:35 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# make sure pacman isn't running
|
2007-05-29 16:53:15 -04:00
|
|
|
if [ -f $lockfile ]; then
|
2005-10-20 16:46:35 -04:00
|
|
|
die "Pacman lockfile was found. Cannot run while pacman is running."
|
|
|
|
fi
|
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
if [ ! -d "$dbroot" ]; then
|
2005-10-20 16:46:35 -04:00
|
|
|
die "$dbroot does not exist or is not a directory"
|
|
|
|
fi
|
|
|
|
|
2006-12-28 13:19:25 -05:00
|
|
|
if [ ! -w "$dbroot" ]; then
|
|
|
|
die "You must have correct permissions to optimize the database"
|
|
|
|
fi
|
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
# do not let pacman run while we do this
|
2007-05-29 16:53:15 -04:00
|
|
|
touch $lockfile
|
2005-10-20 16:46:35 -04:00
|
|
|
|
|
|
|
# step 1: sum the old db
|
2007-01-03 00:45:18 -05:00
|
|
|
stat_busy "Md5sum'ing the old database"
|
|
|
|
find $dbroot -type f | sort | xargs md5sum > /tmp/pacsums.old
|
|
|
|
stat_done
|
|
|
|
|
|
|
|
# step 2: tar it up
|
|
|
|
stat_busy "Tar'ing up $dbroot"
|
|
|
|
cd $dbroot
|
2007-01-11 12:22:21 -05:00
|
|
|
tar -czf /tmp/pacmanDB.tgz ./
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
stat_fail
|
|
|
|
rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
|
|
|
|
die_r "tar'ing up $dbroot failed"
|
|
|
|
fi
|
2007-01-03 00:45:18 -05:00
|
|
|
stat_done
|
|
|
|
|
|
|
|
# step 3: make and sum the new db
|
|
|
|
stat_busy "Making and md5sum'ing the new db"
|
|
|
|
mkdir $dbroot.new
|
2007-01-11 12:22:21 -05:00
|
|
|
tar -zxpf /tmp/pacmanDB.tgz -C $dbroot.new/
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
|
|
|
|
rm -rf "$dbroot.new"
|
|
|
|
die_r "untar'ing $dbroot failed"
|
|
|
|
fi
|
|
|
|
find "$dbroot.new" -type f | sort | sed -e 's/pacman.new/pacman/g' |\
|
|
|
|
xargs md5sum > /tmp/pacsums.new
|
2007-01-03 00:45:18 -05:00
|
|
|
stat_done
|
|
|
|
|
|
|
|
# step 4: compare the sums
|
|
|
|
stat_busy "Checking integrity"
|
2005-10-20 16:46:35 -04:00
|
|
|
diff /tmp/pacsums.old /tmp/pacsums.new >/dev/null 2>&1
|
|
|
|
if [ $? -ne 0 ]; then
|
2007-01-03 00:45:18 -05:00
|
|
|
# failed
|
|
|
|
stat_fail
|
|
|
|
# leave /tmp/pacsums.old and .new for checking to see what doesn't match up
|
2007-05-29 16:53:15 -04:00
|
|
|
rm -rf "$dbroot.new" $lockfile /tmp/pacmanDB.tgz
|
2005-10-20 16:46:35 -04:00
|
|
|
die_r "integrity check FAILED, reverting to old database"
|
|
|
|
fi
|
2007-01-11 12:22:21 -05:00
|
|
|
stat_done
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
# step 5: remove the new temporary database and the old one
|
|
|
|
# and use the .tgz to replace the old one
|
|
|
|
stat_busy "Putting the new database in place"
|
|
|
|
rm -rf "$dbroot.new" "$dbroot"/*
|
|
|
|
tar -zxpf /tmp/pacmanDB.tgz -C "$dbroot"/
|
|
|
|
stat_done
|
2005-10-20 16:46:35 -04:00
|
|
|
|
2007-01-03 00:45:18 -05:00
|
|
|
# remove the lock file, sum files, and .tgz of database
|
2007-05-29 16:53:15 -04:00
|
|
|
rm -f $lockfile /tmp/pacsums.old /tmp/pacsums.new /tmp/pacmanDB.tgz
|
2005-10-20 16:46:35 -04:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Finished. Your pacman database has been optimized."
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
# vim: set ts=2 sw=2 noet:
|