updated on Tue Jan 17 20:03:13 UTC 2012
[aur-mirror.git] / pacfile / pacfile
blobfb5d79756d152e114aa199c07166b5a52a3463f2
1 #!/bin/bash
3 #-----------------------------------------------------------
4 # A utility for discovering which ArchLinux package contains
5 # a given file. Downloads filelists for all active repos in
6 # pacman.conf; uses mirrors in order according to pacman's
7 # mirrorlist file.
9 # Peter Morris 2008
11 # Modified by tuxce.net@gmail.com
12 #-----------------------------------------------------------
14 db=/var/lib/pacman/filelists
15 pacmanconf=/etc/pacman.conf
17 #-----------------------------------------------------------
18 # Print active repos in $pacmanconf.
19 #-----------------------------------------------------------
20 function list_repos() {
21 sed -n '/REPOSITORIES/,$ s/^\[\(.*\)\]/\1/p' $pacmanconf
24 #-----------------------------------------------------------
25 # Download the filelist for repo $1. Assumes $mirrors
26 # is an array of urls to check for $file.
27 #-----------------------------------------------------------
28 function download() {
29 repo=$1
30 file="${repo}.files.tar.gz"
31 shift
32 #for mirror in $(eval echo \${$repo[*]})
33 for mirror in $*
35 wget --connect-timeout=15 -P "$db" "$mirror/${file}"
36 [ -e "${db}/${file}" ] && break
37 done
40 #-----------------------------------------------------------
41 # Decompress $1 into dir $2.
42 #-----------------------------------------------------------
43 function decompress() {
44 file=$1
45 repo=$2
47 mkdir "${db}/${repo}"
48 tar -xzf "${db}/${file}" -C "${db}/${repo}"
49 rm -f "${db}/${file}"
52 #-----------------------------------------------------------
53 # Download the filelist for each active repo in
54 # pacman.conf. Try mirrors in order until successful.
55 #-----------------------------------------------------------
56 function sync() {
57 (LC_ALL=C pacman --debug | grep 'adding new server' \
58 | sed "s|.*adding new server URL to database '\(.*\)': \(.*\)|\1 \2|g"
59 echo) | while read repo_cur url
61 [ -z "$repo_ex" ] && repo_ex=$repo_cur
62 if [ "$repo_ex" != "$repo_cur" ]
63 then
64 echo "Fetching filelist for $repo_ex"
65 download "$repo_ex" "${list[@]}"
66 if [[ $? == 0 ]]; then
67 decompress "$file" "$repo_ex"
68 else
69 echo
70 echo "Error: unable to retrieve $file from any mirror."; echo
72 repo_ex=$repo_cur
73 list=( )
75 list=( "${list[@]}" $url )
76 done
79 #-----------------------------------------------------------
80 # Print usage message.
81 #-----------------------------------------------------------
82 function usage() {
83 echo "usage: $(basename $0) [options] [pattern]"
84 echo
85 echo "Search package filelists for [pattern]."
86 echo "options:"
87 echo " $(basename $0) [-h --help] Print this help message."
88 echo " $(basename $0) [-S --sync] Syncronize package filelists."
91 #-----------------------------------------------------------
92 # Start of `main'.
93 #-----------------------------------------------------------
94 case "$1" in
95 -S|--sync)
96 if [[ $EUID != 0 ]]; then
97 echo "error: you cannot perform this operation unless you are root."
98 exit 1;
99 fi
101 echo "Syncronizing ${db}..."; echo
102 rm -rf "$db" && mkdir -p "$db"
103 sync
106 -h|--help)
107 usage
111 if [[ $# < 1 ]]; then
112 echo "not enough parameters"; echo
113 usage
114 exit 1
117 if [[ ! -d "$db" ]]; then
118 echo "File database \"$db\" does not exist!"
119 echo "Have you synchronized the database?"; echo
120 exit 1
123 grep -rE "$@" "$db" | sed "s|$db/\(.*\)/files:|\1 |g"
125 esac