2 # ==============================================================================
3 # portsreinstall library script
4 # - Wrappers for hiding version differences in the Ports/Packages system -
5 # Copyright (C) 2013-2022 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_DELETE='pkg delete' # Corresponding command for pkg_delete
12 PKGSYS_AVR_REFETCH_TIMES_PER_SITE=1 # Average number (integer) of retrials for retrieving package or distfiles per mirror site
13 PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR=2 # Number (integer) of retrials for check sum error in retrieving a package
15 # ============= Get the time stamp of the ports tree =============
16 pkgsys_get_timestamp_portstree ()
18 stat -t %s -f %m "${PORTS_INDEX_DB}" 2> /dev/null || :
21 # ============= Update the ports tree =============
22 pkgsys_update_portstree ()
24 if [ -e "$PORTSNAP_WORKDIR/INDEX" ]
30 if [ $opt_batch_mode = no ]
36 portsnap fetch $mode > $stdout
39 # ============= Check implementation of the ports tree =============
40 pkgsys_chk_ports_tree_implementation ()
42 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk" || :
43 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Makefile" || :
44 if [ ! -d "${PORTSDIR}" ]
46 message_echo "ERROR: Ports directory ${PORTSDIR} is not found." >&2
49 if [ ! -e "${PORTSDIR}/Makefile" -o ! -e "${PORTSDIR}/Mk/bsd.port.mk" ]
51 message_echo "ERROR: Ports tree ${PORTSDIR} is missing, broken or incompatible." >&2
56 # ============= System defined value for the ports/packages =============
61 tmp_work=${TMPDIR}/pkgsys_sysvar__work
64 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk"
65 make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var" 2> /dev/null
68 # ============= Get the file name of package check sum file =============
69 pkgsys_pkgchksum_file ()
74 # ============= Get the origin of the currently used pkg(8) =============
75 # NOTE: Assumed to be unflavored.
76 pkgsys_portsmgmt_pkg ()
78 local origin_unflavored
79 if [ ! -e "${DBDIR}/PKG_ORIGIN" ]
81 origin_unflavored=`pkgsys_sysvar PKG_ORIGIN` || :
82 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/pkg
83 echo "$origin_unflavored" > ${DBDIR}/PKG_ORIGIN
85 cat "${DBDIR}/PKG_ORIGIN"
88 # ============= Get the origin of the currently used dialog4ports(1) =============
89 # NOTE: Assumed to be unflavored.
90 pkgsys_portsmgmt_dialog4ports ()
92 local origin_unflavored
93 if [ ! -e "${DBDIR}/DIALOGPORT" ]
95 origin_unflavored=`pkgsys_sysvar DIALOGPORT` || :
96 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/dialog4ports
97 echo "$origin_unflavored" > ${DBDIR}/DIALOGPORT
99 cat "${DBDIR}/DIALOGPORT"
102 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
105 local origin origin_unflavored
107 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
108 case $origin_unflavored in
109 ports-mgmt/pkg | ports-mgmt/pkg-devel | ports-mgmt/dialog4ports | ports-mgmt/dialog4ports-static )
116 # ============= Check whether a port is indispensable for package operations =============
117 pkgsys_is_necessary_pkgtool ()
119 local origin origin_unflavored
121 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
122 [ x"$WITH_PKGNG" = x'yes' -a \( x"$origin_unflavored" = x'ports-mgmt/pkg' -o x"$origin_unflavored" = x'ports-mgmt/pkg-devel' \) ]
125 # ============= Get the extended regular expression pattern of ports for pkg(8) =============
126 pkgsys_pkgtools_ports_filter_regexp ()
128 echo '^ports-mgmt/(pkg|pkg-devel|pkg@.*|pkg-devel@.*)$'
131 # ============= Get the extended regular expression pattern of package names for pkg(8) =============
132 pkgsys_pkgtools_pkgs_filter_regexp ()
134 echo '^(pkg|pkg-devel)-[0-9]\.'
137 # ============= Check whether the dialog for selecting port options is dialog4ports =============
138 pkgsys_is_dialog4ports_used ()
140 [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
143 # ============= Get the number of mirror sites for legacy packages =============
144 pkgsys_num_mirrorsites ()
148 echo "$siteroots" | tr '|' '\n' | wc -l
151 # ============= Get a URL one of mirror sites =============
152 pkgsys_url_from_mirrors ()
154 local siteroots subdir nsites id_site site platform version
157 nsites=`pkgsys_num_mirrorsites "$siteroots"`
158 id_site=`(set +e; random -e $nsites; echo $?)`
159 site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
161 version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
163 printf "$subdir\n" "$platform" "$version"
166 # ============= Fetch a file from one of mirror sites =============
167 pkgsys_fetch_from_mirrors ()
169 local siteroots subdir filename dst tmp_work fetch itrial origdir url
174 tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
177 fetch=`pkgsys_sysvar FETCH_CMD`
178 itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
181 while [ $itrial -ge 1 ]
183 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
184 message_echo "INFO: Fetching from $url:"
185 $fetch "$url"&& break
187 itrial=$(($itrial-1))
190 [ -e "$tmp_work/$filename" ] || return
191 mv "$tmp_work/$filename" "$dst"
194 # ============= Get the package check sums file ready =============
195 pkgsys_ready_checksum_file ()
198 tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
199 rm -f "$tmp_savedpath"
200 chksumfile=`pkgsys_pkgchksum_file`
201 if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
203 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
204 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
205 "$chksumfile" "${DBDIR}/checksum"
207 message_echo "WARNING: No check sum file is available." >&2
211 echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
214 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
215 pkgsys_ready_checksum_file__fetched_file ()
217 cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
220 # ============= Fetch a legacy package from one of remote sites =============
221 pkgsys_fetch_legacy_remote ()
223 local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
225 tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
226 tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
229 pkg_regexp=`str_escape_regexp "$pkg"`
230 pkgsys_ready_checksum_file || return
231 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
232 validMD5=`env LANG=C grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
233 if [ -z "$validMD5" ]
235 message_echo "WARNING: No check sum for $pkg.tbz." >&2
239 mkdir -p "${PKGREPOSITORY}"
240 if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
242 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
244 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
245 [ "x$fetchedMD5" = "x$validMD5" ] || rm "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
247 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
249 ln -f "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz" "${PKGREPOSITORY}/$pkg.tbz"
251 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
252 if [ "x$fetchedMD5" = "x$validMD5" ]
256 mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
260 if [ $needs_fetch = yes ]
262 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
263 while [ $itrial -ge 1 ]
265 if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
266 "$pkg.tbz" "$tmp_work"
268 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
269 [ "x$fetchedMD5" = "x$validMD5" ] && break
270 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
272 itrial=$(($itrial-1))
274 [ $itrial -ge 1 ] || return
275 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
277 echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
280 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
281 pkgsys_fetch_legacy_remote__fetched_pkg ()
283 cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
286 # ============= Check whether the dependency of a legacy package is the latest =============
287 pkgsys_is_dependency_of_a_legacypkg_latest ()
289 local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
291 tmp_extract=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:extract
292 tmp_contents=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:contents
293 tmp_origin=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:origin
294 tmp_pkg=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:pkg
295 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
296 [ -e "$pkgarc" ] || return
297 rm -rf "$tmp_extract"
299 tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
300 env LANG=C grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
301 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
302 nlines=`wc -l < $tmp_contents`
304 while [ $iline -le $nlines ]
306 origin_req=`sed -n ${iline}p "$tmp_contents"`
307 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
309 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
310 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
312 message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
319 # ============= Check whether legacy package tools are available =============
320 pkgsys_is_legacy_tool_available ()
325 # ============= Load portsnap.conf(5) for important directories =============
326 pkgsys_load_portsnap_conf ()
330 PORTSNAP_WORKDIR=`sed -n '/^[[:space:]]*WORKDIR[[:space:]]*=[[:space:]]*\([^[:space:]#]*\)/s//\1/p' /etc/portsnap.conf || :`
331 PORTSNAP_WORKDIR=${PORTSNAP_WORKDIR:-/var/db/portsnap}
332 PORTSNAP_PORTSDIR=`sed -n '/^[[:space:]]*PORTSDIR[[:space:]]*=[[:space:]]*\([^[:space:]#]*\)/s//\1/p' /etc/portsnap.conf || :`
333 PORTSNAP_PORTSDIR=${PORTSNAP_PORTSDIR:-/usr/ports}
340 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
341 pkgsys_def_pkgtools ()
343 if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf -o \( -e "${DBDIR}/WITH_PKGNG" -a ! -e /etc/make.conf \) ]
345 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
347 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKG | tr '[:upper:]' '[:lower:]'`
348 [ -n "$PKGSYS_USE_PKGNG" ] || PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
349 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
351 echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
352 mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
355 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
358 export WITH_PKGNG=yes
359 PKGSYS_CMD_PKG_DELETE='pkg delete'
362 TMPDIR=/dev/null ASSUME_ALWAYS_YES=yes PACKAGESITE=file:///nonexistent \
363 pkg info -x 'pkg(-devel)?$' >/dev/null 2>&1
365 pkg_is_tool_available ()
367 local dev_out dev_err
368 if [ -x /usr/sbin/pkg ]
372 if [ $opt_batch_mode = yes ]
377 pkg_is_tool_ready && return
378 env ASSUME_ALWAYS_YES=yes pkg bootstrap -f > $dev_out 2> $dev_err
386 pkg info -qa 2> /dev/null
390 # pkg info -qoa 2> /dev/null
394 # pkg info -qox "$@" 2> /dev/null
398 # pkg info -qox "$@" 2> /dev/null
402 pkg info -qO "$@" 2> /dev/null
406 pkg info -qo "$@" 2> /dev/null
410 pkg info -qd "$@" 2> /dev/null
414 pkg info -qe "$@" 2> /dev/null
418 pkg info -qeO "$@" 2> /dev/null
422 pkg info -Eg "$@" 2> /dev/null
426 pkg info -qr "$@" 2> /dev/null
430 pkg info -Ex "$@" 2> /dev/null
434 pkg info -ql "$@" 2> /dev/null
438 # Return the total storage space occupied by the installed files in bytes
439 pkg info -qs "$@" 2> /dev/null | sed 's/KiB$/*1024/;s/MiB$/*1024^2/;s/GiB$/*1024^3/;s/B$//' | tr '\n' + | sed 's/+$//' | bc -l
442 # For an unflavored pattern, all matching flavors are returned.
444 local glob_unflavored
446 pkg query -g '%At\t%Av' "$glob_unflavored" 2> /dev/null | env LANG=C grep -E '^flavor[[:space:]]' | cut -f 2
448 pkg_info_flavored_origins ()
449 # For an unflavored pattern, all matching flavors are returned.
451 local glob_unflavored origin_unflavored
453 origin_unflavored=`pkg_info_qo "$glob_unflavored" 2> /dev/null || :`
454 flavors=`pkg_info_flavors "$glob_unflavored" 2> /dev/null || :`
457 echo "$origin_unflavored"
459 for flavor in $flavors
461 echo "$origin_unflavored@$flavor"
465 pkg_info_all_flavored_origins ()
467 local tmp_flavored tmp_flavored_ptn
468 tmp_flavored_ptn=${TMPDIR}/pkg_info_all_flavored_origins:flavored_ptn
469 pkg query '%o\t%At\t%Av' 2> /dev/null | env LANG=C grep -E '^[^[:space:]]+[[:space:]]flavor[[:space:]]' | cut -f 1,3 | tr '\t' @ | sed 's/@$//'
470 pkg query '%n\t%At' 2> /dev/null | env LANG=C grep -E '^[^[:space:]]+[[:space:]]flavor$' | cut -f 1 > $tmp_flavored_ptn
471 pkg query '%n\t%o' 2> /dev/null | env LANG=C grep -vFx -f "$tmp_flavored_ptn" | cut -f 2
475 local pkg tmp_stdout tmp_stderr
477 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
478 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
479 pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
481 env LANG=C grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
482 env LANG=C grep '^[^:]*: missing file ' "$tmp_stderr" | sed 's/^[^:]*: missing file //' || :
483 if env LANG=C grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr" # For the old specification of pkg(8)
485 pkg info -ql "$pkg" 2> /dev/null | while read filepath
487 [ -e "$filepath" ] || echo "$filepath"
496 pkg which -q "$filepath" 2> /dev/null
498 pkg_info_gen_pkg_origin_table ()
501 # pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
502 for pkgname in `pkg info -qa 2> /dev/null`
504 for origin in `pkg_info_flavored_origins "$pkgname"`
506 printf '%s\t%s\n' "$pkgname" "$origin"
516 local opt_del opt_quit dev_out dev_err
518 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
522 if [ $opt_batch_mode = yes ]
528 pkg delete -fqy $opt_del "$@" > $dev_out 2> $dev_err || :
529 pkg_is_tool_ready || return 0 # If pkg(8) is deinstalled successfully
530 pkg info -e $opt_quit "$@" > /dev/null 2>&1 || return 0
531 pkg delete -fy $opt_del "$@" > $dev_out 2> $dev_err # Countermeasure for a bug found for pkg-1.3.4 (at least not until 1.2.7_4)
535 local pkgarc tmp_extract prefix prefix_parent pkg opt_quit dev_out dev_err
537 tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
541 if [ $opt_batch_mode = yes ]
547 rm -rf "$tmp_extract"
549 tar xf "$pkgarc" -C "$tmp_extract" > $dev_out 2> $dev_err
550 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
551 if env ASSUME_ALWAYS_YES=YES $tmp_extract/usr/local/sbin/pkg-static add $opt_quit "$pkgarc" > $dev_out 2> $dev_err
553 message_echo "INFO: $pkg is successfully registered."
555 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
561 local option pkgarc pkg pkg_tool pkg_gen opt_add opt_quit dev_out dev_err
568 pkg=`basename "$pkgarc"`
569 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
573 pkg_gen="$pkg_gen $pkgarc"
576 [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
579 rm -rf "${TMPDIR}/pkg_add_f"
580 mkdir -p "${TMPDIR}/pkg_add_f"
582 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
586 if [ $opt_batch_mode = yes ]
592 ( cd "${TMPDIR}/pkg_add_f" && ln -s $pkg_gen && env ASSUME_ALWAYS_YES=YES pkg add -M $option $opt_quit $opt_add * > $dev_out 2> $dev_err )
597 pkg_add_f_common '' "$@"
601 pkg_add_f_common '-f' "$@"
603 pkg_inst_verify_pkg ()
607 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
608 tar tf "$pkgarc" > /dev/null 2>&1
610 pkg_inst_remote_fetch ()
615 [ $opt_batch_mode = yes ] && opt_quit='-q'
616 pkg fetch -yU $opt_quit "$pkg"
618 pkg_inst_remote_verify_fetch ()
622 pkg_inst_verify_pkg "$pkg" && return
623 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
627 local pkg pkgarc opt_add opt_quit dev_out dev_err
629 pkg_inst_remote_verify_fetch "$pkg" || return
630 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
631 rm -rf "${TMPDIR}/pkg_inst_remote"
632 mkdir -p "${TMPDIR}/pkg_inst_remote"
634 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
638 if [ $opt_batch_mode = yes ]
644 ( cd "${TMPDIR}/pkg_inst_remote" && ln -s "$pkgarc" && env ASSUME_ALWAYS_YES=YES pkg add -fM $opt_quit $opt_add * > $dev_out 2> $dev_err )
646 pkg_inst_wild_verify_pkg ()
650 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
651 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
653 pkg_inst_remote_wild_fetch ()
657 if pkg_is_tool_available
659 pkg_inst_remote "$pkg" && return
661 pkgsys_is_legacy_tool_available || return
662 message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
663 pkgsys_fetch_legacy_remote "$pkg"
665 pkg_inst_remote_wild_verify_fetch ()
669 pkg_inst_wild_verify_pkg "$pkg" && return
670 pkg_inst_remote_wild_fetch "$pkg" && pkg_inst_wild_verify_pkg "$pkg"
672 pkg_inst_remote_wild ()
674 local pkg pkgarc opt_add dev_out dev_err
676 if pkg_inst_remote_wild_verify_fetch "$pkg"
678 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
679 rm -rf "${TMPDIR}/pkg_inst_remote_wild"
680 mkdir -p "${TMPDIR}/pkg_inst_remote_wild"
682 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
685 if [ $opt_batch_mode = yes ]
690 ( cd "${TMPDIR}/pkg_inst_remote_wild" && ln -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err) || return
691 message_echo "INFO: Trying to convert the installed legacy package to pkgng."
692 pkg2ng > $dev_out 2> $dev_err || :
693 message_echo "INFO: Checking whether the conversion is successful."
694 pkg info -qe "$pkg" 2> /dev/null
697 pkg_get_remote_repository_version ()
699 local origin origin_unflavored pkg
701 origin_unflavored=`expr "$origin" : '\([^@]*\)'` || return
702 [ -n "$origin_unflavored" ] || return
703 pkg fetch -qyU "$origin_unflavored" || return
704 pkg=`pkg rquery -U %n-%v "$origin_unflavored"` || return
707 pkg_get_pkgs_timestamps ()
709 pkg query '%n-%v\tng-epoch:%t' "$@" 2> /dev/null
711 pkg_get_pkg_timestamp ()
715 pkg query '%t' "$pkg" 2> /dev/null
720 # Deafult configuration for pkg(1)
721 PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
722 PKGNG_SRV_MIRRORS=YES
723 PKGNG_PKG_DBDIR=/var/db/pkg
724 PKGNG_PKG_CACHEDIR=/var/cache/pkg
725 PKGNG_PORTSDIR=/usr/ports
726 PKGNG_PUBKEY=/etc/ssl/pkg.conf
727 PKGNG_HANDLE_RC_SCRIPTS=NO
728 PKGNG_PKG_MULTIREPOS=NO
729 PKGNG_ASSUME_ALWAYS_YES=NO
733 PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
734 # Load configuration for pkg(1)
735 pkg_conf=`pkg query %Fp pkg 2> /dev/null | env LANG=C grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
736 [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
737 env LANG=C grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
738 | env LANG=C grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
741 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
742 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
743 eval PKGNG_$var=\$val
744 misc_get_all_vardefs | env LANG=C grep "^PKGNG_$var="
745 done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
746 . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
750 local origin_portsmgmt packagepath checksumpath pkgname is_successful dev_out dev_err
751 origin_portsmgmt=`pkgsys_portsmgmt_pkg`
752 pkg_is_tool_ready && return
753 packagepath=`pkgsys_get_backup_pkg "$origin_portsmgmt"` && \
754 pkg_add_tools "$packagepath" && return
755 pkg_is_tool_available && return
756 message_echo "WARNING: WITH_PKG or WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
757 pkgsys_ready_checksum_file || return
758 message_echo "INFO: Installing pkgng by legacy package tool."
759 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
760 pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
761 | env LANG=C grep -m 1 '^pkg-[0-9]'` || :
762 [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" && return
763 message_echo "INFO: Failed by package, so installing pkgng by port."
764 env LANG=C grep -Ev '^[[:space:]]*(WITH_PKG|WITH_PKGNG)=' /etc/make.conf > ${TMPDIR}/make.conf 2> /dev/null || :
765 echo WITHOUT_PKG=yes >> ${TMPDIR}/make.conf
766 echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
769 if [ $opt_batch_mode = yes ]
779 message_echo "INFO: Attempting deinstallation of pkg(8) to make sure."
780 portsmgmt_port_path=`pkgsys_get_portpath_from_origin "$origin_portsmgmt"`
781 fs_fix_unionfs_image_if_hidden "${TMPDIR}/make.conf"
782 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" deinstall > $dev_out 2> $dev_err || :
783 message_echo "INFO: Attempting (re)installation by pkg(8)."
784 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" reinstall clean > $dev_out 2> $dev_err
786 pkg2ng > $dev_out 2> $dev_err || :
787 pkg_is_tool_available
790 pkg_update_pkgrepository ()
793 pkg_rescue_tools || return 0
794 if [ $opt_batch_mode = no ]
802 if ! pkg_rescue_tools
804 message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
807 elif ! pkgsys_is_legacy_tool_available
809 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
815 PKGSYS_CMD_PKG_DELETE='pkg_delete'
818 pkgsys_is_legacy_tool_available
820 pkg_is_tool_available ()
822 pkgsys_is_legacy_tool_available
826 pkg_info -Ea 2> /dev/null
830 # pkg_info -qoa 2> /dev/null
834 # pkg_info -qox "$@" 2> /dev/null
838 # pkg_info -qoX "$@" 2> /dev/null
842 pkg_info -qO "$@" 2> /dev/null
846 pkg_info -qo "$@" 2> /dev/null
850 pkg_info -qr "$@" 2> /dev/null | sed -n 's/^@pkgdep[[:space:]]*//p'
854 pkg_info -qe "$@" 2> /dev/null
858 [ `pkg_info -qO "$@" 2> /dev/null | wc -l` -gt 0 ]
862 pkg_info -E "$@" 2> /dev/null
866 pkg_info -qR "$@" 2> /dev/null | env LANG=C grep -v '^$'
870 pkg_info -Ex "$@" 2> /dev/null
874 pkg_info -qL "$@" 2> /dev/null
878 # Return the total storage space occupied by the installed files in bytes
879 pkg_info -qs "$@" 2> /dev/null | sed 's/[^0-9]*/*1024/' | tr '\n' + | sed 's/+$//' | bc -l
883 pkg_info -qe "$@" 2> /dev/null && echo
886 pkg_info_flavored_origins ()
888 local glob_unflavored tmp_stdout
890 pkg_info_qo "$glob_unflavored" 2> /dev/null
892 pkg_info_all_flavored_origins ()
894 pkg_info_qoa 2> /dev/null
898 local pkg tmp_stdout tmp_stderr
900 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
901 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
902 pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
903 sed -n 's/ fails the original MD5 checksum$//p' "$tmp_stdout" 2> /dev/null || :
904 sed -nE "s/^pkg_info: (.*) doesn't exist$/\1/p" "$tmp_stderr" 2> /dev/null || :
910 pkg_info -qW "$filepath" 2> /dev/null
912 pkg_info_gen_pkg_origin_table ()
914 pkg_info -aE 2> /dev/null | while read pkg
916 origin=`pkg_info -qo "$pkg" 2> /dev/null`
917 printf '%s\t%s\n' "$pkg" "$origin"
922 local dev_out dev_err
925 if [ $opt_batch_mode = yes ]
930 pkg_create -b "$@" > $dev_out 2> $dev_err
934 local opt_del dev_out dev_err
936 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
939 if [ $opt_batch_mode = yes ]
944 pkg_delete -f $opt_del "$@" > $dev_out 2> $dev_err
948 local opt_add dev_out dev_err
949 rm -rf "${TMPDIR}/pkg_add_f"
950 mkdir -p "${TMPDIR}/pkg_add_f"
951 ln -s "$@" "${TMPDIR}/pkg_add_f"
953 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
956 if [ $opt_batch_mode = yes ]
961 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -if $opt_add * > $dev_out 2> $dev_err )
965 local opt_add dev_out dev_err
966 rm -rf "${TMPDIR}/pkg_add_f"
967 mkdir -p "${TMPDIR}/pkg_add_f"
968 ln -s "$@" "${TMPDIR}/pkg_add_f"
970 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
973 if [ $opt_batch_mode = yes ]
978 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
980 pkg_inst_verify_pkg ()
984 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg` || return
985 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
987 pkg_inst_remote_fetch ()
991 pkgsys_fetch_legacy_remote "$pkg"
993 pkg_inst_remote_verify_fetch ()
997 pkg_inst_verify_pkg "$pkg" && return
998 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
1002 local pkg pkgarc opt_add dev_out dev_err
1004 if pkg_inst_remote_verify_fetch "$pkg"
1006 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
1007 rm -rf "${TMPDIR}/pkg_inst_remote"
1008 mkdir -p "${TMPDIR}/pkg_inst_remote"
1009 ln -s "$@" "${TMPDIR}/pkg_add_f"
1011 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
1014 if [ $opt_batch_mode = yes ]
1019 ( cd "${TMPDIR}/pkg_inst_remote" && ls -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
1022 pkg_inst_wild_verify_pkg ()
1024 pkg_inst_verify_pkg "$@"
1026 pkg_inst_remote_wild_fetch ()
1028 pkg_inst_remote_fetch "$@"
1030 pkg_inst_remote_wild_verify_fetch ()
1032 pkg_inst_remote_verify_fetch "$@"
1034 pkg_inst_remote_wild ()
1036 pkg_inst_remote "$@"
1038 pkg_get_remote_repository_version ()
1040 local origin checksumpath version_regexp pkgbase_regexp pkg
1042 pkgsys_ready_checksum_file > /dev/null || return
1043 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
1044 version_regexp=`database_build_make "$origin" -V PORTVERSION | str_escape_regexp_filter`
1045 pkgbase_regexp=`database_build_get_new_pkgname "$origin" | sed -E "s/$version_regexp$//" | str_escape_regexp_filter`
1046 pkg=`sed -nE "s/^MD5[[:space:]]*\(($pkgbase_regexp.*)\.tbz\)[[:space:]]*=.*/\1/p" "$checksumpath" | head -n 1`
1049 pkg_get_pkgs_timestamps ()
1051 stat -t %s -f $'legacy:%m\t%N' ${PKG_DBDIR} 2> /dev/null || :
1053 pkg_get_pkg_timestamp ()
1057 portdb=`echo "$pkg" | sed 's/-[0-9].*//'`
1058 stat -t %s -f %m "${PKG_DBDIR}/$portdb" 2> /dev/null || :
1060 pkg_loadconf () { :; }
1061 pkg_rescue_tools () { :; }
1062 pkg_update_pkgrepository () { :; }
1066 # ============= Get the unflavored part of a flavored origin =============
1067 pkgsys_get_unflavored_origin ()
1069 local origin origin_unflavored
1071 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
1072 echo "$origin_unflavored"
1075 # ============= Get the port path from a flavored origin =============
1076 pkgsys_get_portpath_from_origin ()
1078 local origin origin_unflavored
1080 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1081 echo "${PORTSDIR}/$origin_unflavored"
1084 # ============= Get the flavor from a flavored origin =============
1085 pkgsys_get_flavor_from_origin ()
1089 flavor=`echo "$origin" | sed -E 's/^[^@]*@?//'`
1093 # ============= Compose a flavored origin name =============
1094 pkgsys_compose_flavored_origin ()
1099 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1100 [ -z "$flavor" ] || flavor=@$flavor
1101 echo "$origin_unflavored$flavor"
1104 # ============= Check existence of a port for a flavored origin =============
1105 pkgsys_exists_port ()
1107 local origin port_path
1109 port_path=`pkgsys_get_portpath_from_origin "$origin"`
1113 # ============= Get the installed package name from a flavored origin =============
1114 pkgsys_get_installed_pkg_from_origin ()
1116 local origin origin_unflavored flavor_origin flavor_pkg
1118 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1119 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1120 if [ -n "$flavor_origin" ]
1122 for pkgname in `pkg_info_qO "$origin_unflavored" 2> /dev/null`
1124 for flavor_pkg in `pkg_info_flavors "$pkgname"`
1126 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1134 pkg_info_qO "$origin_unflavored" 2> /dev/null
1139 # ============= Get the installed package name from glob patterns =============
1140 pkgsys_get_installed_pkg_from_glob ()
1145 [ -z "$glob" ] && continue
1146 if regexp=`expr "$glob" : ':\(.*\)'`
1148 pkg_info_Ex "$regexp"
1155 # ============= Check existence of an installed package for a flavored origin =============
1156 pkgsys_exists_from_orig ()
1158 local origin origin_unflavored flavor_origin flavor_pkg
1160 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1161 pkg_info_eO "$origin_unflavored" 2> /dev/null || return
1162 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1163 for flavor_pkg in `pkg_info_flavors "$origin_unflavored"`
1165 [ "x$flavor_origin" = "x$flavor_pkg" ] && return
1170 # ============= Generate the package names vs origins table at the initial state =============
1171 pkgsys_gen_init_pkg_origin_table ()
1173 [ -e "${DBDIR}/installed_ports:pkg_vs_origin.tbl" ] && return
1174 pkg_info_gen_pkg_origin_table > ${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp || :
1175 mv "${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp" "${DBDIR}/installed_ports:pkg_vs_origin.tbl"
1178 # ============= Check existence of initially or currently installed package for a flavored origin =============
1179 pkgsys_exists_or_existed_from_orig ()
1183 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1184 | env LANG=C grep -q -Fx "$origin" || pkgsys_exists_from_orig "$origin"
1187 # ============= Get the name of an initially installed package for a flavored origin =============
1188 pkgsys_get_init_pkg_from_orig ()
1190 local origin tmppkg origin_regexp npkgs origin_unflavored flavor_origin flavor_pkg
1192 tmppkg=${TMPDIR}/pkgsys_get_init_pkg_from_orig::pkg
1193 origin_regexp=`str_escape_regexp "$origin"`
1194 sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1195 | cut -f 1 > $tmppkg || :
1196 npkgs=`wc -l < $tmppkg`
1201 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1202 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1203 for pkgname in `pkg_info_qO "$origin_unflavored" 2> /dev/null`
1205 for flavor_pkg in `pkg_info_flavors "$pkgname"`
1207 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1218 # ============= Get the package name of this utility =============
1219 pkgsys_get_my_current_pkg ()
1221 pkg_info_Ex "${PROGRAM}-[0-9].*"
1224 # ============= Get the flavored origin of this utility =============
1225 pkgsys_get_my_origin ()
1228 for pkgname in `pkgsys_get_my_current_pkg`
1230 pkg_info_flavored_origins "$pkgname"
1234 # ============= Get the flavored origin of an initially installed package by ambiguous matching =============
1235 pkgsys_init_pkg_orig_by_ambiguous_matching ()
1237 local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
1239 tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
1240 pkg_info_flavored_origins "$pkg" > $tmporigin
1241 [ `wc -l < $tmporigin` -gt 0 ] && { cat "$tmporigin"; return; }
1244 len_pkg=`echo -n "$pkg" | wc -c`
1245 if [ $len_pkg -gt 0 ]
1249 pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
1250 env LANG=C grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1251 | cut -f 2 > $tmporigin
1252 norigins=`wc -l < $tmporigin`
1253 [ $norigins -gt 0 ] && break
1254 ambsuffix='[a-zA-Z0-9.,_+-]*'
1255 len_pkg=$(($len_pkg-1))
1256 [ $len_pkg -gt 0 ] || break
1257 pkg=`echo -n "$pkg" | head -c $len_pkg`
1260 [ $norigins -eq 1 ] || return
1264 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
1265 pkgsys_show_pkg_updating_commands ()
1267 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1269 if which -s pkg_updating
1271 echo 'pkg-updating(8) or pkg_updating(1)'
1273 echo 'pkg-updating(8)'
1275 elif which -s pkg_updating
1277 echo 'pkg_updating(1)'
1281 # ============= Evaluation of flavored ports globs =============
1282 pkgsys_eval_ports_glob ()
1284 local pkglist unflavored_origlist tmp_flavors
1285 pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
1286 unflavored_origlist=${DBDIR}/pkgsys_eval_ports_glob:origin_unflavored.lst
1287 tmp_flavors=${TMPDIR}/pkgsys_eval_ports_glob:flavors
1288 # Test the access privilege as the superuser or not
1289 if [ ! -r "$pkglist" ]
1291 if touch "$pkglist" 2>/dev/null
1295 pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
1298 if [ ! -r "$unflavored_origlist" ]
1300 if touch "$unflavored_origlist" 2>/dev/null
1302 rm "$unflavored_origlist"
1304 unflavored_origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
1307 # Preparation of customized databases
1309 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
1310 [ -f "$unflavored_origlist" ] \
1311 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
1312 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $unflavored_origlist
1318 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
1319 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
1320 if expr "$glob" : '.*/' > /dev/null 2>&1
1322 if expr "$glob" : '.*@' > /dev/null 2>&1
1324 glob_regexp_unflavored=`expr "$glob_regexp" : '\([^@]*\)' || :`$
1325 glob_regexp_flavor=^`expr "$glob_regexp" : '[^@]*@\([^@]*\)' || :`
1327 env LANG=C grep -E "$glob_regexp_unflavored" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1329 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1330 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1331 tr '[:space:]' '\n' | env LANG=C grep -v '^$' | env LANG=C grep -E "$glob_regexp_flavor" | sed -E "s|^|$origin_unflavored@|"
1334 pkg_info_all_flavored_origins
1335 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1336 } | env LANG=C grep -E "$glob_regexp" 2>&1 || :
1338 env LANG=C grep -E "$glob_regexp" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1340 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1341 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1342 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1343 tr '[:space:]' '\n' | env LANG=C grep -v '^$' > $tmp_flavors || :
1344 if [ `wc -l < $tmp_flavors` -gt 0 ]
1346 sed -E "s|^|$origin_unflavored_rpl@|" "$tmp_flavors"
1348 echo "$origin_unflavored"
1352 glob_regexp_allflavors=`echo "$glob_regexp" | sed 's/$$/@.*$/'`
1354 pkg_info_all_flavored_origins
1355 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1356 } | env LANG=C grep -E -e "$glob_regexp" -e "$glob_regexp_allflavors" 2>&1 || :
1358 if expr "$glob" : '[a-z][a-zA-Z0-9_.+-]*[a-zA-Z0-9_.+]$' > /dev/null 2>&1 && \
1359 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
1361 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1 || :`'-[0-9]'
1363 glob_regexp2=$glob_regexp
1365 env LANG=C grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
1368 sed -n ${index}p "$unflavored_origlist"
1369 done | while read origin_unflavored
1371 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1372 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1373 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1374 tr '[:space:]' '\n' | env LANG=C grep -v '^$' > $tmp_flavors || :
1375 [ `wc -l < $tmp_flavors` -gt 0 ] || echo > $tmp_flavors
1376 sed -E "s/^/$origin_unflavored_rpl /" "$tmp_flavors"
1377 done | while read origin_unflavored flavor
1381 if make -C "${PORTSDIR}/$origin_unflavored" package-name FLAVOR=$flavor 2> /dev/null | \
1382 env LANG=C grep -qE "$glob_regexp2"
1384 echo "$origin_unflavored@$flavor"
1387 if make -C "${PORTSDIR}/$origin_unflavored" package-name 2> /dev/null | \
1388 env LANG=C grep -qE "$glob_regexp2"
1390 echo "$origin_unflavored"
1394 glob_regexp2=`echo "$glob_regexp" | sed -E 's/\$*$//' 2>&1 || :`'[[:space:]]'
1395 env LANG=C grep -E "$glob_regexp2" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1397 for pkgname in `pkg_info_Ex "$glob_regexp"`
1399 pkg_info_flavored_origins "$pkgname"
1405 # ============= Create a back-up package archive =============
1406 pkgsys_create_backup_pkg ()
1408 local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath
1411 rm -rf "${TMPDIR}"/package.tmp
1412 mkdir "${TMPDIR}"/package.tmp
1413 for origin in `pkg_info_flavored_origins "$pkgname"`
1415 if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` && \
1416 [ ! -e "${DBDIR}/requires/$origin/installed_timestamp" -o \
1417 "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
1419 echo "$backup_pkg_old"
1422 if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
1424 pkgname_ptn=`str_escape_regexp "$pkgname"`
1425 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
1426 env LANG=C grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar|pkg)$"` || :
1428 if [ -z "$backup_pkg" ]
1430 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
1433 dbpath=${DBDIR}/backup/$origin
1435 pkg_info_qL "$pkgname" > $dbpath/previously_installed_files
1437 mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
1438 pkgpath=$dstdir/$backup_pkg
1439 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
1440 while read origin_cur pkgpath_cur
1442 [ "$pkgpath_cur" = "$pkgpath" ] && continue
1443 if [ "$origin_cur" = "$origin" ]
1445 rm -f "$pkgpath_cur"
1447 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
1449 done > ${DBDIR}/backup_pkgarcs.lst.tmp
1450 printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
1451 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1456 # ============= Delete a back-up package archive for a flavored port origin =============
1457 pkgsys_delete_backup_pkg ()
1459 local origin origin_regexp
1461 origin_regexp=`str_escape_regexp "$origin"`
1462 env LANG=C grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1463 | cut -f 2 | while read pkgpath_cur
1465 rm -f "$pkgpath_cur"
1467 env LANG=C grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
1468 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
1469 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1472 # ============= Get an existing package archive path for a flavored port origin =============
1473 pkgsys_get_backup_pkg ()
1475 local origin tmpnewest origin_regexp
1477 tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
1478 origin_regexp=`str_escape_regexp "$origin"`
1480 env LANG=C grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1481 | cut -f 2 | while read pkgpath
1483 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
1484 [ -e "$pkgpath" ] || continue
1485 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
1486 echo "$pkgpath" > $tmpnewest
1488 cat "$tmpnewest" 2> /dev/null
1491 # ============= Get a file list to be restored by the current backup package for a flavored port origin =============
1492 pkgsys_get_restored_files_by_backup_pkg ()
1496 cat "${DBDIR}/backup/$origin/previously_installed_files" 2> /dev/null || :
1499 # ============= Check whether any file match restored files by the current backup package for a flavored port origin =============
1500 pkgsys_chk_match_to_restored_files_by_backup_pkg ()
1502 local origin filelist dbfile
1505 dbfile=${DBDIR}/backup/$origin/previously_installed_files
1506 env LANG=C grep -qFx -f "$filelist" "$dbfile" 2> /dev/null
1509 # ============= Get a package name from a package archive file name =============
1510 pkgsys_pkgarc_to_pkgname ()
1514 basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar|pkg)$//'
1517 # ============= Get the file name of an existing package archive for a package name =============
1518 pkgsys_pkgname_to_pkgarc ()
1520 local pkgdir pkgname pkgname_ptn pkgnode
1523 [ -n "$pkgname" ] || return 1
1524 [ -d "$pkgdir" ] || return 1
1525 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1527 pkgname_ptn=`str_escape_regexp "$pkgname"`
1528 pkgnode=`ls "$pkgdir" 2> /dev/null | env LANG=C grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar|pkg)$"` || :
1529 elif [ -e "$pkgdir/$pkgname.tbz" ]
1531 pkgnode=$pkgname.tbz
1533 [ -n "$pkgnode" ] || return 1
1534 echo "$pkgdir/$pkgnode"
1537 # ============= Get flavored port origins matching a glob pattern even if nonexistent =============
1538 pkgsys_eval_ports_glob_even_if_nonexistent ()
1543 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
1544 echo "$glob_pattern" | env LANG=C grep -E '^[a-z]+/([a-zA-Z0-9_.+-]+|[a-zA-Z0-9_.+-]+@[a-zA-Z0-9_.+-]+)$' || :
1545 } | env LANG=C grep -v -e '^$' | sort -u
1548 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
1549 pkgsys_register_evaluated_globs ()
1551 local mode listpath dirpath tmp_evaluated
1555 dirpath=`dirname "$listpath"`
1556 tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
1557 echo "$@" | sed -E 's/[ :]+/\
1558 /g' | env LANG=C grep -v '^$' | sort -u | while read -r glob
1560 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
1561 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
1563 message_echo "WARNING: No matching ports/package glob [$glob]." >&2
1566 cat "$tmp_evaluated"
1567 done | while read origin
1571 remove ) fileedit_rm_a_line "$origin" "$listpath"
1573 add ) fileedit_add_a_line_if_new "$origin" "$listpath"
1579 # ============= Evaluate glob patterns for installed packages =============
1580 pkgsys_eval_installed_pkgs_globs ()
1583 tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
1584 rm -f "$tmp_evaluated"
1585 pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
1586 [ -e "$tmp_evaluated" ] || return 0
1589 pkgsys_exists_or_existed_from_orig "$origin" || echo "$origin"
1590 done < $tmp_evaluated
1593 # ============= Get glob patterns of conflicting packages of a port =============
1594 pkgsys_get_conflicting_pkgs_patterns ()
1596 local mode origin conflicts conflicts_makevar conflicts_config
1599 conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
1602 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
1603 conflicts_config=`database_query_get_config_val "$origin" BUILDCONFLICT`
1606 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
1607 conflicts_config=`database_query_get_config_val "$origin" INSTCONFLICT`
1614 echo "$conflicts $conflicts_makevar $conflicts_config" | tr ' ' '\n' | env LANG=C grep -v '^$' | sort -u
1617 # ============= Get conflicting installed packages of a port =============
1618 pkgsys_get_conflicting_installed_pkgs ()
1620 local mode origin tmp_conflicts
1623 tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
1624 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" | while read pkg_pattern
1626 pkgsys_get_installed_pkg_from_glob "$pkg_pattern" || :
1627 done > $tmp_conflicts
1628 cat "$tmp_conflicts"
1629 [ `wc -l < $tmp_conflicts` -gt 0 ]
1632 # ============= Check whether a package conflicts with a port =============
1633 pkgsys_chk_conflict_by_a_pkg ()
1635 local mode origin pkg tmp_conflicts_ptn
1639 tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
1640 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
1641 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
1642 echo "$pkg" | env LANG=C grep -q -E -f "$tmp_conflicts_ptn"
1645 # ============= Check whether installed files are lost or broken for a package =============
1646 pkgsys_sanitychk_pkgcontents ()
1648 local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1650 var_is_reinstall_encouraged=$2
1651 tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1652 pkg_check_sanity "$pkg" > $tmp_sanity || :
1653 eval "$var_is_reinstall_encouraged=no"
1654 [ `wc -l < $tmp_sanity` -eq 0 ] && return
1655 nlines=`wc -l < $tmp_sanity`
1657 _is_reinstall_encouraged=no
1658 while [ $iline -le $nlines ]
1660 filename=`sed -n ${iline}p "$tmp_sanity"`
1662 if [ ! -e "$filename" ]
1664 _is_reinstall_encouraged=yes
1667 if expr "$filename" : '.*/include/.*' > /dev/null
1669 _is_reinstall_encouraged=yes
1672 filename_esc=`str_escape_regexp "$filename"`
1673 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | env LANG=C grep -q '^ELF '
1675 _is_reinstall_encouraged=yes
1678 pkg_owner=`pkg_which "$filename"`
1679 if [ "$pkg" != "$pkg_owner" ]
1681 _is_reinstall_encouraged=yes
1685 eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1686 for origin in `pkg_info_flavored_origins "$pkg"`
1688 if [ $opt_batch_mode = no ]
1690 message_echo "[$pkg ($origin)]"
1691 sed 's/^/ /' "$tmp_sanity"
1694 pkg_replace=`str_escape_replaceval "$pkg"`
1695 origin_replace=`str_escape_replaceval "$origin"`
1696 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1702 # ============= Check whether the port options database is once saved =============
1703 pkgsys_exists_saved_port_options_timestamps ()
1705 [ -d "${DBDIR}/ls_dbdir" ]
1708 # ============= Get the current all timestamp information of port options =============
1709 pkgsys_get_current_port_options_timestamp ()
1711 local portdb_needle_regexp
1712 portdb_needle_regexp=`str_escape_regexp "$1"`
1714 ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}" | if [ -n "$portdb_needle_regexp" ]
1716 env LANG=C grep -E "[[:space:]]$portdb_needle_regexp$" || :
1720 } 2> /dev/null | cut -w -f 6,7 | while read datetime portdb
1722 [ -d "${PORT_DBDIR}/$portdb" ] || continue
1723 if [ -e "${PORT_DBDIR}/$portdb/options" ]
1725 datetime=`ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}/$portdb/options" 2> /dev/null | cut -w -f 6`
1727 printf '%s\t%s\n' "$portdb" "$datetime"
1731 # ============= Get the saved all timestamp information of port options =============
1732 pkgsys_get_saved_port_options_timestamps_all ()
1734 mkdir -p "${DBDIR}/ls_dbdir"
1735 cat "${DBDIR}/ls_dbdir/"*.log 2> /dev/null || :
1738 # ============= Convert a list of port origins to port options timestamp log names =============
1739 pkgsys_conv_portorigin_to_port_option_timestamp_logname ()
1744 # ============= Get the file name of the port options database of a port =============
1745 pkgsys_get_port_option_database ()
1749 if pkgsys_is_dialog4ports_used
1751 echo "$origin" | sed 's|/|_|;s/@.*//'
1753 database_build_make "$origin" -V UNIQUENAME
1757 # ============= Save the timestamp information of port options of a port =============
1758 pkgsys_save_port_option_timestamp ()
1760 local origin portoptlog portoptdb
1762 portoptlog=`echo "$origin" | pkgsys_conv_portorigin_to_port_option_timestamp_logname`
1763 portoptdb=`pkgsys_get_port_option_database "$origin"`
1764 mkdir -p "${DBDIR}/ls_dbdir"
1765 pkgsys_get_current_port_options_timestamp "$portoptdb" > ${DBDIR}/ls_dbdir/$portoptlog.log 2> /dev/null || :
1768 # ============= Get changed port options from the saved point =============
1769 pkgsys_get_changed_port_options ()
1771 local saved_log current_log tmp_log
1774 tmp_log=${TMPDIR}/pkgsys_get_changed_port_options.log
1776 env LANG=C grep -vxF -f "$current_log" "$saved_log" || :
1777 env LANG=C grep -vxF -f "$saved_log" "$current_log" || :
1778 } | cut -w -f 1 | env LANG=C grep -v '^$' | sort -u > $tmp_log
1779 if pkgsys_is_dialog4ports_used
1781 env LANG=C grep '_' "$tmp_log" || :
1787 # ============= Convert a list of port options database names to port globs =============
1788 pkgsys_conv_portoptiondbs_to_globs ()
1790 if pkgsys_is_dialog4ports_used
1798 # ============= Register nonexistent port options databases =============
1799 pkgsys_register_list_nonexistent_portopriondb ()
1802 dbpath=${DBDIR}/ls_dbdir/%NONEXISTENT%.log
1803 mkdir -p "${DBDIR}/ls_dbdir"
1806 sort -u "$dbpath.new" "$dbpath" > $dbpath.tmp
1807 mv "$dbpath.tmp" "$dbpath"
1810 # ============= Order a list of installed packages by storage space occupied by the installed files =============
1811 pkgsys_order_pkgs_by_size ()
1815 size=`pkg_info_qs "$pkg" || :`
1816 [ -n "$size" ] && echo "$size"$'\t'"$pkg"
1817 done | sort -g | cut -f 2