makepkg: fix variable checks when writing pkginfo
[pacman-ng.git] / scripts / pacman-optimize.sh.in
blob78b2345b436250ab69efd1cf888478ede7bb65de
1 #!/bin/bash
3 # pacman-optimize
4 # @configure_input@
6 # Copyright (c) 2006-2010 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@'
28 dbroot='@localstatedir@/lib/pacman/'
30 msg() {
31 local mesg=$1; shift
32 printf "==> ${mesg}\n" "$@" >&2
35 error () {
36 local mesg=$1; shift
37 printf "==> ERROR: ${mesg}\n" "$@" >&2
40 usage() {
41 printf "pacman-optimize (pacman) %s\n\n" "$myver"
42 printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0"
43 printf "$(gettext "\
44 pacman-optimize is a little hack that should improve the performance\n\
45 of pacman when reading/writing to its filesystem-based database.\n\n")"
46 printf "$(gettext "\
47 Because pacman uses many small files to keep track of packages,\n\
48 there is a tendency for these files to become fragmented over time.\n\
49 This script attempts to relocate these small files into one\n\
50 continuous location on your hard drive. The result is that the hard\n\
51 drive should be able to read them faster, since the hard drive head\n\
52 does not have to move around the disk as much.\n")"
55 version() {
56 printf "pacman-optimize (pacman) %s\n" "$myver"
57 printf "$(gettext "\
58 Copyright (c) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>.\n\
59 Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
60 This is free software; see the source for copying conditions.\n\
61 There is NO WARRANTY, to the extent permitted by law.\n")"
64 die() {
65 error "$@"
66 exit 1
69 die_r() {
70 rm -f "$lockfile"
71 die "$@"
74 # PROGRAM START
76 # determine whether we have gettext; make it a no-op if we do not
77 if ! type gettext &>/dev/null; then
78 gettext() {
79 echo "$@"
83 if [[ $1 = "-h" || $1 = "--help" ]]; then
84 usage
85 exit 0
88 if [[ $1 = "-V" || $1 = "--version" ]]; then
89 version
90 exit 0
93 if [[ -n $1 ]]; then
94 dbroot="$1"
97 # make sure diff is installed
98 if ! type diff >/dev/null 2>&1; then
99 die "$(gettext "diff tool was not found, please install diffutils.")"
102 if [[ ! -d $dbroot ]]; then
103 die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
106 if [[ ! -w $dbroot ]]; then
107 die "$(gettext "You must have correct permissions to optimize the database.")"
110 # strip any trailing slash from our dbroot
111 dbroot="${dbroot%/}"
112 # form the path to our lockfile location
113 lockfile="${dbroot}/db.lck"
115 # make sure pacman isn't running
116 if [[ -f $lockfile ]]; then
117 die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
119 # do not let pacman run while we do this
120 touch "$lockfile"
122 workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
123 die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
125 # step 1: sum the old db
126 msg "$(gettext "MD5sum'ing the old database...")"
127 find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
129 # step 2: tar it up
130 msg "$(gettext "Tar'ing up %s...")" "$dbroot"
131 cd "$dbroot"
132 bsdtar -czf "$workdir/pacman-db.tar.gz" ./
133 if (( $? )); then
134 rm -rf "$workdir"
135 die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
138 # step 3: make and sum the new db side-by-side with the old
139 msg "$(gettext "Making and MD5sum'ing the new database...")"
140 mkdir "$dbroot.new"
141 bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
142 if (( $? )); then
143 rm -rf "$workdir"
144 die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
146 # immediate sync following extraction should get it written continuously on HDD
147 msg "$(gettext "Syncing database to disk...")"
148 sync
149 find "$dbroot.new" -type f | sort | \
150 xargs md5sum | sed 's#.new##' > "$workdir/pacsums.new"
152 # step 4: compare the sums
153 msg "$(gettext "Checking integrity...")"
154 diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
155 if (( $? )); then
156 # failed
157 # leave our pacman-optimize tmpdir for checking to see what doesn't match up
158 rm -rf "$dbroot.new"
159 die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
162 # step 5: shuffle the newly extracted DB into the proper location
163 msg "$(gettext "Rotating database into place...")"
165 fail=0
166 mv "$dbroot" "$dbroot.old" || fail=1
167 mv "$dbroot.new" "$dbroot" || fail=1
168 chmod --reference="$dbroot.old" "$dbroot" || fail=1
169 chown --reference="$dbroot.old" "$dbroot" || fail=1
170 if (( fail )); then
171 # failure with our directory shuffle
172 die_r "$(gettext "New database substitution failed. Check for $dbroot,\n$dbroot.old, and $dbroot.new directories.")"
174 rm -rf "$dbroot.old"
176 # remove the lock file and our working directory with sums and tarfile
177 rm -f "$lockfile"
178 rm -rf "$workdir"
180 echo
181 msg "$(gettext "Finished. Your pacman database has been optimized.")"
182 echo
184 exit 0
186 # vim: set ts=2 sw=2 noet: