Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/sogrep.in
blob: 19fe651d450f7009b14d07be9d66130af75e58c5 (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
#!/bin/bash
#
#   sogrep - find shared library links in an Arch Linux repository.
#
#   Copyright (c) 2019 by Eli Schwartz <eschwartz@archlinux.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

m4_include(lib/common.sh)

# globals
: ${SOLINKS_MIRROR:="https://mirror.pkgbuild.com"}
: ${SOCACHE_DIR:="${XDG_CACHE_HOME:-${HOME}/.cache}/sogrep"}

m4_include(lib/valid-repos.sh)
arches=('x86_64')

# options
REFRESH=0
VERBOSE=0

source /usr/share/makepkg/util/parseopts.sh
source /usr/share/makepkg/util/util.sh

recache() {
    local repo arch verbosity=-s

    (( VERBOSE )) && verbosity=--progress-bar

    for repo in "${_repos[@]}"; do
        for arch in "${arches[@]}"; do
            # delete extracted tarballs from previous sogrep versions
            rm -rf "${SOCACHE_DIR}/${arch}/${repo}"

            # fetch repo links database if newer than our cached copy
            local dbpath=${SOCACHE_DIR}/${arch}/${repo}.links.tar.gz
            mkdir -p "${dbpath%/*}"
            (( VERBOSE )) && echo "Fetching ${repo}.links.tar.gz..."
            curl -LR "${verbosity}" -o "${dbpath}" -z "${dbpath}" \
                "${SOLINKS_MIRROR}/${repo}/os/${arch}/${repo}.links.tar.gz"
        done
    done
}

search() {
    local repo=$1 arch lib=$2 srepos=("${_repos[@]}")

    if [[ $repo != all ]]; then
        if ! in_array "${repo}" "${_repos[@]}"; then
            echo "${BASH_SOURCE[0]##*/}: unrecognized repo '$repo'"
            echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
            exit 1
        fi
        srepos=("${repo}")
    fi

    setup_workdir

    for arch in "${arches[@]}"; do
        for repo in "${srepos[@]}"; do
            local prefix=
            (( VERBOSE && ${#srepos[@]} > 1 )) && prefix=${repo}/
            local db=${SOCACHE_DIR}/${arch}/${repo}.links.tar.gz
            if [[ -f ${db} ]]; then
                local extracted=${WORKDIR}/${arch}/${repo}
                mkdir -p "${extracted}"
                bsdtar -C "${extracted}" -xf "${db}"
                while read -rd '' pkg; do
                    read -r match
                    pkg=${pkg#${extracted}/}
                    pkg="${prefix}${pkg%-*-*/links}"

                    if (( VERBOSE )); then
                        printf '%-35s %s\n' "${pkg}" "${match}"
                    else
                        printf '%s\n' "${pkg}"
                    fi
                done < <(grep -rZ "${lib}" "${extracted}") | sort -u
            fi
        done
    done | resort
}

usage() {
    cat <<- _EOF_
		Usage: ${BASH_SOURCE[0]##*/} [OPTIONS] REPO LIBNAME

		Check the soname links database for Arch Linux repositories containing
		packages linked to a given shared library. If the repository specified
		is "all", then all repositories will be searched, otherwise only the
		named repository will be searched.

		If the links database does not exist, it will be downloaded first.

		OPTIONS
		    -v, --verbose   Show matched links in addition to pkgname
		    -r, --refresh   Refresh the links databases
		    -h, --help      Show this help text
_EOF_
}

# utility function to resort with multiple repos + no-verbose
resort() { sort -u; }

if (( $# == 0 )); then
    echo "error: No arguments passed."
    echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
    exit 1
fi
OPT_SHORT='vrh'
OPT_LONG=('verbose' 'refresh' 'help')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
    exit 1
fi
set  -- "${OPTRET[@]}"

while :; do
    case $1 in
        -v|--verbose)
            resort() { cat; }
            VERBOSE=1
            ;;
        -r|--refresh)
            REFRESH=1
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        --)
            shift; break
            ;;
    esac
    shift
done

if ! (( ( REFRESH && $# == 0 ) || $# == 2 )); then
    echo "error: Incorrect number of arguments passed."
    echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
    exit 1
fi

if (( REFRESH )) || [[ ! -d ${SOCACHE_DIR} ]]; then
    recache
    (( $# == 2 )) || exit 0
fi

search "$@"