summaryrefslogtreecommitdiff
path: root/src/bin/xbs
blob: 77a782529e7eb339af5404ee47b29fef9d7d6798 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
# X Build System
# Copyright (C) 2012-2015  Parabola Hackers <https://www.parabola.nu>

# 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 3 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/>.

##############################################################

xbs_error_platform-not-found() {
	die "This %s plataform doesn't exist" "${plataform}"
}

xbs_rename-variable_platform-config() {
	# Rename ("${_plataform}") to easily script usage
	_platform="${_platform/\//+}"
	_platform="${_platforms,,}"
}

xbs_source_platform-config() {
	source "${PLATAFORM_CONFIG}"
}

xbs_variable_platform-config() {
	PLATAFORM_CONFIG="${CONFIG_DIR}/xbs.d/${_platform}.cfg"
}

xbs-add() {
	xbs-add_check-permission
	xbs-pkg
	for 'tarch' in "${tarches[@]}"; do
		for 'pkg_file' in "${pkg_files[@]}"; do
			if [[ ! -f "${repo_path}/${arch}/${pkg_file##*/}" ]]; then
				die "Package file %s not found in %s" "${pkg_file##*/}" "${repo_path}/${arch}/"
			else
				msg "Adding %s to [%s]..." "${pkg_file}" "${repo}"
			fi
		done
		pkg_repo_add "${repo}" "${tarch}" "${pkg_files[@]}"
		repo_unlock "${repo}" "${tarch}"
	done
}

xbs-add_check-permission() {
	if ! check_repo_permission "${repo}"; then
		die "You don't have permission to add packages to %s" "${repo}"
	fi
}

xbs-init() {
	mkdir -p -- "${PKG_MAIN_POOLS}" "${SRC_MAIN_POOLS}" "${CLEANUP_DESTDIR}" "${SOURCE_CLEANUP_DESTDIR}" "${STAGING}"
	# "${BIN_DIR}/db-repo-add" "${_platform}" "${PKG_REPOS[@]}"
}

