alpm.h: document transaction flags
[pacman-ng.git] / scripts / pacman-optimize.sh.in
blob4e2a328ae40368d873d7391d6a808d97937e10fd
1 #!@BASH_SHELL@
3 # pacman-optimize
4 # @configure_input@
6 # Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
7 # Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # gettext initialization
24 export TEXTDOMAIN='pacman'
25 export TEXTDOMAINDIR='@localedir@'
27 myver='@PACKAGE_VERSION@'
29 eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
30 dbroot="${DBPath:-@localstatedir@/lib/pacman/}"
32 msg() {
33 local mesg=$1; shift
34 printf "==> ${mesg}\n" "$@" >&2
37 error () {
38 local mesg=$1; shift
39 printf "==> ERROR: ${mesg}\n" "$@" >&2
42 usage() {
43 printf "pacman-optimize (pacman) %s\n\n" "$myver"
44 printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0"
45 printf "$(gettext "\
46 pacman-optimize is a little hack that should improve the performance\n\
47 of pacman when reading/writing to its filesystem-based database.\n\n")"
48 printf "$(gettext "\
49 Because pacman uses many small files to keep track of packages,\n\
50 there is a tendency for these files to become fragmented over time.\n\
51 This script attempts to relocate these small files into one\n\
52 continuous location on your hard drive. The result is that the hard\n\
53 drive should be able to read them faster, since the hard drive head\n\
54 does not have to move around the disk as much.\n")"
57 version() {
58 printf "pacman-optimize (pacman) %s\n" "$myver"
59 printf "$(gettext "\
60 Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>.\n\
61 Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
62 This is free software; see the source for copying conditions.\n\
63 There is NO WARRANTY, to the extent permitted by law.\n")"
66 die() {
67 error "$@"
68 exit 1
71 die_r() {
72 rm -f "$lockfile"
73 die "$@"
76 # PROGRAM START
78 # determine whether we have gettext; make it a no-op if we do not
79 if ! type gettext &>/dev/null; then
80 gettext() {
81 echo "$@"
85 if [[ $1 = "-h" || $1 = "--help" ]]; then
86 usage
87 exit 0
90 if [[ $1 = "-V" || $1 = "--version" ]]; then
91 version
92 exit 0
95 if [[ -n $1 ]]; then
96 dbroot="$1"
99 # make sure diff is installed
100 if ! type diff >/dev/null 2>&1; then
101 die "$(gettext "diff tool was not found, please install diffutils.")"
104 if [[ ! -d $dbroot || ! -d $dbroot/local ]]; then
105 die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
108 if [[ ! -w $dbroot ]]; then
109 die "$(gettext "You must have correct permissions to optimize the database.")"
112 # strip any trailing slash from our dbroot
113 dbroot="${dbroot%/}"
114 # form the path to our lockfile location
115 lockfile="${dbroot}/db.lck"
117 # make sure pacman isn't running
118 if [[ -f $lockfile ]]; then
119 die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
121 # do not let pacman run while we do this
122 touch "$lockfile"
124 workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
125 die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
127 # step 1: sum the old db
128 msg "$(gettext "MD5sum'ing the old database...")"
129 find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
131 # step 2: tar it up
132 msg "$(gettext "Tar'ing up %s...")" "$dbroot"
133 cd "$dbroot"
134 bsdtar -czf "$workdir/pacman-db.tar.gz" ./
135 if (( $? )); then
136 rm -rf "$workdir"
137 die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
140 # step 3: make and sum the new db side-by-side with the old
141 msg "$(gettext "Making and MD5sum'ing the new database...")"
142 mkdir "$dbroot.new"
143 bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
144 if (( $? )); then
145 rm -rf "$workdir"
146 die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
148 # immediate sync following extraction should get it written continuously on HDD
149 msg "$(gettext "Syncing database to disk...")"
150 sync
151 find "$dbroot.new" -type f | sort | \
152 xargs md5sum | sed 's#.new##' > "$workdir/pacsums.new"
154 # step 4: compare the sums
155 msg "$(gettext "Checking integrity...")"
156 diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
157 if (( $? )); then
158 # failed
159 # leave our pacman-optimize tmpdir for checking to see what doesn't match up
160 rm -rf "$dbroot.new"
161 die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
164 # step 5: shuffle the newly extracted DB into the proper location
165 msg "$(gettext "Rotating database into place...")"
167 fail=0
168 mv "$dbroot" "$dbroot.old" || fail=1
169 mv "$dbroot.new" "$dbroot" || fail=1
170 chmod --reference="$dbroot.old" "$dbroot" || fail=1
171 chown --reference="$dbroot.old" "$dbroot" || fail=1
172 if (( fail )); then
173 # failure with our directory shuffle
174 die_r "$(gettext "New database substitution failed. Check for $dbroot,\n$dbroot.old, and $dbroot.new directories.")"
176 rm -rf "$dbroot.old"
178 # remove the lock file and our working directory with sums and tarfile
179 rm -f "$lockfile"
180 rm -rf "$workdir"
182 echo
183 msg "$(gettext "Finished. Your pacman database has been optimized.")"
184 echo
186 exit 0
188 # vim: set ts=2 sw=2 noet: