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/}"
34 printf "==> ${mesg}\n" "$@" >&2
39 printf "==> ERROR: ${mesg}\n" "$@" >&2
43 printf "pacman-optimize (pacman) %s\n\n" "$myver"
44 printf "$(gettext "Usage
: %s
[pacman_db_root
]")\n\n" "$0"
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")"
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")"
58 printf "pacman-optimize (pacman) %s\n" "$myver"
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")"
78 # determine whether we have gettext; make it a no-op if we do not
79 if ! type gettext &>/dev
/null
; then
85 if [[ $1 = "-h" ||
$1 = "--help" ]]; then
90 if [[ $1 = "-V" ||
$1 = "--version" ]]; then
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
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
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"
132 msg "$(gettext "Tar'ing up
%s...
")" "$dbroot"
134 bsdtar
-czf "$workdir/pacman-db.tar.gz" .
/
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...
")"
143 bsdtar
-xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
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...")"
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
159 # leave our pacman-optimize tmpdir for checking to see what doesn't match up
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...
")"
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
173 # failure with our directory shuffle
174 die_r
"$(gettext "New database substitution failed. Check
for $dbroot,\n$dbroot.old
, and
$dbroot.new directories.
")"
178 # remove the lock file and our working directory with sums and tarfile
183 msg
"$(gettext "Finished. Your pacman database has been optimized.
")"
188 # vim: set ts=2 sw=2 noet: