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/'
32 printf "==> ${mesg}\n" "$@" >&2
37 printf "==> ERROR: ${mesg}\n" "$@" >&2
41 printf "pacman-optimize (pacman) %s\n\n" "$myver"
42 printf "$(gettext "Usage
: %s
[pacman_db_root
]")\n\n" "$0"
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")"
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")"
56 printf "pacman-optimize (pacman) %s\n" "$myver"
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")"
76 # determine whether we have gettext; make it a no-op if we do not
77 if ! type gettext &>/dev
/null
; then
83 if [[ $1 = "-h" ||
$1 = "--help" ]]; then
88 if [[ $1 = "-V" ||
$1 = "--version" ]]; then
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
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
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"
130 msg "$(gettext "Tar'ing up
%s...
")" "$dbroot"
132 bsdtar
-czf "$workdir/pacman-db.tar.gz" .
/
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...
")"
141 bsdtar
-xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
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...")"
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
157 # leave our pacman-optimize tmpdir for checking to see what doesn't match up
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...
")"
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
171 # failure with our directory shuffle
172 die_r
"$(gettext "New database substitution failed. Check
for $dbroot,\n$dbroot.old
, and
$dbroot.new directories.
")"
176 # remove the lock file and our working directory with sums and tarfile
181 msg
"$(gettext "Finished. Your pacman database has been optimized.
")"
186 # vim: set ts=2 sw=2 noet: