summaryrefslogtreecommitdiff
path: root/scripts/pacman-db-upgrade.sh.in
blob: d2d317bf5d5afcca27054b0bd328ec967e731efb (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash -e
#
#   pacman-db-upgrade - upgrade the local pacman db to a newer format
#   @configure_input@
#
#   Copyright (c) 2010-2014 Pacman Development Team <pacman-dev@archlinux.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@'

declare -r myver='@PACKAGE_VERSION@'

m4_include(library/output_format.sh)

m4_include(library/parseopts.sh)

usage() {
	printf "pacman-db-upgrade (pacman) %s\n" "${myver}"
	echo
	printf -- "$(gettext "Upgrade the local pacman database to a newer format")\n"
	echo
	printf -- "$(gettext "Usage: %s [options]")\n" "$0"
	echo
	printf -- "$(gettext "options:")\n"
	printf -- "$(gettext "  -b, --dbpath <path>  set an alternate database location")\n"
	printf -- "$(gettext "  -h, --help           show this help message and exit")\n"
	printf -- "$(gettext "  -r, --root <path>    set an alternate installation root")\n"
	printf -- "$(gettext "  -V, --version        show version information and exit")\n"
	printf -- "$(gettext "  --config <path>      set an alternate configuration file")\n"
	printf -- "$(gettext "  --nocolor            disable colorized output messages")\n"
	echo
}

version() {
	printf "pacman-db-upgrade (pacman) %s\n" "$myver"
	printf -- "$(gettext "\
Copyright (c) 2010-2014 Pacman Development Team <pacman-dev@archlinux.org>.\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 "$@"
}

get_opt_from_config() {
	local keyname="$1" conffile="$2"
	local key value

	while IFS=$'= \t' read -r key value _; do
		if [[ $key = $keyname ]]; then
			echo "$value"
			return
		fi
	done <"$conffile"
}

resolve_dir() {
	local d="$(cd "$1"; pwd -P)"
	[[ $d == */ ]] || d+=/
	printf "%s" "$d"
}

# 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

USE_COLOR='y'

OPT_SHORT="d:hr:V"
OPT_LONG=('confg' 'dbpath:' 'help' 'nocolor' 'root:' 'version')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
	exit 1 # E_INVALID_OPTION
fi
set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPTRET

while true; do
	case "$1" in
		--config)     shift; conffile="$1" ;;
		-d|--dbpath)  shift; dbroot="$1" ;;
		-r|--root)    shift; pacroot="$1" ;;
		-h|--help)    usage; exit 0 ;;
		--nocolor)    USE_COLOR='n' ;;
		-V|--version) version; exit 0 ;;
		-- )          shift; break 2 ;;
	esac
	shift
done

conffile=${conffile:-@sysconfdir@/pacman.conf}
[[ -z $pacroot ]] && pacroot="$(get_opt_from_config "RootDir" "$conffile")"
[[ -z $dbroot ]] && dbroot="$(get_opt_from_config "DBPath" "$conffile")"

[[ -z $dbroot && -n $pacroot ]] && dbroot="$pacroot/@localstatedir@/lib/pacman"

[[ -z $pacroot ]] && pacroot="@rootdir@"
[[ -z $dbroot ]] && dbroot="@localstatedir@/lib/pacman/"

m4_include(library/term_colors.sh)

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

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

if [[ ! -w $dbroot ]]; then
	die "$(gettext "You must have correct permissions to upgrade 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"

if [[ -f "${dbroot}"/local/ALPM_DB_VERSION ]]; then
	db_version=$(cat "${dbroot}"/local/ALPM_DB_VERSION)
fi

if [[ -z "$db_version" ]]; then
	# pacman-3.4 to 3.5 upgrade - merge depends into desc
	if [[ $(find "$dbroot"/local -name depends) ]]; then
		msg "$(gettext "Pre-3.5 database format detected - upgrading...")"
		for i in "$dbroot"/local/*; do
			if [[ -f "$i"/depends ]]; then
				cat "$i"/depends >> "$i"/desc
				rm "$i"/depends
			fi
		done
		msg "$(gettext "Done.")"
	fi

	# pacman 4.1 to 4.2 upgrade - remove directory symlink support
	dirlist=()

	unset GREP_OPTIONS
	while IFS= read -r dir; do
		dirlist+=("${pacroot}${dir%/}")
	done < <(grep -h '/$' "$dbroot"/local/*/files | sort -ru)

	mapfile -t dirlist < <(find "${dirlist[@]}" -maxdepth 0 -type l)

	if [[ ${#dirlist[@]} != 0 ]]; then
		msg "$(gettext "Pre-4.2 database format detected - upgrading...")"

		pacroot="$(resolve_dir "$pacroot")"

		for dir in "${dirlist[@]}"; do
			realdir="$(resolve_dir "$dir")"

			# verify realdir is inside root
			if [[ ${realdir:0:${#pacroot}} != $pacroot ]]; then
				warning "$(gettext "symlink '%s' points outside pacman root, manual repair required")" "$dir"
				continue
			fi

			# convert to an appropriate form for the replacement
			olddir="${dir:${#pacroot}}/"
			newdir="${realdir:${#pacroot}}"

			# construct the parents of the new directory
			parents=""
			parent="$(dirname "$newdir")"
			while [[ $parent != "." ]]; do
				parents+="$parent/\n"
				parent="$(dirname "$parent")"
			done

			for f in "$dbroot"/local/*/files; do
				awk -v "olddir=$olddir" -v "newdir=$newdir" -v "parents=$parents" '
					BEGIN {
						i = length(olddir) + 1
						file = substr(newdir, 0, length(newdir) - 1)
					}
					{
						if ($0 == olddir) {
							# replace symlink with its target, including parents
							printf("%s", parents)
							printf("%s\n", newdir)
						} else if ($0 == file) {
							# newdir already existed as a file, skip it
						} else if (index($0, olddir) == 1) {
							# update paths that were under olddir
							printf("%s%s\n", newdir, substr($0, i))
						} else {
							# print everything else as-is
							print
						}
					}' "$f" | grep . | LC_ALL=C sort -u > "$f.tmp"
				mv "$f.tmp" "$f"
			done
		done
	fi
fi

echo "9" > "$dbroot"/local/ALPM_DB_VERSION

# remove the lock file
rm -f "$lockfile"

# vim: set noet: