epm-query_file 4.21 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

	if [ -n "$short" ] ; then
51
		__do_short_query "$TOFILE" || return
52
	else
53
		__do_query "$TOFILE" || return
54 55
	fi

56
	# get value of symbolic link
57
	if [ -n "$TOFILE" ] && [ -L "$TOFILE" ] ; then
58
		local LINKTO
59 60
		LINKTO=$(readlink -- "$TOFILE")
		info " > $TOFILE is link to $LINKTO"
61
		LINKTO=$(readlink -f -- "$TOFILE")
62
		__do_query_real_file "$LINKTO"
63
		return
64 65 66
	fi
}

67 68
dpkg_print_name_version()
{
69
	local ver i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
70
	for i in "$@" ; do
71 72
		[ -n "$i" ] || continue
		ver=$(dpkg -s "$i" 2>/dev/null | grep "Version:" | sed -e "s|Version: ||g")
73 74 75 76 77 78 79 80 81
		if [ -z "$ver" ] ; then
			echo "$i"
		else
			echo "$i-$ver"
		fi
	done
}


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

128 129
    docmd $CMD $@
}
130

131 132 133 134 135 136 137 138 139

__do_short_query()
{
    local CMD
    case $PMTYPE in
        *-rpm)
            CMD="rpm -qf --queryformat %{NAME}\n"
            ;;
        NOapt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140 141
            showcmd dpkg -S "$1"
            dpkg_print_name_version "$(dpkg -S $1 | sed -e "s|:.*||" | grep -v "^diversion by")"
142 143
            return ;;
        NOemerge)
144
            assure_exists equery
145 146 147 148 149 150 151 152 153 154 155
            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
            ;;
        *)
156
            fatal "Have no suitable query command for $PMTYPE"
157 158 159 160 161 162 163
            ;;
    esac

    docmd $CMD $@
}


164 165
epm_query_file()
{
166 167
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
168

169

170
    #load_helper epm-search_file
171

172
    res=0
173
    for pkg in $pkg_filenames ; do
174
        __do_query_real_file "$pkg" || res=$?
175
    done
176 177

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