File: dlocate

package info (click to toggle)
dlocate 0.96.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 236 kB
  • ctags: 3
  • sloc: sh: 328; perl: 128; makefile: 54
file content (316 lines) | stat: -rwxr-xr-x 9,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
#
# original script by Jim Pick <jim@jimpick.com>, GPL'd of course
# many changes and enhancement by Craig Sander <cas@taz.net.au>
#
# hacked by cas to use case instead of if/elif/fi
# hacked by cas to add '-ls' option.  also added error checking for
# -L and -ls options.
# hacked by cas to add '-conf' and '-lsconf' options.
# hacked by cas to add '-md5sum' and '-md5check' options.
# hacked by cas to add '-man' option.
# hacked by cas to add '-s' option (requires grep-dctrl).
# hacked by cas to support multiple string/package arguments
# hacked by cas to add simplistic emulation of 'dpkg -l'

DLOCATEDB=/var/lib/dlocate/dlocatedb
DPKGLIST=/var/lib/dlocate/dpkg-list
DPKG_INFO=/var/lib/dpkg/info

if [ -x /usr/bin/locate.findutils ] ; then
    # locate package is installed
    LOCATE='/usr/bin/locate.findutils'
else
    # slocate diverts locate
    LOCATE=$(/usr/sbin/dpkg-divert --truename /usr/bin/locate)
fi

function dlocate_help {
  cat <<__EOF__
Usage: dlocate [option] [string...]

Options:
  (no option) string  list all records that match
  -S          string      list records where files match
  -L          package     list all files in package
  -l          package     almost-emulation of 'dpkg -l'
  -s          package     print package's status
  -ls         package     'ls -ldF' of all files in package
  -du         package     'du -sck' of all files in package
  -conf       package     list conffiles in package
  -lsconf     package     'ls -ldF' of conffiles in package
  -md5sum     package     list package's md5sums (if any)
  -md5check   package     check package's md5sums (if any)
  -man        package     list package's man pages (if any)
  -lsman      package     list full path/filenames of man pages
  -lsbin      package     list full path/filenames of executable files

  --                      stop processing options.  remainder of
                          command-line is filename(s)

  --ignore-case, -i       ignore case when search for filenames or
                          package names.

  -h                      display this help message and exit.
  -V                      display dlocate's version number and exit.

  The -L, -s, and -S commands are roughly analagous to the
  equivalent dpkg commands.
__EOF__
  exit 1
}

function dlocate_version {
  #VERSION_BANNER=$(grep-dctrl -X -P dlocate -n -s Version -d \
  #  /var/lib/dpkg/status | sed -e '2s/.*/(&)/')
  VERSION_BANNER="0.96.1"
  echo "dlocate version "$VERSION_BANNER
  exit
}

function dlocate_option_error () {
  echo "dlocate: unknown option '$1'"
  echo
  echo "Use: 'dlocate -- $1' if you want to search for '$1'"
  exit 1
}

IGNORE_CASE=""
LAST_OPT=0

OPTION="DEFAULT"

SEPARATOR='::::::::'
PKGS=""
for p in "$@"; do
      case "$p" in

        ('--')

            if [ $LAST_OPT -eq 0 ] ; then
                LAST_OPT=1
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        (''|'-h'|'-H'|'--help')

            if [ $LAST_OPT -eq 0 ] ; then
                dlocate_help
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        ('-v'|'-V'|'--version')

            if [ $LAST_OPT -eq 0 ] ; then
                dlocate_version
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        ('--ignore-case'|'-i')

            if [ $LAST_OPT -eq 0 ] ; then
                IGNORE_CASE='-i'
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        ('-S'|'-L'|'-l'|'-s'|'-ls'|'-du'|'-conf'|'-lsconf'|'-md5sum'|'-md5check'|'-man'|'-lsman'|'-lsbin')

            if [ $LAST_OPT -eq 0 ] ; then
                OPTION="$1"
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        (-*)
            if [ $LAST_OPT -eq 0 ] ; then
                dlocate_option_error $1
            else
                PKGS="$PKGS$SEPARATOR$p"
            fi
            ;;

        (*)
            PKGS="$PKGS$SEPARATOR$p"
            ;;
      esac
done

PKGS=$(echo "$PKGS" | sed -e "s/^$SEPARATOR//")
#echo $PKGS

PKGS_REGEXP=$(echo "$PKGS" | sed -e "s/$SEPARATOR/|/g")
#echo $PKGS_REGEXP

FILES_REGEXP="$PKGS_REGEXP"
#echo $FILES_REGEXP

#exit

[ -z "$PKGS_REGEXP" ] && PKGS_REGEXP='^$'


