Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/scripts/pacman-optimize.sh.in
blob: 74af0e5d763b065ed9010a53c1dcd11e03371443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
#
#   pacman-optimize
#   @configure_input@
#
#   Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
#   Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# gettext initialization
export TEXTDOMAIN='pacman-scripts'
export TEXTDOMAINDIR='@localedir@'

myver='@PACKAGE_VERSION@'

eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
dbroot="${DBPath:-@localstatedir@/lib/pacman/}"

m4_include(library/output_format.sh)

usage() {
	printf "pacman-optimize (pacman) %s\n\n" "$myver"
	printf -- "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0"
	printf -- "$(gettext "\
pacman-optimize is a little hack that should improve the performance\n\
of pacman when reading/writing to its filesystem-based database.\n\n")"
	printf -- "$(gettext "\
Because pacman uses many small files to keep track of packages,\n\
there is a tendency for these files to become fragmented over time.\n\
This script attempts to relocate these small files into one\n\
continuous location on your hard drive. The result is that the hard\n\
drive should be able to read them faster, since the hard drive head\n\
does not have to move around the disk as much.\n")"
}

version() {
	printf "pacman-optimize (pacman) %s\n" "$myver"
	printf -- "$(gettext "\
Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>.\n\
Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}

die() {
	error "$@"
	exit 1
}

die_r() {
	rm -f "$lockfile"
	die "$@"
}

# PROGRAM START

# determine whether we have gettext; make it a no-op if we do not
if ! type gettext &>/dev/null; then
	gettext() {
		echo "$@"
	}
fi

if [[ $1 = "-h" || $1 = "--help" ]]; then
	usage
	exit 0
fi

if [[ $1 = "-V" || $1 = "--version" ]]; then
	version
	exit 0
fi

if [[ -n $1 ]]; then
	dbroot="$1"
fi

# make sure diff is installed
if ! type diff >/dev/null 2>&1; then
	die "$(gettext "diff tool was not found, please install diffutils.")"
fi

if [[ ! -d $dbroot || ! -d $dbroot/local ]]; then
	die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
fi

if [[ ! -w $dbroot ]]; then
	die "$(gettext "You must have correct permissions to optimize the database.")"
fi

# strip any trailing slash from our dbroot
dbroot="${dbroot%/}"
# form the path to our lockfile location
lockfile="${dbroot}/db.lck"

# make sure pacman isn't running
if [[ -f $lockfile ]]; then
	die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
fi
# do not let pacman run while we do this
touch "$lockfile"

workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
	die_r "$(gettext "Can not create temp directory for database building.")\n" >&2

# step 1: sum the old db
msg "$(gettext "MD5sum'ing the old database...")"
find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"

# step 2: tar it up
msg "$(gettext "Tar'ing up %s...")" "$dbroot"
cd "$dbroot"
bsdtar -czf "$workdir/pacman-db.tar.gz" ./
if (( $? )); then
	rm -rf "$workdir"
	die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
fi

# step 3: make and sum the new db side-by-side with the old
msg "$(gettext "Making and MD5sum'ing the new database...")"
mkdir "$dbroot.new"
bsdtar -xpf "$workdir/pacman-db.tar.gz" -C "$dbroot.new"
if (( $? )); then
       rm -rf "$workdir"
       die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
fi
# immediate sync following extraction should get it written continuously on HDD
msg "$(gettext "Syncing database to disk...")"
sync
find "$dbroot.new" -type f | sort | \
		xargs md5sum | sed 's#.new##' > "$workdir/pacsums.new"

# step 4: compare the sums
msg "$(gettext "Checking integrity...")"
diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
if (( $? )); then
	# failed
	# leave our pacman-optimize tmpdir for checking to see what doesn't match up
	rm -rf "$dbroot.new"
	die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
fi

# step 5: shuffle the newly extracted DB into the proper location
msg "$(gettext "Rotating database into place...")"

fail=0
mv "$dbroot" "$dbroot.old" || fail=1
mv "$dbroot.new" "$dbroot" || fail=1
chmod --reference="$dbroot.old" "$dbroot" || fail=1
chown --reference="$dbroot.old" "$dbroot" || fail=1
if (( fail )); then
	# failure with our directory shuffle
	die_r "$(gettext "New database substitution failed. Check for $dbroot,\n$dbroot.old, and $dbroot.new directories.")"
fi
rm -rf "$dbroot.old"

# remove the lock file and our working directory with sums and tarfile
rm -f "$lockfile"
rm -rf "$workdir"

echo
msg "$(gettext "Finished. Your pacman database has been optimized.")"
echo

exit 0

# vim: set ts=2 sw=2 noet: