summaryrefslogtreecommitdiffstats
path: root/misc-scripts/ftpdir-cleanup-repo
blob: bfc971eff91a94d0c781a704f99bb83a7f2d12e1 (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
182
183
184
185
186
187
#!/bin/bash

if [ $# -ne 1 ]; then
	msg "usage: $(basename $0) <reponame>"
	exit 1
fi

reponame=$1

############################################################

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

clean_pkgs () {
  if ! ${CLEANUP_DRYRUN}; then
    for pkg in "$@"; do
        if [ -h "$pkg" ]; then
            rm -f "$pkg"
        else
            mv "$pkg" "$CLEANUP_DESTDIR"
        fi
    done
  fi
}

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

ftppath_base="$FTP_BASE/$reponame/os"

for arch in ${ARCHES[@]}; do

  repo_lock $reponame $arch $LOCK_TIMEOUT || continue

  CLEANUP_TMPDIR=$(mktemp -d ${WORKDIR}/cleanup-XXXXXX) || exit 1
  ftppath="$ftppath_base/$arch"
  MISSINGFILES=""
  DELETEFILES=""
  DELETESYMLINKS=""
  EXTRAFILES=""

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

  if ! cd "${CLEANUP_TMPDIR}" ; then
      error "Failed to cd to ${CLEANUP_TMPDIR}"
      exit 1
  fi

  if [ ! -f "$ftppath/$reponame$DBEXT" ]; then
    msg "The file \"$ftppath/$reponame$DBEXT\" could not be found, skipping."
    repo_unlock $reponame $arch
    continue
  fi

  if ! bsdtar xf "$ftppath/$reponame$DBEXT"; then
    error "Command failed: bsdtar xf \"$ftppath/$reponame$DBEXT\""
    exit 1
  fi

  cd "$ftppath"

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

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

  for pkg in *$PKGEXT; do
      if [ ! -e "$pkg" ]; then
          continue
      fi
      pkgname="$(getpkgname $pkg)"
      for p in ${CLEANUP_TMPDIR}/${pkgname}-*; do
          [ ! -d "${p}" ] || continue 2
          dbpkgname=$(grep -A1 '^%FILENAME%$' "${p}/desc" 2>/dev/null| tail -n1)
          if [ "${dbpkgname}" = "${pkgname}" ]; then
              continue 2
          fi
      done
      EXTRAFILES="$EXTRAFILES $pkg"
  done

  rm -rf ${CLEANUP_TMPDIR}

  # Do a quick check to see if a missing ARCHINDEPFILE is in the any dir
  # If it is, and the file is MISSING, restore it
  missfiles="$MISSINGFILES"
  MISSINGFILES=""
  for mf in $missfiles; do
      if [ -e "${ftppath_base}/any/${mf}" ]; then
          msg "Restoring missing 'any' symlink: ${mf}"
          ${CLEANUP_DRYRUN} || ln -s "../any/${mf}" "${ftppath}"
      else
          MISSINGFILES="${MISSINGFILES} ${mf}"
      fi
  done

  repo_unlock $reponame $arch

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

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

  if [ -n "$MISSINGFILES" ]; then
      for f in $MISSINGFILES; do
          error "$f is missing"
      done
  fi

  if [ -n "${DELETEFILES}" ]; then
      msg "The following files are out of date"
      for f in $DELETEFILES; do
          msg2 "$f"
      done
      clean_pkgs ${DELETEFILES}
  fi

  if [ -n "${DELETESYMLINKS}" ]; then
      msg "The following symlinks are out of date"
      for f in $DELETESYMLINKS; do
          msg2 "$f"
      done
      clean_pkgs ${DELETESYMLINKS}
  fi

  if [ -n "${EXTRAFILES}" ]; then
      msg "The following files are in the repo but not the db"
      for f in $EXTRAFILES; do
          msg2 "$f"
      done
      clean_pkgs ${EXTRAFILES}
  fi

done

ARCHINDEPFILES=""

if [ -d "$ftppath_base/any" ]; then
    cd "$ftppath_base/any"
    for pkg in *$PKGEXT; do
        [ -f "$pkg" ] || continue # in case we get a file named "*.pkg.tar.gz"
        found=0
        #check for any existing symlinks
        for arch in ${ARCHES[@]}; do
            if [ -h "$ftppath_base/$arch/$pkg" ]; then
                found=1
                break
            fi
        done
        if [ $found -eq 0 ]; then
            # We found no symlinks to this, delete it
            ARCHINDEPFILES="$ARCHINDEPFILES $pkg"
        fi
    done
fi

if [ -n "$ARCHINDEPFILES" ]; then
  msg "The following architecture independent packages are not symlinked in the architecture repositories."
    for f in $ARCHINDEPFILES; do
        msg2 "$f"
    done
fi

if [ -d "$ftppath_base/any" -a -n "${ARCHINDEPFILES}" ]; then
  cd "$ftppath_base/any"
  clean_pkgs ${ARCHINDEPFILES}
fi