epm-query_file 4.19 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 25 26 27 28 29 30 31 32 33 34 35
# TODO: check for lib64/name situations
__abs_filename()
{
	if echo "$1" | grep -q "/" ; then
		echo "$1"
		return
	fi
	if [ -e "$1" ] ; then
		echo "$(pwd)/$1"
		return
	fi
	echo "$1"
}

36
__do_query_real_file()
37 38 39 40 41
{
	local TOFILE
	
	# get canonical path
	if [ -e "$1" ] ; then
42
		TOFILE="$(__abs_filename "$1")"
43
	else
44
		TOFILE=$(which -- "$1" 2>/dev/null || echo "$1")
45
		if [ "$TOFILE" != "$1" ] ; then
46
			info " > $1 is placed as $TOFILE"
47 48
		fi
	fi
49

50 51 52
	[ -n "$TOFILE" ] || return

	local RES
53
	if [ -n "$short" ] ; then
54 55
		__do_short_query "$TOFILE"
		RES=$?
56
	else
57 58
		__do_query "$TOFILE"
		RES=$?
59 60
	fi

61
	# get value of symbolic link
62
	if [ -L "$TOFILE" ] ; then
63
		local LINKTO
64 65
		LINKTO=$(readlink -- "$TOFILE")
		info " > $TOFILE is link to $LINKTO"
66
		LINKTO=$(readlink -f -- "$TOFILE")
67
		__do_query_real_file "$LINKTO"
68
		return
69 70
	else
		return $RES
71 72 73
	fi
}

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


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

135 136
    docmd $CMD $@
}
137

138 139 140 141 142 143

__do_short_query()
{
    local CMD
    case $PMTYPE in
        *-rpm)
144
            CMD="rpm -qf --queryformat %{NAME}\n"
145
            ;;
146
        apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
147
            showcmd dpkg -S "$1"
148
            a= dpkg -S $1 | sed -e "s|:.*||"
149 150
            return ;;
        NOemerge)
151
            assure_exists equery
152 153 154 155 156 157 158 159 160 161 162
            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
            ;;
        *)
163
            fatal "Have no suitable query command for $PMTYPE"
164 165 166 167 168 169 170
            ;;
    esac

    docmd $CMD $@
}


171 172
epm_query_file()
{
173 174
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
175

176

177
    #load_helper epm-search_file
178

179
    res=0
180
    for pkg in $pkg_filenames ; do
181
        __do_query_real_file "$pkg" || res=$?
182
    done
183 184

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