summaryrefslogtreecommitdiffstats
path: root/scripts/pacman-optimize.in
blob: aaae6d2463fb6b11dded20cd6d2b9ee3cdb9f205 (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
#!/bin/bash
#
#   pacman-optimize
#   @configure_input@
#
#   Copyright (c) 2002-2007 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

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

myver='@PACKAGE_VERSION@'
# TODO fix these paths
dbroot='@ROOTDIR@@DBPATH@'
lockfile='@ROOTDIR@@LOCKFILE@'

msg() {
	local mesg=$1; shift
	printf "==> ${mesg}\n" "$@" >&2
}

error () {
	local mesg=$1; shift
	printf "==> ERROR: ${mesg}\n" "$@" >&2
}

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

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

die() {
	error $@
	exit 1
}

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

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

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

if [ "$1" != "" ]; then
	dbroot="$1"
fi

# make sure pacman isn't running
if [ -f $lockfile ]; then
	die "$(gettext "Pacman lockfile was found. Cannot run while pacman is running.")"
fi

if [ ! -d "$dbroot" ]; 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

# do not let pacman run while we do this
touch $lockfile

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

# step 2: tar it up
msg "$(gettext "Tar'ing up %s...")" "$dbroot"
cd $dbroot
tar -czf /tmp/pacmanDB.tgz ./
if [ $? -ne 0 ]; then
	rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
	die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
fi

# step 3: make and sum the new db
msg "$(gettext "Making and MD5sum'ing the new db...")"
mkdir $dbroot.new
tar -zxpf /tmp/pacmanDB.tgz -C $dbroot.new/
if [ $? -ne 0 ]; then
       rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
       rm -rf "$dbroot.new"
       die_r "$(gettext "Untar'ing $dbroot failed.")"
fi
find "$dbroot.new" -type f | sort | sed -e 's/pacman.new/pacman/g' | \
		xargs md5sum > /tmp/pacsums.new

# step 4: compare the sums
msg "$(gettext "Checking integrity...")"
diff /tmp/pacsums.old /tmp/pacsums.new >/dev/null 2>&1
if [ $? -ne 0 ]; then
	# failed
	# leave /tmp/pacsums.old and .new for checking to see what doesn't match up
	rm -rf "$dbroot.new" $lockfile /tmp/pacmanDB.tgz
	die_r "$(gettext "integrity check FAILED, reverting to old database.")"
fi

# step 5: remove the new temporary database and the old one
#         and use the .tgz to replace the old one
msg "$(gettext "Putting the new database in place...")"
rm -rf "$dbroot.new" "$dbroot"/*
tar -zxpf /tmp/pacmanDB.tgz -C "$dbroot"/

# remove the lock file, sum files, and .tgz of database
rm -f $lockfile /tmp/pacsums.old /tmp/pacsums.new /tmp/pacmanDB.tgz

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

exit 0

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