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

. "$(dirname $0)/../db-functions"
. "$(dirname $0)/../config"

repos="$(get_repos_for_host)"

LOCKFILE="/tmp/.ftpdircleanup.lock"

cleanup () {
	rm -f "$LOCKFILE"
  exit 0
}

ctrl_c() {
    cleanup
}

if [ -f "$LOCKFILE" ]; then
	owner="$(/usr/bin/stat -c %U $LOCKFILE)"
	echo "error: ftp cleanup is already in progress (started by $owner)"
	exit 1
fi

trap cleanup 0
trap ctrl_c 2

/bin/touch "$LOCKFILE"

#adjust the nice level to run at a lower priority
/usr/bin/renice +10 -p $$ > /dev/null

for repo in $repos; do
    $(dirname $0)/../misc-scripts/ftpdir-cleanup $repo
done

to_cleanup=""
for _arch in ${ARCHES[@]}; do
    poolpath="$FTP_BASE/packages/os/$_arch/"
    cd $poolpath
    for pkg in *$PKGEXT; do
        [ -f "$pkg" ] || continue # in case we get a file named "*.pkg.tar.gz"
        LINKS="$(/bin/ls $FTP_BASE/*/os/$_arch/$pkg 2>/dev/null)"
        if [ -n "$LINKS" ]; then
            found=0
            for lnk in $LINKS; do
                if [ -h "$lnk" ]; then
                    found=1
                    break
                fi
            done
            # No links found, clean it up
            if [ $found -eq 0 ]; then
                to_cleanup="$to_cleanup $poolpath/$pkg"
            fi
        fi
    done
done

if [ -n "$to_cleanup" ]; then
    echo "    The following packages are no longer in any repo"
    echo "    They will be moved to $CLEANUP_DESTDIR"
    for f in $to_cleanup; do
        echo "        $(basename "$f")"
    done
    echo ""
    mv $to_cleanup "$CLEANUP_DESTDIR"
fi

cleanup