OSDN Git Service

Additional corrections related to the previous one.
[portsreinstall/current.git] / lib / libpkgsys.sh
1 #!/bin/sh -e
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 # ==============================================================================
8
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
14
15 # ============= Check implementation of the ports tree =============
16 pkgsys_chk_ports_tree_implementation ()
17 {
18         fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk" || :
19         fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Makefile" || :
20         if [ ! -d "${PORTSDIR}" ]
21         then
22                 message_echo "ERROR: Ports directory ${PORTSDIR} is not found." >&2
23                 exit 1
24         fi
25         if [ ! -e "${PORTSDIR}/Makefile" -o ! -e "${PORTSDIR}/Mk/bsd.port.mk" ]
26         then
27                 message_echo "ERROR: Ports tree ${PORTSDIR} is missing, broken or incompatible." >&2
28                 exit 1
29         fi
30 }
31
32 # ============= System defined value for the ports/packages =============
33 pkgsys_sysvar ()
34 {
35         local var tmp_work
36         var=$1
37         tmp_work=${TMPDIR}/pkgsys_sysvar:work
38         rm -rf "$tmp_work"
39         mkdir "$tmp_work"
40         fs_fix_unionfs_image_if_hidden "${PORTSDIR}/Mk/bsd.port.mk"
41         make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var" 2> /dev/null
42 }
43
44 # ============= Get the file name of package check sum file =============
45 pkgsys_pkgchksum_file ()
46 {
47         echo CHECKSUM.MD5
48 }
49
50 # ============= Get the origin of the currently used pkg(8) =============
51 # NOTE: Assumed to be unflavored.
52 pkgsys_portsmgmt_pkg ()
53 {
54         local origin_unflavored
55         if [ ! -e "${DBDIR}/PKG_ORIGIN" ]
56         then
57                 origin_unflavored=`pkgsys_sysvar PKG_ORIGIN` || :
58                 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/pkg
59                 echo "$origin_unflavored" > ${DBDIR}/PKG_ORIGIN
60         fi
61         cat "${DBDIR}/PKG_ORIGIN"
62 }
63
64 # ============= Get the origin of the currently used dialog4ports(1) =============
65 # NOTE: Assumed to be unflavored.
66 pkgsys_portsmgmt_dialog4ports ()
67 {
68         local origin_unflavored
69         if [ ! -e "${DBDIR}/DIALOGPORT" ]
70         then
71                 origin_unflavored=`pkgsys_sysvar DIALOGPORT` || :
72                 [ -n "$origin_unflavored" -a -d "${PORTSDIR}/$origin_unflavored" ] || origin_unflavored=ports-mgmt/dialog4ports
73                 echo "$origin_unflavored" > ${DBDIR}/DIALOGPORT
74         fi
75         cat "${DBDIR}/DIALOGPORT"
76 }
77
78 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
79 pkgsys_is_pkgtool ()
80 {
81         local origin origin_unflavored
82         origin=$1
83         origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
84         case $origin_unflavored in
85         ports-mgmt/pkg | ports-mgmt/pkg-devel | ports-mgmt/dialog4ports | ports-mgmt/dialog4ports-static )
86                 ;;
87         *)      return 1
88                 ;;
89         esac
90 }
91
92 # ============= Check whether a port is indispensable for package operations =============
93 pkgsys_is_necessary_pkgtool ()
94 {
95         local origin origin_unflavored
96         origin=$1
97         origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
98         [ x"$WITH_PKGNG" = x'yes' -a \( x"$origin_unflavored" = x'ports-mgmt/pkg' -o x"$origin_unflavored" = x'ports-mgmt/pkg-devel' \) ]
99 }
100
101 # ============= Get the extended regular expression pattern of ports for pkg(8) =============
102 pkgsys_pkgtools_ports_filter_regexp ()
103 {
104         echo '^ports-mgmt/(pkg|pkg-devel)(|@.*)$'
105 }
106
107 # ============= Get the extended regular expression pattern of package names for pkg(8) =============
108 pkgsys_pkgtools_pkgs_filter_regexp ()
109 {
110         echo '^(pkg|pkg-devel)-[0-9]\.'
111 }
112
113 # ============= Check whether the dialog for selecting port options is dialog4ports =============
114 pkgsys_is_dialog4ports_used ()
115 {
116         [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
117 }
118
119 # ============= Get the number of mirror sites for legacy packages =============
120 pkgsys_num_mirrorsites ()
121 {
122         local siteroots
123         siteroots=$1
124         echo "$siteroots" | tr '|' '\n' | wc -l
125 }
126
127 # ============= Get a URL one of mirror sites =============
128 pkgsys_url_from_mirrors ()
129 {
130         local siteroots subdir nsites id_site site platform version
131         siteroots=$1
132         subdir=$2
133         nsites=`pkgsys_num_mirrorsites "$siteroots"`
134         id_site=`(set +e; random -e $nsites; echo $?)`
135         site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
136         platform=`uname -p`
137         version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
138         echo -n "$site"
139         printf "$subdir\n" "$platform" "$version"
140 }
141
142 # ============= Fetch a file from one of mirror sites =============
143 pkgsys_fetch_from_mirrors ()
144 {
145         local siteroots subdir filename dst tmp_work fetch itrial origdir url
146         siteroots=$1
147         subdir=$2
148         filename=$3
149         dst=$4
150         tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
151         rm -rf "$tmp_work"
152         mkdir "$tmp_work"
153         fetch=`pkgsys_sysvar FETCH_CMD`
154         itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
155         origdir=`pwd`
156         cd "$tmp_work"
157         while [ $itrial -ge 1 ]
158         do
159                 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
160                 message_echo "INFO: Fetching from $url:"
161                 $fetch "$url"&& break
162                 rm -f "$filename"
163                 itrial=$(($itrial-1))
164         done
165         cd "$origdir"
166         [ -e "$tmp_work/$filename" ] || return
167         mv "$tmp_work/$filename" "$dst"
168 }
169
170 # ============= Get the package check sums file ready =============
171 pkgsys_ready_checksum_file ()
172 {
173         local chksumfile
174         tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
175         rm -f "$tmp_savedpath"
176         chksumfile=`pkgsys_pkgchksum_file`
177         if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
178         then
179                 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
180                 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
181                         "$chksumfile" "${DBDIR}/checksum"
182                 then
183                         message_echo "WARNING: No check sum file is available." >&2
184                         return 1
185                 fi
186         fi
187         echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
188 }
189
190 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
191 pkgsys_ready_checksum_file__fetched_file ()
192 {
193         cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
194 }
195
196 # ============= Fetch a legacy package from one of remote sites =============
197 pkgsys_fetch_legacy_remote ()
198 {
199         local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
200         pkg=$1
201         tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
202         tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
203         rm -rf "$tmp_work"
204         mkdir "$tmp_work"
205         pkg_regexp=`str_escape_regexp "$pkg"`
206         pkgsys_ready_checksum_file || return
207         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
208         validMD5=`grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
209         if [ -z "$validMD5" ]
210         then
211                 message_echo "WARNING: No check sum for $pkg.tbz." >&2
212                 return 1
213         fi
214         needs_fetch=yes
215         mkdir -p "${PKGREPOSITORY}"
216         if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
217         then
218                 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
219                 then
220                         fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
221                         [ "x$fetchedMD5" = "x$validMD5" ] || rm "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
222                 fi
223                 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
224                 then
225                         ln -f "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz" "${PKGREPOSITORY}/$pkg.tbz"
226                 else
227                         fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
228                         if [ "x$fetchedMD5" = "x$validMD5" ]
229                         then
230                                 needs_fetch=no
231                         else
232                                 mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
233                         fi
234                 fi
235         fi
236         if [ $needs_fetch = yes ]
237         then
238                 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
239                 while [ $itrial -ge 1 ]
240                 do
241                         if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
242                                 "$pkg.tbz" "$tmp_work"
243                         then
244                                 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
245                                 [ "x$fetchedMD5" = "x$validMD5" ] && break
246                                 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
247                         fi
248                         itrial=$(($itrial-1))
249                 done
250                 [ $itrial -ge 1 ] || return
251                 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
252         fi
253         echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
254 }
255
256 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
257 pkgsys_fetch_legacy_remote__fetched_pkg ()
258 {
259         cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
260 }
261
262 # ============= Check whether the dependency of a legacy package is the latest =============
263 pkgsys_is_dependency_of_a_legacypkg_latest ()
264 {
265         local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
266         pkgarc=$1
267         tmp_extract=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:extract
268         tmp_contents=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:contents
269         tmp_origin=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:origin
270         tmp_pkg=${TMPDIR}/pkgng:pkgsys_is_dependency_of_a_legacypkg_latest:pkg
271         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
272         [ -e "$pkgarc" ] || return
273         rm -rf "$tmp_extract"
274         mkdir "$tmp_extract"
275         tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
276         grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
277                 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
278         nlines=`wc -l < $tmp_contents`
279         iline=1
280         while [ $iline -le $nlines ]
281         do
282                 origin_req=`sed -n ${iline}p "$tmp_contents"`
283                 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
284                 iline=$(($iline+2))
285                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
286                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
287                 then
288                         message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
289                         return 1
290                 fi
291         done
292         :
293 }
294
295 # ============= Check whether legacy package tools are available =============
296 pkgsys_is_legacy_tool_available ()
297 {
298         which -s pkg_info
299 }
300
301 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
302 pkgsys_def_pkgtools ()
303 {
304         if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf -o \( -e "${DBDIR}/WITH_PKGNG" -a ! -e /etc/make.conf \) ]
305         then
306                 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
307         else
308                 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKG | tr '[:upper:]' '[:lower:]'`
309                 [ -n "$PKGSYS_USE_PKGNG" ] || PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
310                 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
311                 then
312                         echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
313                         mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
314                 fi
315         fi
316         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
317         then
318                 export WITH_PKG=yes
319                 export WITH_PKGNG=yes
320                 PKGSYS_CMD_PKG_DELETE='pkg delete'
321                 pkg_is_tool_available ()
322                 {
323                         if [ -x /usr/sbin/pkg ]
324                         then
325                                 pkg -N 2> /dev/null && return
326                                 env ASSUME_ALWAYS_YES=yes pkg bootstrap -f
327                                 pkg -N 2> /dev/null
328                         else
329                                 which -s pkg && return
330                         fi
331                 }
332                 pkg_info_Ea ()
333                 {
334                         pkg info -qa 2> /dev/null
335                 }
336 #               pkg_info_qoa ()
337 #               {
338 #                       pkg info -qoa 2> /dev/null
339 #               }
340 #               pkg_info_qox ()
341 #               {
342 #                       pkg info -qox "$@" 2> /dev/null
343 #               }
344 #               pkg_info_qoX ()
345 #               {
346 #                       pkg info -qox "$@" 2> /dev/null
347 #               }
348                 pkg_info_qO ()
349                 {
350                         pkg info -qO "$@" 2> /dev/null
351                 }
352                 pkg_info_qo ()
353                 {
354                         pkg info -qo "$@" 2> /dev/null
355                 }
356                 pkg_info_qr ()
357                 {
358                         pkg info -qd "$@" 2> /dev/null
359                 }
360                 pkg_info_e ()
361                 {
362                         pkg info -e "$@" 2> /dev/null
363                 }
364                 pkg_info_eO ()
365                 {
366                         pkg info -eO "$@" 2> /dev/null
367                 }
368                 pkg_info_Eg ()
369                 {
370                         pkg info -Eg "$@" 2> /dev/null
371                 }
372                 pkg_info_qR ()
373                 {
374                         pkg info -qr "$@" 2> /dev/null
375                 }
376                 pkg_info_Ex ()
377                 {
378                         pkg info -Ex "$@" 2> /dev/null
379                 }
380                 pkg_info_qL ()
381                 {
382                         pkg info -ql "$@" 2> /dev/null
383                 }
384                 pkg_info_flavor ()
385                 {
386                         local glob_unflavored
387                         glob_unflavored=$1
388                         pkg query -g '%At\t%Av' "$glob_unflavored" 2> /dev/null | grep -E '^flavor[[:space:]]' | cut -f 2
389                 }
390                 pkg_info_flavored_origin ()
391                 {
392                         local glob_unflavored origin_unflavored
393                         glob_unflavored=$1
394                         origin_unflavored=`pkg_info_qo "$glob_unflavored" 2> /dev/null || :`
395                         flavor=`pkg_info_flavor "$glob_unflavored" 2> /dev/null || :`
396                         [ -z "$flavor" ] || flavor=@$flavor
397                         echo "$origin_unflavored$flavor"
398                 }
399                 pkg_info_all_flavored_origins ()
400                 {
401                         local tmp_flavored tmp_flavored_ptn
402                         tmp_flavored_ptn=${TMPDIR}/pkg_info_all_flavored_origins:flavored_ptn
403                         pkg query '%o\t%At\t%Av' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor[[:space:]]' | cut -f 1,3 | tr '\t' @ | sed 's/@$//'
404                         pkg query '%n\t%At' 2> /dev/null | grep -E '^[^[:space:]]+[[:space:]]flavor$' | cut -f 1 > $tmp_flavored_ptn
405                         pkg query '%n\t%o' 2> /dev/null | grep -vFx -f "$tmp_flavored_ptn" | cut -f 2
406                 }
407                 pkg_check_sanity ()
408                 {
409                         local pkg tmp_stdout tmp_stderr
410                         pkg=$1
411                         tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
412                         tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
413                         pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
414                         {
415                                 grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
416                                 grep '^[^:]*: missing file ' "$tmp_stderr" | sed 's/^[^:]*: missing file //' || :
417                                 if grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr" # For the old specification of pkg(8)
418                                 then
419                                         pkg info -ql "$pkg" | while read filepath
420                                         do
421                                                 [ -e "$filepath" ] || echo "$filepath"
422                                         done
423                                 fi
424                         } | sort -u || :
425                 }
426                 pkg_which ()
427                 {
428                         local filepath
429                         filepath=$1
430                         pkg which -q "$filepath" 2> /dev/null
431                 }
432                 pkg_info_gen_pkg_origin_table ()
433                 {
434                         #       pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
435                         pkg info -qa | while read pkgname
436                         do
437                                 origin=`pkg_info_flavored_origin "$pkgname"`
438                                 printf '%s\t%s\n' "$pkgname" "$origin"
439                         done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
440                 }
441                 pkg_create_b ()
442                 {
443                         pkg create "$@"
444                 }
445                 pkg_delete_f ()
446                 {
447                         local opt_del
448                         opt_del=
449                         [ $no_exec_inst_script = yes ] && opt_del='-D'
450                         pkg delete -fqy $opt_del "$@"
451                         pkg -N 2> /dev/null || return 0 # If pkg(8) is deinstalled successfully
452                         pkg info -e "$@" || return 0    # Countermeasure for a bug found for pkg-1.3.4 (at least not until 1.2.7_4)
453                         pkg delete -fy $opt_del "$@"
454                 }
455                 pkg_add_tools ()
456                 {
457                         local pkgarc tmp_extract prefix prefix_parent pkg
458                         pkgarc=$1
459                         tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
460                         rm -rf "$tmp_extract"
461                         mkdir "$tmp_extract"
462                         tar xf "$pkgarc" -C "$tmp_extract"
463                         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
464                         if env ASSUME_ALWAYS_YES=YES $tmp_extract/usr/local/sbin/pkg-static add "$pkgarc"
465                         then
466                                 message_echo "INFO: $pkg is successfully registered."
467                         else
468                                 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
469                                 return 1
470                         fi
471                 }
472                 pkg_add_f ()
473                 {
474                         local pkgarc pkg pkg_tool pkg_gen opt_add
475                         pkg_tool=
476                         pkg_gen=
477                         for pkgarc in "$@"
478                         do
479                                 pkg=`basename "$pkgarc"`
480                                 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
481                                 then
482                                         pkg_tool=$pkgarc
483                                 else
484                                         pkg_gen="$pkg_gen $pkgarc"
485                                 fi
486                         done
487                         [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
488                         if [ -n "$pkg_gen" ]
489                         then
490                                 rm -rf "${TMPDIR}/pkg_add_f"
491                                 mkdir -p "${TMPDIR}/pkg_add_f"
492                                 opt_add=
493                                 [ $no_exec_inst_script = yes ] && opt_add='-I'
494                                 ( cd "${TMPDIR}/pkg_add_f" && ln -s $pkg_gen && env ASSUME_ALWAYS_YES=YES pkg add $opt_add * )
495                         fi
496                 }
497                 pkg_add_fF ()
498                 {
499                         pkg_add_f "$@"
500                 }
501                 pkg_inst_verify_pkg ()
502                 {
503                         local pkg pkgarc
504                         pkg=$1
505                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
506                         tar tf "$pkgarc" > /dev/null 2> /dev/null
507                 }
508                 pkg_inst_remote_fetch ()
509                 {
510                         local pkg
511                         pkg=$1
512                         pkg fetch -yU "$pkg"
513                 }
514                 pkg_inst_remote_verify_fetch ()
515                 {
516                         local pkg
517                         pkg=$1
518                         pkg_inst_verify_pkg "$pkg" && return
519                         pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
520                 }
521                 pkg_inst_remote ()
522                 {
523                         local pkg pkgarc opt_add
524                         pkg=$1
525                         pkg_inst_remote_verify_fetch "$pkg" || return
526                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
527                         rm -rf "${TMPDIR}/pkg_inst_remote"
528                         mkdir -p "${TMPDIR}/pkg_inst_remote"
529                         opt_add=
530                         [ $no_exec_inst_script = yes ] && opt_add='-I'
531                         ( cd "${TMPDIR}/pkg_inst_remote" && ln -s "$pkgarc" && env ASSUME_ALWAYS_YES=YES pkg add $opt_add * )
532                 }
533                 pkg_inst_wild_verify_pkg ()
534                 {
535                         local pkg pkgarc
536                         pkg=$1
537                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}" "$pkg"` || return
538                         pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
539                 }
540                 pkg_inst_remote_wild_fetch ()
541                 {
542                         local pkg
543                         pkg=$1
544                         if pkg_is_tool_available
545                         then
546                                 pkg_inst_remote "$pkg" && return
547                         fi
548                         pkgsys_is_legacy_tool_available || return
549                         message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
550                         pkgsys_fetch_legacy_remote "$pkg"
551                 }
552                 pkg_inst_remote_wild_verify_fetch ()
553                 {
554                         local pkg
555                         pkg=$1
556                         pkg_inst_wild_verify_pkg "$pkg" && return
557                         pkg_inst_remote_wild_fetch "$pkg" && pkg_inst_wild_verify_pkg "$pkg"
558                 }
559                 pkg_inst_remote_wild ()
560                 {
561                         local pkg pkgarc opt_add
562                         pkg=$1
563                         if pkg_inst_remote_wild_verify_fetch "$pkg"
564                         then
565                                 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
566                                 rm -rf "${TMPDIR}/pkg_inst_remote_wild"
567                                 mkdir -p "${TMPDIR}/pkg_inst_remote_wild"
568                                 opt_add=
569                                 [ $no_exec_inst_script = yes ] && opt_add='-I'
570                                 ( cd "${TMPDIR}/pkg_inst_remote_wild" && ln -s "$pkgarc" && pkg_add -ifF $opt_add * ) || return
571                                 message_echo "INFO: Trying to convert the installed legacy package to pkgng."
572                                 pkg2ng || :
573                                 message_echo "INFO: Checking whether the conversion is successful."
574                                 pkg info -e "$pkg"
575                         fi
576                 }
577                 pkg_get_remote_repository_version ()
578                 {
579                         local origin origin_unflavored pkg
580                         origin=$1
581                         origin_unflavored=`expr "$origin" : '\([^@]*\)'` || return
582                         [ -n "$origin_unflavored" ] || return
583                         pkg fetch -qyU "$origin_unflavored" > /dev/null || return
584                         pkg=`pkg rquery -U %n-%v "$origin_unflavored"` || return
585                         echo "$pkg"
586                 }
587                 pkg_get_pkgs_timestamps ()
588                 {
589                         pkg query '%n-%v\tng-epoch:%t' "$@"
590                 }
591                 pkg_get_pkg_timestamp ()
592                 {
593                         local pkg
594                         pkg=$1
595                         pkg query '%t' "$pkg"
596                 }
597                 pkg_loadconf ()
598                 {
599                         local pkg_conf
600                         # Deafult configuration for pkg(1)
601                         PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
602                         PKGNG_SRV_MIRRORS=YES
603                         PKGNG_PKG_DBDIR=/var/db/pkg
604                         PKGNG_PKG_CACHEDIR=/var/cache/pkg
605                         PKGNG_PORTSDIR=/usr/ports
606                         PKGNG_PUBKEY=/etc/ssl/pkg.conf
607                         PKGNG_HANDLE_RC_SCRIPTS=NO
608                         PKGNG_PKG_MULTIREPOS=NO
609                         PKGNG_ASSUME_ALWAYS_YES=NO
610                         PKGNG_SYSLOG=YES
611                         PKGNG_SHLIBS=NO
612                         PKGNG_AUTODEPS=NO
613                         PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
614                         # Load configuration for pkg(1)
615                         pkg_conf=`pkg query %Fp pkg 2> /dev/null | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
616                         [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
617                         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
618                                 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
619                                 | while read srcline
620                         do
621                                 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
622                                 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
623                                 eval PKGNG_$var=\$val
624                                 misc_get_all_vardefs | grep "^PKGNG_$var="
625                         done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
626                         . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
627                 }
628                 pkg_rescue_tools ()
629                 {
630                         local origin_portsmgmt packagepath checksumpath pkgname is_successful
631                         origin_portsmgmt=`pkgsys_portsmgmt_pkg`
632                         packagepath=`pkgsys_get_backup_pkg "$origin_portsmgmt"` && \
633                                 pkg_add_tools "$packagepath" && return
634                         pkg_is_tool_available && return
635                         message_echo "WARNING: WITH_PKG or WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
636                         pkgsys_ready_checksum_file || return
637                         message_echo "INFO: Installing pkgng by legacy package tool."
638                         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
639                         pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
640                                 | grep -m 1 '^pkg-[0-9]'` || :
641                         [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" && return
642                         message_echo "INFO: Failed by package, so installing pkgng by port."
643                         grep -Ev '^[[:space:]]*WITH_PKG(|NG)=' /etc/make.conf > ${TMPDIR}/make.conf 2> /dev/null || :
644                         echo WITHOUT_PKG=yes >> ${TMPDIR}/make.conf
645                         echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
646                         ( set -e
647                                 unset WITH_PKG
648                                 unset WITH_PKGNG
649                                 unset WITHOUT_PKG
650                                 unset WITHOUT_PKGNG
651                                 message_echo "INFO: Attempting deinstallation of pkg(8) to make sure."
652                                 portsmgmt_port_path=`pkgsys_get_portpath_from_origin "$origin_portsmgmt"`
653                                 fs_fix_unionfs_image_if_hidden "${TMPDIR}/make.conf"
654                                 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" deinstall || :
655                                 message_echo "INFO: Attempting (re)installation by pkg(8)."
656                                 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "$portsmgmt_port_path" reinstall clean
657                         ) && {
658                                 pkg2ng || :
659                                 pkg_is_tool_available
660                         }
661                 }
662                 if ! pkg_rescue_tools
663                 then
664                         message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
665                 fi
666                 pkg_loadconf
667         elif ! pkgsys_is_legacy_tool_available
668         then
669                 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
670                 exit 1
671         else
672                 unset WITH_PKG
673                 unset WITH_PKGNG
674                 PKGSYS_USE_PKGNG=no
675                 PKGSYS_CMD_PKG_DELETE='pkg_delete'
676                 pkg_is_tool_available ()
677                 {
678                         pkgsys_is_legacy_tool_available
679                 }
680                 pkg_info_Ea ()
681                 {
682                         pkg_info -Ea 2> /dev/null
683                 }
684 #               pkg_info_qoa ()
685 #               {
686 #                       pkg_info -qoa 2> /dev/null
687 #               }
688 #               pkg_info_qox ()
689 #               {
690 #                       pkg_info -qox "$@" 2> /dev/null
691 #               }
692 #               pkg_info_qoX ()
693 #               {
694 #                       pkg_info -qoX "$@" 2> /dev/null
695 #               }
696                 pkg_info_qO ()
697                 {
698                         pkg_info -qO "$@" 2> /dev/null
699                 }
700                 pkg_info_qo ()
701                 {
702                         pkg_info -qo "$@" 2> /dev/null
703                 }
704                 pkg_info_qr ()
705                 {
706                         pkg_info -qr "$@" 2> /dev/null | sed -n 's/^@pkgdep[[:space:]]*//p'
707                 }
708                 pkg_info_e ()
709                 {
710                         pkg_info -e "$@" 2> /dev/null
711                 }
712                 pkg_info_eO ()
713                 {
714                         [ `pkg_info -qO "$@" 2> /dev/null | wc -l` -gt 0 ]
715                 }
716                 pkg_info_Eg ()
717                 {
718                         pkg_info -E "$@" 2> /dev/null
719                 }
720                 pkg_info_qR ()
721                 {
722                         pkg_info -qR "$@" 2> /dev/null | grep -v '^$'
723                 }
724                 pkg_info_Ex ()
725                 {
726                         pkg_info -Ex "$@" 2> /dev/null
727                 }
728                 pkg_info_qL ()
729                 {
730                         pkg_info -qL "$@" 2> /dev/null
731                 }
732                 pkg_info_flavor ()
733                 {
734                         pkg_info -e "$@" 2> /dev/null && echo
735                         :
736                 }
737                 pkg_info_flavored_origin ()
738                 {
739                         local glob_unflavored tmp_stdout
740                         glob_unflavored=$1
741                         pkg_info_qo "$glob_unflavored" 2> /dev/null
742                 }
743                 pkg_info_all_flavored_origins ()
744                 {
745                         pkg_info_qoa 2> /dev/null
746                 }
747                 pkg_check_sanity ()
748                 {
749                         local pkg tmp_stdout tmp_stderr
750                         pkg=$1
751                         tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
752                         tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
753                         pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
754                         sed -n 's/ fails the original MD5 checksum$//p' "$tmp_stdout" 2> /dev/null || :
755                         sed -nE "s/^pkg_info: (.*) doesn't exist$/\1/p" "$tmp_stderr" 2> /dev/null || :
756                 }
757                 pkg_which ()
758                 {
759                         local filepath
760                         filepath=$1
761                         pkg_info -qW "$filepath" 2> /dev/null
762                 }
763                 pkg_info_gen_pkg_origin_table ()
764                 {
765                         pkg_info -aE 2> /dev/null | while read pkg
766                         do
767                                 origin=`pkg_info -qo "$pkg" 2> /dev/null`
768                                 printf '%s\t%s\n' "$pkg" "$origin"
769                         done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
770                 }
771                 pkg_create_b ()
772                 {
773                         pkg_create -b "$@"
774                 }
775                 pkg_delete_f ()
776                 {
777                         local opt_del
778                         opt_del=
779                         [ $no_exec_inst_script = yes ] && opt_del='-D'
780                         pkg_delete -f $opt_del "$@"
781                 }
782                 pkg_add_f ()
783                 {
784                         local opt_add
785                         rm -rf "${TMPDIR}/pkg_add_f"
786                         mkdir -p "${TMPDIR}/pkg_add_f"
787                         ln -s "$@" "${TMPDIR}/pkg_add_f"
788                         opt_add=
789                         [ $no_exec_inst_script = yes ] && opt_add='-I'
790                         ( cd "${TMPDIR}/pkg_add_f" && pkg_add -if $opt_add * )
791                 }
792                 pkg_add_fF ()
793                 {
794                         local opt_add
795                         rm -rf "${TMPDIR}/pkg_add_f"
796                         mkdir -p "${TMPDIR}/pkg_add_f"
797                         ln -s "$@" "${TMPDIR}/pkg_add_f"
798                         opt_add=
799                         [ $no_exec_inst_script = yes ] && opt_add='-I'
800                         ( cd "${TMPDIR}/pkg_add_f" && pkg_add -ifF $opt_add * )
801                 }
802                 pkg_inst_verify_pkg ()
803                 {
804                         local pkg pkgarc
805                         pkg=$1
806                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg` || return
807                         pkgsys_is_dependency_of_a_legacypkg_latest "$pkgarc"
808                 }
809                 pkg_inst_remote_fetch ()
810                 {
811                         local pkg
812                         pkg=$1
813                         pkgsys_fetch_legacy_remote "$pkg"
814                 }
815                 pkg_inst_remote_verify_fetch ()
816                 {
817                         local pkg
818                         pkg=$1
819                         pkg_inst_verify_pkg "$pkg" && return
820                         pkg_inst_remote_fetch "$pkg" && pkg_inst_verify_pkg "$pkg"
821                 }
822                 pkg_inst_remote ()
823                 {
824                         local pkg pkgarc opt_add
825                         pkg=$1
826                         if pkg_inst_remote_verify_fetch "$pkg"
827                         then
828                                 pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
829                                 rm -rf "${TMPDIR}/pkg_inst_remote"
830                                 mkdir -p "${TMPDIR}/pkg_inst_remote"
831                                 ln -s "$@" "${TMPDIR}/pkg_add_f"
832                                 opt_add=
833                                 [ $no_exec_inst_script = yes ] && opt_add='-I'
834                                 ( cd "${TMPDIR}/pkg_inst_remote" && ls -s "$pkgarc" && pkg_add -ifF $opt_add * )
835                         fi
836                 }
837                 pkg_inst_wild_verify_pkg ()
838                 {
839                         pkg_inst_verify_pkg "$@"
840                 }
841                 pkg_inst_remote_wild_fetch ()
842                 {
843                         pkg_inst_remote_fetch "$@"
844                 }
845                 pkg_inst_remote_wild_verify_fetch ()
846                 {
847                         pkg_inst_remote_verify_fetch "$@"
848                 }
849                 pkg_inst_remote_wild ()
850                 {
851                         pkg_inst_remote "$@"
852                 }
853                 pkg_get_remote_repository_version ()
854                 {
855                         local origin checksumpath version_regexp pkgbase_regexp pkg
856                         origin=$1
857                         pkgsys_ready_checksum_file > /dev/null || return
858                         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
859                         version_regexp=`database_build_make "$origin" -V PORTVERSION | str_escape_regexp_filter`
860                         pkgbase_regexp=`database_build_get_new_pkgname "$origin" | sed -E "s/$version_regexp$//" | str_escape_regexp_filter`
861                         pkg=`sed -nE "s/^MD5[[:space:]]*\(($pkgbase_regexp.*)\.tbz\)[[:space:]]*=.*/\1/p" "$checksumpath" | head -n 1`
862                         echo "$pkg"
863                 }
864                 pkg_get_pkgs_timestamps ()
865                 {
866                         ls -lD legacy:%s ${PKG_DBDIR} | \
867                                 sed -E 's/[[:space:]]+/ /g' | cut -w -f 6,7 | grep -v '^$' | \
868                                 sed -E 's/^([^[:space:]]*)[[:space:]]*([^[:space:]]*)/\2 \1/' | tr ' ' '\t'
869                 }
870                 pkg_get_pkg_timestamp ()
871                 {
872                         local pkg portdb
873                         pkg=$1
874                         portdb=`echo "$pkg" | sed 's/-[0-9].*//'`
875                         ls -lD legacy:%s "${PKG_DBDIR}/$portdb" | \
876                                 sed -E 's/[[:space:]]+/ /g' | cut -w -f 6 | grep -v '^$' 
877                 }
878                 pkg_loadconf () { :; }
879                 pkg_rescue_tools () { :; }
880         fi
881 }
882
883 # ============= Get the unflavored part of a flavored origin =============
884 pkgsys_get_unflavored_origin ()
885 {
886         local origin origin_unflavored
887         origin=$1
888         origin_unflavored=`echo "$origin" | sed 's/@.*$//'`
889         echo "$origin_unflavored"
890 }
891
892 # ============= Get the port path from a flavored origin =============
893 pkgsys_get_portpath_from_origin ()
894 {
895         local origin origin_unflavored
896         origin=$1
897         origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
898         echo "${PORTSDIR}/$origin_unflavored"
899 }
900
901 # ============= Get the flavor from a flavored origin =============
902 pkgsys_get_flavor_from_origin ()
903 {
904         local origin flavor
905         origin=$1
906         flavor=`echo "$origin" | sed -E 's/^[^@]*@?//'`
907         echo "$flavor"
908 }
909
910 # ============= Compose a flavored origin name =============
911 pkgsys_compose_flavored_origin ()
912 {
913         local origin flavor
914         origin=$1
915         flavor=$2
916         origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
917         [ -z "$flavor" ] || flavor=@$flavor
918         echo "$origin_unflavored$flavor"
919 }
920
921 # ============= Check existence of a port for a flavored origin =============
922 pkgsys_exists_port ()
923 {
924         local origin port_path
925         origin=$1
926         port_path=`pkgsys_get_portpath_from_origin "$origin"`
927         [ -d "$port_path" ]
928 }
929
930 # ============= Get the installed package name from a flavored origin =============
931 pkgsys_get_installed_pkg_from_origin ()
932 {
933         local origin origin_unflavored flavor_origin flavor_pkg
934         origin=$1
935         origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
936         flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
937         pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
938         do
939                 flavor_pkg=`pkg_info_flavor "$pkgname"`
940                 if [ "x$flavor_origin" = "x$flavor_pkg" ]
941                 then
942                         echo "$pkgname"
943                         break
944                 fi
945         done
946         :
947 }
948
949 # ============= Get the installed package name from glob patterns =============
950 pkgsys_get_installed_pkg_from_glob ()
951 {
952         local glob regexp
953         for glob in "$@"
954         do
955                 if regexp=`expr "$glob" : ':\(.*\)'`
956                 then
957                         pkg_info_Ex "$regexp"
958                 else
959                         pkg_info_Eg "$glob"
960                 fi
961         done | sort -u
962 }
963
964 # ============= Check existence of an installed package for a flavored origin =============
965 pkgsys_exists_from_orig ()
966 {
967         local origin origin_unflavored flavor_origin flavor_pkg
968         origin=$1
969         origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
970         pkg_info_eO "$origin_unflavored" 2> /dev/null || return
971         flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
972         flavor_pkg=`pkg_info_flavor "$origin_unflavored"`
973         [ "x$flavor_origin" = "x$flavor_pkg" ]
974 }
975
976 # ============= Check existence of initially or currently installed package for a flavored origin =============
977 pkgsys_exists_or_existed_from_orig ()
978 {
979         local origin
980         origin=$1
981         cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
982                 | grep -q -Fx "$origin" || pkgsys_exists_from_orig "$origin"
983 }
984
985 # ============= Get the name of an initially installed package for a flavored origin =============
986 pkgsys_get_init_pkg_from_orig ()
987 {
988         local origin tmppkg origin_regexp npkgs origin_unflavored flavor_origin flavor_pkg
989         origin=$1
990         tmppkg=${TMPDIR}/pkgsys_get_init_pkg_from_orig::pkg
991         origin_regexp=`str_escape_regexp "$origin"`
992         sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
993                 | cut -f 1 > $tmppkg || :
994         npkgs=`wc -l < $tmppkg`
995         if [ $npkgs -gt 0 ]
996         then
997                 cat "$tmppkg"
998         else
999                 origin_unflavored=`pkgsys_get_unflavored_origin "$origin"`
1000                 flavor_origin=`pkgsys_get_flavor_from_origin "$origin"`
1001                 pkg_info_qO "$origin_unflavored" 2> /dev/null | while read pkgname
1002                 do
1003                         flavor_pkg=`pkg_info_flavor "$pkgname"`
1004                         if [ "x$flavor_origin" = "x$flavor_pkg" ]
1005                         then
1006                                 echo "$pkgname"
1007                                 break
1008                         fi
1009                 done
1010         fi
1011         :
1012 }
1013
1014 # ============= Get the package name of this utility =============
1015 pkgsys_get_my_current_pkg ()
1016 {
1017         pkg_info_Ex "${APPNAME}-[0-9].*"
1018 }
1019
1020 # ============= Get the flavored origin of this utility =============
1021 pkgsys_get_my_origin ()
1022 {
1023         pkgsys_get_my_current_pkg | while read pkgname
1024         do
1025                 pkg_info_flavored_origin "$pkgname"
1026         done
1027 }
1028
1029 # ============= Get the flavored origin of an initially installed package by ambiguous matching =============
1030 pkgsys_init_pkg_orig_by_ambiguous_matching ()
1031 {
1032         local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
1033         pkg=$1
1034         origin=`pkg_info_flavored_origin "$pkg"`
1035         [ -n "$origin" ] && { echo "$origin"; return; }
1036         tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
1037         ambsuffix=
1038         norigins=0
1039         len_pkg=`echo -n "$pkg" | wc -c`
1040         if [ $len_pkg -gt 0 ]
1041         then
1042                 while :
1043                 do
1044                         pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
1045                         grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1046                                 | cut -f 2 > $tmporigin
1047                         norigins=`wc -l < $tmporigin`
1048                         [ $norigins -gt 0 ] && break
1049                         ambsuffix='[a-zA-Z0-9.,_+-]*'
1050                         len_pkg=$(($len_pkg-1))
1051                         [ $len_pkg -gt 0 ] || break
1052                         pkg=`echo -n "$pkg" | head -c $len_pkg`
1053                 done
1054         fi
1055         [ $norigins -eq 1 ] || return
1056         cat "$tmporigin"
1057 }
1058
1059 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
1060 pkgsys_show_pkg_updating_commands ()
1061 {
1062         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1063         then
1064                 if which -s pkg_updating
1065                 then
1066                         echo 'pkg-updating(8) or pkg_updating(1)'
1067                 else
1068                         echo 'pkg-updating(8)'
1069                 fi
1070         elif which -s pkg_updating
1071         then
1072                 echo 'pkg_updating(1)'
1073         fi
1074 }
1075
1076 # ============= Evaluation of flavored ports globs =============
1077 pkgsys_eval_ports_glob ()
1078 {
1079         local pkglist unflavored_origlist tmp_flavors
1080         pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
1081         unflavored_origlist=${DBDIR}/pkgsys_eval_ports_glob:origin_unflavored.lst
1082         tmp_flavors=${TMPDIR}/pkgsys_eval_ports_glob:flavors
1083         # Test the access privilege as the superuser or not
1084         if [ ! -r "$pkglist" ]
1085         then
1086                 if touch "$pkglist" 2>/dev/null
1087                 then
1088                         rm "$pkglist"
1089                 else
1090                         pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
1091                 fi
1092         fi
1093         if [ ! -r "$unflavored_origlist" ]
1094         then
1095                 if touch "$unflavored_origlist" 2>/dev/null
1096                 then
1097                         rm "$unflavored_origlist"
1098                 else
1099                         unflavored_origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
1100                 fi
1101         fi
1102         # Preparation of customized databases
1103         [ -f "$pkglist" ] \
1104                 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
1105         [ -f "$unflavored_origlist" ] \
1106                 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
1107                 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $unflavored_origlist
1108         # Evaluation
1109         while [ $# -gt 0 ]
1110         do
1111                 glob=$1
1112                 shift
1113                 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
1114                 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
1115                 if expr "$glob" : '.*/' > /dev/null 2>&1
1116                 then
1117                         if expr "$glob" : '.*@' > /dev/null 2>&1
1118                         then
1119                                 glob_regexp_unflavored=`expr "$glob_regexp" : '\([^@]*\)' || :`$
1120                                 glob_regexp_flavor=^`expr "$glob_regexp" : '[^@]*@\([^@]*\)' || :`
1121                                         
1122                                 grep -E "$glob_regexp_unflavored" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1123                                 do
1124                                         fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1125                                         make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1126                                                 tr '[:space:]' '\n' | grep -v '^$' | grep -E "$glob_regexp_flavor" | sed -E "s|^|$origin_unflavored@|"
1127                                 done
1128                                 {
1129                                         pkg_info_all_flavored_origins
1130                                         cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1131                                 } | grep -E "$glob_regexp" 2>&1 || :
1132                         else
1133                                 grep -E "$glob_regexp" "$unflavored_origlist" 2>&1 | while read origin_unflavored
1134                                 do
1135                                         origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1136                                         fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1137                                         make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1138                                                 tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1139                                         if [ `wc -l < $tmp_flavors` -gt 0 ]
1140                                         then
1141                                                 sed -E "s|^|$origin_unflavored_rpl@|" "$tmp_flavors"
1142                                         else
1143                                                 echo "$origin_unflavored"
1144                                         fi
1145                                 done
1146                         fi
1147                         glob_regexp_allflavors=`echo "$glob_regexp" | sed 's/$$/(|@.*)$/'`
1148                         {
1149                                 pkg_info_all_flavored_origins
1150                                 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
1151                         } | grep -E "$glob_regexp_allflavors" 2>&1 || :
1152                 else
1153                         if expr "$glob" : '[a-z][a-zA-Z0-9_.+-]*[a-zA-Z0-9_.+]$' > /dev/null 2>&1 && \
1154                                 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
1155                         then
1156                                 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1 || :`'-[0-9]'
1157                         else
1158                                 glob_regexp2=$glob_regexp
1159                         fi
1160                         grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
1161                                 | while read index
1162                         do
1163                                 sed -n ${index}p "$unflavored_origlist"
1164                         done | while read origin_unflavored
1165                         do
1166                                 origin_unflavored_rpl=`str_escape_replaceval "$origin_unflavored"`
1167                                 fs_fix_unionfs_image_if_hidden "${PORTSDIR}/$origin_unflavored"
1168                                 make -C "${PORTSDIR}/$origin_unflavored" -V FLAVORS 2> /dev/null | \
1169                                         tr '[:space:]' '\n' | grep -v '^$' > $tmp_flavors || :
1170                                 [ `wc -l < $tmp_flavors` -gt 0 ] || echo > $tmp_flavors
1171                                 sed -E "s/^/$origin_unflavored_rpl /" "$tmp_flavors"
1172                         done | while read origin_unflavored flavor
1173                         do
1174                                 if [ -n "$flavor" ]
1175                                 then
1176                                         if make -C "${PORTSDIR}/$origin_unflavored" package-name FLAVOR=$flavor 2> /dev/null | \
1177                                         grep -qE "$glob_regexp2"
1178                                         then
1179                                                 echo "$origin_unflavored@$flavor"
1180                                         fi
1181                                 else
1182                                         if make -C "${PORTSDIR}/$origin_unflavored" package-name 2> /dev/null | \
1183                                         grep -qE "$glob_regexp2"
1184                                         then
1185                                                 echo "$origin_unflavored"
1186                                         fi
1187                                 fi
1188                         done || :
1189                         glob_regexp2=`echo "$glob_regexp" | sed -E 's/\$*$//' 2>&1 || :`'[[:space:]]'
1190                         grep -E "$glob_regexp2" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
1191                                 | cut -f 2 || :
1192                         pkg_info_Ex "$glob_regexp" | while read pkgname
1193                         do
1194                                 pkg_info_flavored_origin "$pkgname"
1195                         done
1196                 fi
1197         done | sort -u
1198 }
1199
1200 # ============= Create a back-up package archive =============
1201 pkgsys_create_backup_pkg ()
1202 {
1203         local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath
1204         pkgname=$1
1205         dstdir=$2
1206         rm -rf "${TMPDIR}"/package.tmp
1207         mkdir "${TMPDIR}"/package.tmp
1208         origin=`pkg_info_flavored_origin "$pkgname"`
1209         if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` && \
1210                 [ ! -e "${DBDIR}/requires/$origin/installed_timestamp" -o \
1211                         "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
1212         then
1213                 echo "$backup_pkg_old"
1214                 return
1215         fi
1216         if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
1217         then
1218                 pkgname_ptn=`str_escape_regexp "$pkgname"`
1219                 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
1220                         grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1221         fi
1222         if [ -z "$backup_pkg" ]
1223         then
1224                 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
1225                 return 1
1226         fi
1227         dbpath=${DBDIR}/backup/$origin
1228         mkdir -p "$dbpath"
1229         pkg_info_qL "$pkgname" > $dbpath/previously_installed_files
1230         mkdir -p "$dstdir"
1231         mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
1232         pkgpath=$dstdir/$backup_pkg
1233         cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
1234                 while read origin_cur pkgpath_cur
1235                 do
1236                         [ "$pkgpath_cur" = "$pkgpath" ] && continue
1237                         if [ "$origin_cur" = "$origin" ]
1238                         then
1239                                 rm -f "$pkgpath_cur"
1240                         else
1241                                 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
1242                         fi
1243                 done > ${DBDIR}/backup_pkgarcs.lst.tmp
1244         printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
1245         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1246         echo "$pkgpath"
1247 }
1248
1249 # ============= Delete a back-up package archive for a flavored port origin =============
1250 pkgsys_delete_backup_pkg ()
1251 {
1252         local origin origin_regexp
1253         origin=$1
1254         origin_regexp=`str_escape_regexp "$origin"`
1255         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1256                 | cut -f 2 | while read pkgpath_cur
1257                 do
1258                         rm -f "$pkgpath_cur"
1259                 done
1260         grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
1261                 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
1262         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
1263 }
1264
1265 # ============= Get an existing package archive path for a flavored port origin =============
1266 pkgsys_get_backup_pkg ()
1267 {
1268         local origin tmpnewest origin_regexp
1269         origin=$1
1270         tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
1271         origin_regexp=`str_escape_regexp "$origin"`
1272         rm -f "$tmpnewest"
1273         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1274                 | cut -f 2 | while read pkgpath
1275         do
1276                 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
1277                 [ -e "$pkgpath" ] || continue
1278                 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
1279                 echo "$pkgpath" > $tmpnewest
1280         done
1281         cat "$tmpnewest" 2> /dev/null
1282 }
1283
1284 # ============= Get a file list to be restored by the current backup package for a flavored port origin =============
1285 pkgsys_get_restored_files_by_backup_pkg ()
1286 {
1287         local origin
1288         origin=$1
1289         cat "${DBDIR}/backup/$origin/previously_installed_files" 2> /dev/null || :
1290 }
1291
1292 # ============= Check whether any file match restored files by the current backup package for a flavored port origin =============
1293 pkgsys_chk_match_to_restored_files_by_backup_pkg ()
1294 {
1295         local origin filelist dbfile
1296         origin=$1
1297         filelist=$2
1298         dbfile=${DBDIR}/backup/$origin/previously_installed_files
1299         grep -qFx -f "$filelist" "$dbfile" 2> /dev/null
1300 }
1301
1302 # ============= Get a package name from a package archive file name =============
1303 pkgsys_pkgarc_to_pkgname ()
1304 {
1305         local pkgfile_path
1306         pkgfile_path=$1
1307         basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
1308 }
1309
1310 # ============= Get the file name of an existing package archive for a package name =============
1311 pkgsys_pkgname_to_pkgarc ()
1312 {
1313         local pkgdir pkgname pkgname_ptn pkgnode
1314         pkgdir=$1
1315         pkgname=$2
1316         [ -n "$pkgname" ] || return 1
1317         [ -d "$pkgdir" ] || return 1
1318         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
1319         then
1320                 pkgname_ptn=`str_escape_regexp "$pkgname"`
1321                 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
1322         elif [ -e "$pkgdir/$pkgname.tbz" ]
1323         then
1324                 pkgnode=$pkgname.tbz
1325         fi
1326         [ -n "$pkgnode" ] || return 1
1327         echo "$pkgdir/$pkgnode"
1328 }
1329
1330 # ============= Get flavored port origins matching a glob pattern even if nonexistent =============
1331 pkgsys_eval_ports_glob_even_if_nonexistent ()
1332 {
1333         local glob_pattern
1334         glob_pattern=$1
1335         {
1336                 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
1337                 echo "$glob_pattern" | grep -E '^[a-z]+/[a-zA-Z0-9_.+-]+(|@[a-zA-Z0-9_.+-]+)$' || :
1338         } | grep -v -e '^$' | sort -u
1339 }
1340
1341 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
1342 pkgsys_register_evaluated_globs ()
1343 {
1344         local mode listpath dirpath tmp_evaluated
1345         mode=$1
1346         listpath=$2
1347         shift 2
1348         dirpath=`dirname "$listpath"`
1349         tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
1350         echo "$@" | sed -E 's/[ :]+/\
1351 /g' | grep -v '^$' | sort -u | while read -r glob
1352         do
1353                 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
1354                 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
1355                 {
1356                         message_echo "WARNING: No matching ports/package glob [$glob]." >&2
1357                         continue
1358                 }
1359                 cat "$tmp_evaluated"
1360         done | while read origin
1361         do
1362                 mkdir -p "$dirpath"
1363                 case $mode in
1364                 remove )        fileedit_rm_a_line "$origin" "$listpath"
1365                         ;;
1366                 add )   fileedit_add_a_line_if_new "$origin" "$listpath"
1367                         ;;
1368                 esac
1369         done
1370 }
1371
1372 # ============= Evaluate glob patterns for installed packages =============
1373 pkgsys_eval_installed_pkgs_globs ()
1374 {
1375         local tmp_evaluated
1376         tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
1377         rm -f "$tmp_evaluated"
1378         pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
1379         [ -e "$tmp_evaluated" ] || return 0
1380         while read origin
1381         do
1382                 pkgsys_exists_or_existed_from_orig "$origin" || echo "$origin"
1383         done < $tmp_evaluated
1384 }
1385
1386 # ============= Get glob patterns of conflicting packages of a port =============
1387 pkgsys_get_conflicting_pkgs_patterns ()
1388 {
1389         local mode origin conflicts conflicts_makevar conflicts_config
1390         mode=$1
1391         origin=$2
1392         conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
1393         case $mode in
1394         build )
1395                 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
1396                 conflicts_config=`database_query_get_config_val "$origin" BUILDCONFLICT`
1397                 ;;
1398         install )
1399                 conflicts_makevar=`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
1400                 conflicts_config=`database_query_get_config_val "$origin" INSTCONFLICT`
1401                 ;;
1402         *)
1403                 conflicts_makevar=
1404                 conflicts_config=
1405                 ;;
1406         esac
1407         echo "$conflicts $conflicts_makevar $conflicts_config" | tr ' ' '\n' | grep -v '^$' | sort -u
1408 }       
1409
1410 # ============= Get conflicting installed packages of a port =============
1411 pkgsys_get_conflicting_installed_pkgs ()
1412 {
1413         local mode origin tmp_conflicts
1414         mode=$1
1415         origin=$2
1416         tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
1417         pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" | while read pkg_pattern
1418         do
1419                 pkgsys_get_installed_pkg_from_glob "$pkg_pattern" || :
1420         done > $tmp_conflicts
1421         cat "$tmp_conflicts"
1422         [ `wc -l < $tmp_conflicts` -gt 0 ]
1423 }       
1424
1425 # ============= Check whether a package conflicts with a port =============
1426 pkgsys_chk_conflict_by_a_pkg ()
1427 {
1428         local mode origin pkg tmp_conflicts_ptn
1429         mode=$1
1430         origin=$2
1431         pkg=$3
1432         tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
1433         pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
1434                 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
1435         echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
1436 }       
1437
1438 # ============= Check whether installed files are lost or broken for a package =============
1439 pkgsys_sanitychk_pkgcontents ()
1440 {
1441         local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1442         pkg=$1
1443         var_is_reinstall_encouraged=$2
1444         tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1445         pkg_check_sanity "$pkg" > $tmp_sanity || :
1446         eval "$var_is_reinstall_encouraged=no"
1447         [ `wc -l < $tmp_sanity` -eq 0 ] && return
1448         nlines=`wc -l < $tmp_sanity`
1449         iline=1
1450         _is_reinstall_encouraged=no
1451         while [ $iline -le $nlines ]
1452         do
1453                 filename=`sed -n ${iline}p "$tmp_sanity"`
1454                 iline=$(($iline+1))
1455                 if [ ! -e "$filename" ]
1456                 then
1457                         _is_reinstall_encouraged=yes
1458                         break
1459                 fi
1460                 if expr "$filename" : '.*/include/.*' > /dev/null
1461                 then
1462                         _is_reinstall_encouraged=yes
1463                         break
1464                 fi
1465                 filename_esc=`str_escape_regexp "$filename"`
1466                 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
1467                 then
1468                         _is_reinstall_encouraged=yes
1469                         break
1470                 fi
1471                 pkg_owner=`pkg_which "$filename"`
1472                 if [ "$pkg" != "$pkg_owner" ]
1473                 then
1474                         _is_reinstall_encouraged=yes
1475                         break
1476                 fi
1477         done
1478         eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1479         origin=`pkg_info_flavored_origin "$pkg"`
1480         if [ $opt_batch_mode = no ]
1481         then
1482                 message_echo "[$pkg ($origin)]"
1483                 sed 's/^/  /' "$tmp_sanity"
1484                 message_echo
1485         else
1486                 pkg_replace=`str_escape_replaceval "$pkg"`
1487                 origin_replace=`str_escape_replaceval "$origin"`
1488                 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1489         fi
1490         return 1
1491 }
1492
1493 # ============= Check whether the port options database is once saved =============
1494 pkgsys_exists_saved_port_oprions_timestamps ()
1495 {
1496         [ -d "${DBDIR}/ls_dbdir" ]
1497 }
1498
1499 # ============= Get the current all timestamp information of port options =============
1500 pkgsys_get_current_port_oprions_timestamp ()
1501 {
1502         local portdb_needle_regexp
1503         portdb_needle_regexp=`str_escape_regexp "$1"`
1504         {
1505                 ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}" | if [ -n "$portdb_needle_regexp" ]
1506                 then
1507                         grep -E "[[:space:]]$portdb_needle_regexp$" || :
1508                 else
1509                         cat
1510                 fi
1511         } 2> /dev/null | cut -w -f 6,7 | while read datetime portdb
1512         do
1513                 [ -d "${PORT_DBDIR}/$portdb" ] || continue
1514                 if [ -e "${PORT_DBDIR}/$portdb/options" ]
1515                 then
1516                         datetime=`ls -lD %Y%m%d:%H%M%S "${PORT_DBDIR}/$portdb/options" 2> /dev/null | cut -w -f 6`
1517                 fi
1518                 printf '%s\t%s\n' "$portdb" "$datetime"
1519         done
1520 }
1521
1522 # ============= Get the saved all timestamp information of port options =============
1523 pkgsys_get_saved_port_oprions_timestamps_all ()
1524 {
1525         cat "${DBDIR}/ls_dbdir/"*.log > $tmp_ls.db 2> /dev/null || :
1526 }
1527
1528 # ============= Convert a list of port origins to port options timestamp log names =============
1529 pkgsys_conv_portorigin_to_port_oprion_timestamp_logname ()
1530 {
1531         sed 's|/|_|'
1532 }
1533
1534 # ============= Save the timestamp information of port options of a port =============
1535 pkgsys_save_port_oprion_timestamp ()
1536 {
1537         local origin portoptlog portoptdb
1538         origin=$1
1539         portoptlog=`echo "$origin" | pkgsys_conv_portorigin_to_port_oprion_timestamp_logname`
1540         if pkgsys_is_dialog4ports_used
1541         then
1542                 portoptdb=`echo "$portoptlog" | sed 's/@.*//'`
1543         else
1544                 portoptdb=`database_build_make "$origin" -V UNIQUENAME`
1545         fi
1546         mkdir -p "${DBDIR}/ls_dbdir"
1547         pkgsys_get_current_port_oprions_timestamp "$portoptdb" > ${DBDIR}/ls_dbdir/$portoptlog.log 2> /dev/null || :
1548 }
1549
1550 # ============= Get changed port options from the saved point =============
1551 pkgsys_get_changed_port_oprions ()
1552 {
1553         local saved_log current_log tmp_log
1554         saved_log=$1
1555         current_log=$2
1556         tmp_log=${TMPDIR}/pkgsys_get_changed_port_oprions.log
1557         {
1558                 grep -vxF -f "$current_log" "$saved_log" || :
1559                 grep -vxF -f "$saved_log" "$current_log" || :
1560         } | cut -w -f 1 | grep -v '^$' | sort -u > $tmp_log
1561         if pkgsys_is_dialog4ports_used
1562         then
1563                 grep '_' "$tmp_log" || :
1564         else
1565                 cat "$tmp_log"
1566         fi
1567 }
1568
1569 # ============= Convert a list of port origins to port options database names =============
1570 pkgsys_conv_portorigins_to_portoptiondbs ()
1571 {
1572         if pkgsys_is_dialog4ports_used
1573         then
1574                 sed 's|_|/|'
1575         else
1576                 cat "$tmp_ls.diff"
1577         fi
1578 }
1579         
1580 # ============= Register nonexistent port options databases =============
1581 pkgsys_register_list_nonexistent_portopriondb ()
1582 {
1583         local dbpath
1584         dbpath=${DBDIR}/ls_dbdir/%NONEXISTENT%.log
1585         cat > $dbpath.new
1586         touch  "$dbpath"
1587         sort -u "$dbpath.new" "$dbpath" > $dbpath.tmp
1588         mv "$dbpath.tmp" "$dbpath"
1589 }