epm-query_file 2.6 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2009, 2012  Etersoft
# Copyright (C) 2009, 2012  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 21
load_helper epm-search_file

22
# copied from etersoft-build-utils/bin/rpmqf
23 24

# use and modify TOFILE recursively
25
__do_query_real_file()
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
{
	local LINKTO1 LINKTO
	local TOFILE
	
	# get canonical path
	if [ -e "$1" ] ; then
		TOFILE=$1
	else
		TOFILE=`which $1 2>/dev/null || echo $1`
		if [ "$TOFILE" != "$1" ] ; then
		echo "Note: $1 is placed as $TOFILE"
		fi
	fi
	
	# get value of symbolic link
	if [ -L "$TOFILE" ] ; then
42
		__do_query $TOFILE
43 44
		LINKTO=`readlink "$TOFILE"`
		echo "Note: $TOFILE is link to $LINKTO"
45
		__do_query_real_file "$LINKTO"
46
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
47
	FULLFILEPATH=`realpath $TOFILE`
48 49
}

50 51
dpkg_print_name_version()
{
52
	local ver i
53 54 55 56 57 58 59 60 61 62 63
	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
}


64 65 66 67 68 69 70
__do_query()
{
    case $PMTYPE in
        apt-rpm)
            CMD="rpm -qf"
            ;;
        apt-dpkg)
71 72 73
            showcmd dpkg -S $1
            dpkg_print_name_version $(dpkg -S $1 | sed -e "s|:.*||")
            return ;;
74 75 76 77 78 79
        yum-rpm|urpm-rpm)
            CMD="rpm -qf"
            ;;
        zypper-rpm)
            CMD="rpm -qf"
            ;;
80 81 82
        emerge)
            CMD="equery belongs"
            ;;
83 84 85
        pacman)
            CMD="pacman -Qo"
            ;;
86 87
        slackpkg)
            # note: need remove leading slash for grep
88
            docmd grep -R "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
89 90
            return
            ;;
91 92 93 94
        *)
            fatal "Do not known query command for $PMTYPE"
            ;;
    esac
95

96 97
    docmd $CMD $@
}
98 99 100

epm_query_file()
{
101 102
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
103

104
    for pkg in $pkg_filenames ; do
105
        __do_query_real_file "$pkg"
106 107
        __do_query $FULLFILEPATH || pkg_filenames=$pkg epm_search_file
    done
108 109

}