summaryrefslogtreecommitdiff
path: root/cron-jobs/ftpdir-cleanup
blob: ad33b19bc70d83ce3d6a87d90057898424ca9ec2 (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
#!/bin/bash

source "$(dirname "$(readlink -e "$0")")/../etc/dbscripts.cfg"
source "$(dirname "$(readlink -e "$0")")/../share/db-functions"

clean_pkg() {
	local pkg
	local target

	if ! "${cleanup_dryrun}"; then
		for pkg in "$@"; do
			if [ -h "$pkg" ]; then
				rm -f "$pkg" "$pkg.sig"
			else
				mv_acl "$pkg" "${cleanup_destination_directory}/${pkg##*/}"
				if [ -e "$pkg.sig" ]; then
					mv_acl "$pkg.sig" "${cleanup_destination_directory}/${pkg##*/}.sig"
				fi
				touch "${cleanup_destination_directory}/${pkg##*/}"
			fi
		done
	fi
}

"${cleanup_dryrun}" && warning 'dry run mode is active'

for repo in "${repositories[@]}"; do
	for arch in "${native_architectures[@]}"; do
		if [ ! -f "${root_dir}/${repo}/os/${arch}/${repo}${database_extension_suffixfile}" ]; then
			continue
		fi
		# get a list of actual available package files
		find "${root_dir}/${repo}/os/${arch}" -xtype f -name "*${package_extension_suffixfile}" -printf '%f\n' | sort > "${work_directory}/repo-${repo}-${arch}"
		# get a list of package files defined in the repo db
		bsdtar -xOf "${root_dir}/${repo}/os/${arch}/${repo}${database_extension_suffixfile}" | awk '/^%FILENAME%/{getline;print}' | sort > "${work_directory}/db-${repo}-${arch}"

		missing_pkgs=($(comm -13 "${work_directory}/repo-${repo}-${arch}" "${work_directory}/db-${repo}-${arch}"))
		if [ ${#missing_pkgs[@]} -ge 1 ]; then
			error "Missing packages in [%s] (%s)..." "${repo}" "${arch}"
			for missing_pkg in "${missing_pkgs[@]}"; do
				msg2 '%s' "${missing_pkg}"
			done
		fi

		old_pkgs=($(comm -23 "${work_directory}/repo-${repo}-${arch}" "${work_directory}/db-${repo}-${arch}"))
		if [ ${#old_pkgs[@]} -ge 1 ]; then
			msg "Removing old packages from [%s] (%s)..." "${repo}" "${arch}"
			for old_pkg in "${old_pkgs[@]}"; do
				msg2 '%s' "${old_pkg}"
				clean_pkg "${root_dir}/${repo}/os/${arch}/${old_pkg}"
			done
		fi
	done
done

# get a list of all available packages in the pacakge pool
find "${root_dir}/${packages_pool}" -name "*${package_extension_suffixfile}" -printf '%f\n' | sort > "${work_directory}/pool"
# create a list of packages in our db
cat "${work_directory}/db-"* | sort -u > "${work_directory}/db"

old_pkgs=($(comm -23 "${work_directory}/pool" "${work_directory}/db"))
if [ ${#old_pkgs[@]} -ge 1 ]; then
	msg "Removing old packages from package pool..."
	for old_pkg in "${old_pkgs[@]}"; do
		msg2 '%s' "${old_pkg}"
		clean_pkg "${root_dir}/${packages_pool}/${old_pkg}"
	done
fi

old_pkgs=($(find "${cleanup_destination_directory}" -type f -name "*${package_extension_suffixfile}" -mtime +"${cleanup_keep}" -printf '%f\n'))
if [ ${#old_pkgs[@]} -ge 1 ]; then
	msg "Removing old packages from the cleanup directory..."
	for old_pkg in "${old_pkgs[@]}"; do
		msg2 '%s' "${old_pkg}"
		if ! "${cleanup_dryrun}"; then
			rm -f "${cleanup_destination_directory}/${old_pkg}"
			rm -f "${cleanup_destination_directory}/${old_pkg}.sig"
		fi
	done
fi

for repo in "${repositories[@]}"; do
	for arch in "${native_architectures[@]}"; do
		repo_unlock "${repo}" "${arch}"
	done
done

script_unlock