6 # Copyright (c) 2006-2012 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 declare -r 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
)
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")"
50 printf "pacman-optimize (pacman) %s\n" "$myver"
51 printf -- "$(gettext "\
52 Copyright
(c
) 2006-2012 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")"
70 # determine whether we have gettext; make it a no-op if we do not
71 if ! type gettext &>/dev
/null
; then
77 if [[ $1 = "-h" ||
$1 = "--help" ]]; then
82 if [[ $1 = "-V" ||
$1 = "--version" ]]; then
91 if ! type -p openssl
>/dev
/null
; then
92 die
"$(gettext "Cannot
find the
%s binary required
for verifying integrity.
")" "openssl"
95 if [[ ! -d $dbroot ||
! -d $dbroot/local ]]; then
96 die
"$(gettext "%s does not exist or is not a directory.
")" "$dbroot"
99 if [[ ! -w $dbroot ]]; then
100 die
"$(gettext "You must have correct permissions to optimize the database.
")"
103 # strip any trailing slash from our dbroot
105 lockfile
="${dbroot}/db.lck"
106 localdb
="${dbroot}/local"
108 # make sure pacman isn't running
109 if [[ -f $lockfile ]]; then
110 die
"$(gettext "Pacman lock
file was found. Cannot run
while pacman is running.
")"
112 # do not let pacman run while we do this
115 workdir
=$
(mktemp
-d "${TMPDIR:-/tmp}/pacman-optimize.XXXXXXXXXX") ||
116 die_r
"$(gettext "Can not create temp directory
for database building.
")\n" >&2
118 # step 1: sum the old db
119 msg
"$(gettext "MD5sum
'ing the old database...")"
120 (cd "$localdb" && find . -type f -print0 | \
121 xargs -0 openssl dgst -md5 | sort > "$workdir/pacsums.old")
124 msg "$(gettext "Tar'ing up
%s...
")" "$localdb"
125 bsdtar
-czf "$workdir/pacman-db.tar.gz" -C "$localdb" .
/
128 die_r
"$(gettext "Tar
'ing up %s failed.")" "$localdb"
131 # step 3: make and sum the new db side-by-side with the old
132 msg "$(gettext "Making and MD5sum'ing the new database...
")"
134 bsdtar
-xpf "$workdir/pacman-db.tar.gz" -C "$localdb.new"
137 die_r
"$(gettext "Untar
'ing %s failed.")" "$localdb"
139 # immediate sync following extraction should get it written continuously on HDD
140 msg "$(gettext "Syncing database to disk...")"
142 (cd "$localdb.new" && find . -type f -print0 | \
143 xargs -0 openssl dgst -md5 | sort > "$workdir/pacsums.new")
145 # step 4: compare the sums
146 msg "$(gettext "Checking integrity...")"
147 read -ra old_dgst < <(openssl dgst -md5 < "$workdir/pacsums.old")
148 read -ra new_dgst < <(openssl dgst -md5 < "$workdir/pacsums.new")
149 if [[ ${old_dgst[@]:(-1)} != ${new_dgst[@]:(-1)} ]]; then
151 # leave our pacman-optimize tmpdir for checking to see what doesn't match up
152 rm -rf "$localdb.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...
")"
160 mv "$localdb" "$localdb.old" || fail
=1
161 mv "$localdb.new" "$localdb" || fail
=1
162 chmod --reference="$localdb.old" "$localdb" || fail
=1
163 chown
--reference="$localdb.old" "$localdb" || fail
=1
165 # failure with our directory shuffle
166 die_r
"$(gettext "New database substitution failed. Check
for %s
, %s
, and
%s directories.
")" "$localdb" "$localdb.old" "$localdb.new"
168 rm -rf "$localdb.old"
170 # remove the lock file and our working directory with sums and tarfile
175 msg
"$(gettext "Finished. Your pacman database has been optimized.
")"
180 # vim: set ts=2 sw=2 noet: