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
16 # ============= Get the time stamp of the ports tree =============
17 pkgsys_get_timestamp_portstree ()
19 stat -t %s -f %m "${PORTS_INDEX_DB}" 2> /dev/null || :
22 # ============= Update the ports tree =============
23 pkgsys_update_portstree ()
25 if [ -e "$PORTSNAP_WORKDIR/INDEX" ]
31 if [ $opt_batch_mode = no ]
37 portsnap fetch $mode > $stdout
40 # ============= Check implementation of the ports tree =============
41 pkgsys_chk_ports_tree_implementation ()
43 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk" || :
44 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Makefile" || :
45 if [ ! -d "${PORTSDIR}" ]
47 message_echo "ERROR: Ports directory ${PORTSDIR} is not found." >&2
50 if [ ! -e "${PORTSDIR}/Makefile" -o ! -e "${PORTSDIR}/Mk/bsd.port.mk" ]
52 message_echo "ERROR: Ports tree ${PORTSDIR} is missing, broken or incompatible." >&2
57 # ============= System defined value for the ports/packages =============
62 tmp_work=${TMPDIR}/pkgsys_sysvar:work
65 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk"
66 make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var" 2> /dev/null
69 # ============= Get the file name of package check sum file =============
70 pkgsys_pkgchksum_file ()
75 # ============= Get the origin of the currently used pkg(8) =============
76 # NOTE: Assumed to be unflavored.
77 pkgsys_portsmgmt_pkg ()
79 local origin_unflavored
80 if [ ! -e "${DBDIR}/PKG_ORIGIN" ]
82 origin_unflavored=`pkgsys_sysvar PKG_ORIGIN` || :
83 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/pkg
84 echo "$origin_unflavored" > ${DBDIR}/PKG_ORIGIN
86 cat "${DBDIR}/PKG_ORIGIN"
89 # ============= Get the origin of the currently used dialog4ports(1) =============
90 # NOTE: Assumed to be unflavored.
91 pkgsys_portsmgmt_dialog4ports ()
93 local origin_unflavored
94 if [ ! -e "${DBDIR}/DIALOGPORT" ]
96 origin_unflavored=`pkgsys_sysvar DIALOGPORT` || :
97 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/dialog4ports
98 echo "$origin_unflavored" > ${DBDIR}/DIALOGPORT
100 cat "${DBDIR}/DIALOGPORT"
103 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
106 local origin origin_unflavored
108 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
109 case $origin_unflavored in
110 ports-mgmt/pkg | ports-mgmt/pkg-devel | ports-mgmt/dialog4ports | ports-mgmt/dialog4ports-static )
117 # ============= Check whether a port is indispensable for package operations =============
118 pkgsys_is_necessary_pkgtool ()
120 local origin origin_unflavored
122 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
123 [ x"$WITH_PKGNG" = x'yes' -a \( x"$origin_unflavored" = x'ports-mgmt/pkg' -o x"$origin_unflavored" = x'ports-mgmt/pkg-devel' \) ]
126 # ============= Get the extended regular expression pattern of ports for pkg(8) =============
127 pkgsys_pkgtools_ports_filter_regexp ()
129 echo '^ports-mgmt/(pkg|pkg-devel)(|@.*)$'
132 # ============= Get the extended regular expression pattern of package names for pkg(8) =============
133 pkgsys_pkgtools_pkgs_filter_regexp ()
135 echo '^(pkg|pkg-devel)-[0-9]\.'
138 # ============= Check whether the dialog for selecting port options is dialog4ports =============
139 pkgsys_is_dialog4ports_used ()
141 [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
144 # ============= Get the number of mirror sites for legacy packages =============
145 pkgsys_num_mirrorsites ()
149 echo "$siteroots" | tr '|' '\n' | wc -l
152 # ============= Get a URL one of mirror sites =============
153 pkgsys_url_from_mirrors ()
155 local siteroots subdir nsites id_site site platform version
158 nsites=`pkgsys_num_mirrorsites "$siteroots"`
159 id_site=`(set +e; random -e $nsites; echo $?)`
160 site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
162 version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
164 printf "$subdir\n" "$platform" "$version"
167 # ============= Fetch a file from one of mirror sites =============
168 pkgsys_fetch_from_mirrors ()
170 local siteroots subdir filename dst tmp_work fetch itrial origdir url
175 tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
178 fetch=`pkgsys_sysvar FETCH_CMD`
179 itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
182 while [ $itrial -ge 1 ]
184 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
185 message_echo "INFO: Fetching from $url:"
186 $fetch "$url"&& break
188 itrial=$(($itrial-1))
191 [ -e "$tmp_work/$filename" ] || return
192 mv "$tmp_work/$filename" "$dst"
195 # ============= Get the package check sums file ready =============
196 pkgsys_ready_checksum_file ()
199 tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
200 rm -f "$tmp_savedpath"
201 chksumfile=`pkgsys_pkgchksum_file`
202 if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
204 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
205 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
206 "$chksumfile" "${DBDIR}/checksum"
208 message_echo "WARNING: No check sum file is available." >&2
212 echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
215 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
216 pkgsys_ready_checksum_file__fetched_file ()
218 cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
221 # ============= Fetch a legacy package from one of remote sites =============
222 pkgsys_fetch_legacy_remote ()
224 local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
226 tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
227 tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
230 pkg_regexp=`str_escape_regexp "$pkg"`
231 pkgsys_ready_checksum_file || return
232 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
233 validMD5=`grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
234 if [ -z "$validMD5" ]
236 message_echo "WARNING: No check sum for $pkg.tbz." >&2
240 mkdir -p "${PKGREPOSITORY}"
241 if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
243 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
245 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
246 [ "x$fetchedMD5" = "x$validMD5" ] || rm "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
248 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
250 ln -f "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz" "${PKGREPOSITORY}/$pkg.tbz"
252 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
253 if [ "x$fetchedMD5" = "x$validMD5" ]
257 mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
261 if [ $needs_fetch = yes ]
263 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
264 while [ $itrial -ge 1 ]
266 if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
267 "$pkg.tbz" "$tmp_work"
269 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
270 [ "x$fetchedMD5" = "x$validMD5" ] && break
271 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
273 itrial=$(($itrial-1))
275 [ $itrial -ge 1 ] || return
276 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
278 echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
281 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
282 pkgsys_fetch_legacy_remote__fetched_pkg ()
284 cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
287 # ============= Check whether the dependency of a legacy package is the latest =============
288 pkgsys_is_dependency_of_a_legacypkg_latest ()
290 local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
292 tmp_extract=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:extract
293 tmp_contents=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:contents
294 tmp_origin=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:origin
295 tmp_pkg=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:pkg
296 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
297 [ -e "$pkgarc" ] || return
298 rm -rf "$tmp_extract"
300 tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
301 grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
302 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
303 nlines=`wc -l < $tmp_contents`
305 while [ $iline -le $nlines ]
307 origin_req=`sed -n ${iline}p "$tmp_contents"`
308 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
310 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
311 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
313 message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
320 # ============= Check whether legacy package tools are available =============
321 pkgsys_is_legacy_tool_available ()
326 # ============= Load portsnap.conf(5) for important directories =============
327 pkgsys_load_portsnap_conf ()
331 PORTSNAP_WORKDIR=`sed -n '/^[[:space:]]*WORKDIR[[:space:]]*=[[:space:]]*\([^[:space:]#]*\)/s//\1/p' /etc/portsnap.conf || :`
332 PORTSNAP_WORKDIR=${PORTSNAP_WORKDIR:-/var/db/portsnap}
333 PORTSNAP_PORTSDIR=`sed -n '/^[[:space:]]*PORTSDIR[[:space:]]*=[[:space:]]*\([^[:space:]#]*\)/s//\1/p' /etc/portsnap.conf || :`
334 PORTSNAP_PORTSDIR=${PORTSNAP_PORTSDIR:-/usr/ports}
341 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
342 pkgsys_def_pkgtools ()
344 if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf -o \( -e "${DBDIR}/WITH_PKGNG" -a ! -e /etc/make.conf \) ]
346 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
348 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKG | tr '[:upper:]' '[:lower:]'`
349 [ -n "$PKGSYS_USE_PKGNG" ] || PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
350 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
352 echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
353 mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
356 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
359 export WITH_PKGNG=yes
360 PKGSYS_CMD_PKG_DELETE='pkg delete'
363 TMPDIR=/dev/null ASSUME_ALWAYS_YES=yes PACKAGESITE=file:///nonexistent \
364 pkg info -x 'pkg(-devel)?$' >/dev/null 2>&1
366 pkg_is_tool_available ()
368 local dev_out dev_err
369 if [ -x /usr/sbin/pkg ]
373 if [ $opt_batch_mode = yes ]
378 pkg_is_tool_ready && return
379 env ASSUME_ALWAYS_YES=yes pkg bootstrap -f > $dev_out 2> $dev_err
387 pkg info -qa 2> /dev/null
391 # pkg info -qoa 2> /dev/null
395 # pkg info -qox "$@" 2> /dev/null
399 # pkg info -qox "$@" 2> /dev/null
403 pkg info -qO "$@" 2> /dev/null
407 pkg info -qo "$@" 2> /dev/null
411 pkg info -qd "$@" 2> /dev/null
415 pkg info -qe "$@" 2> /dev/null
419 pkg info -qeO "$@" 2> /dev/null
423 pkg info -Eg "$@" 2> /dev/null
427 pkg info -qr "$@" 2> /dev/null
431 pkg info -Ex "$@" 2> /dev/null
435 pkg info -ql "$@" 2> /dev/null
439 # Return the total storage space occupied by the installed files in bytes
440 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
444 local glob_unflavored
446 pkg query -g '%At\t%Av' "$glob_unflavored" 2> /dev/null | grep -E '^flavor[[:space:]]' | cut -f 2
448 pkg_info_flavored_origin ()
450 local glob_unflavored origin_unflavored
452 origin_unflavored=`pkg_info_qo "$glob_unflavored" 2> /dev/null || :`
453 flavor=`pkg_info_flavor "$glob_unflavored" 2> /dev/null || :`
454 [ -z "$flavor" ] || flavor=@$flavor
455 echo "$origin_unflavored$flavor"
457 pkg_info_all_flavored_origins ()
459 local tmp_flavored tmp_flavored_ptn
460 tmp_flavored_ptn=${TMPDIR}/pkg_info_all_flavored_origins:flavored_ptn
461 pkg query '%o\t%At\t%Av' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor[[:space:]]' | cut -f 1,3 | tr '\t' @ | sed 's/@$//'
462 pkg query '%n\t%At' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor$' | cut -f 1 > $tmp_flavored_ptn
463 pkg query '%n\t%o' 2> /dev/null | grep -vFx -f "$tmp_flavored_ptn" | cut -f 2
467 local pkg tmp_stdout tmp_stderr
469 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
470 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
471 pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
473 grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
474 grep '^[^:]*: missing file ' "$tmp_stderr" | sed 's/^[^:]*: missing file //' || :
475 if grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr" # For the old specification of pkg(8)
477 pkg info -ql "$pkg" 2> /dev/null | while read filepath
479 [ -e "$filepath" ] || echo "$filepath"
488 pkg which -q "$filepath" 2> /dev/null
490 pkg_info_gen_pkg_origin_table ()
492 # pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
493 pkg info -qa 2> /dev/null | while read pkgname
495 origin=`pkg_info_flavored_origin "$pkgname"`
496 printf '%s\t%s\n' "$pkgname" "$origin"
505 local opt_del opt_quit dev_out dev_err
507 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
511 if [ $opt_batch_mode = yes ]
517 pkg delete -fqy $opt_del "$@" > $dev_out 2> $dev_err || :
518 pkg_is_tool_ready || return 0 # If pkg(8) is deinstalled successfully
519 pkg info -e $opt_quit "$@" > /dev/null 2>&1 || return 0
520 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)
524 local pkgarc tmp_extract prefix prefix_parent pkg opt_quit dev_out dev_err
526 tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
530 if [ $opt_batch_mode = yes ]
536 rm -rf "$tmp_extract"
538 tar xf "$pkgarc" -C "$tmp_extract" > $dev_out 2> $dev_err
539 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
540 if env ASSUME_ALWAYS_YES=YES $tmp_extract/usr/local/sbin/pkg-static add $opt_quit "$pkgarc" > $dev_out 2> $dev_err
542 message_echo "INFO: $pkg is successfully registered."
544 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
550 local option pkgarc pkg pkg_tool pkg_gen opt_add opt_quit dev_out dev_err
557 pkg=`basename "$pkgarc"`
558 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
562 pkg_gen="$pkg_gen $pkgarc"
565 [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
568 rm -rf "${TMPDIR}/pkg_add_f"
569 mkdir -p "${TMPDIR}/pkg_add_f"
571 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
575 if [ $opt_batch_mode = yes ]
581 ( 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 )
586 pkg_add_f_common '' "$@"
590 pkg_add_f_common '-f' "$@"
592 pkg_inst_verify_pkg ()
596 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
597 tar tf "$pkgarc" > /dev/null 2>&1
599 pkg_inst_remote_fetch ()
604 [ $opt_batch_mode = yes ] && opt_quit='-q'
605 pkg fetch -yU $opt_quit "$pkg"
607 pkg_inst_remote_verify_fetch ()
611 pkg_inst_verify_pkg "$pkg" && return
612 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
616 local pkg pkgarc opt_add opt_quit dev_out dev_err
618 pkg_inst_remote_verify_fetch "$pkg" || return
619 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
620 rm -rf "${TMPDIR}/pkg_inst_remote"
621 mkdir -p "${TMPDIR}/pkg_inst_remote"
623 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
627 if [ $opt_batch_mode = yes ]
633 ( 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 )
635 pkg_inst_wild_verify_pkg ()
639 pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
640 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
642 pkg_inst_remote_wild_fetch ()
646 if pkg_is_tool_available
648 pkg_inst_remote "$pkg" && return
650 pkgsys_is_legacy_tool_available || return
651 message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
652 pkgsys_fetch_legacy_remote "$pkg"
654 pkg_inst_remote_wild_verify_fetch ()
658 pkg_inst_wild_verify_pkg "$pkg" && return
659 pkg_inst_remote_wild_fetch "$pkg" && pkg_inst_wild_verify_pkg "$pkg"
661 pkg_inst_remote_wild ()
663 local pkg pkgarc opt_add dev_out dev_err
665 if pkg_inst_remote_wild_verify_fetch "$pkg"
667 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
668 rm -rf "${TMPDIR}/pkg_inst_remote_wild"
669 mkdir -p "${TMPDIR}/pkg_inst_remote_wild"
671 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
674 if [ $opt_batch_mode = yes ]
679 ( cd "${TMPDIR}/pkg_inst_remote_wild" && ln -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err) || return
680 message_echo "INFO: Trying to convert the installed legacy package to pkgng."
681 pkg2ng > $dev_out 2> $dev_err || :
682 message_echo "INFO: Checking whether the conversion is successful."
683 pkg info -qe "$pkg" 2> /dev/null
686 pkg_get_remote_repository_version ()
688 local origin origin_unflavored pkg
690 origin_unflavored=`expr "$origin" : '\([^@]*\)'` || return
691 [ -n "$origin_unflavored" ] || return
692 pkg fetch -qyU "$origin_unflavored" 2> /dev/null || return
693 pkg=`pkg rquery -U %n-%v "$origin_unflavored" 2> /dev/null` || return
696 pkg_get_pkgs_timestamps ()
698 pkg query '%n-%v\tng-epoch:%t' "$@" 2> /dev/null
700 pkg_get_pkg_timestamp ()
704 pkg query '%t' "$pkg" 2> /dev/null
709 # Deafult configuration for pkg(1)
710 PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
711 PKGNG_SRV_MIRRORS=YES
712 PKGNG_PKG_DBDIR=/var/db/pkg
713 PKGNG_PKG_CACHEDIR=/var/cache/pkg
714 PKGNG_PORTSDIR=/usr/ports
715 PKGNG_PUBKEY=/etc/ssl/pkg.conf
716 PKGNG_HANDLE_RC_SCRIPTS=NO
717 PKGNG_PKG_MULTIREPOS=NO
718 PKGNG_ASSUME_ALWAYS_YES=NO
722 PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
723 # Load configuration for pkg(1)
724 pkg_conf=`pkg query %Fp pkg 2> /dev/null | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
725 [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
726 grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
727 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
730 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
731 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
732 eval PKGNG_$var=\$val
733 misc_get_all_vardefs | grep "^PKGNG_$var="
734 done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
735 . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
739 local origin_portsmgmt packagepath checksumpath pkgname is_successful dev_out dev_err
740 origin_portsmgmt=`pkgsys_portsmgmt_pkg`
741 pkg_is_tool_ready && return
742 packagepath=`pkgsys_get_backup_pkg "$origin_portsmgmt"` && \
743 pkg_add_tools "$packagepath" && return
744 pkg_is_tool_available && return
745 message_echo "WARNING: WITH_PKG or WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
746 pkgsys_ready_checksum_file || return
747 message_echo "INFO: Installing pkgng by legacy package tool."
748 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
749 pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
750 | grep -m 1 '^pkg-[0-9]'` || :
751 [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" && return
752 message_echo "INFO: Failed by package, so installing pkgng by port."
753 grep -Ev '^[[:space:]]*WITH_PKG(|NG)=' /etc/make.conf > ${TMPDIR}/make.conf 2> /dev/null || :
754 echo WITHOUT_PKG=yes >> ${TMPDIR}/make.conf
755 echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
758 if [ $opt_batch_mode = yes ]
768 message_echo "INFO: Attempting deinstallation of pkg(8) to make sure."
769 portsmgmt_port_path=`pkgsys_get_portpath_from_origin "$origin_portsmgmt"`
770 fs_fix_unionfs_image_if_hidden "${TMPDIR}/make.conf"
771 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" deinstall > $dev_out 2> $dev_err || :
772 message_echo "INFO: Attempting (re)installation by pkg(8)."
773 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" reinstall clean > $dev_out 2> $dev_err
775 pkg2ng > $dev_out 2> $dev_err || :
776 pkg_is_tool_available
779 pkg_update_pkgrepository ()
782 pkg_rescue_tools || return 0
783 if [ $opt_batch_mode = no ]
791 if ! pkg_rescue_tools
793 message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
796 elif ! pkgsys_is_legacy_tool_available
798 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
804 PKGSYS_CMD_PKG_DELETE='pkg_delete'
807 pkgsys_is_legacy_tool_available
809 pkg_is_tool_available ()
811 pkgsys_is_legacy_tool_available
815 pkg_info -Ea 2> /dev/null
819 # pkg_info -qoa 2> /dev/null
823 # pkg_info -qox "$@" 2> /dev/null
827 # pkg_info -qoX "$@" 2> /dev/null
831 pkg_info -qO "$@" 2> /dev/null
835 pkg_info -qo "$@" 2> /dev/null
839 pkg_info -qr "$@" 2> /dev/null | sed -n 's/^@pkgdep[[:space:]]*//p'
843 pkg_info -qe "$@" 2> /dev/null
847 [ `pkg_info -qO "$@" 2> /dev/null | wc -l` -gt 0 ]
851 pkg_info -E "$@" 2> /dev/null
855 pkg_info -qR "$@" 2> /dev/null | grep -v '^$'
859 pkg_info -Ex "$@" 2> /dev/null
863 pkg_info -qL "$@" 2> /dev/null
867 # Return the total storage space occupied by the installed files in bytes
868 pkg_info -qs "$@" 2> /dev/null | sed 's/[^0-9]*/*1024/' | tr '\n' + | sed 's/+$//' | bc -l
872 pkg_info -qe "$@" 2> /dev/null && echo
875 pkg_info_flavored_origin ()
877 local glob_unflavored tmp_stdout
879 pkg_info_qo "$glob_unflavored" 2> /dev/null
881 pkg_info_all_flavored_origins ()
883 pkg_info_qoa 2> /dev/null
887 local pkg tmp_stdout tmp_stderr
889 tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
890 tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
891 pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
892 sed -n 's/ fails the original MD5 checksum$//p' "$tmp_stdout" 2> /dev/null || :
893 sed -nE "s/^pkg_info: (.*) doesn't exist$/\1/p" "$tmp_stderr" 2> /dev/null || :
899 pkg_info -qW "$filepath" 2> /dev/null
901 pkg_info_gen_pkg_origin_table ()
903 pkg_info -aE 2> /dev/null | while read pkg
905 origin=`pkg_info -qo "$pkg" 2> /dev/null`
906 printf '%s\t%s\n' "$pkg" "$origin"
911 local dev_out dev_err
914 if [ $opt_batch_mode = yes ]
919 pkg_create -b "$@" > $dev_out 2> $dev_err
923 local opt_del dev_out dev_err
925 [ $opt_no_exec_inst_script = yes ] && opt_del='-D'
928 if [ $opt_batch_mode = yes ]
933 pkg_delete -f $opt_del "$@" > $dev_out 2> $dev_err
937 local opt_add dev_out dev_err
938 rm -rf "${TMPDIR}/pkg_add_f"
939 mkdir -p "${TMPDIR}/pkg_add_f"
940 ln -s "$@" "${TMPDIR}/pkg_add_f"
942 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
945 if [ $opt_batch_mode = yes ]
950 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -if $opt_add * > $dev_out 2> $dev_err )
954 local opt_add dev_out dev_err
955 rm -rf "${TMPDIR}/pkg_add_f"
956 mkdir -p "${TMPDIR}/pkg_add_f"
957 ln -s "$@" "${TMPDIR}/pkg_add_f"
959 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
962 if [ $opt_batch_mode = yes ]
967 ( cd "${TMPDIR}/pkg_add_f" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
969 pkg_inst_verify_pkg ()
973 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg` || return
974 pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
976 pkg_inst_remote_fetch ()
980 pkgsys_fetch_legacy_remote "$pkg"
982 pkg_inst_remote_verify_fetch ()
986 pkg_inst_verify_pkg "$pkg" && return
987 pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
991 local pkg pkgarc opt_add dev_out dev_err
993 if pkg_inst_remote_verify_fetch "$pkg"
995 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
996 rm -rf "${TMPDIR}/pkg_inst_remote"
997 mkdir -p "${TMPDIR}/pkg_inst_remote"
998 ln -s "$@" "${TMPDIR}/pkg_add_f"
1000 [ $opt_no_exec_inst_script = yes ] && opt_add='-I'
1003 if [ $opt_batch_mode = yes ]
1008 ( cd "${TMPDIR}/pkg_inst_remote" && ls -s "$pkgarc" && pkg_add -ifF $opt_add * > $dev_out 2> $dev_err )
1011 pkg_inst_wild_verify_pkg ()
1013 pkg_inst_verify_pkg "$@"
1015 pkg_inst_remote_wild_fetch ()
1017 pkg_inst_remote_fetch "$@"
1019 pkg_inst_remote_wild_verify_fetch ()
1021 pkg_inst_remote_verify_fetch "$@"
1023 pkg_inst_remote_wild ()
1025 pkg_inst_remote "$@"
1027 pkg_get_remote_repository_version ()
1029 local origin checksumpath version_regexp pkgbase_regexp pkg
1031 pkgsys_ready_checksum_file > /dev/null || return
1032 checksumpath=`pkgsys_ready_checksum_file__fetched_file`
1033 version_regexp=`database_build_make "$origin" -V PORTVERSION | str_escape_regexp_filter`
1034 pkgbase_regexp=`database_build_get_new_pkgname "$origin" | sed -E "s/$version_regexp$//" | str_escape_regexp_filter`
1035 pkg=`sed -nE "s/^MD5[[:space:]]*\(($pkgbase_regexp.*)\.tbz\)[[:space:]]*=.*/\1/p" "$checksumpath" | head -n 1`
1038 pkg_get_pkgs_timestamps ()
1040 stat -t %s -f $'legacy:%m\t%N' ${PKG_DBDIR} 2> /dev/null || :
1042 pkg_get_pkg_timestamp ()
1046 portdb=`echo "$pkg" | sed 's/-[0-9].*//'`
1047 stat -t %s -f %m "${PKG_DBDIR}/$portdb" 2> /dev/null || :
1049 pkg_loadconf () { :; }
1050 pkg_rescue_tools () { :; }
1051 pkg_update_pkgrepository () { :; }
1055 # ============= Get the unflavored part of a flavored origin =============
1056 pkgsys_get_unflavored_origin ()
1058 local origin origin_unflavored
1060 origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
1061 echo "$origin_unflavored"
1064 # ============= Get the port path from a flavored origin =============
1065 pkgsys_get_portpath_from_origin ()
1067 local origin origin_unflavored
1069 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1070 echo "${PORTSDIR}/$origin_unflavored"
1073 # ============= Get the flavor from a flavored origin =============
1074 pkgsys_get_flavor_from_origin ()
1078 flavor=`echo "$origin" | sed -E 's/^[^@]*@?//'`
1082 # ============= Compose a flavored origin name =============
1083 pkgsys_compose_flavored_origin ()
1088 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1089 [ -z "$flavor" ] || flavor=@$flavor
1090 echo "$origin_unflavored$flavor"
1093 # ============= Check existence of a port for a flavored origin =============
1094 pkgsys_exists_port ()
1096 local origin port_path
1098 port_path=`pkgsys_get_portpath_from_origin "$origin"`
1102 # ============= Get the installed package name from a flavored origin =============
1103 pkgsys_get_installed_pkg_from_origin ()
1105 local origin origin_unflavored flavor_origin flavor_pkg
1107 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1108 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1109 pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
1111 flavor_pkg=`pkg_info_flavor "$pkgname"`
1112 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1121 # ============= Get the installed package name from glob patterns =============
1122 pkgsys_get_installed_pkg_from_glob ()
1127 if regexp=`expr "$glob" : ':\(.*\)'`
1129 pkg_info_Ex "$regexp"
1136 # ============= Check existence of an installed package for a flavored origin =============
1137 pkgsys_exists_from_orig ()
1139 local origin origin_unflavored flavor_origin flavor_pkg
1141 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1142 pkg_info_eO "$origin_unflavored" 2> /dev/null || return
1143 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1144 flavor_pkg=`pkg_info_flavor "$origin_unflavored"`
1145 [ "x$flavor_origin" = "x$flavor_pkg" ]
1148 # ============= Generate the package names vs origins table at the initial state =============
1149 pkgsys_gen_init_pkg_origin_table ()
1151 [ -e "${DBDIR}/installed_ports:pkg_vs_origin.tbl" ] && return
1152 pkg_info_gen_pkg_origin_table > ${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp || :
1153 mv "${DBDIR}/installed_ports:pkg_vs_origin.tbl.tmp" "${DBDIR}/installed_ports:pkg_vs_origin.tbl"
1156 # ============= Check existence of initially or currently installed package for a flavored origin =============
1157 pkgsys_exists_or_existed_from_orig ()
1161 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1162 | grep -q -Fx "$origin" || pkgsys_exists_from_orig "$origin"
1165 # ============= Get the name of an initially installed package for a flavored origin =============
1166 pkgsys_get_init_pkg_from_orig ()
1168 local origin tmppkg origin_regexp npkgs origin_unflavored flavor_origin flavor_pkg
1170 tmppkg=${TMPDIR}/pkgsys_get_init_pkg_from_orig::pkg
1171 origin_regexp=`str_escape_regexp "$origin"`
1172 sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1173 | cut -f 1 > $tmppkg || :
1174 npkgs=`wc -l < $tmppkg`
1179 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1180 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1181 pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
1183 flavor_pkg=`pkg_info_flavor "$pkgname"`
1184 if [ "x$flavor_origin" = "x$flavor_pkg" ]
1194 # ============= Get the package name of this utility =============
1195 pkgsys_get_my_current_pkg ()
1197 pkg_info_Ex "${PROGRAM}-[0-9].*"
1200 # ============= Get the flavored origin of this utility =============
1201 pkgsys_get_my_origin ()
1203 pkgsys_get_my_current_pkg | while read pkgname
1205 pkg_info_flavored_origin "$pkgname"
1209 # ============= Get the flavored origin of an initially installed package by ambiguous matching =============
1210 pkgsys_init_pkg_orig_by_ambiguous_matching ()
1212 local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
1214 origin=`pkg_info_flavored_origin "$pkg"`
1215 [ -n "$origin" ] && { echo "$origin"; return; }
1216 tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
1219 len_pkg=`echo -n "$pkg" | wc -c`
1220 if [ $len_pkg -gt 0 ]
1224 pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
1225 grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1226 | cut -f 2 > $tmporigin
1227 norigins=`wc -l < $tmporigin`
1228 [ $norigins -gt 0 ] && break
1229 ambsuffix='[a-zA-Z0-9.,_+-]*'
1230 len_pkg=$(($len_pkg-1))
1231 [ $len_pkg -gt 0 ] || break
1232 pkg=`echo -n "$pkg" | head -c $len_pkg`
1235 [ $norigins -eq 1 ] || return
1239 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
1240 pkgsys_show_pkg_updating_commands ()
1242 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1244 if which -s pkg_updating
1246 echo 'pkg-updating(8) or pkg_updating(1)'
1248 echo 'pkg-updating(8)'
1250 elif which -s pkg_updating
1252 echo 'pkg_updating(1)'
1256 # ============= Evaluation of flavored ports globs =============
1257 pkgsys_eval_ports_glob ()
1259 local pkglist unflavored_origlist tmp_flavors
1260 pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
1261 unflavored_origlist=${DBDIR}/pkgsys_eval_ports_glob:origin_unflavored.lst
1262 tmp_flavors=${TMPDIR}/pkgsys_eval_ports_glob:flavors
1263 # Test the access privilege as the superuser or not
1264 if [ ! -r "$pkglist" ]
1266 if touch "$pkglist" 2>/dev/null
1270 pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
1273 if [ ! -r "$unflavored_origlist" ]
1275 if touch "$unflavored_origlist" 2>/dev/null
1277 rm "$unflavored_origlist"
1279 unflavored_origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
1282 # Preparation of customized databases
1284 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
1285 [ -f "$unflavored_origlist" ] \
1286 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
1287 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $unflavored_origlist
1293 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
1294 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
1295 if expr "$glob" : '.*/' > /dev/null 2>&1
1297 if expr "$glob" : '.*@' > /dev/null 2>&1
1299 glob_regexp_unflavored=`expr "$glob_regexp" : '\([^@]*\)' || :`$
1300 glob_regexp_flavor=^`expr "$glob_regexp" : '[^@]*@\([^@]*\)' || :`
1302 grep -E "$glob_regexp_unflavored" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1304 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1305 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1306 tr '[:space:]' '\n' | grep -v '^$' | grep -E "$glob_regexp_flavor" | sed -E "s|^|$origin_unflavored@|"
1309 pkg_info_all_flavored_origins
1310 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1311 } | grep -E "$glob_regexp" 2>&1 || :
1313 grep -E "$glob_regexp" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1315 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1316 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1317 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1318 tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1319 if [ `wc -l < $tmp_flavors` -gt 0 ]
1321 sed -E "s|^|$origin_unflavored_rpl@|" "$tmp_flavors"
1323 echo "$origin_unflavored"
1327 glob_regexp_allflavors=`echo "$glob_regexp" | sed 's/$$/(|@.*)$/'`
1329 pkg_info_all_flavored_origins
1330 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1331 } | grep -E "$glob_regexp_allflavors" 2>&1 || :
1333 if expr "$glob" : '[a-z][a-zA-Z0-9_.+-]*[a-zA-Z0-9_.+]$' > /dev/null 2>&1 && \
1334 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
1336 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1 || :`'-[0-9]'
1338 glob_regexp2=$glob_regexp
1340 grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
1343 sed -n ${index}p "$unflavored_origlist"
1344 done | while read origin_unflavored
1346 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1347 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1348 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1349 tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1350 [ `wc -l < $tmp_flavors` -gt 0 ] || echo > $tmp_flavors
1351 sed -E "s/^/$origin_unflavored_rpl /" "$tmp_flavors"
1352 done | while read origin_unflavored flavor
1356 if make -C "${PORTSDIR}/$origin_unflavored" package-name FLAVOR=$flavor 2> /dev/null | \
1357 grep -qE "$glob_regexp2"
1359 echo "$origin_unflavored@$flavor"
1362 if make -C "${PORTSDIR}/$origin_unflavored" package-name 2> /dev/null | \
1363 grep -qE "$glob_regexp2"
1365 echo "$origin_unflavored"
1369 glob_regexp2=`echo "$glob_regexp" | sed -E 's/\$*$//' 2>&1 || :`'[[:space:]]'
1370 grep -E "$glob_regexp2" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1372 pkg_info_Ex "$glob_regexp" | while read pkgname
1374 pkg_info_flavored_origin "$pkgname"
1380 # ============= Create a back-up package archive =============
1381 pkgsys_create_backup_pkg ()
1383 local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath
1386 rm -rf "${TMPDIR}"/package.tmp
1387 mkdir "${TMPDIR}"/package.tmp
1388 origin=`pkg_info_flavored_origin "$pkgname"`
1389 if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` && \
1390 [ ! -e "${DBDIR}/requires/$origin/installed_timestamp" -o \
1391 "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
1393 echo "$backup_pkg_old"
1396 if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
1398 pkgname_ptn=`str_escape_regexp "$pkgname"`
1399 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
1400 grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1402 if [ -z "$backup_pkg" ]
1404 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
1407 dbpath=${DBDIR}/backup/$origin
1409 pkg_info_qL "$pkgname" > $dbpath/previously_installed_files
1411 mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
1412 pkgpath=$dstdir/$backup_pkg
1413 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
1414 while read origin_cur pkgpath_cur
1416 [ "$pkgpath_cur" = "$pkgpath" ] && continue
1417 if [ "$origin_cur" = "$origin" ]
1419 rm -f "$pkgpath_cur"
1421 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
1423 done > ${DBDIR}/backup_pkgarcs.lst.tmp
1424 printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
1425 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1429 # ============= Delete a back-up package archive for a flavored port origin =============
1430 pkgsys_delete_backup_pkg ()
1432 local origin origin_regexp
1434 origin_regexp=`str_escape_regexp "$origin"`
1435 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1436 | cut -f 2 | while read pkgpath_cur
1438 rm -f "$pkgpath_cur"
1440 grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
1441 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
1442 mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1445 # ============= Get an existing package archive path for a flavored port origin =============
1446 pkgsys_get_backup_pkg ()
1448 local origin tmpnewest origin_regexp
1450 tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
1451 origin_regexp=`str_escape_regexp "$origin"`
1453 grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1454 | cut -f 2 | while read pkgpath
1456 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
1457 [ -e "$pkgpath" ] || continue
1458 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
1459 echo "$pkgpath" > $tmpnewest
1461 cat "$tmpnewest" 2> /dev/null
1464 # ============= Get a file list to be restored by the current backup package for a flavored port origin =============
1465 pkgsys_get_restored_files_by_backup_pkg ()
1469 cat "${DBDIR}/backup/$origin/previously_installed_files" 2> /dev/null || :
1472 # ============= Check whether any file match restored files by the current backup package for a flavored port origin =============
1473 pkgsys_chk_match_to_restored_files_by_backup_pkg ()
1475 local origin filelist dbfile
1478 dbfile=${DBDIR}/backup/$origin/previously_installed_files
1479 grep -qFx -f "$filelist" "$dbfile" 2> /dev/null
1482 # ============= Get a package name from a package archive file name =============
1483 pkgsys_pkgarc_to_pkgname ()
1487 basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
1490 # ============= Get the file name of an existing package archive for a package name =============
1491 pkgsys_pkgname_to_pkgarc ()
1493 local pkgdir pkgname pkgname_ptn pkgnode
1496 [ -n "$pkgname" ] || return 1
1497 [ -d "$pkgdir" ] || return 1
1498 if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1500 pkgname_ptn=`str_escape_regexp "$pkgname"`
1501 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1502 elif [ -e "$pkgdir/$pkgname.tbz" ]
1504 pkgnode=$pkgname.tbz
1506 [ -n "$pkgnode" ] || return 1
1507 echo "$pkgdir/$pkgnode"
1510 # ============= Get flavored port origins matching a glob pattern even if nonexistent =============
1511 pkgsys_eval_ports_glob_even_if_nonexistent ()
1516 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
1517 echo "$glob_pattern" | grep -E '^[a-z]+/[a-zA-Z0-9_.+-]+(|@[a-zA-Z0-9_.+-]+)$' || :
1518 } | grep -v -e '^$' | sort -u
1521 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
1522 pkgsys_register_evaluated_globs ()
1524 local mode listpath dirpath tmp_evaluated
1528 dirpath=`dirname "$listpath"`
1529 tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
1530 echo "$@" | sed -E 's/[ :]+/\
1531 /g' | grep -v '^$' | sort -u | while read -r glob
1533 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
1534 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
1536 message_echo "WARNING: No matching ports/package glob [$glob]." >&2
1539 cat "$tmp_evaluated"
1540 done | while read origin
1544 remove ) fileedit_rm_a_line "$origin" "$listpath"
1546 add ) fileedit_add_a_line_if_new "$origin" "$listpath"
1552 # ============= Evaluate glob patterns for installed packages =============
1553 pkgsys_eval_installed_pkgs_globs ()
1556 tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
1557 rm -f "$tmp_evaluated"
1558 pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
1559 [ -e "$tmp_evaluated" ] || return 0
1562 pkgsys_exists_or_existed_from_orig "$origin" || echo "$origin"
1563 done < $tmp_evaluated
1566 # ============= Get glob patterns of conflicting packages of a port =============
1567 pkgsys_get_conflicting_pkgs_patterns ()
1569 local mode origin conflicts conflicts_makevar conflicts_config
1572 conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
1575 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
1576 conflicts_config=`database_query_get_config_val "$origin" BUILDCONFLICT`
1579 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
1580 conflicts_config=`database_query_get_config_val "$origin" INSTCONFLICT`
1587 echo "$conflicts $conflicts_makevar $conflicts_config" | tr ' ' '\n' | grep -v '^$' | sort -u
1590 # ============= Get conflicting installed packages of a port =============
1591 pkgsys_get_conflicting_installed_pkgs ()
1593 local mode origin tmp_conflicts
1596 tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
1597 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" | while read pkg_pattern
1599 pkgsys_get_installed_pkg_from_glob "$pkg_pattern" || :
1600 done > $tmp_conflicts
1601 cat "$tmp_conflicts"
1602 [ `wc -l < $tmp_conflicts` -gt 0 ]
1605 # ============= Check whether a package conflicts with a port =============
1606 pkgsys_chk_conflict_by_a_pkg ()
1608 local mode origin pkg tmp_conflicts_ptn
1612 tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
1613 pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
1614 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
1615 echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
1618 # ============= Check whether installed files are lost or broken for a package =============
1619 pkgsys_sanitychk_pkgcontents ()
1621 local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1623 var_is_reinstall_encouraged=$2
1624 tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1625 pkg_check_sanity "$pkg" > $tmp_sanity || :
1626 eval "$var_is_reinstall_encouraged=no"
1627 [ `wc -l < $tmp_sanity` -eq 0 ] && return
1628 nlines=`wc -l < $tmp_sanity`
1630 _is_reinstall_encouraged=no
1631 while [ $iline -le $nlines ]
1633 filename=`sed -n ${iline}p "$tmp_sanity"`
1635 if [ ! -e "$filename" ]
1637 _is_reinstall_encouraged=yes
1640 if expr "$filename" : '.*/include/.*' > /dev/null
1642 _is_reinstall_encouraged=yes
1645 filename_esc=`str_escape_regexp "$filename"`
1646 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
1648 _is_reinstall_encouraged=yes
1651 pkg_owner=`pkg_which "$filename"`
1652 if [ "$pkg" != "$pkg_owner" ]
1654 _is_reinstall_encouraged=yes
1658 eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1659 origin=`pkg_info_flavored_origin "$pkg"`
1660 if [ $opt_batch_mode = no ]
1662 message_echo "[$pkg ($origin)]"
1663 sed 's/^/ /' "$tmp_sanity"
1666 pkg_replace=`str_escape_replaceval "$pkg"`
1667 origin_replace=`str_escape_replaceval "$origin"`
1668 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1673 # ============= Check whether the port options database is once saved =============
1674 pkgsys_exists_saved_port_oprions_timestamps ()
1676 [ -d "${DBDIR}/ls_dbdir" ]
1679 # ============= Get the current all timestamp information of port options =============
1680 pkgsys_get_current_port_oprions_timestamp ()
1682 local portdb_needle_regexp
1683 portdb_needle_regexp=`str_escape_regexp "$1"`
1685 ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}" | if [ -n "$portdb_needle_regexp" ]
1687 grep -E "[[:space:]]$portdb_needle_regexp$" || :
1691 } 2> /dev/null | cut -w -f 6,7 | while read datetime portdb
1693 [ -d "${PORT_DBDIR}/$portdb" ] || continue
1694 if [ -e "${PORT_DBDIR}/$portdb/options" ]
1696 datetime=`ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}/$portdb/options" 2> /dev/null | cut -w -f 6`
1698 printf '%s\t%s\n' "$portdb" "$datetime"
1702 # ============= Get the saved all timestamp information of port options =============
1703 pkgsys_get_saved_port_oprions_timestamps_all ()
1705 mkdir -p "${DBDIR}/ls_dbdir"
1706 cat "${DBDIR}/ls_dbdir/"*.log 2> /dev/null || :
1709 # ============= Convert a list of port origins to port options timestamp log names =============
1710 pkgsys_conv_portorigin_to_port_oprion_timestamp_logname ()
1715 # ============= Get the file name of the port options database of a port =============
1716 pkgsys_get_port_oprion_database ()
1720 if pkgsys_is_dialog4ports_used
1722 echo "$origin" | sed 's|/|_|;s/@.*//'
1724 database_build_make "$origin" -V UNIQUENAME
1728 # ============= Save the timestamp information of port options of a port =============
1729 pkgsys_save_port_oprion_timestamp ()
1731 local origin portoptlog portoptdb
1733 portoptlog=`echo "$origin" | pkgsys_conv_portorigin_to_port_oprion_timestamp_logname`
1734 portoptdb=`pkgsys_get_port_oprion_database "$origin"`
1735 mkdir -p "${DBDIR}/ls_dbdir"
1736 pkgsys_get_current_port_oprions_timestamp "$portoptdb" > ${DBDIR}/ls_dbdir/$portoptlog.log 2> /dev/null || :
1739 # ============= Get changed port options from the saved point =============
1740 pkgsys_get_changed_port_oprions ()
1742 local saved_log current_log tmp_log
1745 tmp_log=${TMPDIR}/pkgsys_get_changed_port_oprions.log
1747 grep -vxF -f "$current_log" "$saved_log" || :
1748 grep -vxF -f "$saved_log" "$current_log" || :
1749 } | cut -w -f 1 | grep -v '^$' | sort -u > $tmp_log
1750 if pkgsys_is_dialog4ports_used
1752 grep '_' "$tmp_log" || :
1758 # ============= Convert a list of port options database names to port globs =============
1759 pkgsys_conv_portoptiondbs_to_globs ()
1761 if pkgsys_is_dialog4ports_used
1769 # ============= Register nonexistent port options databases =============
1770 pkgsys_register_list_nonexistent_portopriondb ()
1773 dbpath=${DBDIR}/ls_dbdir/%NONEXISTENT%.log
1774 mkdir -p "${DBDIR}/ls_dbdir"
1777 sort -u "$dbpath.new" "$dbpath" > $dbpath.tmp
1778 mv "$dbpath.tmp" "$dbpath"
1781 # ============= Order a list of installed packages by storage space occupied by the installed files =============
1782 pkgsys_order_pkgs_by_size ()
1786 size=`pkg_info_qs "$pkg" || :`
1787 [ -n "$size" ] && echo "$size"$'\t'"$pkg"
1788 done | sort -g | cut -f 2