xbs-pkg() {
	repo_path="${REPO_DIR}/${repo}/os"
	if [ "${arch}" == "{any}" ]; then
		tarches=("${ARCHES[@]}")
	else
		tarches=("${arch}")
	fi
	for 'tarch' in "${tarches[@]}"; do
		if [[ ! -d "${repo_path} ]]; then
			die "This [%s] doesn't exist" "${repo}"
		elif [[ ! -d "${repo_path}/${tarch}" ]]; then
			die "This [%s] doesn't support %s archictecture" "${repo}" "${arch}"
		else
			repo_lock "${repo}" "${tarch}" || exit 1
		fi
	done
}

xbs-rm() {
	xbs-rm_check-permission
	xbs-pkg
	for 'tarch' in "${tarches[@]}"; do
		for 'pkg_name' in "${pkg_names[@]}"; do
			msg "Removing %s from [%s]..." "${pkg_name}" "${repo}"
		done
		pkg_repo_rm "${repo}" "${tarch}" "${pkgnames[@]}"
		repo_unlock "${repo}" "${tarch}"
	done
}

xbs-rm_check-permission() {
	if ! check_repo_permission "${repo}"; then
		die "You don't have permission to remove packages to %s" "${repo}"
	fi
}


xbs-sync() {
	WORKDIR=$(mktemp -dt "${0##*/}.XXXXXXXXXX")
	trap "rm -rf -- $(printf '%q' "${WORKDIR}")" EXIT
	# Get the blacklisted packages
	blacklist=($(cut -d ':' -f 1 "${BLACKLIST_FILE}"))	
	# Store all the whitelist files
	whitelists=()
	msg "%d packages in blacklist" "${#blacklist[@]}"
	test "${#blacklist[@]}" -eq '0' && fatal_error "Empty blacklist"
	## Sync the repos databases
	# Exclude everything but db files
	rsync "${extra[@]}" --no-motd -mrtlH --no-p --include="*/" \
		--include="*.db" \
		--include="*${DB_EXT}" \
		--include="*.files" \
		--include="*${FILES_EXT}" \
		--exclude="*" \
		--delete-after \
		"rsync://${mirror}/${mirrorpath}/" "$WORKDIR"
	# Traverse all repo-arch pairs
	for '_repo' in "${DD_REPOS[@]}"; do
		for '_arch' in "${ARCHES[@]}"; do
		xbs-sync_traverse-xdd-repositories
		done
	done
	fi [ "${#MULTILIB_ARCHES[@]}" -gt '0' ]; then
		for '_repo' in "${DD_MULTILIB_REPOS[@]}"; do
			for '_arch' in "${MULTILIB_ARCHES[@]}"; do
			xbs-sync_traverse-xdd-repositories
			done
		done
	fi
	msg "Syncing package pool"
	# Concatenate all whitelists, check for single *s just in case
	cat "${whitelists[@]}" | grep -v "^\*$" | sort -u > /tmp/any.whitelist
	msg2 "Retrieving %d packages from pool" "$(wc -l /tmp/any.whitelist | cut -d' ' -f1)"
	# Sync
	# *Don't delete-after*, this is the job of cleanup scripts. It will remove our
	# packages too
	local pkgpool
	for pkgpool in "${ARCHPKGPOOLS[@]}"; do
		rsync "${extra[@]}" --no-motd -rtlH \
			--delay-updates \
			--safe-links \
			--include-from=/tmp/any.whitelist \
			--exclude="*" \
			"rsync://${mirror}/${mirrorpath}/${pkgpool}/" \
			"${FTP_BASE}/${pkgpool}/"
	done
	# Sync sources
	msg "Syncing source pool"
	#sed "s|\.pkg\.tar\.|.src.tar.|" /tmp/any.whitelist > /tmp/any-src.whitelist
	#msg2 "Retrieving %d sources from pool" $(wc -l < /tmp/any-src.whitelist)
	# Sync
	# *Don't delete-after*, this is the job of cleanup scripts. It will remove our
	# packages too
	local srcpool
	for srcpool in "${ARCHSRCPOOLS[@]}"; do
		rsync "${extra[@]}" --no-motd -rtlH \
			--delay-updates \
			--safe-links \
			--include-from=/tmp/any.whitelist \
			--exclude="*" \
			"rsync://${mirror}/${mirrorpath}/${srcpool}/" \
			"${FTP_BASE}/${srcpool}/"
	done
	date -u +%s > "${FTP_BASE}/lastsync"
	# Cleanup
	unset blacklist whitelists _arch _repo repo_file
}

xbs-sync_traverse-xdd-repositories() {
	msg "Processing %s-%s" "${_repo}-${_arch}"
	db_file=$(get_repo_file "${_repo}" "${_arch}")${DB_EXT}
	files_file=$(get_repo_file "${_repo}" "${_arch}")${FILES_EXT}
	if [ ! -f "${db_file}" ]; then
		warning "%s doesn't exist, skipping this repo-derivated" "${db_file}"
		continue
	fi
	if [ ! -f "${files_file}" ]; then
		warning "%s doesn't exist, skipping this repo-derivated" "${files_file}"
		continue
	fi
	# Remove blacklisted packages and count them
	# TODO capture all removed packages for printing on debug mode
	msg2 "Removing blacklisted packages from %s database..." .db
	LC_ALL=C repo-remove "${db_file}" "${blacklist[@]}" \
	|& sed -n 's/-> Removing/	&/p'
	msg2 "Removing blacklisted packages from %s database..." .files
	LC_ALL=C repo-remove "${files_file}" "${blacklist[@]}" \
	|& sed -n 's/-> Removing/	&/p'
	# Get db contents
	db=($(get_repo_content "${db_file}"))
	msg2 "Process clean db for syncing..."
	# Create a whitelist, add * wildcard to end
	# TODO due to lack of -arch suffix, the pool sync retrieves every arch even if
	# we aren't syncing them
	# IMPORTANT: the . in the sed command is needed because an empty
	# whitelist would consist of a single * allowing any package to
	# pass through
	printf '%s\n' "${db[@]}" | sed "s|.$|&*|g" > "/tmp/${_repo}-${_arch}.whitelist"
	msg2 "%d packages in whitelist" "$(wc -l /tmp/${_repo}-${_arch}.whitelist | cut -d' ' -f1)"
	# Sync excluding everything but whitelist
	# We delete here for cleanup
	rsync "${extra[@]}" --no-motd -rtlH \
		--delete-after \
		--delete-excluded \
		--delay-updates \
		--include-from="/tmp/${_repo}-${_arch}.whitelist" \
		--exclude="*" \
		"rsync://${mirror}/${mirrorpath}/${_repo}/os/${_arch}/" \
		"${FTP_BASE}/${_repo}/os/${_arch}/"
	# Add a new whitelist
	whitelists+=(/tmp/${_repo}-${_arch}.whitelist)
	msg "Putting databases back in place"
	rsync "${extra[@]}" --no-motd -rtlH \
		--delay-updates \
		--safe-links \
		"${WORKDIR}/${_repo}/os/${_arch}/" \
		"${FTP_BASE}/${_repo}/os/${_arch}/"
	# Cleanup
	unset db
}

##############################################################

CONFIG_DIR="$(dirname "$(readlink -e "$0")")/etc"

source "${CONFIG_DIR}/xbs.cfg"
source "${SHARE_DIR}/xbs-functions"

rules=${1}

case "${rules}" in
	add|remove|rm)
		# Add or remove package to repository defined in config
		
		if [ "${#}" -lt '4' ]; then
			msg "usage: %s <add || rm> <platform> <repo> <arch> <pkg_file> ..." "${0##*/}"
			exit 1
		}
		platform="${2}"
		repo="${3}"
		arch="${4}"
		pkg_files=("${@:5}")
		for '_platform' in "${platforms[@]}"; do
			xbs_rename-variable_platform-config
			xbs_variable_platform-config
			if  [ -f "${PLATAFORM_CONFIG}" ]; then
				xbs_source_platform-config
				if [ "${rules}" == 'add' ]; then
					xbs-add
				elif [ "${rules}" == 'rm' || "${rules}" == 'remove' ]; then
					xbs-rm
				fi
			else
				xbs_error_platform-not-found
			fi
		done
		;;
	check-libraries|check-libs|chl|list-nonfree|list-nonlibre|lnf|lnl|list-unsigned-packages|list-unsigd-pkgs|lup)
		if [ "${rules}" == 'check-libraries'  || "${rules}" == 'check-libs' || "${rules}" == 'chl' ]; then
			"${BIN_DIR}/db-check-package-libraries.py ${@:2}"
		elif [ "${rules}" == 'list-nonfree' || "${rules}" == 'list-nonlibre'  || "${rules}" == 'lnf' || "${rules}" == 'lnl' ]; then
			"${BIN_DIR}/db-list-nonfree.py ${@:2}"
		elif [ "${rules}" == 'list-unsigned-packages'  || "${rules}" == 'list-unsigd-pkgs' || "${rules}" == 'lup' ]; then
			"${BIN_DIR}/db-list-unsigned-packages.py ${@:2}"
		fi
		;;
	init|sync|import)
		# init: Creates the repository structure defined in config
		# sync or import: Sync and import derivated repositories based on info contained in repo.db files
		platforms=("${@:2}")
		if [ "${#}" -lt '2' ]; then
			if  [ "${rules}" == 'init' ] && [ "${DISTRO_TYPE}" == 'native' ]; then
				for '_platform' in "${PLATFORMS[@]}"; do
					xbs_variable_platform-config
					xbs_source_platform-config
					xbs-init
				done
			elif  [ "${rules}" == 'sync' || "${rules}" == 'import' ] && [ "${DISTRO_TYPE}" == 'derivated' ]; then
				for '_platform' in "${DD_PLATFORMS[@]}"; do
					xbs_variable_platform-config
					xbs_source_platform-config
					xbs-sync
				done
			fi
		else
			for '_platform' in "${platforms[@]}"; do
				xbs_rename-variable_platform-config
				xbs_variable_platform-config
				if  [ -f "${PLATAFORM_CONFIG}" ]; then
					xbs_source_platform-config
					if [ "${rules}" == 'init' ]; then
						xbs-init
					elif [ "${rules}" == 'sync' || "${rules}" == 'import' ] && [ "${DISTRO_TYPE}" == 'derivated' ]; then
						xbs-sync
					fi
				else
					xbs_error_platform-not-found
				fi
			done
		fi
		;;
	*)
		# XBS Usage
		msg "usage: %s <add || rm || check-libs || list-nonfree || list-unsigd-pkgs || init || sync> <value> ..." "${0##*/}"
		;;
esac