-
Notifications
You must be signed in to change notification settings - Fork 15
/
ftpdir-cleanup
executable file
·145 lines (126 loc) · 4.57 KB
/
ftpdir-cleanup
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
#!/bin/bash
. "$(dirname "$(readlink -e "$0")")/../config"
. "$(dirname "$(readlink -e "$0")")/../db-functions"
clean_pkg() {
local pkg
local target
if [[ $CLEANUP_DRYRUN != true ]]; then
for pkg in "$@"; do
if [[ -h $pkg ]]; then
rm -f "$pkg" "$pkg.sig"
else
mv_acl "$pkg" "$CLEANUP_DESTDIR/${pkg##*/}"
if [[ -e $pkg.sig ]]; then
mv_acl "$pkg.sig" "$CLEANUP_DESTDIR/${pkg##*/}.sig"
fi
touch "${CLEANUP_DESTDIR}/${pkg##*/}"
fi
done
fi
}
script_lock
for repo in "${PKGREPOS[@]}"; do
for arch in "${ARCHES[@]}"; do
repo_lock "${repo}" "${arch}" || exit 1
done
done
[[ $CLEANUP_DRYRUN = true ]] && warning 'dry run mode is active'
for repo in "${PKGREPOS[@]}" "${DEBUGREPOS[@]}"; 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
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}"
# 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[@]} >= 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 "${WORKDIR}/repo-${repo}-${arch}" "${WORKDIR}/db-${repo}-${arch}"))
if (( ${#old_pkgs[@]} >= 1 )); then
msg "Removing old packages from [%s] (%s)..." "$repo" "$arch"
for old_pkg in "${old_pkgs[@]}"; do
msg2 '%s' "${old_pkg}"
clean_pkg "${FTP_BASE}/${repo}/os/${arch}/${old_pkg}"
# We might have a case where a packager has removed the debug
# options from a package and ran db-remove. We need to remove
# these packages, so we look for it then just run repo-remove
# for it to be cleaned up during this iteration
[[ ! -f "${FTP_BASE}/${PKGPOOL}/${old_pkg}" ]] && continue
pkgbase="$(getpkgbase "${FTP_BASE}/${PKGPOOL}/${old_pkg}")"
pkgver="$(getpkgver "${FTP_BASE}/${PKGPOOL}/${old_pkg}")"
if debug_pkgfile=$(maybe_getpkgfile "${FTP_BASE}/${repo}-debug/os/${arch}/${pkgbase}-debug-${pkgver}-${arch}"${PKGEXTS}); then
if bsdtar -xOf "${FTP_BASE}/${repo}-debug/os/${arch}/${repo}-debug${DBEXT}" | awk '/^%FILENAME%/{getline;print}' | grep "${debug_pkgfile##*/}"; then
msg2 'Found debug package, running repo-remove on %s...' "${debug_pkgfile##*/}"
arch_repo_modify remove "${repo}-debug" "${arch}" "${pkgbase}-debug"
fi
fi
done
fi
done
done
# get a list of all available packages in the package pool
for f in "$FTP_BASE/${PKGPOOL}"/*${PKGEXTS}; do
printf '%s\n' "${f##*/}"
done | sort > "${WORKDIR}/pool"
# create a list of packages in our db
if [[ -s ${WORKDIR}/pool ]]; then
cat "${WORKDIR}"/db-!(*-debug-*) 2>/dev/null | sort -u > "${WORKDIR}/db"
fi
# seperate list for debug packages
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
old_pkgs=($(comm -23 "${WORKDIR}/pool" "${WORKDIR}/db"))
if (( ${#old_pkgs[@]} >= 1 )); then
msg "Removing old packages from package pool..."
for old_pkg in "${old_pkgs[@]}"; do
msg2 '%s' "${old_pkg}"
clean_pkg "$FTP_BASE/${PKGPOOL}/${old_pkg}"
done
fi
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
unset old_pkgs
unset old_debug_pkgs
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
if (( ${#old_pkgs[@]} >= 1 )); then
msg "Removing old packages from the cleanup directory..."
for old_pkg in "${old_pkgs[@]}"; do
msg2 '%s' "${old_pkg}"
if [[ $CLEANUP_DRYRUN != true ]]; 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