Deleted Added
full compact
index.subr (258420) index.subr (259533)
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: stable/10/usr.sbin/bsdconfig/share/packages/index.subr 258420 2013-11-21 03:38:47Z dteske $
27# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/packages/index.subr 259533 2013-12-18 01:14:25Z gjb $
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
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_DIRECTORY)
110 __site="file://$__data/packages/$PKG_ABI" ;;
111 $DEVICE_TYPE_FLOPPY)
112 __site="file://${__data:-$MOUNTPOINT}/packages/$PKG_ABI" ;;
113 $DEVICE_TYPE_FTP)
114 f_getvar $VAR_FTP_PATH __site
115 __site="$__site/packages/$PKG_ABI" ;;
116 $DEVICE_TYPE_HTTP)
117 f_getvar $VAR_HTTP_PATH __site
118 __site="$__site/$PKG_ABI/latest" ;;
119 $DEVICE_TYPE_HTTP_PROXY)
120 f_getvar $VAR_HTTP_PROXY_PATH __site
121 __site="$__site/packages/$PKG_ABI" ;;
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
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_DIRECTORY)
110 __site="file://$__data/packages/$PKG_ABI" ;;
111 $DEVICE_TYPE_FLOPPY)
112 __site="file://${__data:-$MOUNTPOINT}/packages/$PKG_ABI" ;;
113 $DEVICE_TYPE_FTP)
114 f_getvar $VAR_FTP_PATH __site
115 __site="$__site/packages/$PKG_ABI" ;;
116 $DEVICE_TYPE_HTTP)
117 f_getvar $VAR_HTTP_PATH __site
118 __site="$__site/$PKG_ABI/latest" ;;
119 $DEVICE_TYPE_HTTP_PROXY)
120 f_getvar $VAR_HTTP_PROXY_PATH __site
121 __site="$__site/packages/$PKG_ABI" ;;
122 $DEVICE_TYPE_CDROM)
123 __site="file://$MOUNTPOINT/packages/$PKG_ABI"
124 export REPOS_DIR="$MOUNTPOINT/packages/repos" ;;
122 *) # UFS, DISK, CDROM, USB, DOS, NFS, etc.
123 __site="file://$MOUNTPOINT/packages/$PKG_ABI"
124 esac
125
126 export PACKAGESITE="$__site"
127 f_dprintf "PACKAGESITE=[%s]" "$PACKAGESITE"
128 if ! f_eval_catch $__funcname pkg "pkg update"; then
129 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
130 f_device_shutdown media
131 return $FAILURE
132 fi
133
134 #
135 # Try to get contents from validated on-disk cache
136 #
137
138 #
139 # Calculate digest used to determine if the on-disk persistant cache
140 # INDEX (containing this digest on the first line) is valid and can be
141 # used to quickly populate the environment.
142 #
143 local __sqlite_digest
144 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
145 f_show_err "$msg_no_pkg_database_found"
146 f_device_shutdown media
147 return $FAILURE
148 fi
149
150 #
151 # Check to see if the persistant cache INDEX file exists
152 #
153 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
154 #
155 # Attempt to populate the environment with the (soon to be)
156 # validated on-disk cache. If validation fails, fall-back to
157 # generating a fresh cache.
158 #
159 if eval $__var_to_set='$(
160 ( # Get digest as the first word on first line
161 read digest rest_ignored
162
163 #
164 # If the stored digest matches the calculated-
165 # one populate the environment from the on-disk
166 # cache and provide success exit status.
167 #
168 if [ "$digest" = "$__sqlite_digest" ]; then
169 cat
170 exit $SUCCESS
171 else
172 # Otherwise, return the current value
173 eval echo \"\$__var_to_set\"
174 exit $FAILURE
175 fi
176 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
177 )'; then
178 f_show_info \
179 "$msg_located_index_now_reading_package_data_from_it"
180 if ! f_index_read "$__var_to_set"; then
181 f_show_err \
182 "$msg_io_or_format_error_on_index_file"
183 return $FAILURE
184 fi
185 _INDEX_INITTED=1
186 return $SUCCESS
187 fi
188 # Otherwise, fall-thru to create a fresh cache from scratch
189 fi
190
191 #
192 # If we reach this point, we need to generate the data from scratch
193 #
194
195 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
196 if ! eval "$( pkg rquery "%n-%v %C" | awk '
197 { categories[$1] = categories[$1] " " $2 }
198 END {
199 for (package in categories)
200 {
201 cats = categories[package]
202 sub(/^ /, "", cats)
203 gsub(/[^[:alnum:]_]/, "_", package)
204 printf "local _%s_categories=\"%s\";\n", package, cats
205 }
206 }' )"; then
207 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
208 f_device_shutdown media
209 return $FAILURE
210 fi
211
212 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
213 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
214 { rundeps[$1] = rundeps[$1] " " $2 }
215 END {
216 for (package in rundeps)
217 {
218 deps = rundeps[package]
219 sub(/^ /, "", deps)
220 gsub(/[^[:alnum:]_]/, "_", package)
221 printf "local _%s_rundeps=\"%s\";\n", package, deps
222 }
223 }' )"; then
224 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
225 f_device_shutdown media
226 return $FAILURE
227 fi
228
229 f_show_info "$msg_generating_index_from_pkg_database"
230 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
231 while read LINE; do
232 package="${LINE%%|*}";
233 f_str2varname "$package" varpkg;
234 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
235 \"\|\$_${varpkg}_categories\|\" LINE
236 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
237 \"\|\$_${varpkg}_rundeps\|\" LINE
238 echo "$LINE"
239 done
240 )' # always returns true (status of last item in pipe-chain)
241 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
242
243 #
244 # Attempt to create the persistant on-disk cache
245 #
246
247 # Create a new temporary file to write to
248 local __tmpfile="$( mktemp -t "$pgm" )"
249 if [ "$__tmpfile" ]; then
250 # Write the temporary file contents
251 echo "$__sqlite_digest" > "$__tmpfile"
252 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
253
254 # Finally, move the temporary file into place
255 case "$PACKAGES_INDEX_CACHEFILE" in
256 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
257 esac
258 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
259 fi
260
261 f_show_info "$msg_located_index_now_reading_package_data_from_it"
262 if ! f_index_read "$__var_to_set"; then
263 f_show_err "$msg_io_or_format_error_on_index_file"
264 return $FAILURE
265 fi
266
267 _INDEX_INITTED=1
268 return $SUCCESS
269}
270
271# f_index_read [$var_to_get]
272#
273# Process the INDEX file (contents contained in $var_to_get) and...
274#
275# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
276# 2. For convenience, create $_npkgs holding the total number of all packages
277# 3. extract associative categories for each package into $_categories_$varpkg
278# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
279# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
280# 6. create $_npkgs_$varcat holding the total number of packages in category
281#
282# NOTE: $varpkg is the product of f_str2varname $package varpkg
283# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
284# NOTE: We only show categories for which there are at least one package.
285# NOTE: $varcat is the product of f_str2varname $category varcat
286#
287f_index_read()
288{
289 local var_to_get="${1:-PACKAGE_INDEX}"
290
291 # Export variables required by awk(1) below
292 export msg_no_description_provided
293 export msg_all msg_all_desc
294 export VALID_VARNAME_CHARS
295 export msg_packages
296
297 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
298 function asorti(src, dest)
299 {
300 # Copy src indices to dest and calculate array length
301 nitems = 0; for (i in src) dest[++nitems] = i
302
303 # Sort the array of indices (dest) using insertion sort method
304 for (i = 1; i <= nitems; k = i++)
305 {
306 idx = dest[i]
307 while ((k > 0) && (dest[k] > idx))
308 {
309 dest[k+1] = dest[k]
310 k--
311 }
312 dest[k+1] = idx
313 }
314
315 return nitems
316 }
317 function print_category(category, npkgs, desc)
318 {
319 cat = category
320 # Accent the category if the first page has been
321 # cached (also acting as a visitation indicator)
322 if ( ENVIRON["_index_page_" varcat "_1"] )
323 cat = cat "*"
324 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
325 cat, npkgs, desc
326 }
327 BEGIN {
328 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
329 default_desc = ENVIRON["msg_no_description_provided"]
330 packages = ENVIRON["msg_packages"]
331 tpkgs = 0
332 prefix = ""
333 }
334 {
335 tpkgs++
336 varpkg = $1
337 gsub("[^" valid_chars "]", "_", varpkg)
338 print "_categories_" varpkg "=\"" $7 "\""
339 split($7, pkg_categories, /[[:space:]]+/)
340 for (pkg_category in pkg_categories)
341 categories[pkg_categories[pkg_category]]++
342 print "_rundeps_" varpkg "=\"" $9 "\""
343 }
344 END {
345 print "_npkgs=" tpkgs # For convenience, total package count
346
347 n = asorti(categories, categories_sorted)
348
349 # Produce package counts for each category
350 for (i = 1; i <= n; i++)
351 {
352 cat = varcat = categories_sorted[i]
353 npkgs = categories[cat]
354 gsub("[^" valid_chars "]", "_", varcat)
355 print "_npkgs_" varcat "=\"" npkgs "\""
356 }
357
358 # Create menu list and generate list of categories at same time
359 print "CATEGORY_MENU_LIST=\""
360 print_category(ENVIRON["msg_all"], tpkgs,
361 ENVIRON["msg_all_desc"])
362 category_list = ""
363 for (i = 1; i <= n; i++)
364 {
365 cat = varcat = categories_sorted[i]
366 npkgs = categories[cat]
367 cur_prefix = tolower(substr(cat, 1, 1))
368 if ( prefix != cur_prefix )
369 prefix = cur_prefix
370 else
371 cat = " " cat
372 gsub("[^" valid_chars "]", "_", varcat)
373 desc = ENVIRON["_category_" varcat]
374 if ( ! desc ) desc = default_desc
375 print_category(cat, npkgs, desc)
376 category_list = category_list " " cat
377 }
378 print "\""
379
380 # Produce the list of categories (calculated in above block)
381 sub(/^ /, "", category_list)
382 print "PACKAGE_CATEGORIES=\"" category_list "\""
383
384 }' )" # End-Quote
385}
386
387# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
388#
389# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
390# NULL; but should not be missing) into a series of sequential variables
391# corresponding to "pages" containing up to $pagesize packages. The package
392# INDEX data must be contained in the variable $var_to_get. The extracted pages
393# are stored in variables ${var_basename}_# -- where "#" is a the page number.
394# If $category is set, only packages for that category are extracted.
395# Otherwise, if $category is "All", missing, or NULL, all packages are
396# extracted and no filtering is done.
397#
398f_index_extract_pages()
399{
400 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
401 local category="$4" # Optional
402
403 eval "$(
404 debug= f_getvar "$var_to_get" | awk -F'|' \
405 -v cat="$category" \
406 -v pagesize="$pagesize" \
407 -v var_basename="$var_basename" \
408 -v i18n_all="$msg_all" '
409 BEGIN { n = page = 0 }
410 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
411 {
412 if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \
413 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
414 starting_new_page = (n++ == (pagesize * page))
415 if ( starting_new_page )
416 printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
417 var_basename "_" ++page "='\''"
418 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
419 }
420 END { if ( n > 0 ) print "'\''" }'
421 )"
422}
423
424# f_index_search $var_to_get $name [$var_to_set]
425#
426# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
427# NULL; but should not be missing) for $name, returning the first match.
428# Matches are strict (not regular expressions) and must match the beginning
429# portion of the package name to be considered a match. If $var_to_set is
430# missing or NULL, output is sent to standard output. If a match is found,
431# returns success; otherwise failure.
432#
433f_index_search()
434{
435 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
436 local __var_to_set="$3"
437
438 f_dprintf "f_index_search: Searching package data (in %s) for %s" \
439 "$__var_to_get" "$__pkg_basename"
440
441 local __pkg=
442 __pkg=$( debug= f_getvar "$__var_to_get" |
443 awk -F'|' -v basename="$__pkg_basename" '
444 BEGIN { n = length(basename) }
445 substr($1, 0, n) == basename { print $1; exit }
446 ' )
447 if [ ! "$__pkg" ]; then
448 f_dprintf "f_index_search: No packages matching %s found" \
449 "$__pkg_basename"
450 return $FAILURE
451 fi
452
453 f_dprintf "f_index_search: Found package %s" "$__pkg"
454 if [ "$__var_to_set" ]; then
455 setvar "$__var_to_set" "$__pkg"
456 else
457 echo "$__pkg"
458 fi
459 return $SUCCESS
460}
461
462############################################################ MAIN
463
464f_dprintf "%s: Successfully loaded." packages/index.subr
465
466fi # ! $_PACKAGES_INDEX_SUBR
125 *) # UFS, DISK, CDROM, USB, DOS, NFS, etc.
126 __site="file://$MOUNTPOINT/packages/$PKG_ABI"
127 esac
128
129 export PACKAGESITE="$__site"
130 f_dprintf "PACKAGESITE=[%s]" "$PACKAGESITE"
131 if ! f_eval_catch $__funcname pkg "pkg update"; then
132 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
133 f_device_shutdown media
134 return $FAILURE
135 fi
136
137 #
138 # Try to get contents from validated on-disk cache
139 #
140
141 #
142 # Calculate digest used to determine if the on-disk persistant cache
143 # INDEX (containing this digest on the first line) is valid and can be
144 # used to quickly populate the environment.
145 #
146 local __sqlite_digest
147 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
148 f_show_err "$msg_no_pkg_database_found"
149 f_device_shutdown media
150 return $FAILURE
151 fi
152
153 #
154 # Check to see if the persistant cache INDEX file exists
155 #
156 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
157 #
158 # Attempt to populate the environment with the (soon to be)
159 # validated on-disk cache. If validation fails, fall-back to
160 # generating a fresh cache.
161 #
162 if eval $__var_to_set='$(
163 ( # Get digest as the first word on first line
164 read digest rest_ignored
165
166 #
167 # If the stored digest matches the calculated-
168 # one populate the environment from the on-disk
169 # cache and provide success exit status.
170 #
171 if [ "$digest" = "$__sqlite_digest" ]; then
172 cat
173 exit $SUCCESS
174 else
175 # Otherwise, return the current value
176 eval echo \"\$__var_to_set\"
177 exit $FAILURE
178 fi
179 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
180 )'; then
181 f_show_info \
182 "$msg_located_index_now_reading_package_data_from_it"
183 if ! f_index_read "$__var_to_set"; then
184 f_show_err \
185 "$msg_io_or_format_error_on_index_file"
186 return $FAILURE
187 fi
188 _INDEX_INITTED=1
189 return $SUCCESS
190 fi
191 # Otherwise, fall-thru to create a fresh cache from scratch
192 fi
193
194 #
195 # If we reach this point, we need to generate the data from scratch
196 #
197
198 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
199 if ! eval "$( pkg rquery "%n-%v %C" | awk '
200 { categories[$1] = categories[$1] " " $2 }
201 END {
202 for (package in categories)
203 {
204 cats = categories[package]
205 sub(/^ /, "", cats)
206 gsub(/[^[:alnum:]_]/, "_", package)
207 printf "local _%s_categories=\"%s\";\n", package, cats
208 }
209 }' )"; then
210 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
211 f_device_shutdown media
212 return $FAILURE
213 fi
214
215 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
216 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
217 { rundeps[$1] = rundeps[$1] " " $2 }
218 END {
219 for (package in rundeps)
220 {
221 deps = rundeps[package]
222 sub(/^ /, "", deps)
223 gsub(/[^[:alnum:]_]/, "_", package)
224 printf "local _%s_rundeps=\"%s\";\n", package, deps
225 }
226 }' )"; then
227 f_show_err "$msg_unable_to_pkg_rquery_package_dependencies"
228 f_device_shutdown media
229 return $FAILURE
230 fi
231
232 f_show_info "$msg_generating_index_from_pkg_database"
233 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
234 while read LINE; do
235 package="${LINE%%|*}";
236 f_str2varname "$package" varpkg;
237 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
238 \"\|\$_${varpkg}_categories\|\" LINE
239 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
240 \"\|\$_${varpkg}_rundeps\|\" LINE
241 echo "$LINE"
242 done
243 )' # always returns true (status of last item in pipe-chain)
244 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
245
246 #
247 # Attempt to create the persistant on-disk cache
248 #
249
250 # Create a new temporary file to write to
251 local __tmpfile="$( mktemp -t "$pgm" )"
252 if [ "$__tmpfile" ]; then
253 # Write the temporary file contents
254 echo "$__sqlite_digest" > "$__tmpfile"
255 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
256
257 # Finally, move the temporary file into place
258 case "$PACKAGES_INDEX_CACHEFILE" in
259 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
260 esac
261 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
262 fi
263
264 f_show_info "$msg_located_index_now_reading_package_data_from_it"
265 if ! f_index_read "$__var_to_set"; then
266 f_show_err "$msg_io_or_format_error_on_index_file"
267 return $FAILURE
268 fi
269
270 _INDEX_INITTED=1
271 return $SUCCESS
272}
273
274# f_index_read [$var_to_get]
275#
276# Process the INDEX file (contents contained in $var_to_get) and...
277#
278# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
279# 2. For convenience, create $_npkgs holding the total number of all packages
280# 3. extract associative categories for each package into $_categories_$varpkg
281# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
282# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
283# 6. create $_npkgs_$varcat holding the total number of packages in category
284#
285# NOTE: $varpkg is the product of f_str2varname $package varpkg
286# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
287# NOTE: We only show categories for which there are at least one package.
288# NOTE: $varcat is the product of f_str2varname $category varcat
289#
290f_index_read()
291{
292 local var_to_get="${1:-PACKAGE_INDEX}"
293
294 # Export variables required by awk(1) below
295 export msg_no_description_provided
296 export msg_all msg_all_desc
297 export VALID_VARNAME_CHARS
298 export msg_packages
299
300 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
301 function asorti(src, dest)
302 {
303 # Copy src indices to dest and calculate array length
304 nitems = 0; for (i in src) dest[++nitems] = i
305
306 # Sort the array of indices (dest) using insertion sort method
307 for (i = 1; i <= nitems; k = i++)
308 {
309 idx = dest[i]
310 while ((k > 0) && (dest[k] > idx))
311 {
312 dest[k+1] = dest[k]
313 k--
314 }
315 dest[k+1] = idx
316 }
317
318 return nitems
319 }
320 function print_category(category, npkgs, desc)
321 {
322 cat = category
323 # Accent the category if the first page has been
324 # cached (also acting as a visitation indicator)
325 if ( ENVIRON["_index_page_" varcat "_1"] )
326 cat = cat "*"
327 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n",
328 cat, npkgs, desc
329 }
330 BEGIN {
331 valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
332 default_desc = ENVIRON["msg_no_description_provided"]
333 packages = ENVIRON["msg_packages"]
334 tpkgs = 0
335 prefix = ""
336 }
337 {
338 tpkgs++
339 varpkg = $1
340 gsub("[^" valid_chars "]", "_", varpkg)
341 print "_categories_" varpkg "=\"" $7 "\""
342 split($7, pkg_categories, /[[:space:]]+/)
343 for (pkg_category in pkg_categories)
344 categories[pkg_categories[pkg_category]]++
345 print "_rundeps_" varpkg "=\"" $9 "\""
346 }
347 END {
348 print "_npkgs=" tpkgs # For convenience, total package count
349
350 n = asorti(categories, categories_sorted)
351
352 # Produce package counts for each category
353 for (i = 1; i <= n; i++)
354 {
355 cat = varcat = categories_sorted[i]
356 npkgs = categories[cat]
357 gsub("[^" valid_chars "]", "_", varcat)
358 print "_npkgs_" varcat "=\"" npkgs "\""
359 }
360
361 # Create menu list and generate list of categories at same time
362 print "CATEGORY_MENU_LIST=\""
363 print_category(ENVIRON["msg_all"], tpkgs,
364 ENVIRON["msg_all_desc"])
365 category_list = ""
366 for (i = 1; i <= n; i++)
367 {
368 cat = varcat = categories_sorted[i]
369 npkgs = categories[cat]
370 cur_prefix = tolower(substr(cat, 1, 1))
371 if ( prefix != cur_prefix )
372 prefix = cur_prefix
373 else
374 cat = " " cat
375 gsub("[^" valid_chars "]", "_", varcat)
376 desc = ENVIRON["_category_" varcat]
377 if ( ! desc ) desc = default_desc
378 print_category(cat, npkgs, desc)
379 category_list = category_list " " cat
380 }
381 print "\""
382
383 # Produce the list of categories (calculated in above block)
384 sub(/^ /, "", category_list)
385 print "PACKAGE_CATEGORIES=\"" category_list "\""
386
387 }' )" # End-Quote
388}
389
390# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
391#
392# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
393# NULL; but should not be missing) into a series of sequential variables
394# corresponding to "pages" containing up to $pagesize packages. The package
395# INDEX data must be contained in the variable $var_to_get. The extracted pages
396# are stored in variables ${var_basename}_# -- where "#" is a the page number.
397# If $category is set, only packages for that category are extracted.
398# Otherwise, if $category is "All", missing, or NULL, all packages are
399# extracted and no filtering is done.
400#
401f_index_extract_pages()
402{
403 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3"
404 local category="$4" # Optional
405
406 eval "$(
407 debug= f_getvar "$var_to_get" | awk -F'|' \
408 -v cat="$category" \
409 -v pagesize="$pagesize" \
410 -v var_basename="$var_basename" \
411 -v i18n_all="$msg_all" '
412 BEGIN { n = page = 0 }
413 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
414 {
415 if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \
416 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
417 starting_new_page = (n++ == (pagesize * page))
418 if ( starting_new_page )
419 printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
420 var_basename "_" ++page "='\''"
421 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
422 }
423 END { if ( n > 0 ) print "'\''" }'
424 )"
425}
426
427# f_index_search $var_to_get $name [$var_to_set]
428#
429# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is
430# NULL; but should not be missing) for $name, returning the first match.
431# Matches are strict (not regular expressions) and must match the beginning
432# portion of the package name to be considered a match. If $var_to_set is
433# missing or NULL, output is sent to standard output. If a match is found,
434# returns success; otherwise failure.
435#
436f_index_search()
437{
438 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2"
439 local __var_to_set="$3"
440
441 f_dprintf "f_index_search: Searching package data (in %s) for %s" \
442 "$__var_to_get" "$__pkg_basename"
443
444 local __pkg=
445 __pkg=$( debug= f_getvar "$__var_to_get" |
446 awk -F'|' -v basename="$__pkg_basename" '
447 BEGIN { n = length(basename) }
448 substr($1, 0, n) == basename { print $1; exit }
449 ' )
450 if [ ! "$__pkg" ]; then
451 f_dprintf "f_index_search: No packages matching %s found" \
452 "$__pkg_basename"
453 return $FAILURE
454 fi
455
456 f_dprintf "f_index_search: Found package %s" "$__pkg"
457 if [ "$__var_to_set" ]; then
458 setvar "$__var_to_set" "$__pkg"
459 else
460 echo "$__pkg"
461 fi
462 return $SUCCESS
463}
464
465############################################################ MAIN
466
467f_dprintf "%s: Successfully loaded." packages/index.subr
468
469fi # ! $_PACKAGES_INDEX_SUBR