summaryrefslogtreecommitdiffstats
path: root/cron-jobs/create-filelists
blob: a37080fba2a541e02d6353f609ef9dcccdcbb8f8 (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
#!/bin/bash

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

reposdir=${FTP_BASE}
targetdir=${FTP_BASE}

script_lock

# location where the package DB is extracted so we know what to include
DBDIR="$(mktemp -d ${WORKDIR}/create-filelists.dbdir.XXXXXX)" || exit 1
# location where the old files DB is extracted to save us some work
CACHEDIR="$(mktemp -d ${WORKDIR}/create-filelists.cachedir.XXXXXX)" || exit 1
# location where the new files DB is built up and eventually zipped
MYTMPDIR="$(mktemp -d ${WORKDIR}/create-filelists.tmpdir.XXXXXX)" || exit 1

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

case "${DBEXT}" in
	*.gz)  TAR_OPT="z" ;;
	*.bz2) TAR_OPT="j" ;;
	*.xz)  TAR_OPT="J" ;;
	*) die "Unknown compression type for DBEXT=${DBEXT}" ;;
esac

FILESEXT="${DBEXT//db/files}"

for repo in ${PKGREPO[@]}; do
    REPO_DB_FILE="${repo}$DBEXT"
    FILES_DB_FILE="${repo}$FILESEXT"
    for arch in ${ARCHES[@]}; do
        pushd "$reposdir" >/dev/null

        repodir="${repo}/os/${arch}"
        cached="no"

        # extract package db archive
        if [ -f "${targetdir}/${repodir}/${REPO_DB_FILE}" ]; then
            mkdir -p "${DBDIR}/${repodir}"
            bsdtar -xf "${targetdir}/${repodir}/${REPO_DB_FILE}" -C "${DBDIR}/${repodir}"
        else
            error "Fail! Does the repo $repo with arch $arch even exist?"
            continue
        fi

        # extract old file archive
        if [ -f "${targetdir}/${repodir}/${FILES_DB_FILE}" ]; then
            mkdir -p "${CACHEDIR}/${repodir}"
            bsdtar -xf "${targetdir}/${repodir}/${FILES_DB_FILE}" -C "${CACHEDIR}/${repodir}"
            cached="yes"
        fi

        # create file lists
        for pkg in $(ls ${DBDIR}/${repodir}); do
            dbpkgdir="${DBDIR}/${repodir}/${pkg}"
            cachepkgdir="${CACHEDIR}/${repodir}/${pkg}"
            tmppkgdir="${MYTMPDIR}/${repodir}/${pkg}"
            mkdir -p "$tmppkgdir"
            ln "${dbpkgdir}/desc" "${tmppkgdir}/desc"
            ln "${dbpkgdir}/depends" "${tmppkgdir}/depends"
            if [ -f "${cachepkgdir}/files" ]; then
                ln "${cachepkgdir}/files" "${tmppkgdir}/files"
            else
                filename=$(grep -A1 '^%FILENAME%$' "${dbpkgdir}/desc" | tail -n1)
                echo '%FILES%' > "${tmppkgdir}/files"
                bsdtar --exclude=.* -tf "$repodir/$filename" >> "${tmppkgdir}/files"
                cached="no"
            fi
        done

        # create new file archive
        if [ "$cached" == "no" ]; then
            # at least one package has changed, so let's rebuild the archive
            pkgdir="${targetdir}/${repodir}"
            mkdir -p "$pkgdir"
            pushd "${MYTMPDIR}/${repodir}" >/dev/null
            [ -f "${pkgdir}/${FILES_DB_FILE}" ] && rm "${pkgdir}/${FILES_DB_FILE}"
            bsdtar --exclude=*${DBEXT//\.db/} -c${TAR_OPT}f "${pkgdir}/${FILES_DB_FILE}" *
            popd >/dev/null
        fi
        popd >/dev/null
    done
done

script_unlock

# vim: set ts=4 sw=4 et ft=sh: