OSDN Git Service

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