OSDN Git Service

Version 3.0.1+toward_3.0.2_20130703190902
[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 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=no     # no: legacy pkg_* tools, yes: the new generation package (pkgng)
11 PKGSYS_CMD_PKG_INFO='pkg_info'  # Corresponding command for pkg_info
12 PKGSYS_CMD_PKG_CREATE='pkg_create'      # Corresponding command for pkg_create
13 PKGSYS_CMD_PKG_DELETE='pkg_delete'      # Corresponding command for pkg_delete
14 PKGSYS_CMD_PKG_ADD='pkg_add'    # Corresponding command for pkg_add
15 PKGSYS_AVR_REFETCH_TIMES_PER_SITE=1     # Average number (integer) of retrials for retrieving package or distfiles per mirror site
16 PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR=2        #  Number (integer) of retrials for check sum error in retrieving a package
17
18 # ============= System defined value for the ports/packages =============
19 pkgsys_sysvar ()
20 {
21         local var tmp_work
22         var=$1
23         tmp_work=${TMPDIR}/pkgsys_sysvar:work
24         rm -rf "$tmp_work"
25         mkdir "$tmp_work"
26         make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var"
27 }
28
29 # ============= Get the file name of package check sum file =============
30 pkgsys_pkgchksum_file ()
31 {
32         echo CHECKSUM.MD5
33 }
34
35 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
36 pkgsys_is_pkgtool ()
37 {
38         case $1 in
39         ports-mgmt/pkg|ports-mgmt/dialog4ports)
40                 ;;
41         *)      return 1;;
42         esac
43 }
44
45 # ============= Check whether the dialog for selecting port options is dialog4ports =============
46 pkgsys_is_dialog4ports_used ()
47 {
48         [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
49 }
50
51 # ============= Get the number of mirror sites for legacy packages =============
52 pkgsys_num_mirrorsites ()
53 {
54         local siteroots
55         siteroots=$1
56         echo "$siteroots" | tr '|' '\n' | wc -l
57 }
58
59 # ============= Get a URL one of mirror sites =============
60 pkgsys_url_from_mirrors ()
61 {
62         local siteroots subdir nsites id_site site platform version
63         siteroots=$1
64         subdir=$2
65         nsites=`pkgsys_num_mirrorsites "$siteroots"`
66         id_site=`(set +e; random -e $nsites; echo $?)`
67         site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
68         platform=`uname -p`
69         version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
70         echo -n "$site"
71         printf "$subdir\n" "$platform" "$version"
72 }
73
74 # ============= Fetch a file from one of mirror sites =============
75 pkgsys_fetch_from_mirrors ()
76 {
77         local siteroots subdir filename dst tmp_work fetch itrial origdir url
78         siteroots=$1
79         subdir=$2
80         filename=$3
81         dst=$4
82         tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
83         rm -rf "$tmp_work"
84         mkdir "$tmp_work"
85         fetch=`pkgsys_sysvar FETCH_CMD`
86         itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
87         origdir=`pwd`
88         cd "$tmp_work"
89         while [ $itrial -ge 1 ]
90         do
91                 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
92                 message_echo "INFO: Fetching from $url:"
93                 $fetch "$url"&& break
94                 rm -f "$filename"
95                 itrial=$(($itrial-1))
96         done
97         cd "$origdir"
98         [ -e "$tmp_work/$filename" ] || return
99         mv "$tmp_work/$filename" "$dst"
100 }
101
102 # ============= Get the package check sums file ready =============
103 pkgsys_ready_checksum_file ()
104 {
105         local chksumfile
106         tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
107         rm -f "$tmp_savedpath"
108         chksumfile=`pkgsys_pkgchksum_file`
109         if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
110         then
111                 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
112                 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
113                         "$chksumfile" "${DBDIR}/checksum"
114                 then
115                         message_echo "WARNING: No check sum file is available." >&2
116                         return 1
117                 fi
118         fi
119         echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
120 }
121
122 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
123 pkgsys_ready_checksum_file__fetched_file ()
124 {
125         cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
126 }
127
128 # ============= Fetch a legacy package from one of remote sites =============
129 pkgsys_fetch_legacy_remote ()
130 {
131         local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
132         pkg=$1
133         tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
134         tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
135         rm -rf "$tmp_work"
136         mkdir "$tmp_work"
137         pkg_regexp=`str_escape_regexp "$pkg"`
138         pkgsys_ready_checksum_file || return
139         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
140         validMD5=`grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
141         if [ -z "$validMD5" ]
142         then
143                 message_echo "WARNING: No check sum for $pkg.tbz." >&2
144                 return 1
145         fi
146         needs_fetch=yes
147         [ -d "${PKGREPOSITORY}" ] || mkdir -p "${PKGREPOSITORY}"
148         if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
149         then
150                 fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
151                 if [ "x$fetchedMD5" = "x$validMD5" ]
152                 then
153                         needs_fetch=no
154                 else
155                         mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
156                 fi
157         fi
158         if [ $needs_fetch = yes ]
159         then
160                 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
161                 while [ $itrial -ge 1 ]
162                 do
163                         if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
164                                 "$pkg.tbz" "$tmp_work"
165                         then
166                                 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
167                                 [ "x$fetchedMD5" = "x$validMD5" ] && break
168                                 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
169                         fi
170                         itrial=$(($itrial-1))
171                 done
172                 [ $itrial -ge 1 ] || return
173                 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
174         fi
175         echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
176 }
177
178 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
179 pkgsys_fetch_legacy_remote__fetched_pkg ()
180 {
181         cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
182 }
183
184 # ============= Check whether the dependency of a legacy package is the latest =============
185 pkg_is_dependency_of_a_legacypkg_latest ()
186 {
187         local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
188         pkgarc=$1
189         tmp_extract=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:extract
190         tmp_contents=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:contents
191         tmp_origin=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:origin
192         tmp_pkg=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:pkg
193         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
194         [ -e "$pkgarc" ] || return
195         rm -rf "$tmp_extract"
196         mkdir "$tmp_extract"
197         tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
198         grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
199                 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
200         nlines=`wc -l < $tmp_contents`
201         iline=1
202         while [ $iline -le $nlines ]
203         do
204                 origin_req=`sed -n ${iline}p "$tmp_contents"`
205                 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
206                 iline=$(($iline+2))
207                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
208                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
209                 then
210                         message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
211                         return 1
212                 fi
213         done
214         :
215 }
216
217 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
218 pkgsys_def_pkgtools ()
219 {
220         if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf ]
221         then
222                 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
223         else
224                 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
225                 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
226                 then
227                         echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
228                         mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
229                 fi
230         fi
231         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
232         then
233                 export WITH_PKGNG=yes
234                 PKGSYS_CMD_PKG_INFO='pkg info'
235                 PKGSYS_CMD_PKG_CREATE='pkg create'
236                 PKGSYS_CMD_PKG_DELETE='pkg delete'
237                 PKGSYS_CMD_PKG_ADD='pkg add'
238                 pkg_is_tool_available ()
239                 {
240                         which -a pkg | grep -v '^/usr/sbin/pkg$' | grep -q '/sbin/pkg$'
241                 }
242                 pkg_info_Ea ()
243                 {
244                         pkg info -qa 2> /dev/null
245                 }
246                 pkg_info_qoa ()
247                 {
248                         pkg info -qoa 2> /dev/null
249                 }
250 #               pkg_info_qox ()
251 #               {
252 #                       pkg info -qox "$@" 2> /dev/null
253 #               }
254                 pkg_info_qoX ()
255                 {
256                         pkg info -qoX "$@" 2> /dev/null
257                 }
258                 pkg_info_qO ()
259                 {
260                         pkg info -qO "$@" 2> /dev/null
261                 }
262                 pkg_info_qo ()
263                 {
264                         pkg info -qo "$@" 2> /dev/null
265                 }
266                 pkg_info_qr ()
267                 {
268                         pkg info -qd "$@" 2> /dev/null
269                 }
270                 pkg_info_e ()
271                 {
272                         pkg info -e "$@" 2> /dev/null
273                 }
274                 pkg_info_eO ()
275                 {
276                         pkg info -eO "$1" 2> /dev/null
277                 }
278                 pkg_info_Eg ()
279                 {
280                         pkg info -Eg "$@" 2> /dev/null
281                 }
282                 pkg_info_qR ()
283                 {
284                         pkg info -qr "$@" 2> /dev/null
285                 }
286                 pkg_info_Ex ()
287                 {
288                         pkg info -Ex "$@" 2> /dev/null
289                 }
290                 pkg_info_qL ()
291                 {
292                         pkg info -ql "$@" 2> /dev/null
293                 }
294                 pkg_check_sanity ()
295                 {
296                         local pkg
297                         pkg=$1
298                         pkg check -s "$pkg" 2> /dev/null
299                 }
300                 pkg_info_gen_pkg_origin_table ()
301                 {
302                         pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
303                 }
304                 pkg_create_b ()
305                 {
306                         pkg create "$@"
307                 }
308                 pkg_delete_f ()
309                 {
310                         pkg delete -fqy "$@"
311                 }
312                 pkg_add_tools ()
313                 {
314                         local pkgarc tmp_extract prefix prefix_parent pkg
315                         pkgarc=$1
316                         tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
317                         rm -rf "$tmp_extract"
318                         mkdir "$tmp_extract"
319                         tar xf "$pkgarc" -C "$tmp_extract"
320                         prefix=`grep -m 1 '^prefix: ' "$tmp_extract/+MANIFEST" | sed 's/^prefix: *//'`
321                         prefix_parent=`dirname "$prefix"`
322                         cp -Rp "$tmp_extract/$prefix" "$prefix_parent"/
323                         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
324                         message_echo "INFO: Contents of $pkg are temporarily installed by simple copy."
325                         if env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
326                         then
327                                 message_echo "INFO: $pkg is successfully registered."
328                         else
329                                 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
330                         fi
331                 }
332                 pkg_add_f ()
333                 {
334                         local pkgarc pkg pkg_tool pkg_gen
335                         pkg_tool=
336                         pkg_gen=
337                         for pkgarc in "$@"
338                         do
339                                 pkg=`basename "$pkgarc"`
340                                 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
341                                 then
342                                         pkg_tool=$pkgarc
343                                 else
344                                         pkg_gen="$pkg_gen $pkgarc"
345                                 fi
346                         done
347                         [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
348                         [ -n "$pkg_gen" ] && env ASSUME_ALWAYS_YES=YES pkg add $pkg_gen
349                 }
350                 pkg_add_fF ()
351                 {
352                         pkg_add_f "$@"
353                 }
354                 pkg_inst_remote_fetch ()
355                 {
356                         local pkg mode pkgarc
357                         pkg=$1
358                         mode=$2
359                         tmp_extract=${TMPDIR}/pkgng:pkg_inst_remote:extract
360                         pkg fetch -yU "$pkg" || return
361                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
362                         [ "x$mode" = xnodepschk ] && return
363                         rm -rf "$tmp_extract"
364                         mkdir "$tmp_extract"
365                         tar xf "$pkgarc" -C "$tmp_extract" +MANIFEST
366                         sed -E '1,/^deps:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*([^:]+):[[:space:]]*\{origin:[[:space:]]*([^,]+),[[:space:]]*version:[[:space:]]*([^}]+)\}/\2\\\1-\3/' "$tmp_extract/+MANIFEST" \
367                                 | tr '\\' '\t' | while read origin_req pkg_req
368                         do
369                                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
370                                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
371                                 then
372                                         message_echo "WARNING: Requirements of remote package $pkg are not latest." >&2
373                                         return 1
374                                 fi
375                         done
376                         :
377                 }
378                 pkg_inst_remote ()
379                 {
380                         local pkg mode pkgarc
381                         pkg=$1
382                         mode=$2
383                         pkg_inst_remote_fetch "$pkg" "$mode" || return
384                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
385                         env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
386                 }
387                 pkg_inst_remote_wild_fetch ()
388                 {
389                         local pkg mode pkgarc
390                         pkg=$1
391                         mode=$2
392                         if pkg_is_tool_available
393                         then
394                                 pkg_inst_remote "$pkg" "$mode" && return
395                         fi
396                         message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
397                         pkgsys_fetch_legacy_remote "$pkg" || return
398                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
399                         [ "x$mode" = xnodepschk ] && return
400                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
401                 }
402                 pkg_inst_remote_wild ()
403                 {
404                         local pkg mode pkgarc
405                         pkg=$1
406                         mode=$2
407                         pkg_inst_remote_wild_fetch "$pkg" "$mode" || return
408                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
409                         pkg_add -ifF "$pkgarc" || return
410                         message_echo "INFO: Trying to convert the installed legacy package to pkgng."
411                         pkg2ng || :
412                         message_echo "INFO: Checking whether the conversion is successful."
413                         pkg info -e "$pkg"
414                 }
415                 pkg_loadconf ()
416                 {
417                         local pkg_conf
418                         # Deafult configuration for pkg(1)
419                         PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
420                         PKGNG_SRV_MIRRORS=YES
421                         PKGNG_PKG_DBDIR=/var/db/pkg
422                         PKGNG_PKG_CACHEDIR=/var/cache/pkg
423                         PKGNG_PORTSDIR=/usr/ports
424                         PKGNG_PUBKEY=/etc/ssl/pkg.conf
425                         PKGNG_HANDLE_RC_SCRIPTS=NO
426                         PKGNG_PKG_MULTIREPOS=NO
427                         PKGNG_ASSUME_ALWAYS_YES=NO
428                         PKGNG_SYSLOG=YES
429                         PKGNG_SHLIBS=NO
430                         PKGNG_AUTODEPS=NO
431                         PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
432                         # Load configuration for pkg(1)
433                         pkg_conf=`pkg query %Fp pkg | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'`
434                         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
435                                 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
436                                 | while read srcline
437                         do
438                                 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
439                                 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
440                                 eval PKGNG_$var=\$val
441                                 misc_get_all_vardefs | grep -E "^PKGNG_$var="
442                         done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
443                         . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
444                 }
445                 pkg_rescue_tools ()
446                 {
447                         local packagepath checksumpath pkgname
448                         packagepath=`pkgsys_get_backup_pkg ports-mgmt/pkg` && \
449                                 pkg_add_tools "$packagepath" && return
450                         [ -x /usr/local/sbin/pkg ] && return
451                         message_echo "WARNING: WITH_PKGNG is set, but pkgng is missing. It is installed now." >&2
452                         [ -x /usr/sbin/pkg ] && /usr/sbin/pkg && return
453                         pkgsys_ready_checksum_file || return
454                         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
455                         pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
456                                 | grep -m 1 -E -e "^pkg-[0-9]"` || :
457                         [ -z "$pkgname" ] && pkg_inst_remote_wild "$pkgname" nodepschk && return
458                         make -C "${PORTSDIR}/ports-mgmt/pkg" install clean && return
459                         message_echo "ERROR: WITH_PKGNG is set, but pkgng is missing. It is installed now." >&2
460                         :
461                 }
462                 pkg_rescue_tools
463                 pkg_loadconf
464         else
465                 unset WITH_PKGNG
466                 PKGSYS_USE_PKGNG=no
467                 PKGSYS_CMD_PKG_INFO='pkg_info'
468                 PKGSYS_CMD_PKG_CREATE='pkg_create'
469                 PKGSYS_CMD_PKG_DELETE='pkg_delete'
470                 PKGSYS_CMD_PKG_ADD='pkg_add'
471                 pkg_is_tool_available ()
472                 {
473                         :
474                 }
475                 pkg_info_Ea ()
476                 {
477                         pkg_info -Ea 2> /dev/null
478                 }
479                 pkg_info_qoa ()
480                 {
481                         pkg_info -qoa 2> /dev/null
482                 }
483 #               pkg_info_qox ()
484 #               {
485 #                       pkg_info -qox "$@" 2> /dev/null
486 #               }
487                 pkg_info_qoX ()
488                 {
489                         pkg_info -qoX "$@" 2> /dev/null
490                 }
491                 pkg_info_qO ()
492                 {
493                         pkg_info -qO "$@" 2> /dev/null
494                 }
495                 pkg_info_qo ()
496                 {
497                         pkg_info -qo "$@" 2> /dev/null
498                 }
499                 pkg_info_qr ()
500                 {
501                         pkg_info -qr "$@" | grep '^@pkgdep ' | sed 's/^@pkgdep[[:space:]]*//' 2> /dev/null
502                 }
503                 pkg_info_e ()
504                 {
505                         pkg_info -e "$@" 2> /dev/null
506                 }
507                 pkg_info_eO ()
508                 {
509                         [ `pkg_info -qO "$1" 2> /dev/null | wc -l` -gt 0 ]
510                 }
511                 pkg_info_Eg ()
512                 {
513                         pkg_info -E "$@" 2> /dev/null
514                 }
515                 pkg_info_qR ()
516                 {
517                         pkg_info -qR "$@" | grep -v '^$' 2> /dev/null
518                 }
519                 pkg_info_Ex ()
520                 {
521                         pkg_info -Ex "$@" 2> /dev/null
522                 }
523                 pkg_info_qL ()
524                 {
525                         pkg_info -qL "$@" 2> /dev/null
526                 }
527                 pkg_check_sanity ()
528                 {
529                         local pkg
530                         pkg=$1
531                         pkg_info -qg "$pkg" 2> /dev/null
532                 }
533                 pkg_info_gen_pkg_origin_table ()
534                 {
535                         pkg_info -aE 2> /dev/null | while read pkg
536                         do
537                                 origin=`pkg_info -qo "$pkg" 2> /dev/null`
538                                 printf '%s\t%s\n' "$pkg" "$origin"
539                         done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
540                 }
541                 pkg_create_b ()
542                 {
543                         pkg_create -b "$@"
544                 }
545                 pkg_delete_f ()
546                 {
547                         pkg_delete -f "$@"
548                 }
549                 pkg_add_f ()
550                 {
551                         pkg_add -if "$@"
552                 }
553                 pkg_add_fF ()
554                 {
555                         pkg_add -ifF "$@"
556                 }
557                 pkg_inst_remote_fetch ()
558                 {
559                         local pkg mode pkgarc
560                         pkg=$1
561                         mode=$2
562                         pkgsys_fetch_legacy_remote "$pkg" || return
563                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
564                         [ "x$mode" = xnodepschk ] && return
565                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
566                 }
567                 pkg_inst_remote ()
568                 {
569                         local pkg mode pkgarc
570                         pkg=$1
571                         mode=$2
572                         pkg_inst_remote_fetch "$pkg" "$mode" || return
573                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
574                         pkg_add "$pkgarc" || return
575                 }
576                 pkg_inst_remote_wild_fetch ()
577                 {
578                         pkg_inst_remote_fetch "$1" "$2"
579                 }
580                 pkg_inst_remote_wild ()
581                 {
582                         pkg_inst_remote "$1" "$2"
583                 }
584                 pkg_loadconf () { :; }
585                 pkg_rescue_tools () { :; }
586         fi
587 }
588
589 # ============= Check existence of initially or currently installed package for an origin =============
590 pkgsys_pkg_info_eO ()
591 {
592         local origin origin_regexp
593         origin=$1
594         origin_regexp=`str_escape_regexp "$origin"`
595         cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
596                 | grep -q -E "^$origin_regexp$" && return
597         pkg_info_eO "$origin"
598 }
599
600 # ============= Get the name of an initially installed package for an origin =============
601 pkgsys_pkg_info_qO_init ()
602 {
603         local origin tmppkg origin_regexp npkgs
604         origin=$1
605         tmppkg=${TMPDIR}/pkgsys_pkg_info_qO_init::pkg
606         origin_regexp=`str_escape_regexp "$origin"`
607         { sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
608                 | cut -f 1 > $tmppkg
609         npkgs=`wc -l < $tmppkg`
610         if [ $npkgs -gt 0 ]
611         then
612                 cat "$tmppkg"
613         else
614                 pkg_info_qO "$origin"
615         fi
616 }
617
618 # ============= Get the package name of this utility =============
619 pkgsys_get_my_current_pkg ()
620 {
621         pkg_info_Ex "${APPNAME}-[0-9].*"
622 }
623
624 # ============= Get the origin of this utility =============
625 pkgsys_get_my_origin ()
626 {
627         pkg_info_qo "`pkgsys_get_my_current_pkg`"
628 }
629
630 # ============= Get the origin of an initially installed package by ambiguous matching =============
631 pkgsys_init_pkg_orig_by_ambiguous_matching ()
632 {
633         local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
634         pkg=$1
635         origin=`pkg_info_qo "$pkg" || :`
636         [ -n "$origin" ] && { echo "$origin"; return; }
637         tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
638         ambsuffix=
639         norigins=0
640         len_pkg=`echo -n "$pkg" | wc -c`
641         while [ $len_pkg -gt 0 ]
642         do
643                 pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
644                 grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
645                         | cut -f 2 > $tmporigin
646                 norigins=`wc -l < $tmporigin`
647                 [ $norigins -gt 0 ] && break
648                 len_pkg=$(($len_pkg-1))
649                 pkg=`echo -n "$pkg" | head -c $len_pkg`
650                 ambsuffix='[a-zA-Z0-9.,_+-]*'
651         done
652         [ $norigins -eq 1 ] || return
653         cat "$tmporigin"
654 }
655
656 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
657 pkgsys_show_pkg_updating_commands ()
658 {
659         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
660         then
661                 if which -s pkg_updating
662                 then
663                         echo 'pkg-updating(8) or pkg_updating(1)'
664                 else
665                         echo 'pkg-updating(8)'
666                 fi
667         elif which -s pkg_updating
668         then
669                 echo 'pkg_updating(1)'
670         fi
671 }
672
673 # ============= Evaluation of ports globs =============
674 pkgsys_eval_ports_glob ()
675 {
676         local pkglist origlist
677         pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
678         origlist=${DBDIR}/pkgsys_eval_ports_glob:origin.lst
679         if [ ! -r "$pkglist" ]
680         then
681                 if touch "$pkglist" 2>/dev/null
682                 then
683                         rm "$pkglist"
684                 else
685                         pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
686                 fi
687         fi
688         if [ ! -r "$origlist" ]
689         then
690                 if touch "$origlist" 2>/dev/null
691                 then
692                         rm "$origlist"
693                 else
694                         origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
695                 fi
696         fi
697         [ -f "$pkglist" ] \
698                 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
699         [ -f "$origlist" ] \
700                 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
701                 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $origlist
702         while [ $# -gt 0 ]
703         do
704                 glob=$1
705                 shift
706                 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
707                 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
708                 if expr "$glob" : '[^/][^/]*\/[^/][^/]*$' > /dev/null 2>&1
709                 then
710                         grep -E "$glob_regexp" "$origlist" 2>&1 || :
711                         {
712                                 pkg_info_qoa
713                                 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
714                         } | grep -E "$glob_regexp" 2>&1 || :
715                 else
716                         if expr "$glob" : '^[a-z][a-zA-Z0-9+-]*[a-zA-Z0-9+]$' > /dev/null 2>&1 && \
717                                 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
718                         then
719                                 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'-[0-9]'
720                         else
721                                 glob_regexp2=$glob_regexp
722                         fi
723                         grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
724                                 | while read index
725                         do
726                                 sed -n ${index}p "$origlist"
727                         done || :
728                         glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'[[:space:]]'
729                         { sed -n -E "/$glob_regexp2/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
730                                 | cut -f 2
731                         pkg_info_qoX "$glob_regexp" || :
732                 fi
733         done | sort -u
734 }
735
736 # ============= Create a back-up package archive =============
737 pkgsys_create_backup_pkg ()
738 {
739         local pkgname dstdir origin backup_pkg_old origin_regexp pkgname_ptn backup_pkg pkgpath
740         pkgname=$1
741         dstdir=$2
742         rm -rf "${TMPDIR}"/package.tmp
743         mkdir "${TMPDIR}"/package.tmp
744         origin=`pkg_info_qo "$pkgname"`
745         if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` \
746                 [ "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
747         then
748                 echo "$backup_pkg_old"
749                 return
750         fi
751         if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
752         then
753                 pkgname_ptn=`str_escape_regexp "$pkgname"`
754                 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
755                         grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
756         fi
757         if [ -z "$backup_pkg" ]
758         then
759                 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
760                 return 1
761         fi
762         [ -d "$dstdir" ] || mkdir -p "$dstdir"
763         mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
764         pkgpath=$dstdir/$backup_pkg
765         origin_regexp=`str_escape_regexp "$origin"`
766         cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
767                 while read origin_cur pkgpath_cur
768                 do
769                         [ "$pkgpath_cur" = "$pkgpath" ] && continue
770                         if [ "$origin_cur" = "$origin" ]
771                         then
772                                 rm -f "$pkgpath_cur"
773                         else
774                                 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
775                         fi
776                 done > ${DBDIR}/backup_pkgarcs.lst.tmp
777         printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
778         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
779         echo "$pkgpath"
780 }
781
782 # ============= Delete a back-up package archive for a port origin =============
783 pkgsys_delete_backup_pkg ()
784 {
785         local origin origin_regexp
786         origin=$1
787         origin_regexp=`str_escape_regexp "$origin"`
788         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
789                 | cut -f 2 | while read pkgpath_cur
790                 do
791                         rm -f "$pkgpath_cur"
792                 done
793         grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
794                 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
795         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
796 }
797
798 # ============= Get an existing package archive path for a port origin =============
799 pkgsys_get_backup_pkg ()
800 {
801         local origin origin_regexp
802         origin=$1
803         tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
804         origin_regexp=`str_escape_regexp "$origin"`
805         rm -f "$tmpnewest"
806         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
807                 | cut -f 2 | while read pkgpath
808         do
809                 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
810                 [ -e "$pkgpath" ] || continue
811                 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
812                 echo "$pkgpath" > $tmpnewest
813         done
814         cat "$tmpnewest" 2> /dev/null
815 }
816
817 # ============= Get a package name from a package archive file name =============
818 pkgsys_pkgarc_to_pkgname ()
819 {
820         local pkgfile_path
821         pkgfile_path=$1
822         basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
823 }
824
825 # ============= Get the file name of an existing package archive for a package name =============
826 pkgsys_pkgname_to_pkgarc ()
827 {
828         local pkgdir pkgname pkgname_ptn pkgnode
829         pkgdir=$1
830         pkgname=$2
831         [ -n "$pkgname" ] || return 1
832         [ -d "$pkgdir" ] || return 1
833         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
834         then
835                 pkgname_ptn=`str_escape_regexp "$pkgname"`
836                 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
837         elif [ -e "$pkgdir/$pkgname.tbz" ]
838         then
839                 pkgnode=$pkgname.tbz
840         fi
841         [ -n "$pkgnode" ] || return 1
842         echo "$pkgdir/$pkgnode"
843 }
844
845 # ============= Get port origins matching a glob pattern even if nonexistent =============
846 pkgsys_eval_ports_glob_even_if_nonexistent ()
847 {
848         local glob_pattern
849         glob_pattern=$1
850         {
851                 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
852                 echo "$glob_pattern" | grep '^[a-z][a-z]*/[a-zA-Z0-9_+-][a-zA-Z0-9_+-]*$' || :
853         } | grep -v -e '^$' | sort -u
854 }
855
856 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
857 pkgsys_register_evaluated_globs ()
858 {
859         local mode listpath dirpath tmp_evaluated
860         mode=$1
861         listpath=$2
862         shift 2
863         dirpath=`dirname "$listpath"`
864         tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
865         echo "$@" | sed -E 's/[ :]+/\
866 /g' | grep -v '^$' | sort -u | while read glob
867         do
868                 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
869                 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
870                 {
871                         message_echo "WARNING: No matching ports/package glob [$glob]." >&2
872                         continue
873                 }
874                 cat "$tmp_evaluated"
875         done | while read origin
876         do
877                 [ -d "$dirpath" ] || mkdir -p "$dirpath"
878                 case $mode in
879                 remove) fileedit_rm_a_line "$origin" "$listpath";;
880                 add)    fileedit_add_a_line_if_new "$origin" "$listpath";;
881                 esac
882         done
883 }
884
885 # ============= Evaluate glob patterns for installed packages =============
886 pkgsys_eval_installed_pkgs_globs ()
887 {
888         local tmp_evaluated
889         tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
890         rm -f "$tmp_evaluated"
891         pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
892         [ -e "$tmp_evaluated" ] || return 0
893         while read origin
894         do
895                 pkgsys_pkg_info_eO "$origin" || echo "$origin"
896         done < $tmp_evaluated
897 }
898
899 # ============= Get glob patterns of conflicting packages of a port =============
900 pkgsys_get_conflicting_pkgs_patterns ()
901 {
902         local mode origin conflicts
903         mode=$1
904         origin=$2
905         conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
906         case $mode in
907         build)
908                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
909                 ;;
910         install)
911                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
912                 ;;
913         esac
914         echo "$conflicts"
915 }       
916
917 # ============= Get conflicting installed packages of a port =============
918 pkgsys_get_conflicting_installed_pkgs ()
919 {
920         local mode origin tmp_conflicts
921         mode=$1
922         origin=$2
923         tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
924         pkg_info_Eg `pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin"` > $tmp_conflicts || :
925         cat "$tmp_conflicts"
926         [ `wc -l < $tmp_conflicts` -gt 0 ]
927 }       
928
929 # ============= Check whether a package conflicts with a port =============
930 pkgsys_chk_conflict_by_a_pkg ()
931 {
932         local mode origin pkg tmp_conflicts_ptn
933         mode=$1
934         origin=$2
935         pkg=$3
936         tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
937         pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
938                 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
939         echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
940 }       
941
942 # ============= Check whether installed files are lost or broken for a package =============
943 pkgsys_sanitychk_pkgcontents ()
944 {
945         local pkg tmp_sanity origin
946         pkg=$1
947         tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
948         pkg_check_sanity "$pkg" > $tmp_sanity || :
949         [ `wc -l < $tmp_sanity` -eq 0 ] && return
950         origin=`pkg_info_qo "$pkg"`
951         if [ $opt_batch_mode = no ]
952         then
953                 message_echo "[$pkg ($origin)]"
954                 sed 's/^/  /' "$tmp_sanity"
955                 message_echo
956         else
957                 pkg_replace=`str_escape_replaceval "$pkg"`
958                 origin_replace=`str_escape_replaceval "$origin"`
959                 sed "s/^/$pkg_replace\\\\$origin_replace\\\\/" "$tmp_sanity" | tr '\\' '\t'
960         fi
961         return 1
962 }