epm-query_file 4.78 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2009, 2012, 2016  Etersoft
# Copyright (C) 2009, 2012, 2016  Vitaly Lipatov <lav@etersoft.ru>
5
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18 19
#

20
# copied from etersoft-build-utils/bin/rpmqf
21

22 23 24
# TODO: check for lib64/name situations
__abs_filename()
{
25 26 27 28 29 30 31 32 33
    if echo "$1" | grep -q "/" ; then
        echo "$1"
        return
    fi
    if [ -e "$1" ] ; then
        echo "$(pwd)/$1"
        return
    fi
    echo "$1"
34 35
}

36
__do_query_real_file()
37
{
38 39 40 41 42 43
    local TOFILE
    
    # get canonical path
    if [ -e "$1" ] ; then
        TOFILE="$(__abs_filename "$1")"
    else
44
        TOFILE="$(print_command_path "$1" || echo "$1")"
45
        if [ "$TOFILE" != "$1" ] ; then
46 47 48 49 50 51
            # work against usrmerge
            local t="$(realpath "$(dirname "$TOFILE")")/$(basename "$TOFILE")" #"
            if [ "$TOFILE" != "$t" ] ; then
                #info " > $TOFILE is placed as $t"
                TOFILE="$t"
            fi
52 53 54
            info " > $1 is placed as $TOFILE"
        fi
    fi
55

56
    [ -n "$TOFILE" ] || return
57

58 59 60 61 62 63 64 65
    local RES
    if [ -n "$short" ] ; then
        __do_short_query "$TOFILE"
        RES=$?
    else
        __do_query "$TOFILE"
        RES=$?
    fi
66

67 68 69 70 71 72 73 74 75 76 77
    # get value of symbolic link
    if [ -L "$TOFILE" ] ; then
        local LINKTO
        LINKTO=$(readlink -- "$TOFILE")
        info " > $TOFILE is link to $LINKTO"
        LINKTO=$(readlink -f -- "$TOFILE")
        __do_query_real_file "$LINKTO"
        return
    else
        return $RES
    fi
78 79
}

80 81
dpkg_print_name_version()
{
82 83 84
    local ver i
    for i in "$@" ; do
        [ -n "$i" ] || continue
Vitaly Lipatov's avatar
Vitaly Lipatov committed
85
        ver=$(a='' dpkg -s "$i" 2>/dev/null | grep "Version:" | sed -e "s|Version: ||g")
86 87 88 89 90 91
        if [ -z "$ver" ] ; then
            echo "$i"
        else
            echo "$i-$ver"
        fi
    done
92 93 94
}


95 96
__do_query()
{
97
    local CMD
98
    case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
99
        *-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
100
            showcmd dpkg -S "$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
101
            dpkg_print_name_version "$(a='' dpkg -S "$1" | grep -v "^diversion by" | sed -e "s|:.*||")"
102
            return ;;
103
        *-rpm)
104 105
            CMD="rpm -qf"
            ;;
106
        emerge)
107
            assure_exists equery
108 109
            CMD="equery belongs"
            ;;
110 111 112
        pacman)
            CMD="pacman -Qo"
            ;;
113 114 115
        pkgng)
            CMD="pkg which"
            ;;
116 117 118
        conary)
            CMD="conary query --path"
            ;;
119 120
        slackpkg)
            # note: need remove leading slash for grep
121
            docmd grep -R -- "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
122 123
            return
            ;;
124
        opkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
125
            CMD="opkg search"
126
            ;;
127 128 129
        eopkg)
            CMD="eopkg search-file"
            ;;
130 131 132
        pisi)
            CMD="pisi search-file"
            ;;
133 134
        xbps)
            # FIXME: maybe it is search file?
135
            CMD="xbps-query -o"
136
            ;;
137 138
        aptcyg)
            #CMD="apt-cyg packageof"
139
            # is not implemented locally
140 141
            return 1
            ;;
142
        *)
143
            fatal 'Have no suitable query command for $PMTYPE'
144 145
            ;;
    esac
146

147 148
    docmd $CMD $@
}
149

150 151 152 153 154 155

__do_short_query()
{
    local CMD
    case $PMTYPE in
        *-rpm)
156
            CMD="rpm -qf --queryformat %{NAME}\n"
157
            ;;
158
        apt-dpkg)
159
            docmd dpkg -S "$1" | sed -e "s|:.*||"
160 161
            return ;;
        NOemerge)
162
            assure_exists equery
163 164 165 166 167 168 169 170 171 172 173
            CMD="equery belongs"
            ;;
        NOpacman)
            CMD="pacman -Qo"
            ;;
        NOslackpkg)
            # note: need remove leading slash for grep
            docmd grep -R "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
            return
            ;;
        *)
174
            fatal 'Have no suitable query command for $PMTYPE'
175 176 177 178 179 180 181
            ;;
    esac

    docmd $CMD $@
}


182 183
epm_query_file()
{
184 185
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
186

187

188
    #load_helper epm-search_file
189

190
    res=0
191
    for pkg in $pkg_filenames ; do
192
        __do_query_real_file "$pkg" || res=$?
193
    done
194 195

    [ "$res" = "0" ] || info "Try epm sf for search file in all packages of the repositories"
196 197
    #|| pkg_filenames="$FULLFILEPATH" epm_search_file
    return $res
198
}