epm-query_file 4.27 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
__do_query_real_file()
23 24 25 26 27
{
	local TOFILE
	
	# get canonical path
	if [ -e "$1" ] ; then
28
		TOFILE="$1"
29
	else
30
		TOFILE=$(which "$1" 2>/dev/null || echo "$1")
31
		if [ "$TOFILE" != "$1" ] ; then
32
			info "Note: $1 is placed as $TOFILE"
33 34 35 36 37
		fi
	fi
	
	# get value of symbolic link
	if [ -L "$TOFILE" ] ; then
38 39
		local LINKTO
		__do_query "$TOFILE"
40
		LINKTO=$(readlink -f "$TOFILE")
41
		info "Note: $TOFILE is link to $LINKTO"
42
		__do_query_real_file "$LINKTO"
43
		return
44
	fi
45 46

	FULLFILEPATH="$TOFILE"
47 48
}

49 50
dpkg_print_name_version()
{
51
	local ver i
52 53 54 55 56 57 58 59 60 61 62
	for i in $* ; do
		ver=$(dpkg -s $i 2>/dev/null | grep "Version:" | sed -e "s|Version: ||g")
		if [ -z "$ver" ] ; then
			echo "$i"
		else
			echo "$i-$ver"
		fi
	done
}


63 64
__do_query()
{
65
    local CMD
66 67 68 69
    case $PMTYPE in
        apt-rpm)
            CMD="rpm -qf"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
70
        *-dpkg)
71
            showcmd dpkg -S $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
72
            dpkg_print_name_version $(dpkg -S $1 | grep -v "^diversion by" | sed -e "s|:.*||")
73
            return ;;
74
        yum-rpm|dnf-rpm|urpm-rpm)
75 76 77 78 79
            CMD="rpm -qf"
            ;;
        zypper-rpm)
            CMD="rpm -qf"
            ;;
80
        emerge)
81
            assure_exists equery
82 83
            CMD="equery belongs"
            ;;
84 85 86
        pacman)
            CMD="pacman -Qo"
            ;;
87 88 89
        pkgng)
            CMD="pkg which"
            ;;
90 91 92
        conary)
            CMD="conary query --path"
            ;;
93 94
        slackpkg)
            # note: need remove leading slash for grep
95
            docmd grep -R -- "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
96 97
            return
            ;;
98 99 100
        ipkg)
            CMD="ipkg files"
            ;;
101 102
        xbps)
            # FIXME: maybe it is search file?
103
            CMD="xbps-query -o"
104
            ;;
105 106 107 108 109
        aptcyg)
            #CMD="apt-cyg packageof"
            # do not realized locally
            return 1
            ;;
110
        *)
111
            fatal "Have no suitable query command for $PMTYPE"
112 113
            ;;
    esac
114

115 116
    docmd $CMD $@
}
117

118 119 120 121 122 123 124 125 126 127

__do_short_query()
{
    local CMD
    case $PMTYPE in
        *-rpm)
            CMD="rpm -qf --queryformat %{NAME}\n"
            ;;
        NOapt-dpkg)
            showcmd dpkg -S $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
128
            dpkg_print_name_version $(dpkg -S $1 | sed -e "s|:.*||" | grep -v "^diversion by")
129 130
            return ;;
        NOemerge)
131
            assure_exists equery
132 133 134 135 136 137 138 139 140 141 142
            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
            ;;
        *)
143
            fatal "Have no suitable query command for $PMTYPE"
144 145 146 147 148 149 150
            ;;
    esac

    docmd $CMD $@
}


151 152
epm_query_file()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153
    # И где это используется?
154 155
    # in short mode print handle only real names and do short output
    # TODO: move to separate command?
156
    # FIXME: it is possible use query
157
    if [ -n "$short" ] ; then
158 159
        [ -n "$pkg_files$pkg_dirs" ] || fatal "Run query without file names (needed path to files)"
        __do_short_query $pkg_files $pkg_dirs
160 161 162
         return
    fi

163 164
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
165

166 167 168

    load_helper epm-search_file

169
    for pkg in $pkg_filenames ; do
170
        __do_query_real_file "$pkg"
171
        __do_query "$FULLFILEPATH" || pkg_filenames="$FULLFILEPATH" epm_search_file
172
    done
173 174

}