index.subr revision 294866
1250323Sdteskeif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1 2250323Sdteske# 3294864Sdteske# Copyright (c) 2013-2016 Devin Teske 4252980Sdteske# All rights reserved. 5250323Sdteske# 6250323Sdteske# Redistribution and use in source and binary forms, with or without 7250323Sdteske# modification, are permitted provided that the following conditions 8250323Sdteske# are met: 9250323Sdteske# 1. Redistributions of source code must retain the above copyright 10250323Sdteske# notice, this list of conditions and the following disclaimer. 11250323Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12250323Sdteske# notice, this list of conditions and the following disclaimer in the 13250323Sdteske# documentation and/or other materials provided with the distribution. 14250323Sdteske# 15250323Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17250323Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18250323Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19250323Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21250323Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22250323Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23250323Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24250323Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25250323Sdteske# SUCH DAMAGE. 26250323Sdteske# 27250323Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 294866 2016-01-27 00:12:58Z dteske $ 28250323Sdteske# 29250323Sdteske############################################################ INCLUDES 30250323Sdteske 31250323SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32250323Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33250323Sdteskef_dprintf "%s: loading includes..." packages/index.subr 34250323Sdteskef_include $BSDCFG_SHARE/device.subr 35250323Sdteskef_include $BSDCFG_SHARE/media/common.subr 36268999Sdteskef_include $BSDCFG_SHARE/packages/musthavepkg.subr 37250323Sdteskef_include $BSDCFG_SHARE/strings.subr 38250323Sdteske 39250323SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 40250323Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr 41250323Sdteske 42250323Sdteske############################################################ GLOBALS 43250323Sdteske 44250323SdteskePACKAGE_INDEX= 45250323Sdteske_INDEX_INITTED= 46250323Sdteske 47257795Sdteske# 48257795Sdteske# Default path to pkg(8) repo-packagesite.sqlite database 49257795Sdteske# 50294864SdteskeSQLITE_REPO="/var/db/pkg/repo-FreeBSD.sqlite" 51257795Sdteske 52257795Sdteske# 53257795Sdteske# Default path to on-disk cache INDEX file 54257795Sdteske# 55257795SdteskePACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache" 56257795Sdteske 57250323Sdteske############################################################ FUNCTIONS 58250323Sdteske 59257795Sdteske# f_index_initialize [$var_to_set] 60250323Sdteske# 61257795Sdteske# Read and initialize the global index. Returns success unless media cannot be 62257795Sdteske# initialized for any reason (e.g. user cancels media selection dialog or an 63257795Sdteske# error occurs). The index is sorted before being loaded into $var_to_set. 64250323Sdteske# 65250323Sdteske# NOTE: The index is processed with f_index_read() [below] after being loaded. 66250323Sdteske# 67250323Sdteskef_index_initialize() 68250323Sdteske{ 69258265Sdteske local __funcname=f_index_initialize 70268999Sdteske local __var_to_set="${1:-PACKAGE_INDEX}" 71250323Sdteske 72250323Sdteske [ "$_INDEX_INITTED" ] && return $SUCCESS 73250323Sdteske 74250323Sdteske # Got any media? 75250323Sdteske f_media_verify || return $FAILURE 76250323Sdteske 77268999Sdteske # Make sure we have a usable pkg(8) with $PKG_ABI 78268999Sdteske f_musthavepkg_init 79268999Sdteske 80250323Sdteske # Does it move when you kick it? 81264840Sdteske f_device_init device_media || return $FAILURE 82250323Sdteske 83257795Sdteske f_show_info "$msg_attempting_to_update_repository_catalogue" 84257795Sdteske 85257795Sdteske # 86257795Sdteske # Generate $PACKAGESITE variable for pkg(8) based on media type 87257795Sdteske # 88257795Sdteske local __type __data __site 89257795Sdteske device_media get type __type 90257795Sdteske device_media get private __data 91257795Sdteske case "$__type" in 92258267Sdteske $DEVICE_TYPE_DIRECTORY) 93258267Sdteske __site="file://$__data/packages/$PKG_ABI" ;; 94258267Sdteske $DEVICE_TYPE_FLOPPY) 95258267Sdteske __site="file://${__data:-$MOUNTPOINT}/packages/$PKG_ABI" ;; 96258267Sdteske $DEVICE_TYPE_FTP) 97258267Sdteske f_getvar $VAR_FTP_PATH __site 98258267Sdteske __site="$__site/packages/$PKG_ABI" ;; 99258267Sdteske $DEVICE_TYPE_HTTP) 100258267Sdteske f_getvar $VAR_HTTP_PATH __site 101258267Sdteske __site="$__site/$PKG_ABI/latest" ;; 102258267Sdteske $DEVICE_TYPE_HTTP_PROXY) 103258267Sdteske f_getvar $VAR_HTTP_PROXY_PATH __site 104258267Sdteske __site="$__site/packages/$PKG_ABI" ;; 105259427Sgjb $DEVICE_TYPE_CDROM) 106259427Sgjb __site="file://$MOUNTPOINT/packages/$PKG_ABI" 107259427Sgjb export REPOS_DIR="$MOUNTPOINT/packages/repos" ;; 108258267Sdteske *) # UFS, DISK, CDROM, USB, DOS, NFS, etc. 109258267Sdteske __site="file://$MOUNTPOINT/packages/$PKG_ABI" 110257795Sdteske esac 111257795Sdteske 112268999Sdteske f_dprintf "PACKAGESITE=[%s]" "$__site" 113268999Sdteske if ! f_eval_catch $__funcname pkg \ 114268999Sdteske 'PACKAGESITE="%s" pkg update' "$__site" 115268999Sdteske then 116257795Sdteske f_show_err "$msg_unable_to_update_pkg_from_selected_media" 117264840Sdteske f_device_shutdown device_media 118250323Sdteske return $FAILURE 119250323Sdteske fi 120257795Sdteske 121257795Sdteske # 122257795Sdteske # Try to get contents from validated on-disk cache 123257795Sdteske # 124257795Sdteske 125257795Sdteske # 126257795Sdteske # Calculate digest used to determine if the on-disk persistant cache 127257795Sdteske # INDEX (containing this digest on the first line) is valid and can be 128257795Sdteske # used to quickly populate the environment. 129257795Sdteske # 130257795Sdteske local __sqlite_digest 131257795Sdteske if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then 132257795Sdteske f_show_err "$msg_no_pkg_database_found" 133264840Sdteske f_device_shutdown device_media 134257795Sdteske return $FAILURE 135257795Sdteske fi 136257795Sdteske 137257795Sdteske # 138257795Sdteske # Check to see if the persistant cache INDEX file exists 139257795Sdteske # 140257795Sdteske if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then 141257795Sdteske # 142257795Sdteske # Attempt to populate the environment with the (soon to be) 143257795Sdteske # validated on-disk cache. If validation fails, fall-back to 144257795Sdteske # generating a fresh cache. 145257795Sdteske # 146257795Sdteske if eval $__var_to_set='$( 147257795Sdteske ( # Get digest as the first word on first line 148257795Sdteske read digest rest_ignored 149257795Sdteske 150257795Sdteske # 151257795Sdteske # If the stored digest matches the calculated- 152257795Sdteske # one populate the environment from the on-disk 153257795Sdteske # cache and provide success exit status. 154257795Sdteske # 155257795Sdteske if [ "$digest" = "$__sqlite_digest" ]; then 156257795Sdteske cat 157257795Sdteske exit $SUCCESS 158257795Sdteske else 159257795Sdteske # Otherwise, return the current value 160257795Sdteske eval echo \"\$__var_to_set\" 161257795Sdteske exit $FAILURE 162257795Sdteske fi 163257795Sdteske ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null 164257795Sdteske )'; then 165257795Sdteske if ! f_index_read "$__var_to_set"; then 166257795Sdteske f_show_err \ 167257795Sdteske "$msg_io_or_format_error_on_index_file" 168257795Sdteske return $FAILURE 169257795Sdteske fi 170257795Sdteske _INDEX_INITTED=1 171257795Sdteske return $SUCCESS 172257795Sdteske fi 173257795Sdteske # Otherwise, fall-thru to create a fresh cache from scratch 174257795Sdteske fi 175257795Sdteske 176257795Sdteske # 177257795Sdteske # If we reach this point, we need to generate the data from scratch 178257795Sdteske # 179257795Sdteske 180294866Sdteske eval "$__var_to_set"='$( pkg rquery -I | ( 181294866Sdteske exec 2<&1; dpv -ko /dev/stderr >&$TERMINAL_STDOUT_PASSTHRU \ 182294866Sdteske -b "$DIALOG_BACKTITLE" \ 183294866Sdteske -- "$msg_generating_index_from_pkg_database" 184294866Sdteske ) | sort )' 185250323Sdteske 186257795Sdteske # 187257795Sdteske # Attempt to create the persistant on-disk cache 188257795Sdteske # 189257795Sdteske 190257795Sdteske # Create a new temporary file to write to 191259054Sdteske local __tmpfile 192259054Sdteske if f_eval_catch -dk __tmpfile $__funcname mktemp \ 193259054Sdteske 'mktemp -t "%s"' "$pgm" 194259054Sdteske then 195257795Sdteske # Write the temporary file contents 196257795Sdteske echo "$__sqlite_digest" > "$__tmpfile" 197257795Sdteske debug= f_getvar "$__var_to_set" >> "$__tmpfile" 198257795Sdteske 199257795Sdteske # Finally, move the temporary file into place 200257795Sdteske case "$PACKAGES_INDEX_CACHEFILE" in 201267680Sdteske */*) f_eval_catch -d $__funcname mkdir \ 202259054Sdteske 'mkdir -p "%s"' "${PACKAGES_INDEX_CACHEFILE%/*}" 203257795Sdteske esac 204259054Sdteske f_eval_catch -d $__funcname mv 'mv -f "%s" "%s"' \ 205259054Sdteske "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE" 206257795Sdteske fi 207257795Sdteske 208250323Sdteske if ! f_index_read "$__var_to_set"; then 209257795Sdteske f_show_err "$msg_io_or_format_error_on_index_file" 210250323Sdteske return $FAILURE 211250323Sdteske fi 212250323Sdteske 213250323Sdteske _INDEX_INITTED=1 214250323Sdteske return $SUCCESS 215250323Sdteske} 216250323Sdteske 217250323Sdteske# f_index_read [$var_to_get] 218250323Sdteske# 219250323Sdteske# Process the INDEX file (contents contained in $var_to_get) and... 220250323Sdteske# 221250323Sdteske# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 222250323Sdteske# 2. For convenience, create $_npkgs holding the total number of all packages 223250323Sdteske# 3. extract associative categories for each package into $_categories_$varpkg 224250323Sdteske# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 225250323Sdteske# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 226250323Sdteske# 6. create $_npkgs_$varcat holding the total number of packages in category 227250323Sdteske# 228250323Sdteske# NOTE: $varpkg is the product of f_str2varname $package varpkg 229250323Sdteske# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 230250323Sdteske# NOTE: We only show categories for which there are at least one package. 231250323Sdteske# NOTE: $varcat is the product of f_str2varname $category varcat 232250323Sdteske# 233250323Sdteskef_index_read() 234250323Sdteske{ 235250323Sdteske local var_to_get="${1:-PACKAGE_INDEX}" 236250323Sdteske 237250323Sdteske # Export variables required by awk(1) below 238250323Sdteske export msg_no_description_provided 239250323Sdteske export msg_all msg_all_desc 240250323Sdteske export VALID_VARNAME_CHARS 241250538Sdteske export msg_packages 242250323Sdteske 243250323Sdteske eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 244273068Sdteske function _asorti(src, dest) 245250323Sdteske { 246273067Sdteske k = nitems = 0 247273067Sdteske 248250323Sdteske # Copy src indices to dest and calculate array length 249273067Sdteske for (i in src) dest[++nitems] = i 250250323Sdteske 251250323Sdteske # Sort the array of indices (dest) using insertion sort method 252250323Sdteske for (i = 1; i <= nitems; k = i++) 253250323Sdteske { 254250323Sdteske idx = dest[i] 255250323Sdteske while ((k > 0) && (dest[k] > idx)) 256250323Sdteske { 257250323Sdteske dest[k+1] = dest[k] 258250323Sdteske k-- 259250323Sdteske } 260250323Sdteske dest[k+1] = idx 261250323Sdteske } 262250323Sdteske 263250323Sdteske return nitems 264250323Sdteske } 265250323Sdteske function print_category(category, npkgs, desc) 266250323Sdteske { 267250323Sdteske cat = category 268250323Sdteske # Accent the category if the first page has been 269250323Sdteske # cached (also acting as a visitation indicator) 270250323Sdteske if ( ENVIRON["_index_page_" varcat "_1"] ) 271250323Sdteske cat = cat "*" 272250538Sdteske printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n", 273250323Sdteske cat, npkgs, desc 274250323Sdteske } 275250323Sdteske BEGIN { 276250323Sdteske valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 277250323Sdteske default_desc = ENVIRON["msg_no_description_provided"] 278250538Sdteske packages = ENVIRON["msg_packages"] 279250410Sdteske tpkgs = 0 280250323Sdteske prefix = "" 281250323Sdteske } 282250323Sdteske { 283250410Sdteske tpkgs++ 284250323Sdteske varpkg = $1 285250323Sdteske gsub("[^" valid_chars "]", "_", varpkg) 286250323Sdteske print "_categories_" varpkg "=\"" $7 "\"" 287250323Sdteske split($7, pkg_categories, /[[:space:]]+/) 288250323Sdteske for (pkg_category in pkg_categories) 289250323Sdteske categories[pkg_categories[pkg_category]]++ 290250323Sdteske print "_rundeps_" varpkg "=\"" $9 "\"" 291250323Sdteske } 292250323Sdteske END { 293250410Sdteske print "_npkgs=" tpkgs # For convenience, total package count 294250323Sdteske 295273068Sdteske n = _asorti(categories, categories_sorted) 296250323Sdteske 297250323Sdteske # Produce package counts for each category 298250323Sdteske for (i = 1; i <= n; i++) 299250323Sdteske { 300250323Sdteske cat = varcat = categories_sorted[i] 301250323Sdteske npkgs = categories[cat] 302250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 303250323Sdteske print "_npkgs_" varcat "=\"" npkgs "\"" 304250323Sdteske } 305250323Sdteske 306250323Sdteske # Create menu list and generate list of categories at same time 307250323Sdteske print "CATEGORY_MENU_LIST=\"" 308250410Sdteske print_category(ENVIRON["msg_all"], tpkgs, 309250323Sdteske ENVIRON["msg_all_desc"]) 310250323Sdteske category_list = "" 311250323Sdteske for (i = 1; i <= n; i++) 312250323Sdteske { 313250323Sdteske cat = varcat = categories_sorted[i] 314250323Sdteske npkgs = categories[cat] 315250323Sdteske cur_prefix = tolower(substr(cat, 1, 1)) 316250323Sdteske if ( prefix != cur_prefix ) 317250323Sdteske prefix = cur_prefix 318250323Sdteske else 319250323Sdteske cat = " " cat 320250323Sdteske gsub("[^" valid_chars "]", "_", varcat) 321250323Sdteske desc = ENVIRON["_category_" varcat] 322250323Sdteske if ( ! desc ) desc = default_desc 323250323Sdteske print_category(cat, npkgs, desc) 324250323Sdteske category_list = category_list " " cat 325250323Sdteske } 326250323Sdteske print "\"" 327250323Sdteske 328250323Sdteske # Produce the list of categories (calculated in above block) 329250323Sdteske sub(/^ /, "", category_list) 330250323Sdteske print "PACKAGE_CATEGORIES=\"" category_list "\"" 331250323Sdteske 332294866Sdteske }' | ( exec 2<&1; dpv -ko /dev/stderr >&$TERMINAL_STDOUT_PASSTHRU \ 333294866Sdteske -b "$DIALOG_BACKTITLE" -- "$msg_reading_package_index_data" 334294866Sdteske ) )" # End-Quote 335250323Sdteske} 336250323Sdteske 337250323Sdteske# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 338250323Sdteske# 339252738Sdteske# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 340252738Sdteske# NULL; but should not be missing) into a series of sequential variables 341250323Sdteske# corresponding to "pages" containing up to $pagesize packages. The package 342250323Sdteske# INDEX data must be contained in the variable $var_to_get. The extracted pages 343250323Sdteske# are stored in variables ${var_basename}_# -- where "#" is a the page number. 344250323Sdteske# If $category is set, only packages for that category are extracted. 345250323Sdteske# Otherwise, if $category is "All", missing, or NULL, all packages are 346250323Sdteske# extracted and no filtering is done. 347250323Sdteske# 348250323Sdteskef_index_extract_pages() 349250323Sdteske{ 350252738Sdteske local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3" 351250323Sdteske local category="$4" # Optional 352250323Sdteske 353250323Sdteske eval "$( 354250323Sdteske debug= f_getvar "$var_to_get" | awk -F'|' \ 355250323Sdteske -v cat="$category" \ 356250323Sdteske -v pagesize="$pagesize" \ 357250538Sdteske -v var_basename="$var_basename" \ 358250538Sdteske -v i18n_all="$msg_all" ' 359250323Sdteske BEGIN { n = page = 0 } 360250323Sdteske /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 361250323Sdteske { 362250538Sdteske if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \ 363250323Sdteske "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 364250323Sdteske starting_new_page = (n++ == (pagesize * page)) 365250323Sdteske if ( starting_new_page ) 366250323Sdteske printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 367250323Sdteske var_basename "_" ++page "='\''" 368250323Sdteske printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 369250323Sdteske } 370250323Sdteske END { if ( n > 0 ) print "'\''" }' 371250323Sdteske )" 372250323Sdteske} 373250323Sdteske 374252745Sdteske# f_index_search $var_to_get $name [$var_to_set] 375252745Sdteske# 376252753Sdteske# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 377252745Sdteske# NULL; but should not be missing) for $name, returning the first match. 378252745Sdteske# Matches are strict (not regular expressions) and must match the beginning 379252745Sdteske# portion of the package name to be considered a match. If $var_to_set is 380252745Sdteske# missing or NULL, output is sent to standard output. If a match is found, 381252745Sdteske# returns success; otherwise failure. 382252745Sdteske# 383252745Sdteskef_index_search() 384252745Sdteske{ 385252745Sdteske local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2" 386252745Sdteske local __var_to_set="$3" 387252745Sdteske 388252745Sdteske f_dprintf "f_index_search: Searching package data (in %s) for %s" \ 389252745Sdteske "$__var_to_get" "$__pkg_basename" 390252745Sdteske 391252745Sdteske local __pkg= 392252745Sdteske __pkg=$( debug= f_getvar "$__var_to_get" | 393252745Sdteske awk -F'|' -v basename="$__pkg_basename" ' 394252745Sdteske BEGIN { n = length(basename) } 395252745Sdteske substr($1, 0, n) == basename { print $1; exit } 396252745Sdteske ' ) 397252745Sdteske if [ ! "$__pkg" ]; then 398252745Sdteske f_dprintf "f_index_search: No packages matching %s found" \ 399252745Sdteske "$__pkg_basename" 400252745Sdteske return $FAILURE 401252745Sdteske fi 402252745Sdteske 403252745Sdteske f_dprintf "f_index_search: Found package %s" "$__pkg" 404252745Sdteske if [ "$__var_to_set" ]; then 405252745Sdteske setvar "$__var_to_set" "$__pkg" 406252745Sdteske else 407252745Sdteske echo "$__pkg" 408252745Sdteske fi 409252745Sdteske return $SUCCESS 410252745Sdteske} 411252745Sdteske 412250323Sdteske############################################################ MAIN 413250323Sdteske 414250323Sdteskef_dprintf "%s: Successfully loaded." packages/index.subr 415250323Sdteske 416250323Sdteskefi # ! $_PACKAGES_INDEX_SUBR 417