Deleted Added
full compact
index.subr (257817) index.subr (258265)
1if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
2#
3# Copyright (c) 2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
1if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
2#
3# Copyright (c) 2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 257817 2013-11-07 21:16:32Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 258265 2013-11-17 17:42:15Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." packages/index.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/media/common.subr
36f_include $BSDCFG_SHARE/strings.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig"
39f_include_lang $BSDCFG_LIBE/include/messages.subr
40
41############################################################ GLOBALS
42
43PACKAGE_INDEX=
44_INDEX_INITTED=
45
46#
47# Default path to pkg(8) repo-packagesite.sqlite database
48#
49SQLITE_REPO="/var/db/pkg/repo-packagesite.sqlite"
50
51#
52# Default path to on-disk cache INDEX file
53#
54PACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache"
55
56#
57# INDEX format for FreeBSD-6.0 or higher:
58#
59# package|port-origin|install-prefix|comment|port-desc-file|maintainer|
60# categories|build-deps|run-deps|www-site|reserve|reserve|reserve|disc
61#
62INDEX_FORMAT="%n-%v" # package
63INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o" # port-origin
64INDEX_FORMAT="$INDEX_FORMAT|%p" # install-prefix
65INDEX_FORMAT="$INDEX_FORMAT|%c" # comment
66INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o/pkg-descr" # port-desc-file
67INDEX_FORMAT="$INDEX_FORMAT|%m" # maintainer
68INDEX_FORMAT="$INDEX_FORMAT|@CATEGORIES@" # place-holder
69INDEX_FORMAT="$INDEX_FORMAT|" # build-deps
70INDEX_FORMAT="$INDEX_FORMAT|@RUNDEPS@" # place-holder
71INDEX_FORMAT="$INDEX_FORMAT|%w" # www-site
72INDEX_FORMAT="$INDEX_FORMAT|" # reserved
73INDEX_FORMAT="$INDEX_FORMAT|" # reserved
74INDEX_FORMAT="$INDEX_FORMAT|" # reserved
75INDEX_FORMAT="$INDEX_FORMAT|" # disc
76
77############################################################ FUNCTIONS
78
79# f_index_initialize [$var_to_set]
80#
81# Read and initialize the global index. Returns success unless media cannot be
82# initialized for any reason (e.g. user cancels media selection dialog or an
83# error occurs). The index is sorted before being loaded into $var_to_set.
84#
85# NOTE: The index is processed with f_index_read() [below] after being loaded.
86#
87f_index_initialize()
88{
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." packages/index.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/media/common.subr
36f_include $BSDCFG_SHARE/strings.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig"
39f_include_lang $BSDCFG_LIBE/include/messages.subr
40
41############################################################ GLOBALS
42
43PACKAGE_INDEX=
44_INDEX_INITTED=
45
46#
47# Default path to pkg(8) repo-packagesite.sqlite database
48#
49SQLITE_REPO="/var/db/pkg/repo-packagesite.sqlite"
50
51#
52# Default path to on-disk cache INDEX file
53#
54PACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache"
55
56#
57# INDEX format for FreeBSD-6.0 or higher:
58#
59# package|port-origin|install-prefix|comment|port-desc-file|maintainer|
60# categories|build-deps|run-deps|www-site|reserve|reserve|reserve|disc
61#
62INDEX_FORMAT="%n-%v" # package
63INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o" # port-origin
64INDEX_FORMAT="$INDEX_FORMAT|%p" # install-prefix
65INDEX_FORMAT="$INDEX_FORMAT|%c" # comment
66INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o/pkg-descr" # port-desc-file
67INDEX_FORMAT="$INDEX_FORMAT|%m" # maintainer
68INDEX_FORMAT="$INDEX_FORMAT|@CATEGORIES@" # place-holder
69INDEX_FORMAT="$INDEX_FORMAT|" # build-deps
70INDEX_FORMAT="$INDEX_FORMAT|@RUNDEPS@" # place-holder
71INDEX_FORMAT="$INDEX_FORMAT|%w" # www-site
72INDEX_FORMAT="$INDEX_FORMAT|" # reserved
73INDEX_FORMAT="$INDEX_FORMAT|" # reserved
74INDEX_FORMAT="$INDEX_FORMAT|" # reserved
75INDEX_FORMAT="$INDEX_FORMAT|" # disc
76
77############################################################ FUNCTIONS
78
79# f_index_initialize [$var_to_set]
80#
81# Read and initialize the global index. Returns success unless media cannot be
82# initialized for any reason (e.g. user cancels media selection dialog or an
83# error occurs). The index is sorted before being loaded into $var_to_set.
84#
85# NOTE: The index is processed with f_index_read() [below] after being loaded.
86#
87f_index_initialize()
88{
89 local __funcname=f_index_initialize
89 local __var_to_set="${2:-PACKAGE_INDEX}"
90
91 [ "$_INDEX_INITTED" ] && return $SUCCESS
92
93 # Got any media?
94 f_media_verify || return $FAILURE
95
96 # Does it move when you kick it?
97 f_device_init media || return $FAILURE
98
99 f_show_info "$msg_attempting_to_update_repository_catalogue"
100
101 #
102 # Generate $PACKAGESITE variable for pkg(8) based on media type
103 #
104 local __type __data __site
105 device_media get type __type
106 device_media get private __data
107 case "$__type" in
108 $DEVICE_TYPE_UFS|$DEVICE_TYPE_DISK) __site="file://$MOUNTPOINT" ;;
109 $DEVICE_TYPE_DIRECTORY) __site="file://$__data" ;;
110 $DEVICE_TYPE_FLOPPY) __site="file://${__data:-$MOUNTPOINT}" ;;
111 $DEVICE_TYPE_FTP) f_getvar $VAR_FTP_PATH __site ;;
112 $DEVICE_TYPE_HTTP_PROXY) f_getvar $VAR_HTTP_PROXY_PATH __site ;;
113 $DEVICE_TYPE_HTTP) f_getvar $VAR_HTTP_PATH __site ;;
114 $DEVICE_TYPE_CDROM) __site="file://$MOUNTPOINT" ;;
115 $DEVICE_TYPE_USB) __site="file://$MOUNTPOINT" ;;
116 $DEVICE_TYPE_DOS) __site="file://$MOUNTPOINT" ;;
117 $DEVICE_TYPE_NFS) __site="file://$MOUNTPOINT" ;;
118 esac
119
90 local __var_to_set="${2:-PACKAGE_INDEX}"
91
92 [ "$_INDEX_INITTED" ] && return $SUCCESS
93
94 # Got any media?
95 f_media_verify || return $FAILURE
96
97 # Does it move when you kick it?
98 f_device_init media || return $FAILURE
99
100 f_show_info "$msg_attempting_to_update_repository_catalogue"
101
102 #
103 # Generate $PACKAGESITE variable for pkg(8) based on media type
104 #
105 local __type __data __site
106 device_media get type __type
107 device_media get private __data
108 case "$__type" in
109 $DEVICE_TYPE_UFS|$DEVICE_TYPE_DISK) __site="file://$MOUNTPOINT" ;;
110 $DEVICE_TYPE_DIRECTORY) __site="file://$__data" ;;
111 $DEVICE_TYPE_FLOPPY) __site="file://${__data:-$MOUNTPOINT}" ;;
112 $DEVICE_TYPE_FTP) f_getvar $VAR_FTP_PATH __site ;;
113 $DEVICE_TYPE_HTTP_PROXY) f_getvar $VAR_HTTP_PROXY_PATH __site ;;
114 $DEVICE_TYPE_HTTP) f_getvar $VAR_HTTP_PATH __site ;;
115 $DEVICE_TYPE_CDROM) __site="file://$MOUNTPOINT" ;;
116 $DEVICE_TYPE_USB) __site="file://$MOUNTPOINT" ;;
117 $DEVICE_TYPE_DOS) __site="file://$MOUNTPOINT" ;;
118 $DEVICE_TYPE_NFS) __site="file://$MOUNTPOINT" ;;
119 esac
120
120 if ! PACKAGESITE="$__site" f_quietly pkg update; then
121 export PACKAGESITE="$__site"
122 f_dprintf "PACKAGESITE=[%s]" "$PACKAGESITE"
123 if ! f_eval_catch $__funcname pkg "pkg update"; then
121 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
122 f_device_shutdown media
123 return $FAILURE
124 fi
125
126 #
127 # Try to get contents from validated on-disk cache
128 #
129
130 #
131 # Calculate digest used to determine if the on-disk persistant cache
132 # INDEX (containing this digest on the first line) is valid and can be
133 # used to quickly populate the environment.
134 #
135 local __sqlite_digest
136 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
137 f_show_err "$msg_no_pkg_database_found"
138 f_device_shutdown media
139 return $FAILURE
140 fi
141
142 #
143 # Check to see if the persistant cache INDEX file exists
144 #
145 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
146 #
147 # Attempt to populate the environment with the (soon to be)
148 # validated on-disk cache. If validation fails, fall-back to
149 # generating a fresh cache.
150 #
151 if eval $__var_to_set='$(
152 ( # Get digest as the first word on first line
153 read digest rest_ignored
154
155 #
156 # If the stored digest matches the calculated-
157 # one populate the environment from the on-disk
158 # cache and provide success exit status.
159 #
160 if [ "$digest" = "$__sqlite_digest" ]; then
161 cat
162 exit $SUCCESS
163 else
164 # Otherwise, return the current value
165 eval echo \"\$__var_to_set\"
166 exit $FAILURE
167 fi
168 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
169 )'; then
170 f_show_info \
171 "$msg_located_index_now_reading_package_data_from_it"
172 if ! f_index_read "$__var_to_set"; then
173 f_show_err \
174 "$msg_io_or_format_error_on_index_file"
175 return $FAILURE
176 fi
177 _INDEX_INITTED=1
178 return $SUCCESS
179 fi
180 # Otherwise, fall-thru to create a fresh cache from scratch
181 fi
182
183 #
184 # If we reach this point, we need to generate the data from scratch
185 #
186
187 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
188 if ! eval "$( pkg rquery "%n-%v %C" | awk '
189 { categories[$1] = categories[$1] " " $2 }
190 END {
191 for (package in categories)
192 {
193 cats = categories[package]
194 sub(/^ /, "", cats)
195 gsub(/[^[:alnum:]_]/, "_", package)
196 printf "local _%s_categories=\"%s\";\n", package, cats
197 }
198 }' )"; then
199 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
200 f_device_shutdown media
201 return $FAILURE
202 fi
203
204 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
205 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
206 { rundeps[$1] = rundeps[$1] " " $2 }
207 END {
208 for (package in rundeps)
209 {
210 deps = rundeps[package]
211 sub(/^ /, "", deps)
212 gsub(/[^[:alnum:]_]/, "_", package)
213 printf "local _%s_rundeps=\"%s\";\n", package, deps
214 }
215 }' )"; then
216 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
217 f_device_shutdown media
218 return $FAILURE
219 fi
220
221 f_show_info "$msg_generating_index_from_pkg_database"
222 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
223 while read LINE; do
224 package="${LINE%%|*}";
225 f_str2varname "$package" varpkg;
226 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
227 \"\|\$_${varpkg}_categories\|\" LINE
228 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
229 \"\|\$_${varpkg}_rundeps\|\" LINE
230 echo "$LINE"
231 done
232 )' # always returns true (status of last item in pipe-chain)
233 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
234
235 #
236 # Attempt to create the persistant on-disk cache
237 #
238
239 # Create a new temporary file to write to
240 local __tmpfile="$( mktemp -t "$pgm" )"
241 if [ "$__tmpfile" ]; then
242 # Write the temporary file contents
243 echo "$__sqlite_digest" > "$__tmpfile"
244 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
245
246 # Finally, move the temporary file into place
247 case "$PACKAGES_INDEX_CACHEFILE" in
248 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
249 esac
250 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
251 fi
252
253 f_show_info "$msg_located_index_now_reading_package_data_from_it"
254 if ! f_index_read "$__var_to_set"; then
255 f_show_err "$msg_io_or_format_error_on_index_file"
256 return $FAILURE
257 fi
258
259 _INDEX_INITTED=1
260 return $SUCCESS
261}
262
263# f_index_read [$var_to_get]
264#
265# Process the INDEX file (contents contained in $var_to_get) and...
266#
267# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
268# 2. For convenience, create $_npkgs holding the total number of all packages
269# 3. extract associative categories for each package into $_categories_$varpkg
270# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
271# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
272# 6. create $_npkgs_$varcat holding the total number of packages in category
273#
274# NOTE: $varpkg is the product of f_str2varname $package varpkg
275# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
276# NOTE: We only show categories for which there are at least one package.
277# NOTE: $varcat is the product of f_str2varname $category varcat
278#
279f_index_read()
280{
281 local var_to_get="${1:-PACKAGE_INDEX}"
282
283 # Export variables required by awk(1) below
284 export msg_no_description_provided
285 export msg_all msg_all_desc
286 export VALID_VARNAME_CHARS
287 export msg_packages
288
289 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
290 function asorti(src, dest)
291 {
292 # Copy src indices to dest and calculate array length
293 nitems = 0; for (i in src) dest[++nitems] = i
294
295 # Sort the array of indices (dest) using insertion sort method
296 for (i = 1; i <= nitems; k = i++)
297 {
298 idx = dest[i]
299 while ((k > 0) && (dest[k] > idx))
300 {
301 dest[k+1] = dest[k]
302 k--
303 }
304 dest[k+1] = idx
305 }
306
307 return nitems
308 }
309 function print_category(category, npkgs, desc)
310 {
311 cat = category
312 # Accent the category if the first page has been
313 # cached (also acting as a visitation indicator)
314 if ( ENVIRON["_index_page_" varcat "_1"] )
315 cat = cat "*"
316 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
317 cat, npkgs, desc
318 }
319 BEGIN {
320 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
321 default_desc = ENVIRON["msg_no_description_provided"]
322 packages = ENVIRON["msg_packages"]
323 tpkgs = 0
324 prefix = ""
325 }
326 {
327 tpkgs++
328 varpkg = $1
329 gsub("[^" valid_chars "]", "_", varpkg)
330 print "_categories_" varpkg "=\"" $7 "\""
331 split($7, pkg_categories, /[[:space:]]+/)
332 for (pkg_category in pkg_categories)
333 categories[pkg_categories[pkg_category]]++
334 print "_rundeps_" varpkg "=\"" $9 "\""
335 }
336 END {
337 print "_npkgs=" tpkgs # For convenience, total package count
338
339 n = asorti(categories, categories_sorted)
340
341 # Produce package counts for each category
342 for (i = 1; i <= n; i++)
343 {
344 cat = varcat = categories_sorted[i]
345 npkgs = categories[cat]
346 gsub("[^" valid_chars "]", "_", varcat)
347 print "_npkgs_" varcat "=\"" npkgs "\""
348 }
349
350 # Create menu list and generate list of categories at same time
351 print "CATEGORY_MENU_LIST=\""
352 print_category(ENVIRON["msg_all"], tpkgs,
353 ENVIRON["msg_all_desc"])
354 category_list = ""
355 for (i = 1; i <= n; i++)
356 {
357 cat = varcat = categories_sorted[i]
358 npkgs = categories[cat]
359 cur_prefix = tolower(substr(cat, 1, 1))
360 if ( prefix != cur_prefix )
361 prefix = cur_prefix
362 else
363 cat = " " cat
364 gsub("[^" valid_chars "]", "_", varcat)
365 desc = ENVIRON["_category_" varcat]
366 if ( ! desc ) desc = default_desc
367 print_category(cat, npkgs, desc)
368 category_list = category_list " " cat
369 }
370 print "\""
371
372 # Produce the list of categories (calculated in above block)
373 sub(/^ /, "", category_list)
374 print "PACKAGE_CATEGORIES=\"" category_list "\""
375
376 }' )" # End-Quote
377}
378
379# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
380#
381# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
382# NULL; but should not be missing) into a series of sequential variables
383# corresponding to "pages" containing up to $pagesize packages. The package
384# INDEX data must be contained in the variable $var_to_get. The extracted pages
385# are stored in variables ${var_basename}_# -- where "#" is a the page number.
386# If $category is set, only packages for that category are extracted.
387# Otherwise, if $category is "All", missing, or NULL, all packages are
388# extracted and no filtering is done.
389#
390f_index_extract_pages()
391{
392 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
393 local category="$4" # Optional
394
395 eval "$(
396 debug= f_getvar "$var_to_get" | awk -F'|' \
397 -v cat="$category" \
398 -v pagesize="$pagesize" \
399 -v var_basename="$var_basename" \
400 -v i18n_all="$msg_all" '
401 BEGIN { n = page = 0 }
402 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
403 {
404 if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \
405 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
406 starting_new_page = (n++ == (pagesize * page))
407 if ( starting_new_page )
408 printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
409 var_basename "_" ++page "='\''"
410 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
411 }
412 END { if ( n > 0 ) print "'\''" }'
413 )"
414}
415
416# f_index_search $var_to_get $name [$var_to_set]
417#
418# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
419# NULL; but should not be missing) for $name, returning the first match.
420# Matches are strict (not regular expressions) and must match the beginning
421# portion of the package name to be considered a match. If $var_to_set is
422# missing or NULL, output is sent to standard output. If a match is found,
423# returns success; otherwise failure.
424#
425f_index_search()
426{
427 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
428 local __var_to_set="$3"
429
430 f_dprintf "f_index_search: Searching package data (in %s) for %s" \
431 "$__var_to_get" "$__pkg_basename"
432
433 local __pkg=
434 __pkg=$( debug= f_getvar "$__var_to_get" |
435 awk -F'|' -v basename="$__pkg_basename" '
436 BEGIN { n = length(basename) }
437 substr($1, 0, n) == basename { print $1; exit }
438 ' )
439 if [ ! "$__pkg" ]; then
440 f_dprintf "f_index_search: No packages matching %s found" \
441 "$__pkg_basename"
442 return $FAILURE
443 fi
444
445 f_dprintf "f_index_search: Found package %s" "$__pkg"
446 if [ "$__var_to_set" ]; then
447 setvar "$__var_to_set" "$__pkg"
448 else
449 echo "$__pkg"
450 fi
451 return $SUCCESS
452}
453
454############################################################ MAIN
455
456f_dprintf "%s: Successfully loaded." packages/index.subr
457
458fi # ! $_PACKAGES_INDEX_SUBR
124 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
125 f_device_shutdown media
126 return $FAILURE
127 fi
128
129 #
130 # Try to get contents from validated on-disk cache
131 #
132
133 #
134 # Calculate digest used to determine if the on-disk persistant cache
135 # INDEX (containing this digest on the first line) is valid and can be
136 # used to quickly populate the environment.
137 #
138 local __sqlite_digest
139 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
140 f_show_err "$msg_no_pkg_database_found"
141 f_device_shutdown media
142 return $FAILURE
143 fi
144
145 #
146 # Check to see if the persistant cache INDEX file exists
147 #
148 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
149 #
150 # Attempt to populate the environment with the (soon to be)
151 # validated on-disk cache. If validation fails, fall-back to
152 # generating a fresh cache.
153 #
154 if eval $__var_to_set='$(
155 ( # Get digest as the first word on first line
156 read digest rest_ignored
157
158 #
159 # If the stored digest matches the calculated-
160 # one populate the environment from the on-disk
161 # cache and provide success exit status.
162 #
163 if [ "$digest" = "$__sqlite_digest" ]; then
164 cat
165 exit $SUCCESS
166 else
167 # Otherwise, return the current value
168 eval echo \"\$__var_to_set\"
169 exit $FAILURE
170 fi
171 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
172 )'; then
173 f_show_info \
174 "$msg_located_index_now_reading_package_data_from_it"
175 if ! f_index_read "$__var_to_set"; then
176 f_show_err \
177 "$msg_io_or_format_error_on_index_file"
178 return $FAILURE
179 fi
180 _INDEX_INITTED=1
181 return $SUCCESS
182 fi
183 # Otherwise, fall-thru to create a fresh cache from scratch
184 fi
185
186 #
187 # If we reach this point, we need to generate the data from scratch
188 #
189
190 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
191 if ! eval "$( pkg rquery "%n-%v %C" | awk '
192 { categories[$1] = categories[$1] " " $2 }
193 END {
194 for (package in categories)
195 {
196 cats = categories[package]
197 sub(/^ /, "", cats)
198 gsub(/[^[:alnum:]_]/, "_", package)
199 printf "local _%s_categories=\"%s\";\n", package, cats
200 }
201 }' )"; then
202 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
203 f_device_shutdown media
204 return $FAILURE
205 fi
206
207 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
208 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
209 { rundeps[$1] = rundeps[$1] " " $2 }
210 END {
211 for (package in rundeps)
212 {
213 deps = rundeps[package]
214 sub(/^ /, "", deps)
215 gsub(/[^[:alnum:]_]/, "_", package)
216 printf "local _%s_rundeps=\"%s\";\n", package, deps
217 }
218 }' )"; then
219 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
220 f_device_shutdown media
221 return $FAILURE
222 fi
223
224 f_show_info "$msg_generating_index_from_pkg_database"
225 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
226 while read LINE; do
227 package="${LINE%%|*}";
228 f_str2varname "$package" varpkg;
229 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
230 \"\|\$_${varpkg}_categories\|\" LINE
231 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
232 \"\|\$_${varpkg}_rundeps\|\" LINE
233 echo "$LINE"
234 done
235 )' # always returns true (status of last item in pipe-chain)
236 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
237
238 #
239 # Attempt to create the persistant on-disk cache
240 #
241
242 # Create a new temporary file to write to
243 local __tmpfile="$( mktemp -t "$pgm" )"
244 if [ "$__tmpfile" ]; then
245 # Write the temporary file contents
246 echo "$__sqlite_digest" > "$__tmpfile"
247 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
248
249 # Finally, move the temporary file into place
250 case "$PACKAGES_INDEX_CACHEFILE" in
251 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
252 esac
253 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
254 fi
255
256 f_show_info "$msg_located_index_now_reading_package_data_from_it"
257 if ! f_index_read "$__var_to_set"; then
258 f_show_err "$msg_io_or_format_error_on_index_file"
259 return $FAILURE
260 fi
261
262 _INDEX_INITTED=1
263 return $SUCCESS
264}
265
266# f_index_read [$var_to_get]
267#
268# Process the INDEX file (contents contained in $var_to_get) and...
269#
270# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
271# 2. For convenience, create $_npkgs holding the total number of all packages
272# 3. extract associative categories for each package into $_categories_$varpkg
273# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
274# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
275# 6. create $_npkgs_$varcat holding the total number of packages in category
276#
277# NOTE: $varpkg is the product of f_str2varname $package varpkg
278# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
279# NOTE: We only show categories for which there are at least one package.
280# NOTE: $varcat is the product of f_str2varname $category varcat
281#
282f_index_read()
283{
284 local var_to_get="${1:-PACKAGE_INDEX}"
285
286 # Export variables required by awk(1) below
287 export msg_no_description_provided
288 export msg_all msg_all_desc
289 export VALID_VARNAME_CHARS
290 export msg_packages
291
292 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
293 function asorti(src, dest)
294 {
295 # Copy src indices to dest and calculate array length
296 nitems = 0; for (i in src) dest[++nitems] = i
297
298 # Sort the array of indices (dest) using insertion sort method
299 for (i = 1; i <= nitems; k = i++)
300 {
301 idx = dest[i]
302 while ((k > 0) && (dest[k] > idx))
303 {
304 dest[k+1] = dest[k]
305 k--
306 }
307 dest[k+1] = idx
308 }
309
310 return nitems
311 }
312 function print_category(category, npkgs, desc)
313 {
314 cat = category
315 # Accent the category if the first page has been
316 # cached (also acting as a visitation indicator)
317 if ( ENVIRON["_index_page_" varcat "_1"] )
318 cat = cat "*"
319 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
320 cat, npkgs, desc
321 }
322 BEGIN {
323 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
324 default_desc = ENVIRON["msg_no_description_provided"]
325 packages = ENVIRON["msg_packages"]
326 tpkgs = 0
327 prefix = ""
328 }
329 {
330 tpkgs++
331 varpkg = $1
332 gsub("[^" valid_chars "]", "_", varpkg)
333 print "_categories_" varpkg "=\"" $7 "\""
334 split($7, pkg_categories, /[[:space:]]+/)
335 for (pkg_category in pkg_categories)
336 categories[pkg_categories[pkg_category]]++
337 print "_rundeps_" varpkg "=\"" $9 "\""
338 }
339 END {
340 print "_npkgs=" tpkgs # For convenience, total package count
341
342 n = asorti(categories, categories_sorted)
343
344 # Produce package counts for each category
345 for (i = 1; i <= n; i++)
346 {
347 cat = varcat = categories_sorted[i]
348 npkgs = categories[cat]
349 gsub("[^" valid_chars "]", "_", varcat)
350 print "_npkgs_" varcat "=\"" npkgs "\""
351 }
352
353 # Create menu list and generate list of categories at same time
354 print "CATEGORY_MENU_LIST=\""
355 print_category(ENVIRON["msg_all"], tpkgs,
356 ENVIRON["msg_all_desc"])
357 category_list = ""
358 for (i = 1; i <= n; i++)
359 {
360 cat = varcat = categories_sorted[i]
361 npkgs = categories[cat]
362 cur_prefix = tolower(substr(cat, 1, 1))
363 if ( prefix != cur_prefix )
364 prefix = cur_prefix
365 else
366 cat = " " cat
367 gsub("[^" valid_chars "]", "_", varcat)
368 desc = ENVIRON["_category_" varcat]
369 if ( ! desc ) desc = default_desc
370 print_category(cat, npkgs, desc)
371 category_list = category_list " " cat
372 }
373 print "\""
374
375 # Produce the list of categories (calculated in above block)
376 sub(/^ /, "", category_list)
377 print "PACKAGE_CATEGORIES=\"" category_list "\""
378
379 }' )" # End-Quote
380}
381
382# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
383#
384# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
385# NULL; but should not be missing) into a series of sequential variables
386# corresponding to "pages" containing up to $pagesize packages. The package
387# INDEX data must be contained in the variable $var_to_get. The extracted pages
388# are stored in variables ${var_basename}_# -- where "#" is a the page number.
389# If $category is set, only packages for that category are extracted.
390# Otherwise, if $category is "All", missing, or NULL, all packages are
391# extracted and no filtering is done.
392#
393f_index_extract_pages()
394{
395 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
396 local category="$4" # Optional
397
398 eval "$(
399 debug= f_getvar "$var_to_get" | awk -F'|' \
400 -v cat="$category" \
401 -v pagesize="$pagesize" \
402 -v var_basename="$var_basename" \
403 -v i18n_all="$msg_all" '
404 BEGIN { n = page = 0 }
405 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
406 {
407 if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \
408 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
409 starting_new_page = (n++ == (pagesize * page))
410 if ( starting_new_page )
411 printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
412 var_basename "_" ++page "='\''"
413 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
414 }
415 END { if ( n > 0 ) print "'\''" }'
416 )"
417}
418
419# f_index_search $var_to_get $name [$var_to_set]
420#
421# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
422# NULL; but should not be missing) for $name, returning the first match.
423# Matches are strict (not regular expressions) and must match the beginning
424# portion of the package name to be considered a match. If $var_to_set is
425# missing or NULL, output is sent to standard output. If a match is found,
426# returns success; otherwise failure.
427#
428f_index_search()
429{
430 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
431 local __var_to_set="$3"
432
433 f_dprintf "f_index_search: Searching package data (in %s) for %s" \
434 "$__var_to_get" "$__pkg_basename"
435
436 local __pkg=
437 __pkg=$( debug= f_getvar "$__var_to_get" |
438 awk -F'|' -v basename="$__pkg_basename" '
439 BEGIN { n = length(basename) }
440 substr($1, 0, n) == basename { print $1; exit }
441 ' )
442 if [ ! "$__pkg" ]; then
443 f_dprintf "f_index_search: No packages matching %s found" \
444 "$__pkg_basename"
445 return $FAILURE
446 fi
447
448 f_dprintf "f_index_search: Found package %s" "$__pkg"
449 if [ "$__var_to_set" ]; then
450 setvar "$__var_to_set" "$__pkg"
451 else
452 echo "$__pkg"
453 fi
454 return $SUCCESS
455}
456
457############################################################ MAIN
458
459f_dprintf "%s: Successfully loaded." packages/index.subr
460
461fi # ! $_PACKAGES_INDEX_SUBR