epm-query_file 4.42 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 "Note: $1 is placed as $TOFILE"
47 48 49 50 51
		fi
	fi
	
	# get value of symbolic link
	if [ -L "$TOFILE" ] ; then
52 53
		local LINKTO
		__do_query "$TOFILE"
54
		LINKTO=$(readlink -f "$TOFILE")
55
		info "Note: $TOFILE is link to $LINKTO"
56
		__do_query_real_file "$LINKTO"
57
		return
58
	fi
59 60

	FULLFILEPATH="$TOFILE"
61 62
}

63 64
dpkg_print_name_version()
{
65
	local ver i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
66
	for i in "$@" ; do
67 68 69 70 71 72 73 74 75 76
		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
}


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

123 124
    docmd $CMD $@
}
125

126 127 128 129 130 131 132 133 134

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

    docmd $CMD $@
}


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

171 172
    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"
173

174

175
    #load_helper epm-search_file
176

177
    for pkg in $pkg_filenames ; do
178
        __do_query_real_file "$pkg"
179
        __do_query "$FULLFILEPATH" || info "Try epm sf for search file in all packages in repository" #|| pkg_filenames="$FULLFILEPATH" epm_search_file
180
    done
181 182

}