ftpdir-cleanup 3.71 KB
Newer Older
1
2
#!/bin/bash

3
4
. "$(dirname "$(readlink -e "$0")")/../config"
. "$(dirname "$(readlink -e "$0")")/../db-functions"
5

Pierre Schmitz's avatar
Pierre Schmitz committed
6
7
8
9
clean_pkg() {
	local pkg
	local target

10
	if [[ $CLEANUP_DRYRUN != true ]]; then
Pierre Schmitz's avatar
Pierre Schmitz committed
11
		for pkg in "$@"; do
Eli Schwartz's avatar
Eli Schwartz committed
12
			if [[ -h $pkg ]]; then
13
				rm -f "$pkg" "$pkg.sig"
Pierre Schmitz's avatar
Pierre Schmitz committed
14
			else
15
				mv_acl "$pkg" "$CLEANUP_DESTDIR/${pkg##*/}"
Eli Schwartz's avatar
Eli Schwartz committed
16
				if [[ -e $pkg.sig ]]; then
17
					mv_acl "$pkg.sig" "$CLEANUP_DESTDIR/${pkg##*/}.sig"
18
				fi
Pierre Schmitz's avatar
Pierre Schmitz committed
19
				touch "${CLEANUP_DESTDIR}/${pkg##*/}"
Pierre Schmitz's avatar
Pierre Schmitz committed
20
21
22
23
24
			fi
		done
	fi
}

25
script_lock
26

27
28
29
for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		repo_lock "${repo}" "${arch}" || exit 1
Pierre Schmitz's avatar
Pierre Schmitz committed
30
31
32
	done
done

33
[[ $CLEANUP_DRYRUN = true ]] && warning 'dry run mode is active'
34

35
for repo in "${PKGREPOS[@]}" "${DEBUGREPOS[@]}"; do
36
	for arch in "${ARCHES[@]}"; do
Eli Schwartz's avatar
Eli Schwartz committed
37
		if [[ ! -f ${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT} ]]; then
Pierre Schmitz's avatar
Pierre Schmitz committed
38
39
40
			continue
		fi
		# get a list of actual available package files
41
42
43
44
45
		for f in "${FTP_BASE}"/${repo}/os/${arch}/*${PKGEXTS}; do
			if [[ -f $f ]]; then
				printf '%s\n' "${f##*/}"
			fi
		done | sort > "${WORKDIR}/repo-${repo}-${arch}"
Pierre Schmitz's avatar
Pierre Schmitz committed
46
47
48
49
		# 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}"))
Eli Schwartz's avatar
Eli Schwartz committed
50
		if (( ${#missing_pkgs[@]} >= 1 )); then
51
			error "Missing packages in [%s] (%s)..." "$repo" "$arch"
52
			for missing_pkg in "${missing_pkgs[@]}"; do
53
				msg2 '%s' "${missing_pkg}"
Pierre Schmitz's avatar
Pierre Schmitz committed
54
55
56
57
			done
		fi

		old_pkgs=($(comm -23 "${WORKDIR}/repo-${repo}-${arch}" "${WORKDIR}/db-${repo}-${arch}"))
Eli Schwartz's avatar
Eli Schwartz committed
58
		if (( ${#old_pkgs[@]} >= 1 )); then
59
			msg "Removing old packages from [%s] (%s)..." "$repo" "$arch"
60
			for old_pkg in "${old_pkgs[@]}"; do
61
				msg2 '%s' "${old_pkg}"
Pierre Schmitz's avatar
Pierre Schmitz committed
62
63
64
65
				clean_pkg "${FTP_BASE}/${repo}/os/${arch}/${old_pkg}"
			done
		fi
	done
66
67
done

68
# get a list of all available packages in the package pool
69
HAS_POOL=0
70
71
72
for f in "$FTP_BASE/${PKGPOOL}"/*${PKGEXTS}; do
	printf '%s\n' "${f##*/}"
done | sort > "${WORKDIR}/pool"
73

Pierre Schmitz's avatar
Pierre Schmitz committed
74
# create a list of packages in our db
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if [[ -s ${WORKDIR}/pool ]]; then
	cat "${WORKDIR}"/db-!(*-debug-*) 2>/dev/null | sort -u > "${WORKDIR}/db"
fi

# seperate list for debug packages
HAS_DEBUGPOOL=0
for f in "$FTP_BASE/${DEBUGPKGPOOL}"/*${PKGEXTS}; do
	printf '%s\n' "${f##*/}"
done | sort > "${WORKDIR}/debugpool"

# seperate list for packages in our debug db
if [[ -s ${WORKDIR}/debugpool ]]; then
	cat "${WORKDIR}"/db-*-debug-* 2>/dev/null | sort -u > "${WORKDIR}/debug-db"
fi
Pierre Schmitz's avatar
Pierre Schmitz committed
89
90

old_pkgs=($(comm -23 "${WORKDIR}/pool" "${WORKDIR}/db"))
Eli Schwartz's avatar
Eli Schwartz committed
91
if (( ${#old_pkgs[@]} >= 1 )); then
Pierre Schmitz's avatar
Pierre Schmitz committed
92
	msg "Removing old packages from package pool..."
93
	for old_pkg in "${old_pkgs[@]}"; do
94
		msg2 '%s' "${old_pkg}"
95
		clean_pkg "$FTP_BASE/${PKGPOOL}/${old_pkg}"
Pierre Schmitz's avatar
Pierre Schmitz committed
96
	done
97
fi
Pierre Schmitz's avatar
Pierre Schmitz committed
98

99
100
101
102
103
104
105
106
107
old_debug_pkgs=($(comm -23 "${WORKDIR}/debugpool" "${WORKDIR}/debug-db"))
if (( ${#old_debug_pkgs[@]} >= 1 )); then
	msg "Removing old packages from debug package pool..."
	for old_debug_pkg in "${old_debug_pkgs[@]}"; do
		msg2 '%s' "${old_debug_pkg}"
		clean_pkg "$FTP_BASE/${DEBUGPKGPOOL}/${old_debug_pkg}"
	done
fi

108
unset old_pkgs
109
unset old_debug_pkgs
110
111
112
113
114
115
touch -d "${CLEANUP_KEEP} days ago"  "${WORKDIR}/cleanup_timestamp"
for f in "${CLEANUP_DESTDIR}"/**/*${PKGEXTS}; do
	if [[ ${WORKDIR}/cleanup_timestamp -nt $f ]]; then
		old_pkgs+=("${f##*/}")
	fi
done
Eli Schwartz's avatar
Eli Schwartz committed
116
if (( ${#old_pkgs[@]} >= 1 )); then
117
	msg "Removing old packages from the cleanup directory..."
118
	for old_pkg in "${old_pkgs[@]}"; do
119
		msg2 '%s' "${old_pkg}"
120
		if [[ $CLEANUP_DRYRUN != true ]]; then
121
122
123
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}"
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}.sig"
		fi
124
125
	done
fi
Pierre Schmitz's avatar
Pierre Schmitz committed
126

127
128
129
for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		repo_unlock "${repo}" "${arch}"
Pierre Schmitz's avatar
Pierre Schmitz committed
130
	done
131
132
done

133
script_unlock