2 # ==============================================================================
3 # portsreinstall library script
4 # - Wrappers for hiding version differences in the Ports/Packages system -
5 # Copyright (C) 2013 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
9 # ============= Variables =============
10 PKGSYS_USE_PKGNG=yes # no: legacy pkg_* tools, yes: the new generation package (pkgng)
11 PKGSYS_CMD_PKG_INFO='pkg info' # Corresponding command for pkg_info
12 PKGSYS_CMD_PKG_CREATE='pkg create' # Corresponding command for pkg_create
13 PKGSYS_CMD_PKG_DELETE='pkg delete' # Corresponding command for pkg_delete
14 PKGSYS_CMD_PKG_ADD='pkg add' # Corresponding command for pkg_add
15 PKGSYS_AVR_REFETCH_TIMES_PER_SITE=1 # Average number (integer) of retrials for retrieving package or distfiles per mirror site
16 PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR=2 # Number (integer) of retrials for check sum error in retrieving a package
18 # ============= System defined value for the ports/packages =============
23 tmp_work=${TMPDIR}/pkgsys_sysvar:work
26 make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var" 2> /dev/null
29 # ============= Get the file name of package check sum file =============
30 pkgsys_pkgchksum_file ()
35 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
39 ports-mgmt/pkg|ports-mgmt/dialog4ports)
45 # ============= Check whether the dialog for selecting port options is dialog4ports =============
46 pkgsys_is_dialog4ports_used ()
48 [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
51 # ============= Get the number of mirror sites for legacy packages =============
52 pkgsys_num_mirrorsites ()
56 echo "$siteroots" | tr '|' '\n' | wc -l
59 # ============= Get a URL one of mirror sites =============
60 pkgsys_url_from_mirrors ()
62 local siteroots subdir nsites id_site site platform version
65 nsites=`pkgsys_num_mirrorsites "$siteroots"`
66 id_site=`(set +e; random -e $nsites; echo $?)`
67 site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
69 version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
71 printf "$subdir\n" "$platform" "$version"
74 # ============= Fetch a file from one of mirror sites =============
75 pkgsys_fetch_from_mirrors ()
77 local siteroots subdir filename dst tmp_work fetch itrial origdir url
82 tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
85 fetch=`pkgsys_sysvar FETCH_CMD`
86 itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
89 while [ $itrial -ge 1 ]
91 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
92 message_echo "INFO: Fetching from $url:"
98 [ -e "$tmp_work/$filename" ] || return
99 mv "$tmp_work/$filename" "$dst"
102 # ============= Get the package check sums file ready =============
103 pkgsys_ready_checksum_file ()
106 tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
107 rm -f "$tmp_savedpath"
108 chksumfile=`pkgsys_pkgchksum_file`
109 if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
111 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
112 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
113 "$chksumfile" "${DBDIR}/checksum"
115 message_echo "WARNING: No check sum file is available." >&2
119 echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
122 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
123 pkgsys_ready_checksum_file__fetched_file ()
125 cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
128 # ============= Fetch a legacy package from one of remote sites =============
129 pkgsys_fetch_legacy_remote ()
131 local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
133 tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
134 tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
137 pkg_regexp=`str_escape_regexp "$pkg"`
138 pkgsys_ready_checksum_file || return
139 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
140 validMD5=`grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
141 if [ -z "$validMD5" ]
143 message_echo "WARNING: No check sum for $pkg.tbz." >&2
147 [ -d "${PKGREPOSITORY}" ] || mkdir -p "${PKGREPOSITORY}"
148 if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
150 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
152 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
153 [ "x$fetchedMD5" = "x$validMD5" ] || rm "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
155 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
157 ln -f "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz" "${PKGREPOSITORY}/$pkg.tbz"
159 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
160 if [ "x$fetchedMD5" = "x$validMD5" ]
164 mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
168 if [ $needs_fetch = yes ]
170 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
171 while [ $itrial -ge 1 ]
173 if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
174 "$pkg.tbz" "$tmp_work"
176 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
177 [ "x$fetchedMD5" = "x$validMD5" ] && break
178 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
180 itrial=$(($itrial-1))
182 [ $itrial -ge 1 ] || return
183 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
185 echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
188 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
189 pkgsys_fetch_legacy_remote__fetched_pkg ()
191 cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
194 # ============= Check whether the dependency of a legacy package is the latest =============
195 pkg_is_dependency_of_a_legacypkg_latest ()
197 local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
199 tmp_extract=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:extract
200 tmp_contents=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:contents
201 tmp_origin=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:origin
202 tmp_pkg=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:pkg
203 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
204 [ -e "$pkgarc" ] || return
205 rm -rf "$tmp_extract"
207 tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
208 grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
209 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
210 nlines=`wc -l < $tmp_contents`
212 while [ $iline -le $nlines ]
214 origin_req=`sed -n ${iline}p "$tmp_contents"`
215 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
217 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
218 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
220 message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
227 # ============= Check whether legacy package tools are available =============
228 pkgsys_is_legacy_tool_available ()
233 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
234 pkgsys_def_pkgtools ()
236 if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf -o \( -e "${DBDIR}/WITH_PKGNG" -a ! -e /etc/make.conf \) ]
238 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
240 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
241 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
243 echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
244 mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
247 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
249 export WITH_PKGNG=yes
250 PKGSYS_CMD_PKG_INFO='pkg info'
251 PKGSYS_CMD_PKG_CREATE='pkg create'
252 PKGSYS_CMD_PKG_DELETE='pkg delete'
253 PKGSYS_CMD_PKG_ADD='pkg add'
254 pkg_is_tool_available ()
256 if [ -x /usr/sbin/pkg ]
258 pkg -N 2> /dev/null && return
259 env ASSUME_ALWAYS_YES=yes pkg bootstrap -f
260 pkg_is_tool_available
262 which -s pkg && return
267 pkg info -qa 2> /dev/null
271 pkg info -qoa 2> /dev/null
275 # pkg info -qox "$@" 2> /dev/null
279 pkg info -qoX "$@" 2> /dev/null
283 pkg info -qO "$@" 2> /dev/null
287 pkg info -qo "$@" 2> /dev/null
291 pkg info -qd "$@" 2> /dev/null
295 pkg info -e "$@" 2> /dev/null
299 pkg info -eO "$1" 2> /dev/null
303 pkg info -Eg "$@" 2> /dev/null
307 pkg info -qr "$@" 2> /dev/null
311 pkg info -Ex "$@" 2> /dev/null
315 pkg info -ql "$@" 2> /dev/null
321 pkg check -s "$pkg" 2> /dev/null
327 pkg which -q "$filepath" 2> /dev/null
329 pkg_info_gen_pkg_origin_table ()
331 pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
343 local pkgarc tmp_extract prefix prefix_parent pkg
345 tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
346 rm -rf "$tmp_extract"
348 tar xf "$pkgarc" -C "$tmp_extract"
349 prefix=`grep -m 1 '^prefix: ' "$tmp_extract/+MANIFEST" | sed 's/^prefix: *//'`
350 prefix_parent=`dirname "$prefix"`
351 cp -Rp "$tmp_extract/$prefix" "$prefix_parent"/
352 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
353 message_echo "INFO: Contents of $pkg are temporarily installed by simple copy."
354 if env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
356 message_echo "INFO: $pkg is successfully registered."
358 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
363 local pkgarc pkg pkg_tool pkg_gen
368 pkg=`basename "$pkgarc"`
369 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
373 pkg_gen="$pkg_gen $pkgarc"
376 [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
377 [ -n "$pkg_gen" ] && env ASSUME_ALWAYS_YES=YES pkg add $pkg_gen
383 pkg_inst_remote_fetch ()
385 local pkg mode pkgarc
388 tmp_extract=${TMPDIR}/pkgng:pkg_inst_remote:extract
389 pkg fetch -yU "$pkg" || return
390 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
391 [ "x$mode" = xnodepschk ] && return
392 rm -rf "$tmp_extract"
394 tar xf "$pkgarc" -C "$tmp_extract" +MANIFEST
395 sed -E '1,/^deps:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*([^:]+):[[:space:]]*\{origin:[[:space:]]*([^,]+),[[:space:]]*version:[[:space:]]*([^}]+)\}/\2\\\1-\3/' "$tmp_extract/+MANIFEST" \
396 | tr '\\' '\t' | while read origin_req pkg_req
398 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
399 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
401 message_echo "WARNING: Requirements of remote package $pkg are not latest." >&2
409 local pkg mode pkgarc
412 pkg_inst_remote_fetch "$pkg" "$mode" || return
413 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
414 env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
416 pkg_inst_remote_wild_fetch ()
418 local pkg mode pkgarc
421 if pkg_is_tool_available
423 pkg_inst_remote "$pkg" "$mode" && return
425 pkgsys_is_legacy_tool_available || return
426 message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
427 pkgsys_fetch_legacy_remote "$pkg" || return
428 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
429 [ "x$mode" = xnodepschk ] && return
430 pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
432 pkg_inst_remote_wild ()
434 local pkg mode pkgarc
437 pkg_inst_remote_wild_fetch "$pkg" "$mode" || return
438 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
439 pkg_add -ifF "$pkgarc" || return
440 message_echo "INFO: Trying to convert the installed legacy package to pkgng."
442 message_echo "INFO: Checking whether the conversion is successful."
448 # Deafult configuration for pkg(1)
449 PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
450 PKGNG_SRV_MIRRORS=YES
451 PKGNG_PKG_DBDIR=/var/db/pkg
452 PKGNG_PKG_CACHEDIR=/var/cache/pkg
453 PKGNG_PORTSDIR=/usr/ports
454 PKGNG_PUBKEY=/etc/ssl/pkg.conf
455 PKGNG_HANDLE_RC_SCRIPTS=NO
456 PKGNG_PKG_MULTIREPOS=NO
457 PKGNG_ASSUME_ALWAYS_YES=NO
461 PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
462 # Load configuration for pkg(1)
463 pkg_conf=`pkg query %Fp pkg | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'`
464 grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
465 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
468 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
469 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
470 eval PKGNG_$var=\$val
471 misc_get_all_vardefs | grep -E "^PKGNG_$var="
472 done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
473 . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
477 local packagepath checksumpath pkgname is_successful
478 packagepath=`pkgsys_get_backup_pkg ports-mgmt/pkg` && \
479 pkg_add_tools "$packagepath" && return
480 pkg_is_tool_available && return
481 message_echo "WARNING: WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
482 pkgsys_ready_checksum_file || return
483 message_echo "INFO: Installing pkgng by legacy package tool."
484 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
485 pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
486 | grep -m 1 -E -e "^pkg-[0-9]"` || :
487 [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" nodepschk && return
488 message_echo "INFO: Failed by package, so installing pkgng by port."
489 grep -v '^[[:space:]]*WITH_PKGNG=' /etc/make.conf > ${TMPDIR}/make.conf
490 echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
495 message_echo "INFO: Attempting deinstallation of ports-mgmt/pkg to make sure."
496 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" deinstall || :
497 message_echo "INFO: Attempting (re)installation by ports-mgmt/pkg."
498 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" reinstall clean
501 pkg_is_tool_available
504 if ! pkg_rescue_tools
506 message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
509 elif ! pkgsys_is_legacy_tool_available
511 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
516 PKGSYS_CMD_PKG_INFO='pkg_info'
517 PKGSYS_CMD_PKG_CREATE='pkg_create'
518 PKGSYS_CMD_PKG_DELETE='pkg_delete'
519 PKGSYS_CMD_PKG_ADD='pkg_add'
520 pkg_is_tool_available ()
522 pkgsys_is_legacy_tool_available
526 pkg_info -Ea 2> /dev/null
530 pkg_info -qoa 2> /dev/null
534 # pkg_info -qox "$@" 2> /dev/null
538 pkg_info -qoX "$@" 2> /dev/null
542 pkg_info -qO "$@" 2> /dev/null
546 pkg_info -qo "$@" 2> /dev/null
550 pkg_info -qr "$@" | grep '^@pkgdep ' | sed 's/^@pkgdep[[:space:]]*//' 2> /dev/null
554 pkg_info -e "$@" 2> /dev/null
558 [ `pkg_info -qO "$1" 2> /dev/null | wc -l` -gt 0 ]
562 pkg_info -E "$@" 2> /dev/null
566 pkg_info -qR "$@" | grep -v '^$' 2> /dev/null
570 pkg_info -Ex "$@" 2> /dev/null
574 pkg_info -qL "$@" 2> /dev/null
580 pkg_info -qg "$pkg" 2> /dev/null
586 pkg_info -qW "$filepath" 2> /dev/null
588 pkg_info_gen_pkg_origin_table ()
590 pkg_info -aE 2> /dev/null | while read pkg
592 origin=`pkg_info -qo "$pkg" 2> /dev/null`
593 printf '%s\t%s\n' "$pkg" "$origin"
594 done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
612 pkg_inst_remote_fetch ()
614 local pkg mode pkgarc
617 pkgsys_fetch_legacy_remote "$pkg" || return
618 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
619 [ "x$mode" = xnodepschk ] && return
620 pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
624 local pkg mode pkgarc
627 pkg_inst_remote_fetch "$pkg" "$mode" || return
628 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
629 pkg_add "$pkgarc" || return
631 pkg_inst_remote_wild_fetch ()
633 pkg_inst_remote_fetch "$1" "$2"
635 pkg_inst_remote_wild ()
637 pkg_inst_remote "$1" "$2"
639 pkg_loadconf () { :; }
640 pkg_rescue_tools () { :; }
644 # ============= Check existence of initially or currently installed package for an origin =============
645 pkgsys_pkg_info_eO ()
647 local origin origin_regexp
649 origin_regexp=`str_escape_regexp "$origin"`
650 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
651 | grep -q -E "^$origin_regexp$" && return
652 pkg_info_eO "$origin"
655 # ============= Get the name of an initially installed package for an origin =============
656 pkgsys_pkg_info_qO_init ()
658 local origin tmppkg origin_regexp npkgs
660 tmppkg=${TMPDIR}/pkgsys_pkg_info_qO_init::pkg
661 origin_regexp=`str_escape_regexp "$origin"`
662 { sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
664 npkgs=`wc -l < $tmppkg`
669 pkg_info_qO "$origin"
673 # ============= Get the package name of this utility =============
674 pkgsys_get_my_current_pkg ()
676 pkg_info_Ex "${APPNAME}-[0-9].*"
679 # ============= Get the origin of this utility =============
680 pkgsys_get_my_origin ()
682 pkg_info_qo "`pkgsys_get_my_current_pkg`"
685 # ============= Get the origin of an initially installed package by ambiguous matching =============
686 pkgsys_init_pkg_orig_by_ambiguous_matching ()
688 local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
690 origin=`pkg_info_qo "$pkg" || :`
691 [ -n "$origin" ] && { echo "$origin"; return; }
692 tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
695 len_pkg=`echo -n "$pkg" | wc -c`
696 if [ $len_pkg -gt 0 ]
700 pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
701 grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
702 | cut -f 2 > $tmporigin
703 norigins=`wc -l < $tmporigin`
704 [ $norigins -gt 0 ] && break
705 ambsuffix='[a-zA-Z0-9.,_+-]*'
706 len_pkg=$(($len_pkg-1))
707 [ $len_pkg -gt 0 ] || break
708 pkg=`echo -n "$pkg" | head -c $len_pkg`
711 [ $norigins -eq 1 ] || return
715 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
716 pkgsys_show_pkg_updating_commands ()
718 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
720 if which -s pkg_updating
722 echo 'pkg-updating(8) or pkg_updating(1)'
724 echo 'pkg-updating(8)'
726 elif which -s pkg_updating
728 echo 'pkg_updating(1)'
732 # ============= Evaluation of ports globs =============
733 pkgsys_eval_ports_glob ()
735 local pkglist origlist
736 pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
737 origlist=${DBDIR}/pkgsys_eval_ports_glob:origin.lst
738 if [ ! -r "$pkglist" ]
740 if touch "$pkglist" 2>/dev/null
744 pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
747 if [ ! -r "$origlist" ]
749 if touch "$origlist" 2>/dev/null
753 origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
757 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
759 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
760 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $origlist
765 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
766 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
767 if expr "$glob" : '[^/][^/]*\/[^/][^/]*$' > /dev/null 2>&1
769 grep -E "$glob_regexp" "$origlist" 2>&1 || :
772 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
773 } | grep -E "$glob_regexp" 2>&1 || :
775 if expr "$glob" : '^[a-z][a-zA-Z0-9+-]*[a-zA-Z0-9+]$' > /dev/null 2>&1 && \
776 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
778 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'-[0-9]'
780 glob_regexp2=$glob_regexp
782 grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
785 sed -n ${index}p "$origlist"
787 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'[[:space:]]'
788 { sed -n -E "/$glob_regexp2/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
790 pkg_info_qoX "$glob_regexp" || :
795 # ============= Create a back-up package archive =============
796 pkgsys_create_backup_pkg ()
798 local pkgname dstdir origin backup_pkg_old origin_regexp pkgname_ptn backup_pkg pkgpath
801 rm -rf "${TMPDIR}"/package.tmp
802 mkdir "${TMPDIR}"/package.tmp
803 origin=`pkg_info_qo "$pkgname"`
804 if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` \
805 [ "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
807 echo "$backup_pkg_old"
810 if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
812 pkgname_ptn=`str_escape_regexp "$pkgname"`
813 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
814 grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
816 if [ -z "$backup_pkg" ]
818 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
821 [ -d "$dstdir" ] || mkdir -p "$dstdir"
822 mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
823 pkgpath=$dstdir/$backup_pkg
824 origin_regexp=`str_escape_regexp "$origin"`
825 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
826 while read origin_cur pkgpath_cur
828 [ "$pkgpath_cur" = "$pkgpath" ] && continue
829 if [ "$origin_cur" = "$origin" ]
833 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
835 done > ${DBDIR}/backup_pkgarcs.lst.tmp
836 printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
837 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
841 # ============= Delete a back-up package archive for a port origin =============
842 pkgsys_delete_backup_pkg ()
844 local origin origin_regexp
846 origin_regexp=`str_escape_regexp "$origin"`
847 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
848 | cut -f 2 | while read pkgpath_cur
852 grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
853 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
854 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
857 # ============= Get an existing package archive path for a port origin =============
858 pkgsys_get_backup_pkg ()
860 local origin origin_regexp
862 tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
863 origin_regexp=`str_escape_regexp "$origin"`
865 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
866 | cut -f 2 | while read pkgpath
868 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
869 [ -e "$pkgpath" ] || continue
870 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
871 echo "$pkgpath" > $tmpnewest
873 cat "$tmpnewest" 2> /dev/null
876 # ============= Get a package name from a package archive file name =============
877 pkgsys_pkgarc_to_pkgname ()
881 basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
884 # ============= Get the file name of an existing package archive for a package name =============
885 pkgsys_pkgname_to_pkgarc ()
887 local pkgdir pkgname pkgname_ptn pkgnode
890 [ -n "$pkgname" ] || return 1
891 [ -d "$pkgdir" ] || return 1
892 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
894 pkgname_ptn=`str_escape_regexp "$pkgname"`
895 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
896 elif [ -e "$pkgdir/$pkgname.tbz" ]
900 [ -n "$pkgnode" ] || return 1
901 echo "$pkgdir/$pkgnode"
904 # ============= Get port origins matching a glob pattern even if nonexistent =============
905 pkgsys_eval_ports_glob_even_if_nonexistent ()
910 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
911 echo "$glob_pattern" | grep '^[a-z][a-z]*/[a-zA-Z0-9_+-][a-zA-Z0-9_+-]*$' || :
912 } | grep -v -e '^$' | sort -u
915 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
916 pkgsys_register_evaluated_globs ()
918 local mode listpath dirpath tmp_evaluated
922 dirpath=`dirname "$listpath"`
923 tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
924 echo "$@" | sed -E 's/[ :]+/\
925 /g' | grep -v '^$' | sort -u | while read glob
927 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
928 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
930 message_echo "WARNING: No matching ports/package glob [$glob]." >&2
934 done | while read origin
936 [ -d "$dirpath" ] || mkdir -p "$dirpath"
938 remove) fileedit_rm_a_line "$origin" "$listpath";;
939 add) fileedit_add_a_line_if_new "$origin" "$listpath";;
944 # ============= Evaluate glob patterns for installed packages =============
945 pkgsys_eval_installed_pkgs_globs ()
948 tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
949 rm -f "$tmp_evaluated"
950 pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
951 [ -e "$tmp_evaluated" ] || return 0
954 pkgsys_pkg_info_eO "$origin" || echo "$origin"
955 done < $tmp_evaluated
958 # ============= Get glob patterns of conflicting packages of a port =============
959 pkgsys_get_conflicting_pkgs_patterns ()
961 local mode origin conflicts
964 conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
967 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
970 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
973 echo "$conflicts" | tr ' ' '\n' | grep -v '^$' || :
976 # ============= Get conflicting installed packages of a port =============
977 pkgsys_get_conflicting_installed_pkgs ()
979 local mode origin tmp_conflicts
982 tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
983 pkg_info_Eg `pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin"` > $tmp_conflicts || :
985 [ `wc -l < $tmp_conflicts` -gt 0 ]
988 # ============= Check whether a package conflicts with a port =============
989 pkgsys_chk_conflict_by_a_pkg ()
991 local mode origin pkg tmp_conflicts_ptn
995 tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
996 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
997 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
998 echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
1001 # ============= Check whether installed files are lost or broken for a package =============
1002 pkgsys_sanitychk_pkgcontents ()
1004 local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1006 var_is_reinstall_encouraged=$2
1007 tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1008 pkg_check_sanity "$pkg" > $tmp_sanity || :
1009 eval "$var_is_reinstall_encouraged=no"
1010 [ `wc -l < $tmp_sanity` -eq 0 ] && return
1011 nlines=`wc -l < $tmp_sanity`
1013 _is_reinstall_encouraged=no
1014 while [ $iline -le $nlines ]
1016 src=`sed -n ${iline}p "$tmp_sanity"`
1018 filename=`echo "$src" | cut -d ' ' -f 1`
1020 until [ -e "$filename" -o "$filename" = "$src" ]
1022 filename="$filename "`echo "$src" | cut -d ' ' -f $icol`
1024 [ -e "$filename" ] || continue
1025 if expr "$filename" : '.*/include/.*' > /dev/null
1027 _is_reinstall_encouraged=yes
1030 filename_esc=`str_escape_regexp "$filename"`
1031 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
1033 _is_reinstall_encouraged=yes
1036 pkg_owner=`pkg_which "$filename"`
1037 if [ "$pkg" != "$pkg_owner" ]
1039 _is_reinstall_encouraged=yes
1043 eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1044 origin=`pkg_info_qo "$pkg"`
1045 if [ $opt_batch_mode = no ]
1047 message_echo "[$pkg ($origin)]"
1048 sed 's/^/ /' "$tmp_sanity"
1051 pkg_replace=`str_escape_replaceval "$pkg"`
1052 origin_replace=`str_escape_replaceval "$origin"`
1053 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'