summaryrefslogtreecommitdiffstats
path: root/misc-scripts/ftpdir-cleanup
blob: 7de64b98a76d5f181c851888c0b51455591b71f8 (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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash

if [ $# -ne 3 ]; then
	echo "usage: $(basename $0) <reponame> <arch> <dest-dir>"
	exit 1
fi

reponame=$1
arch=$2
dest=$3

##### Arch specific stuff. TODO make this configurable #####
ftppath_base="/srv/ftp/$reponame/os"
############################################################

ftppath="$ftppath_base/$arch"

if [ ! -d "$ftppath" ]; then
    echo "FTP path '$ftppath' does not exist"
    exit 1
fi

if [ ! -f /etc/makepkg.conf ]; then
    echo "/etc/makepkg.conf not found! Aborting"
    exit 1
fi

. /etc/makepkg.conf

getpkgname() {
	local tmp

	tmp=${1##*/}
	tmp=${tmp%$PKGEXT}
	tmp=${tmp%-$arch}
	echo ${tmp%-*-*}
}

getpkgname_ver() {
	local tmp

	tmp=${1##*/}
	tmp=${tmp%$PKGEXT}
	echo ${tmp%-$arch}
}

MISSINGFILES=""
DELETEFILES=""
EXTRAFILES=""

TMPDIR=$(mktemp -d /tmp/cleanup.XXXXXX) || exit 1

cd "${TMPDIR}"
bsdtar xf "$ftppath/$reponame.db.tar.$DB_COMPRESSION"

for pkg in *; do
  filename=$(grep -A1 '^%FILENAME%$' "${pkg}/desc" | tail -n1)
  [ -z "${filename}" ] && filename="${pkg}${PKGEXT}"

  if [ ! -e "${ftppath}/${filename}" ]; then
    MISSINGFILES="${MISSINGFILES} ${filename}"
  else
    pkgname="$(getpkgname ${filename})"
    for otherfile in ${ftppath}/${pkgname}-*; do
      otherfile="$(basename ${otherfile})"
      if [ "${otherfile}" != "${filename}" -a "${pkgname}" = "$(getpkgname ${otherfile})" ]; then
        DELETEFILES="${DELETEFILES} ${otherfile}"
      fi
    done
  fi
done

cd "$ftppath"
for pkg in *$arch$PKGEXT; do
    pkgname="$(getpkgname $pkg)"
    for p in ${TMPDIR}/${pkgname}-*; do
        if [ -d "${p}" -a "$(getpkgname $(basename ${p}))" = "${pkgname}" ]; then
            continue 2
        fi
    done
    EXTRAFILES="$EXTRAFILES $pkg"
done 

if [ -d "$ftppath_base/any" ]; then
    cd "$ftppath_base/any"
    for pkg in *$PKGEXT; do
        if [ ! -h $ftppath_base/i686/$pkg -a ! -h $ftppath_base/x86_64/$pkg ]; then
            ARCHINDEPFILES="$ARCHINDEPFILES $pkg"
        fi
    done
fi

cd "$ftppath"
rm -rf ${TMPDIR}

#Make sure we've done *something* before outputting anything
if [ -z "$DELETEFILES$MISSINGFILES$EXTRAFILES$ARCHINDEPFILES" ]; then
    exit 0
fi

#Do a quick check. If we have a MISSINGFILE and an ARCHINDEPFILE of the same
#name to remove, then let's restore the symlink.

archfiles="$ARCHINDEPFILES"
ARCHINDEPFILES=""
for af in $archfiles; do
    af_satisfied=0
    for mf in $MISSINGFILES; do
        if [ "${af}" = "${mf}" ]; then
            ln -s "${ftppath_base}/any/${af}" "${ftppath}"
            af_satisfied=1
        fi

    done
    if [ $af_satisfied -eq 0 ]; then
        ARCHINDEPFILES="${ARCHINDEPFILES} ${af}"
    fi
done
#rescan missing files now
missfiles="$MISSINGFILES"
MISSINGFILES=""
for mf in $missfiles; do
    if [ ! -e "${ftppath}/${mf}" ]; then
        MISSINGFILES="${MISSINGFILES} ${mf}"
    else
        echo "Missing file has been restored: $mf"
    fi
done

echo "Scan complete for $reponame ($arch) at ${ftppath}"

if [ -n "$DELETEFILES" ]; then
    echo "    The following files are out of date"
    echo "    They will be moved to '$dest'"
    for f in $DELETEFILES; do
        echo "        $f"
    done
    echo ""
fi

if [ -n "$MISSINGFILES" ]; then
    echo "    The following files are missing in the repo"
    for f in $MISSINGFILES; do
        echo "        $f"
    done
    echo ""
fi

if [ -n "$EXTRAFILES" ]; then
    echo "    The following files are in the repo but not the db"
    echo "    They will be moved to '$dest'"
    for f in $EXTRAFILES; do
        echo "        $f"
    done
fi

if [ -n "$ARCHINDEPFILES" ]; then
	echo "    The following architecture independent packages"
	echo "    are not symlinked in the architecture repositories."
	echo "    They will be moved to '$dest'"
    for f in $ARCHINDEPFILES; do
        echo "        $f"
    done
fi

if [ -n "${DELETEFILES}" ]; then
    mv ${DELETEFILES} "$dest"
    echo ""
fi

if [ -n "${EXTRAFILES}" ]; then
    mv ${EXTRAFILES} "$dest"
    echo ""
fi

cd "$ftppath_base/any"
if [ -n "${ARCHINDEPFILES}" ]; then
    mv ${ARCHINDEPFILES} "$dest"
    echo ""
fi