if [ "$OPTION" = '-l' ] ; then

    [ -z "$COLUMNS" ] &&
      COLUMNS=$(stty -a 2>&- |
      sed -ne '/columns/s/.*columns \([0-9]*\)[^0-9].*/\1/p');

    [ 0"$COLUMNS" -lt 80 ] && COLUMNS=80;

    ((fieldw=(COLUMNS-24)/4));

    # dpkg uses ((fieldd=fieldw*2+16));
    #
    # limiting the output to COLUMNS-2 characters and losing up to
    # additional 3 characters due to the rounding error.

    ((fieldd=COLUMNS-fieldw*2-6));

    fmt_eq=$(echo ==================================================== |
      cut -c-$fieldw)

    HEADER=$( printf \
"Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ %-${fieldw}s %-${fieldw}s %s
+++-${fmt_eq}-${fmt_eq}-${fmt_eq}${fmt_eq}==================\\n" \
              Name Version Description)

    BODY_FMT_STRING="%-2s  %-${fieldw}.${fieldw}s %-${fieldw}.${fieldw}s %-${fieldd}.${fieldd}s\\n"

    BODY=$( egrep $IGNORE_CASE -- "$PKGS_REGEXP" "$DPKGLIST" | \
            awk -F'\t' "{ printf \"$BODY_FMT_STRING\", \$1, \$2, \$3, \$4 }" |
            sed -e 's/ *$//g'
          )

    [ -n "$BODY" ] && echo -e "$HEADER\n$BODY"

elif [ "$OPTION" = '-S' ] ; then

    $LOCATE -d $DLOCATEDB $IGNORE_CASE -- $PKGS | egrep $IGNORE_CASE "^[-a-zA-Z0-9_.+]+:.*($FILES_REGEXP)"
    result=$?

elif [ "$OPTION" = 'DEFAULT' ] ; then

    $LOCATE -d $DLOCATEDB $IGNORE_CASE -- $PKGS
    result=$?

else

    for PKG in $PKGS; do

        case "$OPTION" in

            '-L')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    cat $DPKG_INFO/$PKG.list
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
                ;;

            '-s')
                    grep-dctrl -X -P $PKG /var/lib/dpkg/status || \
                    echo Package $PKG does not exist. >&2
                ;;

            '-ls')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    xargs -r ls -ldF < $DPKG_INFO/$PKG.list
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
                ;;

            '-du')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    du -sck $(cat $DPKG_INFO/$PKG.list | xargs -r ls -1dF | grep -v "@$\|/$" )
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
                ;;

            '-conf')
                if [ -s $DPKG_INFO/$PKG.conffiles ] ; then
                    cat $DPKG_INFO/$PKG.conffiles
                else
                    echo Package $PKG not installed or has no conffiles. >&2
                fi
                ;;

            '-lsconf')
                if [ -s $DPKG_INFO/$PKG.conffiles ] ; then
                    ls -ldF $(cat $DPKG_INFO/$PKG.conffiles)
                else
                    echo Package $PKG not installed or has no conffiles. >&2
                fi
                ;;

            '-md5sum')
                if [ -s $DPKG_INFO/$PKG.md5sums ] ; then
                    cat $DPKG_INFO/$PKG.md5sums
                else
                    echo Package $PKG not installed or has no md5sums. >&2
                fi
                ;;

            '-lsman')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    dlocate -L $PKG | \
                    grep '/man.*/.*[0-9]/'
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
;;

            '-man')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    dlocate -L $PKG | \
                        sed -n -e 's/\.gz$//' \
                               -e '/\/man.*\/.*[0-9]/s/^.*\/\([^\/]*\)\.\(.*\)/\2 \1/p'
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
                ;;

            '-lsbin')
                if [ -s $DPKG_INFO/$PKG.list ] ; then
                    dlocate -L $PKG | \
                    xargs -r ls -lLd 2> /dev/null | \
                    grep -v "^[^-]" | \
                    egrep '^-.{2,8}[xs]' | \
                    sed -e 's/.* \//\//' | \
                    xargs -r ls -1
                else
                    echo Package $PKG not installed or $PKG.list is empty. >&2
                fi
;;

            '-md5check')
                if [ -s $DPKG_INFO/$PKG.md5sums ] ; then
                    pushd / >/dev/null 2>&1
                    cat $DPKG_INFO/$PKG.md5sums | \
                        md5sum -c -
                    popd >/dev/null 2>&1
                else
                    echo Package $PKG not installed or has no md5sums. >&2
                fi
                ;;

        esac
    done
fi


test -n "$result" && exit $result