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