1if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1 2# 3# Copyright (c) 2013-2016 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# 28############################################################ INCLUDES 29 30BSDCFG_SHARE="/usr/share/bsdconfig" 31. $BSDCFG_SHARE/common.subr || exit 1 32f_dprintf "%s: loading includes..." packages/index.subr 33f_include $BSDCFG_SHARE/device.subr 34f_include $BSDCFG_SHARE/media/common.subr 35f_include $BSDCFG_SHARE/packages/musthavepkg.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-FreeBSD.sqlite" 50 51# 52# Default path to on-disk cache INDEX file 53# 54PACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache" 55 56############################################################ FUNCTIONS 57 58# f_index_initialize [$var_to_set] 59# 60# Read and initialize the global index. Returns success unless media cannot be 61# initialized for any reason (e.g. user cancels media selection dialog or an 62# error occurs). The index is sorted before being loaded into $var_to_set. 63# 64# NOTE: The index is processed with f_index_read() [below] after being loaded. 65# 66f_index_initialize() 67{ 68 local __funcname=f_index_initialize 69 local __var_to_set="${1:-PACKAGE_INDEX}" 70 71 [ "$_INDEX_INITTED" ] && return $SUCCESS 72 73 # Got any media? 74 f_media_verify || return $FAILURE 75 76 # Make sure we have a usable pkg(8) with $PKG_ABI 77 f_musthavepkg_init 78 79 # Does it move when you kick it? 80 f_device_init device_media || return $FAILURE 81 82 f_show_info "$msg_attempting_to_update_repository_catalogue" 83 84 # 85 # Generate $PACKAGESITE variable for pkg(8) based on media type 86 # 87 local __type __data __site 88 device_media get type __type 89 device_media get private __data 90 case "$__type" in 91 $DEVICE_TYPE_DIRECTORY) 92 __site="file://$__data/packages/$PKG_ABI" ;; 93 $DEVICE_TYPE_HTTP) 94 f_getvar $VAR_HTTP_PATH __site 95 __site="$__site/$PKG_ABI/latest" ;; 96 $DEVICE_TYPE_HTTP_PROXY) 97 f_getvar $VAR_HTTP_PROXY_PATH __site 98 __site="$__site/packages/$PKG_ABI" ;; 99 $DEVICE_TYPE_CDROM) 100 __site="file://$MOUNTPOINT/packages/$PKG_ABI" 101 export REPOS_DIR="$MOUNTPOINT/packages/repos" ;; 102 *) # UFS, DISK, CDROM, USB, DOS, NFS, etc. 103 __site="file://$MOUNTPOINT/packages/$PKG_ABI" 104 esac 105 106 f_dprintf "PACKAGESITE=[%s]" "$__site" 107 if ! f_eval_catch $__funcname pkg \ 108 'PACKAGESITE="%s" pkg update' "$__site" 109 then 110 f_show_err "$msg_unable_to_update_pkg_from_selected_media" 111 f_device_shutdown device_media 112 return $FAILURE 113 fi 114 115 # 116 # Try to get contents from validated on-disk cache 117 # 118 119 # 120 # Calculate digest used to determine if the on-disk persistent cache 121 # INDEX (containing this digest on the first line) is valid and can be 122 # used to quickly populate the environment. 123 # 124 local __sqlite_digest 125 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then 126 f_show_err "$msg_no_pkg_database_found" 127 f_device_shutdown device_media 128 return $FAILURE 129 fi 130 131 # 132 # Check to see if the persistent cache INDEX file exists 133 # 134 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then 135 # 136 # Attempt to populate the environment with the (soon to be) 137 # validated on-disk cache. If validation fails, fall-back to 138 # generating a fresh cache. 139 # 140 if eval $__var_to_set='$( 141 ( # Get digest as the first word on first line 142 read digest rest_ignored 143 144 # 145 # If the stored digest matches the calculated- 146 # one populate the environment from the on-disk 147 # cache and provide success exit status. 148 # 149 if [ "$digest" = "$__sqlite_digest" ]; then 150 cat 151 exit $SUCCESS 152 else 153 # Otherwise, return the current value 154 eval echo \"\$__var_to_set\" 155 exit $FAILURE 156 fi 157 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null 158 )'; then 159 if ! f_index_read "$__var_to_set"; then 160 f_show_err \ 161 "$msg_io_or_format_error_on_index_file" 162 return $FAILURE 163 fi 164 _INDEX_INITTED=1 165 return $SUCCESS 166 fi 167 # Otherwise, fall-thru to create a fresh cache from scratch 168 fi 169 170 # 171 # If we reach this point, we need to generate the data from scratch 172 # 173 174 eval "$__var_to_set"='$( pkg rquery -I | ( 175 exec 2<&1; dpv -ko /dev/stderr >&$TERMINAL_STDOUT_PASSTHRU \ 176 -b "$DIALOG_BACKTITLE" \ 177 -- "$msg_generating_index_from_pkg_database" 178 ) | sort )' 179 180 # 181 # Attempt to create the persistent on-disk cache 182 # 183 184 # Create a new temporary file to write to 185 local __tmpfile 186 if f_eval_catch -dk __tmpfile $__funcname mktemp \ 187 'mktemp -t "%s"' "$pgm" 188 then 189 # Write the temporary file contents 190 echo "$__sqlite_digest" > "$__tmpfile" 191 debug= f_getvar "$__var_to_set" >> "$__tmpfile" 192 193 # Finally, move the temporary file into place 194 case "$PACKAGES_INDEX_CACHEFILE" in 195 */*) f_eval_catch -d $__funcname mkdir \ 196 'mkdir -p "%s"' "${PACKAGES_INDEX_CACHEFILE%/*}" 197 esac 198 f_eval_catch -d $__funcname mv 'mv -f "%s" "%s"' \ 199 "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE" 200 fi 201 202 if ! f_index_read "$__var_to_set"; then 203 f_show_err "$msg_io_or_format_error_on_index_file" 204 return $FAILURE 205 fi 206 207 _INDEX_INITTED=1 208 return $SUCCESS 209} 210 211# f_index_read [$var_to_get] 212# 213# Process the INDEX file (contents contained in $var_to_get) and... 214# 215# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts 216# 2. For convenience, create $_npkgs holding the total number of all packages 217# 3. extract associative categories for each package into $_categories_$varpkg 218# 4. extract runtime dependencies for each package into $_rundeps_$varpkg 219# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES 220# 6. create $_npkgs_$varcat holding the total number of packages in category 221# 222# NOTE: $varpkg is the product of f_str2varname $package varpkg 223# NOTE: $package is the name as it appears in the INDEX (no archive suffix) 224# NOTE: We only show categories for which there are at least one package. 225# NOTE: $varcat is the product of f_str2varname $category varcat 226# 227f_index_read() 228{ 229 local var_to_get="${1:-PACKAGE_INDEX}" 230 231 # Export variables required by awk(1) below 232 export msg_no_description_provided 233 export msg_all msg_all_desc 234 export VALID_VARNAME_CHARS 235 export msg_packages 236 237 eval "$( debug= f_getvar "$var_to_get" | awk -F'|' ' 238 function _asorti(src, dest) 239 { 240 k = nitems = 0 241 242 # Copy src indices to dest and calculate array length 243 for (i in src) dest[++nitems] = i 244 245 # Sort the array of indices (dest) using insertion sort method 246 for (i = 1; i <= nitems; k = i++) 247 { 248 idx = dest[i] 249 while ((k > 0) && (dest[k] > idx)) 250 { 251 dest[k+1] = dest[k] 252 k-- 253 } 254 dest[k+1] = idx 255 } 256 257 return nitems 258 } 259 function print_category(category, npkgs, desc) 260 { 261 cat = category 262 # Accent the category if the first page has been 263 # cached (also acting as a visitation indicator) 264 if ( ENVIRON["_index_page_" varcat "_1"] ) 265 cat = cat "*" 266 printf "'\''%s'\'' '\''%s " packages "'\'' '\''%s'\''\n", 267 cat, npkgs, desc 268 } 269 BEGIN { 270 valid_chars = ENVIRON["VALID_VARNAME_CHARS"] 271 default_desc = ENVIRON["msg_no_description_provided"] 272 packages = ENVIRON["msg_packages"] 273 tpkgs = 0 274 prefix = "" 275 } 276 { 277 tpkgs++ 278 varpkg = $1 279 gsub("[^" valid_chars "]", "_", varpkg) 280 print "_categories_" varpkg "=\"" $7 "\"" 281 split($7, pkg_categories, /[[:space:]]+/) 282 for (pkg_category in pkg_categories) 283 categories[pkg_categories[pkg_category]]++ 284 print "_rundeps_" varpkg "=\"" $9 "\"" 285 } 286 END { 287 print "_npkgs=" tpkgs # For convenience, total package count 288 289 n = _asorti(categories, categories_sorted) 290 291 # Produce package counts for each category 292 for (i = 1; i <= n; i++) 293 { 294 cat = varcat = categories_sorted[i] 295 npkgs = categories[cat] 296 gsub("[^" valid_chars "]", "_", varcat) 297 print "_npkgs_" varcat "=\"" npkgs "\"" 298 } 299 300 # Create menu list and generate list of categories at same time 301 print "CATEGORY_MENU_LIST=\"" 302 print_category(ENVIRON["msg_all"], tpkgs, 303 ENVIRON["msg_all_desc"]) 304 category_list = "" 305 for (i = 1; i <= n; i++) 306 { 307 cat = varcat = categories_sorted[i] 308 npkgs = categories[cat] 309 cur_prefix = tolower(substr(cat, 1, 1)) 310 if ( prefix != cur_prefix ) 311 prefix = cur_prefix 312 else 313 cat = " " cat 314 gsub("[^" valid_chars "]", "_", varcat) 315 desc = ENVIRON["_category_" varcat] 316 if ( ! desc ) desc = default_desc 317 print_category(cat, npkgs, desc) 318 category_list = category_list " " cat 319 } 320 print "\"" 321 322 # Produce the list of categories (calculated in above block) 323 sub(/^ /, "", category_list) 324 print "PACKAGE_CATEGORIES=\"" category_list "\"" 325 326 }' | ( exec 2<&1; dpv -ko /dev/stderr >&$TERMINAL_STDOUT_PASSTHRU \ 327 -b "$DIALOG_BACKTITLE" -- "$msg_reading_package_index_data" 328 ) )" # End-Quote 329} 330 331# f_index_extract_pages $var_to_get $var_basename $pagesize [$category] 332# 333# Extracts the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 334# NULL; but should not be missing) into a series of sequential variables 335# corresponding to "pages" containing up to $pagesize packages. The package 336# INDEX data must be contained in the variable $var_to_get. The extracted pages 337# are stored in variables ${var_basename}_# -- where "#" is a the page number. 338# If $category is set, only packages for that category are extracted. 339# Otherwise, if $category is "All", missing, or NULL, all packages are 340# extracted and no filtering is done. 341# 342f_index_extract_pages() 343{ 344 local var_to_get="${1:-PACKAGE_INDEX}" var_basename="$2" pagesize="$3" 345 local category="$4" # Optional 346 347 eval "$( 348 debug= f_getvar "$var_to_get" | awk -F'|' \ 349 -v cat="$category" \ 350 -v pagesize="$pagesize" \ 351 -v var_basename="$var_basename" \ 352 -v i18n_all="$msg_all" ' 353 BEGIN { n = page = 0 } 354 /'\''/{ gsub(/'\''/, "'\''\\'\'\''") } 355 { 356 if ( cat !~ "(^$|^" i18n_all "$)" && $7 !~ \ 357 "(^|[[:space:]])" cat "([[:space:]]|$)" ) next 358 starting_new_page = (n++ == (pagesize * page)) 359 if ( starting_new_page ) 360 printf "%s%s", ( n > 1 ? "'\''\n" : "" ), 361 var_basename "_" ++page "='\''" 362 printf "%s%s", ( starting_new_page ? "" : "\n" ), $0 363 } 364 END { if ( n > 0 ) print "'\''" }' 365 )" 366} 367 368# f_index_search $var_to_get $name [$var_to_set] 369# 370# Search the package INDEX ($PACKAGE_INDEX by default if/when $var_to_get is 371# NULL; but should not be missing) for $name, returning the first match. 372# Matches are strict (not regular expressions) and must match the beginning 373# portion of the package name to be considered a match. If $var_to_set is 374# missing or NULL, output is sent to standard output. If a match is found, 375# returns success; otherwise failure. 376# 377f_index_search() 378{ 379 local __var_to_get="${1:-PACKAGE_INDEX}" __pkg_basename="$2" 380 local __var_to_set="$3" 381 382 f_dprintf "f_index_search: Searching package data (in %s) for %s" \ 383 "$__var_to_get" "$__pkg_basename" 384 385 local __pkg= 386 __pkg=$( debug= f_getvar "$__var_to_get" | 387 awk -F'|' -v basename="$__pkg_basename" ' 388 BEGIN { n = length(basename) } 389 substr($1, 0, n) == basename { print $1; exit } 390 ' ) 391 if [ ! "$__pkg" ]; then 392 f_dprintf "f_index_search: No packages matching %s found" \ 393 "$__pkg_basename" 394 return $FAILURE 395 fi 396 397 f_dprintf "f_index_search: Found package %s" "$__pkg" 398 if [ "$__var_to_set" ]; then 399 setvar "$__var_to_set" "$__pkg" 400 else 401 echo "$__pkg" 402 fi 403 return $SUCCESS 404} 405 406############################################################ MAIN 407 408f_dprintf "%s: Successfully loaded." packages/index.subr 409 410fi # ! $_PACKAGES_INDEX_SUBR 411