2 # ==============================================================================
3 # portsreinstall library script
4 # - Wrappers for hiding version differences in the Ports/Packages system -
5 # Copyright (C) 2013-2018 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)(|@.*)$'
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=`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 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
443 local glob_unflavored
445 pkg query -g '%At\t%Av' "$glob_unflavored" 2> /dev/null | grep -E '^flavor[[:space:]]' | cut -f 2
447 pkg_info_flavored_origin ()
449 local glob_unflavored origin_unflavored
451 origin_unflavored=`pkg_info_qo "$glob_unflavored" 2> /dev/null || :`
452 flavor=`pkg_info_flavor "$glob_unflavored" 2> /dev/null || :`
453 [ -z "$flavor" ] || flavor=@$flavor
454 echo "$origin_unflavored$flavor"
456 pkg_info_all_flavored_origins ()
458 local tmp_flavored tmp_flavored_ptn
459 tmp_flavored_ptn=${TMPDIR}/pkg_info_all_flavored_origins:flavored_ptn
460 pkg query '%o\t%At\t%Av' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor[[:space:]]' | cut -f 1,3 | tr '\t' @ | sed 's/@$//'
461 pkg query '%n\t%At' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor$' | cut -f 1 > $tmp_flavored_ptn
462 pkg query '%n\t%o' 2> /dev/null | grep -vFx -f "$tmp_flavored_ptn" | cut -f 2
466 local pkg tmp_stdout tmp_stderr
468 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
469 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
470 pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
472 grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
473 grep '^[^:]*: missing file ' "$tmp_stderr" | sed 's/^[^:]*: missing file //' || :
474 if grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr" # For the old specification of pkg(8)
476 pkg info -ql "$pkg" 2> /dev/null | while read filepath
478 [ -e "$filepath" ] || echo "$filepath"
487 pkg which -q "$filepath" 2> /dev/null
489 pkg_info_gen_pkg_origin_table ()
491 # pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
492 pkg info -qa 2> /dev/null | while read pkgname
494 origin=`pkg_info_flavored_origin "$pkgname"`
495 printf '%s\t%s\n' "$pkgname" "$origin"
504 local opt_del opt_quit dev_out dev_err
506 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
510 if [ $opt_batch_mode = yes ]
516 pkg delete -fqy $opt_del "$@" > $dev_out 2> $dev_err || :
517 pkg_is_tool_ready || return 0 # If pkg(8) is deinstalled successfully
518 pkg info -e $opt_quit "$@" > /dev/null 2>&1 || return 0
519 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)
523 local pkgarc tmp_extract prefix prefix_parent pkg opt_quit dev_out dev_err
525 tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
529 if [ $opt_batch_mode = yes ]
535 rm -rf "$tmp_extract"
537 tar xf "$pkgarc" -C "$tmp_extract" > $dev_out 2> $dev_err
538 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
539 if env ASSUME_ALWAYS_YES=YES $tmp_extract/usr/local/sbin/pkg-static add $opt_quit "$pkgarc" > $dev_out 2> $dev_err
541 message_echo "INFO: $pkg is successfully registered."
543 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
549 local option pkgarc pkg pkg_tool pkg_gen opt_add opt_quit dev_out dev_err
556 pkg=`basename "$pkgarc"`
557 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
561 pkg_gen="$pkg_gen $pkgarc"
564 [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
567 rm -rf "${TMPDIR}/pkg_add_f"
568 mkdir -p "${TMPDIR}/pkg_add_f"
570 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
574 if [ $opt_batch_mode = yes ]
580 ( 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 )
585 pkg_add_f_common '' "$@"
589 pkg_add_f_common '-f' "$@"
591 pkg_inst_verify_pkg ()
595 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
596 tar tf "$pkgarc" > /dev/null 2>&1
598 pkg_inst_remote_fetch ()
603 [ $opt_batch_mode = yes ] && opt_quit='-q'
604 pkg fetch -yU $opt_quit "$pkg"
606 pkg_inst_remote_verify_fetch ()
610 pkg_inst_verify_pkg "$pkg" && return
611 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
615 local pkg pkgarc opt_add opt_quit dev_out dev_err
617 pkg_inst_remote_verify_fetch "$pkg" || return
618 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
619 rm -rf "${TMPDIR}/pkg_inst_remote"
620 mkdir -p "${TMPDIR}/pkg_inst_remote"
622 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
626 if [ $opt_batch_mode = yes ]
632 ( 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 )
634 pkg_inst_wild_verify_pkg ()
638 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
639 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
641 pkg_inst_remote_wild_fetch ()
645 if pkg_is_tool_available
647 pkg_inst_remote "$pkg" && return
649 pkgsys_is_legacy_tool_available || return
650 message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
651 pkgsys_fetch_legacy_remote "$pkg"
653 pkg_inst_remote_wild_verify_fetch ()
657 pkg_inst_wild_verify_pkg "$pkg" && return
658 pkg_inst_remote_wild_fetch "$pkg" && pkg_inst_wild_verify_pkg "$pkg"
660 pkg_inst_remote_wild ()
662 local pkg pkgarc opt_add dev_out dev_err
664 if pkg_inst_remote_wild_verify_fetch "$pkg"
666 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
667 rm -rf "${TMPDIR}/pkg_inst_remote_wild"
668 mkdir -p "${TMPDIR}/pkg_inst_remote_wild"
670 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
673 if [ $opt_batch_mode = yes ]
678 ( cd "${TMPDIR}/pkg_inst_remote_wild" && ln -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err) || return
679 message_echo "INFO: Trying to convert the installed legacy package to pkgng."
680 pkg2ng > $dev_out 2> $dev_err || :
681 message_echo "INFO: Checking whether the conversion is successful."
682 pkg info -qe "$pkg" 2> /dev/null
685 pkg_get_remote_repository_version ()
687 local origin origin_unflavored pkg
689 origin_unflavored=`expr "$origin" : '\([^@]*\)'` || return
690 [ -n "$origin_unflavored" ] || return
691 pkg fetch -qyU "$origin_unflavored" || return
692 pkg=`pkg rquery -U %n-%v "$origin_unflavored"` || return
695 pkg_get_pkgs_timestamps ()
697 pkg query '%n-%v\tng-epoch:%t' "$@" 2> /dev/null
699 pkg_get_pkg_timestamp ()
703 pkg query '%t' "$pkg" 2> /dev/null
708 # Deafult configuration for pkg(1)
709 PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
710 PKGNG_SRV_MIRRORS=YES
711 PKGNG_PKG_DBDIR=/var/db/pkg
712 PKGNG_PKG_CACHEDIR=/var/cache/pkg
713 PKGNG_PORTSDIR=/usr/ports
714 PKGNG_PUBKEY=/etc/ssl/pkg.conf
715 PKGNG_HANDLE_RC_SCRIPTS=NO
716 PKGNG_PKG_MULTIREPOS=NO
717 PKGNG_ASSUME_ALWAYS_YES=NO
721 PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
722 # Load configuration for pkg(1)
723 pkg_conf=`pkg query %Fp pkg 2> /dev/null | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
724 [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
725 grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
726 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
729 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
730 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
731 eval PKGNG_$var=\$val
732 misc_get_all_vardefs | grep "^PKGNG_$var="
733 done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
734 . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
738 local origin_portsmgmt packagepath checksumpath pkgname is_successful dev_out dev_err
739 origin_portsmgmt=`pkgsys_portsmgmt_pkg`
740 pkg_is_tool_ready && return
741 packagepath=`pkgsys_get_backup_pkg "$origin_portsmgmt"` && \
742 pkg_add_tools "$packagepath" && return
743 pkg_is_tool_available && return
744 message_echo "WARNING: WITH_PKG or WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
745 pkgsys_ready_checksum_file || return
746 message_echo "INFO: Installing pkgng by legacy package tool."
747 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
748 pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
749 | grep -m 1 '^pkg-[0-9]'` || :
750 [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" && return
751 message_echo "INFO: Failed by package, so installing pkgng by port."
752 grep -Ev '^[[:space:]]*WITH_PKG(|NG)=' /etc/make.conf > ${TMPDIR}/make.conf 2> /dev/null || :
753 echo WITHOUT_PKG=yes >> ${TMPDIR}/make.conf
754 echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
757 if [ $opt_batch_mode = yes ]
767 message_echo "INFO: Attempting deinstallation of pkg(8) to make sure."
768 portsmgmt_port_path=`pkgsys_get_portpath_from_origin "$origin_portsmgmt"`
769 fs_fix_unionfs_image_if_hidden "${TMPDIR}/make.conf"
770 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" deinstall > $dev_out 2> $dev_err || :
771 message_echo "INFO: Attempting (re)installation by pkg(8)."
772 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" reinstall clean > $dev_out 2> $dev_err
774 pkg2ng > $dev_out 2> $dev_err || :
775 pkg_is_tool_available
778 pkg_update_pkgrepository ()
781 pkg_rescue_tools || return 0
782 if [ $opt_batch_mode = no ]
790 if ! pkg_rescue_tools
792 message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
795 elif ! pkgsys_is_legacy_tool_available
797 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
803 PKGSYS_CMD_PKG_DELETE='pkg_delete'
806 pkgsys_is_legacy_tool_available
808 pkg_is_tool_available ()
810 pkgsys_is_legacy_tool_available
814 pkg_info -Ea 2> /dev/null
818 # pkg_info -qoa 2> /dev/null
822 # pkg_info -qox "$@" 2> /dev/null
826 # pkg_info -qoX "$@" 2> /dev/null
830 pkg_info -qO "$@" 2> /dev/null
834 pkg_info -qo "$@" 2> /dev/null
838 pkg_info -qr "$@" 2> /dev/null | sed -n 's/^@pkgdep[[:space:]]*//p'
842 pkg_info -qe "$@" 2> /dev/null
846 [ `pkg_info -qO "$@" 2> /dev/null | wc -l` -gt 0 ]
850 pkg_info -E "$@" 2> /dev/null
854 pkg_info -qR "$@" 2> /dev/null | grep -v '^$'
858 pkg_info -Ex "$@" 2> /dev/null
862 pkg_info -qL "$@" 2> /dev/null
866 # Return the total storage space occupied by the installed files in bytes
867 pkg_info -qs "$@" 2> /dev/null | sed 's/[^0-9]*/*1024/' | tr '\n' + | sed 's/+$//' | bc -l
871 pkg_info -qe "$@" 2> /dev/null && echo
874 pkg_info_flavored_origin ()
876 local glob_unflavored tmp_stdout
878 pkg_info_qo "$glob_unflavored" 2> /dev/null
880 pkg_info_all_flavored_origins ()
882 pkg_info_qoa 2> /dev/null
886 local pkg tmp_stdout tmp_stderr
888 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
889 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
890 pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
891 sed -n 's/ fails the original MD5 checksum$//p' "$tmp_stdout" 2> /dev/null || :
892 sed -nE "s/^pkg_info: (.*) doesn't exist$/\1/p" "$tmp_stderr" 2> /dev/null || :
898 pkg_info -qW "$filepath" 2> /dev/null
900 pkg_info_gen_pkg_origin_table ()
902 pkg_info -aE 2> /dev/null | while read pkg
904 origin=`pkg_info -qo "$pkg" 2> /dev/null`
905 printf '%s\t%s\n' "$pkg" "$origin"
910 local dev_out dev_err
913 if [ $opt_batch_mode = yes ]
918 pkg_create -b "$@" > $dev_out 2> $dev_err
922 local opt_del dev_out dev_err
924 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
927 if [ $opt_batch_mode = yes ]
932 pkg_delete -f $opt_del "$@" > $dev_out 2> $dev_err
936 local opt_add dev_out dev_err
937 rm -rf "${TMPDIR}/pkg_add_f"
938 mkdir -p "${TMPDIR}/pkg_add_f"
939 ln -s "$@" "${TMPDIR}/pkg_add_f"
941 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
944 if [ $opt_batch_mode = yes ]
949 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -if $opt_add * > $dev_out 2> $dev_err )
953 local opt_add dev_out dev_err
954 rm -rf "${TMPDIR}/pkg_add_f"
955 mkdir -p "${TMPDIR}/pkg_add_f"
956 ln -s "$@" "${TMPDIR}/pkg_add_f"
958 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
961 if [ $opt_batch_mode = yes ]
966 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
968 pkg_inst_verify_pkg ()
972 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg` || return
973 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
975 pkg_inst_remote_fetch ()
979 pkgsys_fetch_legacy_remote "$pkg"
981 pkg_inst_remote_verify_fetch ()
985 pkg_inst_verify_pkg "$pkg" && return
986 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
990 local pkg pkgarc opt_add dev_out dev_err
992 if pkg_inst_remote_verify_fetch "$pkg"
994 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
995 rm -rf "${TMPDIR}/pkg_inst_remote"
996 mkdir -p "${TMPDIR}/pkg_inst_remote"
997 ln -s "$@" "${TMPDIR}/pkg_add_f"
999 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
1002 if [ $opt_batch_mode = yes ]
1007 ( cd "${TMPDIR}/pkg_inst_remote" && ls -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
1010 pkg_inst_wild_verify_pkg ()
1012 pkg_inst_verify_pkg "$@"
1014 pkg_inst_remote_wild_fetch ()
1016 pkg_inst_remote_fetch "$@"
1018 pkg_inst_remote_wild_verify_fetch ()
1020 pkg_inst_remote_verify_fetch "$@"
1022 pkg_inst_remote_wild ()
1024 pkg_inst_remote "$@"
1026 pkg_get_remote_repository_version ()
1028 local origin checksumpath version_regexp pkgbase_regexp pkg
1030 pkgsys_ready_checksum_file > /dev/null || return
1031 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
1032 version_regexp=`database_build_make "$origin" -V PORTVERSION | str_escape_regexp_filter`
1033 pkgbase_regexp=`database_build_get_new_pkgname "$origin" | sed -E "s/$version_regexp$//" | str_escape_regexp_filter`
1034 pkg=`sed -nE "s/^MD5[[:space:]]*\(($pkgbase_regexp.*)\.tbz\)[[:space:]]*=.*/\1/p" "$checksumpath" | head -n 1`
1037 pkg_get_pkgs_timestamps ()
1039 stat -t %s -f $'legacy:%m\t%N' ${PKG_DBDIR} 2> /dev/null || :
1041 pkg_get_pkg_timestamp ()
1045 portdb=`echo "$pkg" | sed 's/-[0-9].*//'`
1046 stat -t %s -f %m "${PKG_DBDIR}/$portdb" 2> /dev/null || :
1048 pkg_loadconf () { :; }
1049 pkg_rescue_tools () { :; }
1050 pkg_update_pkgrepository () { :; }
1054 # ============= Get the unflavored part of a flavored origin =============
1055 pkgsys_get_unflavored_origin ()
1057 local origin origin_unflavored
1059 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
1060 echo "$origin_unflavored"
1063 # ============= Get the port path from a flavored origin =============
1064 pkgsys_get_portpath_from_origin ()
1066 local origin origin_unflavored
1068 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1069 echo "${PORTSDIR}/$origin_unflavored"
1072 # ============= Get the flavor from a flavored origin =============
1073 pkgsys_get_flavor_from_origin ()
1077 flavor=`echo "$origin" | sed -E 's/^[^@]*@?//'`
1081 # ============= Compose a flavored origin name =============
1082 pkgsys_compose_flavored_origin ()
1087 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1088 [ -z "$flavor" ] || flavor=@$flavor
1089 echo "$origin_unflavored$flavor"
1092 # ============= Check existence of a port for a flavored origin =============
1093 pkgsys_exists_port ()
1095 local origin port_path
1097 port_path=`pkgsys_get_portpath_from_origin "$origin"`
1101 # ============= Get the installed package name from a flavored origin =============
1102 pkgsys_get_installed_pkg_from_origin ()
1104 local origin origin_unflavored flavor_origin flavor_pkg
1106 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1107 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1108 pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
1110 flavor_pkg=`pkg_info_flavor "$pkgname"`
1111 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1120 # ============= Get the installed package name from glob patterns =============
1121 pkgsys_get_installed_pkg_from_glob ()
1126 if regexp=`expr "$glob" : ':\(.*\)'`
1128 pkg_info_Ex "$regexp"
1135 # ============= Check existence of an installed package for a flavored origin =============
1136 pkgsys_exists_from_orig ()
1138 local origin origin_unflavored flavor_origin flavor_pkg
1140 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1141 pkg_info_eO "$origin_unflavored" 2> /dev/null || return
1142 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1143 flavor_pkg=`pkg_info_flavor "$origin_unflavored"`
1144 [ "x$flavor_origin" = "x$flavor_pkg" ]
1147 # ============= Generate the package names vs origins table at the initial state =============
1148 pkgsys_gen_init_pkg_origin_table ()
1150 [ -e "${DBDIR}/installed_ports:pkg_vs_origin.tbl" ] && return
1151 pkg_info_gen_pkg_origin_table > ${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp || :
1152 mv "${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp" "${DBDIR}/installed_ports:pkg_vs_origin.tbl"
1155 # ============= Check existence of initially or currently installed package for a flavored origin =============
1156 pkgsys_exists_or_existed_from_orig ()
1160 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1161 | grep -q -Fx "$origin" || pkgsys_exists_from_orig "$origin"
1164 # ============= Get the name of an initially installed package for a flavored origin =============
1165 pkgsys_get_init_pkg_from_orig ()
1167 local origin tmppkg origin_regexp npkgs origin_unflavored flavor_origin flavor_pkg
1169 tmppkg=${TMPDIR}/pkgsys_get_init_pkg_from_orig::pkg
1170 origin_regexp=`str_escape_regexp "$origin"`
1171 sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1172 | cut -f 1 > $tmppkg || :
1173 npkgs=`wc -l < $tmppkg`
1178 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1179 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1180 pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
1182 flavor_pkg=`pkg_info_flavor "$pkgname"`
1183 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1193 # ============= Get the package name of this utility =============
1194 pkgsys_get_my_current_pkg ()
1196 pkg_info_Ex "${PROGRAM}-[0-9].*"
1199 # ============= Get the flavored origin of this utility =============
1200 pkgsys_get_my_origin ()
1202 pkgsys_get_my_current_pkg | while read pkgname
1204 pkg_info_flavored_origin "$pkgname"
1208 # ============= Get the flavored origin of an initially installed package by ambiguous matching =============
1209 pkgsys_init_pkg_orig_by_ambiguous_matching ()
1211 local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
1213 origin=`pkg_info_flavored_origin "$pkg"`
1214 [ -n "$origin" ] && { echo "$origin"; return; }
1215 tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
1218 len_pkg=`echo -n "$pkg" | wc -c`
1219 if [ $len_pkg -gt 0 ]
1223 pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
1224 grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1225 | cut -f 2 > $tmporigin
1226 norigins=`wc -l < $tmporigin`
1227 [ $norigins -gt 0 ] && break
1228 ambsuffix='[a-zA-Z0-9.,_+-]*'
1229 len_pkg=$(($len_pkg-1))
1230 [ $len_pkg -gt 0 ] || break
1231 pkg=`echo -n "$pkg" | head -c $len_pkg`
1234 [ $norigins -eq 1 ] || return
1238 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
1239 pkgsys_show_pkg_updating_commands ()
1241 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1243 if which -s pkg_updating
1245 echo 'pkg-updating(8) or pkg_updating(1)'
1247 echo 'pkg-updating(8)'
1249 elif which -s pkg_updating
1251 echo 'pkg_updating(1)'
1255 # ============= Evaluation of flavored ports globs =============
1256 pkgsys_eval_ports_glob ()
1258 local pkglist unflavored_origlist tmp_flavors
1259 pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
1260 unflavored_origlist=${DBDIR}/pkgsys_eval_ports_glob:origin_unflavored.lst
1261 tmp_flavors=${TMPDIR}/pkgsys_eval_ports_glob:flavors
1262 # Test the access privilege as the superuser or not
1263 if [ ! -r "$pkglist" ]
1265 if touch "$pkglist" 2>/dev/null
1269 pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
1272 if [ ! -r "$unflavored_origlist" ]
1274 if touch "$unflavored_origlist" 2>/dev/null
1276 rm "$unflavored_origlist"
1278 unflavored_origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
1281 # Preparation of customized databases
1283 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
1284 [ -f "$unflavored_origlist" ] \
1285 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
1286 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $unflavored_origlist
1292 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
1293 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
1294 if expr "$glob" : '.*/' > /dev/null 2>&1
1296 if expr "$glob" : '.*@' > /dev/null 2>&1
1298 glob_regexp_unflavored=`expr "$glob_regexp" : '\([^@]*\)' || :`$
1299 glob_regexp_flavor=^`expr "$glob_regexp" : '[^@]*@\([^@]*\)' || :`
1301 grep -E "$glob_regexp_unflavored" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1303 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1304 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1305 tr '[:space:]' '\n' | grep -v '^$' | grep -E "$glob_regexp_flavor" | sed -E "s|^|$origin_unflavored@|"
1308 pkg_info_all_flavored_origins
1309 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1310 } | grep -E "$glob_regexp" 2>&1 || :
1312 grep -E "$glob_regexp" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1314 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1315 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1316 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1317 tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1318 if [ `wc -l < $tmp_flavors` -gt 0 ]
1320 sed -E "s|^|$origin_unflavored_rpl@|" "$tmp_flavors"
1322 echo "$origin_unflavored"
1326 glob_regexp_allflavors=`echo "$glob_regexp" | sed 's/$$/(|@.*)$/'`
1328 pkg_info_all_flavored_origins
1329 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1330 } | grep -E "$glob_regexp_allflavors" 2>&1 || :
1332 if expr "$glob" : '[a-z][a-zA-Z0-9_.+-]*[a-zA-Z0-9_.+]$' > /dev/null 2>&1 && \
1333 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
1335 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1 || :`'-[0-9]'
1337 glob_regexp2=$glob_regexp
1339 grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
1342 sed -n ${index}p "$unflavored_origlist"
1343 done | while read origin_unflavored
1345 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1346 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1347 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1348 tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1349 [ `wc -l < $tmp_flavors` -gt 0 ] || echo > $tmp_flavors
1350 sed -E "s/^/$origin_unflavored_rpl /" "$tmp_flavors"
1351 done | while read origin_unflavored flavor
1355 if make -C "${PORTSDIR}/$origin_unflavored" package-name FLAVOR=$flavor 2> /dev/null | \
1356 grep -qE "$glob_regexp2"
1358 echo "$origin_unflavored@$flavor"
1361 if make -C "${PORTSDIR}/$origin_unflavored" package-name 2> /dev/null | \
1362 grep -qE "$glob_regexp2"
1364 echo "$origin_unflavored"
1368 glob_regexp2=`echo "$glob_regexp" | sed -E 's/\$*$//' 2>&1 || :`'[[:space:]]'
1369 grep -E "$glob_regexp2" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1371 pkg_info_Ex "$glob_regexp" | while read pkgname
1373 pkg_info_flavored_origin "$pkgname"
1379 # ============= Create a back-up package archive =============
1380 pkgsys_create_backup_pkg ()
1382 local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath
1385 rm -rf "${TMPDIR}"/package.tmp
1386 mkdir "${TMPDIR}"/package.tmp
1387 origin=`pkg_info_flavored_origin "$pkgname"`
1388 if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` && \
1389 [ ! -e "${DBDIR}/requires/$origin/installed_timestamp" -o \
1390 "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
1392 echo "$backup_pkg_old"
1395 if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
1397 pkgname_ptn=`str_escape_regexp "$pkgname"`
1398 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
1399 grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1401 if [ -z "$backup_pkg" ]
1403 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
1406 dbpath=${DBDIR}/backup/$origin
1408 pkg_info_qL "$pkgname" > $dbpath/previously_installed_files
1410 mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
1411 pkgpath=$dstdir/$backup_pkg
1412 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
1413 while read origin_cur pkgpath_cur
1415 [ "$pkgpath_cur" = "$pkgpath" ] && continue
1416 if [ "$origin_cur" = "$origin" ]
1418 rm -f "$pkgpath_cur"
1420 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
1422 done > ${DBDIR}/backup_pkgarcs.lst.tmp
1423 printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
1424 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1428 # ============= Delete a back-up package archive for a flavored port origin =============
1429 pkgsys_delete_backup_pkg ()
1431 local origin origin_regexp
1433 origin_regexp=`str_escape_regexp "$origin"`
1434 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1435 | cut -f 2 | while read pkgpath_cur
1437 rm -f "$pkgpath_cur"
1439 grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
1440 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
1441 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1444 # ============= Get an existing package archive path for a flavored port origin =============
1445 pkgsys_get_backup_pkg ()
1447 local origin tmpnewest origin_regexp
1449 tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
1450 origin_regexp=`str_escape_regexp "$origin"`
1452 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1453 | cut -f 2 | while read pkgpath
1455 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
1456 [ -e "$pkgpath" ] || continue
1457 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
1458 echo "$pkgpath" > $tmpnewest
1460 cat "$tmpnewest" 2> /dev/null
1463 # ============= Get a file list to be restored by the current backup package for a flavored port origin =============
1464 pkgsys_get_restored_files_by_backup_pkg ()
1468 cat "${DBDIR}/backup/$origin/previously_installed_files" 2> /dev/null || :
1471 # ============= Check whether any file match restored files by the current backup package for a flavored port origin =============
1472 pkgsys_chk_match_to_restored_files_by_backup_pkg ()
1474 local origin filelist dbfile
1477 dbfile=${DBDIR}/backup/$origin/previously_installed_files
1478 grep -qFx -f "$filelist" "$dbfile" 2> /dev/null
1481 # ============= Get a package name from a package archive file name =============
1482 pkgsys_pkgarc_to_pkgname ()
1486 basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
1489 # ============= Get the file name of an existing package archive for a package name =============
1490 pkgsys_pkgname_to_pkgarc ()
1492 local pkgdir pkgname pkgname_ptn pkgnode
1495 [ -n "$pkgname" ] || return 1
1496 [ -d "$pkgdir" ] || return 1
1497 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1499 pkgname_ptn=`str_escape_regexp "$pkgname"`
1500 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1501 elif [ -e "$pkgdir/$pkgname.tbz" ]
1503 pkgnode=$pkgname.tbz
1505 [ -n "$pkgnode" ] || return 1
1506 echo "$pkgdir/$pkgnode"
1509 # ============= Get flavored port origins matching a glob pattern even if nonexistent =============
1510 pkgsys_eval_ports_glob_even_if_nonexistent ()
1515 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
1516 echo "$glob_pattern" | grep -E '^[a-z]+/[a-zA-Z0-9_.+-]+(|@[a-zA-Z0-9_.+-]+)$' || :
1517 } | grep -v -e '^$' | sort -u
1520 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
1521 pkgsys_register_evaluated_globs ()
1523 local mode listpath dirpath tmp_evaluated
1527 dirpath=`dirname "$listpath"`
1528 tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
1529 echo "$@" | sed -E 's/[ :]+/\
1530 /g' | grep -v '^$' | sort -u | while read -r glob
1532 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
1533 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
1535 message_echo "WARNING: No matching ports/package glob [$glob]." >&2
1538 cat "$tmp_evaluated"
1539 done | while read origin
1543 remove ) fileedit_rm_a_line "$origin" "$listpath"
1545 add ) fileedit_add_a_line_if_new "$origin" "$listpath"
1551 # ============= Evaluate glob patterns for installed packages =============
1552 pkgsys_eval_installed_pkgs_globs ()
1555 tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
1556 rm -f "$tmp_evaluated"
1557 pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
1558 [ -e "$tmp_evaluated" ] || return 0
1561 pkgsys_exists_or_existed_from_orig "$origin" || echo "$origin"
1562 done < $tmp_evaluated
1565 # ============= Get glob patterns of conflicting packages of a port =============
1566 pkgsys_get_conflicting_pkgs_patterns ()
1568 local mode origin conflicts conflicts_makevar conflicts_config
1571 conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
1574 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
1575 conflicts_config=`database_query_get_config_val "$origin" BUILDCONFLICT`
1578 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
1579 conflicts_config=`database_query_get_config_val "$origin" INSTCONFLICT`
1586 echo "$conflicts $conflicts_makevar $conflicts_config" | tr ' ' '\n' | grep -v '^$' | sort -u
1589 # ============= Get conflicting installed packages of a port =============
1590 pkgsys_get_conflicting_installed_pkgs ()
1592 local mode origin tmp_conflicts
1595 tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
1596 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" | while read pkg_pattern
1598 pkgsys_get_installed_pkg_from_glob "$pkg_pattern" || :
1599 done > $tmp_conflicts
1600 cat "$tmp_conflicts"
1601 [ `wc -l < $tmp_conflicts` -gt 0 ]
1604 # ============= Check whether a package conflicts with a port =============
1605 pkgsys_chk_conflict_by_a_pkg ()
1607 local mode origin pkg tmp_conflicts_ptn
1611 tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
1612 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
1613 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
1614 echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
1617 # ============= Check whether installed files are lost or broken for a package =============
1618 pkgsys_sanitychk_pkgcontents ()
1620 local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1622 var_is_reinstall_encouraged=$2
1623 tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1624 pkg_check_sanity "$pkg" > $tmp_sanity || :
1625 eval "$var_is_reinstall_encouraged=no"
1626 [ `wc -l < $tmp_sanity` -eq 0 ] && return
1627 nlines=`wc -l < $tmp_sanity`
1629 _is_reinstall_encouraged=no
1630 while [ $iline -le $nlines ]
1632 filename=`sed -n ${iline}p "$tmp_sanity"`
1634 if [ ! -e "$filename" ]
1636 _is_reinstall_encouraged=yes
1639 if expr "$filename" : '.*/include/.*' > /dev/null
1641 _is_reinstall_encouraged=yes
1644 filename_esc=`str_escape_regexp "$filename"`
1645 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
1647 _is_reinstall_encouraged=yes
1650 pkg_owner=`pkg_which "$filename"`
1651 if [ "$pkg" != "$pkg_owner" ]
1653 _is_reinstall_encouraged=yes
1657 eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1658 origin=`pkg_info_flavored_origin "$pkg"`
1659 if [ $opt_batch_mode = no ]
1661 message_echo "[$pkg ($origin)]"
1662 sed 's/^/ /' "$tmp_sanity"
1665 pkg_replace=`str_escape_replaceval "$pkg"`
1666 origin_replace=`str_escape_replaceval "$origin"`
1667 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1672 # ============= Check whether the port options database is once saved =============
1673 pkgsys_exists_saved_port_oprions_timestamps ()
1675 [ -d "${DBDIR}/ls_dbdir" ]
1678 # ============= Get the current all timestamp information of port options =============
1679 pkgsys_get_current_port_oprions_timestamp ()
1681 local portdb_needle_regexp
1682 portdb_needle_regexp=`str_escape_regexp "$1"`
1684 ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}" | if [ -n "$portdb_needle_regexp" ]
1686 grep -E "[[:space:]]$portdb_needle_regexp$" || :
1690 } 2> /dev/null | cut -w -f 6,7 | while read datetime portdb
1692 [ -d "${PORT_DBDIR}/$portdb" ] || continue
1693 if [ -e "${PORT_DBDIR}/$portdb/options" ]
1695 datetime=`ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}/$portdb/options" 2> /dev/null | cut -w -f 6`
1697 printf '%s\t%s\n' "$portdb" "$datetime"
1701 # ============= Get the saved all timestamp information of port options =============
1702 pkgsys_get_saved_port_oprions_timestamps_all ()
1704 mkdir -p "${DBDIR}/ls_dbdir"
1705 cat "${DBDIR}/ls_dbdir/"*.log 2> /dev/null || :
1708 # ============= Convert a list of port origins to port options timestamp log names =============
1709 pkgsys_conv_portorigin_to_port_oprion_timestamp_logname ()
1714 # ============= Get the file name of the port options database of a port =============
1715 pkgsys_get_port_oprion_database ()
1719 if pkgsys_is_dialog4ports_used
1721 echo "$origin" | sed 's|/|_|;s/@.*//'
1723 database_build_make "$origin" -V UNIQUENAME
1727 # ============= Save the timestamp information of port options of a port =============
1728 pkgsys_save_port_oprion_timestamp ()
1730 local origin portoptlog portoptdb
1732 portoptlog=`echo "$origin" | pkgsys_conv_portorigin_to_port_oprion_timestamp_logname`
1733 portoptdb=`pkgsys_get_port_oprion_database "$origin"`
1734 mkdir -p "${DBDIR}/ls_dbdir"
1735 pkgsys_get_current_port_oprions_timestamp "$portoptdb" > ${DBDIR}/ls_dbdir/$portoptlog.log 2> /dev/null || :
1738 # ============= Get changed port options from the saved point =============
1739 pkgsys_get_changed_port_oprions ()
1741 local saved_log current_log tmp_log
1744 tmp_log=${TMPDIR}/pkgsys_get_changed_port_oprions.log
1746 grep -vxF -f "$current_log" "$saved_log" || :
1747 grep -vxF -f "$saved_log" "$current_log" || :
1748 } | cut -w -f 1 | grep -v '^$' | sort -u > $tmp_log
1749 if pkgsys_is_dialog4ports_used
1751 grep '_' "$tmp_log" || :
1757 # ============= Convert a list of port options database names to port globs =============
1758 pkgsys_conv_portoptiondbs_to_globs ()
1760 if pkgsys_is_dialog4ports_used
1768 # ============= Register nonexistent port options databases =============
1769 pkgsys_register_list_nonexistent_portopriondb ()
1772 dbpath=${DBDIR}/ls_dbdir/%NONEXISTENT%.log
1773 mkdir -p "${DBDIR}/ls_dbdir"
1776 sort -u "$dbpath.new" "$dbpath" > $dbpath.tmp
1777 mv "$dbpath.tmp" "$dbpath"
1780 # ============= Order a list of installed packages by storage space occupied by the installed files =============
1781 pkgsys_order_pkgs_by_size ()
1785 size=`pkg_info_qs "$pkg" || :`
1786 [ -n "$size" ] && echo "$size"$'\t'"$pkg"
1787 done | sort -g | cut -f 2