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

. "$(dirname "$(readlink -e "$0")")/../config"
. "$(dirname "$(readlink -e "$0")")/../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 -f "$pkg" "$CLEANUP_DESTDIR"
				if [ -e "$pkg.sig" ]; then
					mv -f "$pkg.sig" "$CLEANUP_DESTDIR"
				fi
				touch "${CLEANUP_DESTDIR}/${pkg##*/}"
			fi
		done
	fi
}


${CLEANUP_DRYRUN} && warning 'dry run mode is active'

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

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

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

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

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

# cleanup of legacy $repo/os/any directories
for repo in "${PKGREPOS[@]}"; do
	if [ ! -d "${FTP_BASE}/${repo}/os/any" ]; then
		continue
	fi
	if [ -n "$(find "${FTP_BASE}/${repo}/os/any" -type d -empty)" ]; then
		msg "Removing empty legacy directory ${repo}/os/any"
		${CLEANUP_DRYRUN} || rmdir "${FTP_BASE}/${repo}/os/any"
		continue
	fi
	find "${FTP_BASE}/${repo}/os/any" -name "*${PKGEXT}" -printf '%f\n' | sort > "${WORKDIR}/any-${repo}"
	cat "${WORKDIR}/db-${repo}-"* | sort -u > "${WORKDIR}/all-${repo}"

	old_pkgs=($(comm -23 "${WORKDIR}/any-${repo}" "${WORKDIR}/all-${repo}"))
	if [ ${#old_pkgs[@]} -ge 1 ]; then
		msg "Removing old packages from [${repo}] (any)..."
		for old_pkg in ${old_pkgs[@]}; do
			msg2 "${old_pkg}"
			clean_pkg "${FTP_BASE}/${repo}/os/any/${old_pkg}"
		done
	fi
done

old_pkgs=($(find ${CLEANUP_DESTDIR} -type f -name "*${PKGEXT}" -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 "${old_pkg}"
		if ! ${CLEANUP_DRYRUN}; then
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}"
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}.sig"
		fi
	done
fi

for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		repo_unlock ${repo} ${arch}
	done
done

script_unlock