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