OSDN Git Service

Version 3.0.3+toward_3.0.4_20131115074959
[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" 2> /dev/null
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 # ============= Check whether legacy package tools are available =============
218 pkgsys_is_legacy_tool_available ()
219 {
220         which -s pkg_info
221 }
222
223 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
224 pkgsys_def_pkgtools ()
225 {
226         if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf ]
227         then
228                 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
229         else
230                 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
231                 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
232                 then
233                         echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
234                         mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
235                 fi
236         fi
237         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
238         then
239                 export WITH_PKGNG=yes
240                 PKGSYS_CMD_PKG_INFO='pkg info'
241                 PKGSYS_CMD_PKG_CREATE='pkg create'
242                 PKGSYS_CMD_PKG_DELETE='pkg delete'
243                 PKGSYS_CMD_PKG_ADD='pkg add'
244                 pkg_is_tool_available ()
245                 {
246                         which -a pkg | grep -v '^/usr/sbin/pkg$' | grep -q '/sbin/pkg$' && return
247                         [ -x /usr/sbin/pkg ] || return
248                         /usr/sbin/pkg
249                         pkg_is_tool_available
250                 }
251                 pkg_info_Ea ()
252                 {
253                         pkg info -qa 2> /dev/null
254                 }
255                 pkg_info_qoa ()
256                 {
257                         pkg info -qoa 2> /dev/null
258                 }
259 #               pkg_info_qox ()
260 #               {
261 #                       pkg info -qox "$@" 2> /dev/null
262 #               }
263                 pkg_info_qoX ()
264                 {
265                         pkg info -qoX "$@" 2> /dev/null
266                 }
267                 pkg_info_qO ()
268                 {
269                         pkg info -qO "$@" 2> /dev/null
270                 }
271                 pkg_info_qo ()
272                 {
273                         pkg info -qo "$@" 2> /dev/null
274                 }
275                 pkg_info_qr ()
276                 {
277                         pkg info -qd "$@" 2> /dev/null
278                 }
279                 pkg_info_e ()
280                 {
281                         pkg info -e "$@" 2> /dev/null
282                 }
283                 pkg_info_eO ()
284                 {
285                         pkg info -eO "$1" 2> /dev/null
286                 }
287                 pkg_info_Eg ()
288                 {
289                         pkg info -Eg "$@" 2> /dev/null
290                 }
291                 pkg_info_qR ()
292                 {
293                         pkg info -qr "$@" 2> /dev/null
294                 }
295                 pkg_info_Ex ()
296                 {
297                         pkg info -Ex "$@" 2> /dev/null
298                 }
299                 pkg_info_qL ()
300                 {
301                         pkg info -ql "$@" 2> /dev/null
302                 }
303                 pkg_check_sanity ()
304                 {
305                         local pkg
306                         pkg=$1
307                         pkg check -s "$pkg" 2> /dev/null
308                 }
309                 pkg_which ()
310                 {
311                         local filepath
312                         filepath=$1
313                         pkg which -q "$filepath" 2> /dev/null
314                 }
315                 pkg_info_gen_pkg_origin_table ()
316                 {
317                         pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
318                 }
319                 pkg_create_b ()
320                 {
321                         pkg create "$@"
322                 }
323                 pkg_delete_f ()
324                 {
325                         pkg delete -fqy "$@"
326                 }
327                 pkg_add_tools ()
328                 {
329                         local pkgarc tmp_extract prefix prefix_parent pkg
330                         pkgarc=$1
331                         tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
332                         rm -rf "$tmp_extract"
333                         mkdir "$tmp_extract"
334                         tar xf "$pkgarc" -C "$tmp_extract"
335                         prefix=`grep -m 1 '^prefix: ' "$tmp_extract/+MANIFEST" | sed 's/^prefix: *//'`
336                         prefix_parent=`dirname "$prefix"`
337                         cp -Rp "$tmp_extract/$prefix" "$prefix_parent"/
338                         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
339                         message_echo "INFO: Contents of $pkg are temporarily installed by simple copy."
340                         if env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
341                         then
342                                 message_echo "INFO: $pkg is successfully registered."
343                         else
344                                 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
345                         fi
346                 }
347                 pkg_add_f ()
348                 {
349                         local pkgarc pkg pkg_tool pkg_gen
350                         pkg_tool=
351                         pkg_gen=
352                         for pkgarc in "$@"
353                         do
354                                 pkg=`basename "$pkgarc"`
355                                 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
356                                 then
357                                         pkg_tool=$pkgarc
358                                 else
359                                         pkg_gen="$pkg_gen $pkgarc"
360                                 fi
361                         done
362                         [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
363                         [ -n "$pkg_gen" ] && env ASSUME_ALWAYS_YES=YES pkg add $pkg_gen
364                 }
365                 pkg_add_fF ()
366                 {
367                         pkg_add_f "$@"
368                 }
369                 pkg_inst_remote_fetch ()
370                 {
371                         local pkg mode pkgarc
372                         pkg=$1
373                         mode=$2
374                         tmp_extract=${TMPDIR}/pkgng:pkg_inst_remote:extract
375                         pkg fetch -yU "$pkg" || return
376                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
377                         [ "x$mode" = xnodepschk ] && return
378                         rm -rf "$tmp_extract"
379                         mkdir "$tmp_extract"
380                         tar xf "$pkgarc" -C "$tmp_extract" +MANIFEST
381                         sed -E '1,/^deps:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*([^:]+):[[:space:]]*\{origin:[[:space:]]*([^,]+),[[:space:]]*version:[[:space:]]*([^}]+)\}/\2\\\1-\3/' "$tmp_extract/+MANIFEST" \
382                                 | tr '\\' '\t' | while read origin_req pkg_req
383                         do
384                                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
385                                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
386                                 then
387                                         message_echo "WARNING: Requirements of remote package $pkg are not latest." >&2
388                                         return 1
389                                 fi
390                         done
391                         :
392                 }
393                 pkg_inst_remote ()
394                 {
395                         local pkg mode pkgarc
396                         pkg=$1
397                         mode=$2
398                         pkg_inst_remote_fetch "$pkg" "$mode" || return
399                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
400                         env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
401                 }
402                 pkg_inst_remote_wild_fetch ()
403                 {
404                         local pkg mode pkgarc
405                         pkg=$1
406                         mode=$2
407                         if pkg_is_tool_available
408                         then
409                                 pkg_inst_remote "$pkg" "$mode" && return
410                         fi
411                         pkgsys_is_legacy_tool_available || return
412                         message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
413                         pkgsys_fetch_legacy_remote "$pkg" || return
414                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
415                         [ "x$mode" = xnodepschk ] && return
416                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
417                 }
418                 pkg_inst_remote_wild ()
419                 {
420                         local pkg mode pkgarc
421                         pkg=$1
422                         mode=$2
423                         pkg_inst_remote_wild_fetch "$pkg" "$mode" || return
424                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
425                         pkg_add -ifF "$pkgarc" || return
426                         message_echo "INFO: Trying to convert the installed legacy package to pkgng."
427                         pkg2ng || :
428                         message_echo "INFO: Checking whether the conversion is successful."
429                         pkg info -e "$pkg"
430                 }
431                 pkg_loadconf ()
432                 {
433                         local pkg_conf
434                         # Deafult configuration for pkg(1)
435                         PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
436                         PKGNG_SRV_MIRRORS=YES
437                         PKGNG_PKG_DBDIR=/var/db/pkg
438                         PKGNG_PKG_CACHEDIR=/var/cache/pkg
439                         PKGNG_PORTSDIR=/usr/ports
440                         PKGNG_PUBKEY=/etc/ssl/pkg.conf
441                         PKGNG_HANDLE_RC_SCRIPTS=NO
442                         PKGNG_PKG_MULTIREPOS=NO
443                         PKGNG_ASSUME_ALWAYS_YES=NO
444                         PKGNG_SYSLOG=YES
445                         PKGNG_SHLIBS=NO
446                         PKGNG_AUTODEPS=NO
447                         PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
448                         # Load configuration for pkg(1)
449                         pkg_conf=`pkg query %Fp pkg | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'`
450                         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
451                                 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
452                                 | while read srcline
453                         do
454                                 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
455                                 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
456                                 eval PKGNG_$var=\$val
457                                 misc_get_all_vardefs | grep -E "^PKGNG_$var="
458                         done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
459                         . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
460                 }
461                 pkg_rescue_tools ()
462                 {
463                         local packagepath checksumpath pkgname
464                         packagepath=`pkgsys_get_backup_pkg ports-mgmt/pkg` && \
465                                 pkg_add_tools "$packagepath" && return
466                         pkg_is_tool_available && return
467                         message_echo "WARNING: WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
468                         pkgsys_ready_checksum_file || return
469                         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
470                         pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
471                                 | grep -m 1 -E -e "^pkg-[0-9]"` || :
472                         [ -z "$pkgname" ] && pkg_inst_remote_wild "$pkgname" nodepschk && return
473                         make -C "${PORTSDIR}/ports-mgmt/pkg" install clean && return
474                         message_echo "ERROR: WITH_PKGNG is set, but pkgng is missing. It is installed now." >&2
475                         :
476                 }
477                 pkg_rescue_tools
478                 pkg_loadconf
479         else
480                 unset WITH_PKGNG
481                 PKGSYS_USE_PKGNG=no
482                 PKGSYS_CMD_PKG_INFO='pkg_info'
483                 PKGSYS_CMD_PKG_CREATE='pkg_create'
484                 PKGSYS_CMD_PKG_DELETE='pkg_delete'
485                 PKGSYS_CMD_PKG_ADD='pkg_add'
486                 pkg_is_tool_available ()
487                 {
488                         pkgsys_is_legacy_tool_available
489                 }
490                 pkg_info_Ea ()
491                 {
492                         pkg_info -Ea 2> /dev/null
493                 }
494                 pkg_info_qoa ()
495                 {
496                         pkg_info -qoa 2> /dev/null
497                 }
498 #               pkg_info_qox ()
499 #               {
500 #                       pkg_info -qox "$@" 2> /dev/null
501 #               }
502                 pkg_info_qoX ()
503                 {
504                         pkg_info -qoX "$@" 2> /dev/null
505                 }
506                 pkg_info_qO ()
507                 {
508                         pkg_info -qO "$@" 2> /dev/null
509                 }
510                 pkg_info_qo ()
511                 {
512                         pkg_info -qo "$@" 2> /dev/null
513                 }
514                 pkg_info_qr ()
515                 {
516                         pkg_info -qr "$@" | grep '^@pkgdep ' | sed 's/^@pkgdep[[:space:]]*//' 2> /dev/null
517                 }
518                 pkg_info_e ()
519                 {
520                         pkg_info -e "$@" 2> /dev/null
521                 }
522                 pkg_info_eO ()
523                 {
524                         [ `pkg_info -qO "$1" 2> /dev/null | wc -l` -gt 0 ]
525                 }
526                 pkg_info_Eg ()
527                 {
528                         pkg_info -E "$@" 2> /dev/null
529                 }
530                 pkg_info_qR ()
531                 {
532                         pkg_info -qR "$@" | grep -v '^$' 2> /dev/null
533                 }
534                 pkg_info_Ex ()
535                 {
536                         pkg_info -Ex "$@" 2> /dev/null
537                 }
538                 pkg_info_qL ()
539                 {
540                         pkg_info -qL "$@" 2> /dev/null
541                 }
542                 pkg_check_sanity ()
543                 {
544                         local pkg
545                         pkg=$1
546                         pkg_info -qg "$pkg" 2> /dev/null
547                 }
548                 pkg_which ()
549                 {
550                         local filepath
551                         filepath=$1
552                         pkg_info -qW "$filepath" 2> /dev/null
553                 }
554                 pkg_info_gen_pkg_origin_table ()
555                 {
556                         pkg_info -aE 2> /dev/null | while read pkg
557                         do
558                                 origin=`pkg_info -qo "$pkg" 2> /dev/null`
559                                 printf '%s\t%s\n' "$pkg" "$origin"
560                         done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
561                 }
562                 pkg_create_b ()
563                 {
564                         pkg_create -b "$@"
565                 }
566                 pkg_delete_f ()
567                 {
568                         pkg_delete -f "$@"
569                 }
570                 pkg_add_f ()
571                 {
572                         pkg_add -if "$@"
573                 }
574                 pkg_add_fF ()
575                 {
576                         pkg_add -ifF "$@"
577                 }
578                 pkg_inst_remote_fetch ()
579                 {
580                         local pkg mode pkgarc
581                         pkg=$1
582                         mode=$2
583                         pkgsys_fetch_legacy_remote "$pkg" || return
584                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
585                         [ "x$mode" = xnodepschk ] && return
586                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
587                 }
588                 pkg_inst_remote ()
589                 {
590                         local pkg mode pkgarc
591                         pkg=$1
592                         mode=$2
593                         pkg_inst_remote_fetch "$pkg" "$mode" || return
594                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
595                         pkg_add "$pkgarc" || return
596                 }
597                 pkg_inst_remote_wild_fetch ()
598                 {
599                         pkg_inst_remote_fetch "$1" "$2"
600                 }
601                 pkg_inst_remote_wild ()
602                 {
603                         pkg_inst_remote "$1" "$2"
604                 }
605                 pkg_loadconf () { :; }
606                 pkg_rescue_tools () { :; }
607         fi
608 }
609
610 # ============= Check existence of initially or currently installed package for an origin =============
611 pkgsys_pkg_info_eO ()
612 {
613         local origin origin_regexp
614         origin=$1
615         origin_regexp=`str_escape_regexp "$origin"`
616         cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
617                 | grep -q -E "^$origin_regexp$" && return
618         pkg_info_eO "$origin"
619 }
620
621 # ============= Get the name of an initially installed package for an origin =============
622 pkgsys_pkg_info_qO_init ()
623 {
624         local origin tmppkg origin_regexp npkgs
625         origin=$1
626         tmppkg=${TMPDIR}/pkgsys_pkg_info_qO_init::pkg
627         origin_regexp=`str_escape_regexp "$origin"`
628         { sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
629                 | cut -f 1 > $tmppkg
630         npkgs=`wc -l < $tmppkg`
631         if [ $npkgs -gt 0 ]
632         then
633                 cat "$tmppkg"
634         else
635                 pkg_info_qO "$origin"
636         fi
637 }
638
639 # ============= Get the package name of this utility =============
640 pkgsys_get_my_current_pkg ()
641 {
642         pkg_info_Ex "${APPNAME}-[0-9].*"
643 }
644
645 # ============= Get the origin of this utility =============
646 pkgsys_get_my_origin ()
647 {
648         pkg_info_qo "`pkgsys_get_my_current_pkg`"
649 }
650
651 # ============= Get the origin of an initially installed package by ambiguous matching =============
652 pkgsys_init_pkg_orig_by_ambiguous_matching ()
653 {
654         local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
655         pkg=$1
656         origin=`pkg_info_qo "$pkg" || :`
657         [ -n "$origin" ] && { echo "$origin"; return; }
658         tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
659         ambsuffix=
660         norigins=0
661         len_pkg=`echo -n "$pkg" | wc -c`
662         if [ $len_pkg -gt 0 ]
663         then
664                 while :
665                 do
666                         pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
667                         grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
668                                 | cut -f 2 > $tmporigin
669                         norigins=`wc -l < $tmporigin`
670                         [ $norigins -gt 0 ] && break
671                         ambsuffix='[a-zA-Z0-9.,_+-]*'
672                         len_pkg=$(($len_pkg-1))
673                         [ $len_pkg -gt 0 ] || break
674                         pkg=`echo -n "$pkg" | head -c $len_pkg`
675                 done
676         fi
677         [ $norigins -eq 1 ] || return
678         cat "$tmporigin"
679 }
680
681 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
682 pkgsys_show_pkg_updating_commands ()
683 {
684         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
685         then
686                 if which -s pkg_updating
687                 then
688                         echo 'pkg-updating(8) or pkg_updating(1)'
689                 else
690                         echo 'pkg-updating(8)'
691                 fi
692         elif which -s pkg_updating
693         then
694                 echo 'pkg_updating(1)'
695         fi
696 }
697
698 # ============= Evaluation of ports globs =============
699 pkgsys_eval_ports_glob ()
700 {
701         local pkglist origlist
702         pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
703         origlist=${DBDIR}/pkgsys_eval_ports_glob:origin.lst
704         if [ ! -r "$pkglist" ]
705         then
706                 if touch "$pkglist" 2>/dev/null
707                 then
708                         rm "$pkglist"
709                 else
710                         pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
711                 fi
712         fi
713         if [ ! -r "$origlist" ]
714         then
715                 if touch "$origlist" 2>/dev/null
716                 then
717                         rm "$origlist"
718                 else
719                         origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
720                 fi
721         fi
722         [ -f "$pkglist" ] \
723                 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
724         [ -f "$origlist" ] \
725                 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
726                 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $origlist
727         while [ $# -gt 0 ]
728         do
729                 glob=$1
730                 shift
731                 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
732                 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
733                 if expr "$glob" : '[^/][^/]*\/[^/][^/]*$' > /dev/null 2>&1
734                 then
735                         grep -E "$glob_regexp" "$origlist" 2>&1 || :
736                         {
737                                 pkg_info_qoa
738                                 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
739                         } | grep -E "$glob_regexp" 2>&1 || :
740                 else
741                         if expr "$glob" : '^[a-z][a-zA-Z0-9+-]*[a-zA-Z0-9+]$' > /dev/null 2>&1 && \
742                                 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
743                         then
744                                 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'-[0-9]'
745                         else
746                                 glob_regexp2=$glob_regexp
747                         fi
748                         grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
749                                 | while read index
750                         do
751                                 sed -n ${index}p "$origlist"
752                         done || :
753                         glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'[[:space:]]'
754                         { sed -n -E "/$glob_regexp2/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null || :; } \
755                                 | cut -f 2
756                         pkg_info_qoX "$glob_regexp" || :
757                 fi
758         done | sort -u
759 }
760
761 # ============= Create a back-up package archive =============
762 pkgsys_create_backup_pkg ()
763 {
764         local pkgname dstdir origin backup_pkg_old origin_regexp pkgname_ptn backup_pkg pkgpath
765         pkgname=$1
766         dstdir=$2
767         rm -rf "${TMPDIR}"/package.tmp
768         mkdir "${TMPDIR}"/package.tmp
769         origin=`pkg_info_qo "$pkgname"`
770         if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` \
771                 [ "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
772         then
773                 echo "$backup_pkg_old"
774                 return
775         fi
776         if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
777         then
778                 pkgname_ptn=`str_escape_regexp "$pkgname"`
779                 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
780                         grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
781         fi
782         if [ -z "$backup_pkg" ]
783         then
784                 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
785                 return 1
786         fi
787         [ -d "$dstdir" ] || mkdir -p "$dstdir"
788         mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
789         pkgpath=$dstdir/$backup_pkg
790         origin_regexp=`str_escape_regexp "$origin"`
791         cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
792                 while read origin_cur pkgpath_cur
793                 do
794                         [ "$pkgpath_cur" = "$pkgpath" ] && continue
795                         if [ "$origin_cur" = "$origin" ]
796                         then
797                                 rm -f "$pkgpath_cur"
798                         else
799                                 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
800                         fi
801                 done > ${DBDIR}/backup_pkgarcs.lst.tmp
802         printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
803         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
804         echo "$pkgpath"
805 }
806
807 # ============= Delete a back-up package archive for a port origin =============
808 pkgsys_delete_backup_pkg ()
809 {
810         local origin origin_regexp
811         origin=$1
812         origin_regexp=`str_escape_regexp "$origin"`
813         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
814                 | cut -f 2 | while read pkgpath_cur
815                 do
816                         rm -f "$pkgpath_cur"
817                 done
818         grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
819                 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
820         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
821 }
822
823 # ============= Get an existing package archive path for a port origin =============
824 pkgsys_get_backup_pkg ()
825 {
826         local origin origin_regexp
827         origin=$1
828         tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
829         origin_regexp=`str_escape_regexp "$origin"`
830         rm -f "$tmpnewest"
831         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
832                 | cut -f 2 | while read pkgpath
833         do
834                 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
835                 [ -e "$pkgpath" ] || continue
836                 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
837                 echo "$pkgpath" > $tmpnewest
838         done
839         cat "$tmpnewest" 2> /dev/null
840 }
841
842 # ============= Get a package name from a package archive file name =============
843 pkgsys_pkgarc_to_pkgname ()
844 {
845         local pkgfile_path
846         pkgfile_path=$1
847         basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
848 }
849
850 # ============= Get the file name of an existing package archive for a package name =============
851 pkgsys_pkgname_to_pkgarc ()
852 {
853         local pkgdir pkgname pkgname_ptn pkgnode
854         pkgdir=$1
855         pkgname=$2
856         [ -n "$pkgname" ] || return 1
857         [ -d "$pkgdir" ] || return 1
858         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
859         then
860                 pkgname_ptn=`str_escape_regexp "$pkgname"`
861                 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
862         elif [ -e "$pkgdir/$pkgname.tbz" ]
863         then
864                 pkgnode=$pkgname.tbz
865         fi
866         [ -n "$pkgnode" ] || return 1
867         echo "$pkgdir/$pkgnode"
868 }
869
870 # ============= Get port origins matching a glob pattern even if nonexistent =============
871 pkgsys_eval_ports_glob_even_if_nonexistent ()
872 {
873         local glob_pattern
874         glob_pattern=$1
875         {
876                 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
877                 echo "$glob_pattern" | grep '^[a-z][a-z]*/[a-zA-Z0-9_+-][a-zA-Z0-9_+-]*$' || :
878         } | grep -v -e '^$' | sort -u
879 }
880
881 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
882 pkgsys_register_evaluated_globs ()
883 {
884         local mode listpath dirpath tmp_evaluated
885         mode=$1
886         listpath=$2
887         shift 2
888         dirpath=`dirname "$listpath"`
889         tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
890         echo "$@" | sed -E 's/[ :]+/\
891 /g' | grep -v '^$' | sort -u | while read glob
892         do
893                 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
894                 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
895                 {
896                         message_echo "WARNING: No matching ports/package glob [$glob]." >&2
897                         continue
898                 }
899                 cat "$tmp_evaluated"
900         done | while read origin
901         do
902                 [ -d "$dirpath" ] || mkdir -p "$dirpath"
903                 case $mode in
904                 remove) fileedit_rm_a_line "$origin" "$listpath";;
905                 add)    fileedit_add_a_line_if_new "$origin" "$listpath";;
906                 esac
907         done
908 }
909
910 # ============= Evaluate glob patterns for installed packages =============
911 pkgsys_eval_installed_pkgs_globs ()
912 {
913         local tmp_evaluated
914         tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
915         rm -f "$tmp_evaluated"
916         pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
917         [ -e "$tmp_evaluated" ] || return 0
918         while read origin
919         do
920                 pkgsys_pkg_info_eO "$origin" || echo "$origin"
921         done < $tmp_evaluated
922 }
923
924 # ============= Get glob patterns of conflicting packages of a port =============
925 pkgsys_get_conflicting_pkgs_patterns ()
926 {
927         local mode origin conflicts
928         mode=$1
929         origin=$2
930         conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
931         case $mode in
932         build)
933                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
934                 ;;
935         install)
936                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
937                 ;;
938         esac
939         echo "$conflicts" | tr ' ' '\n' | grep -v '^$' || :
940 }       
941
942 # ============= Get conflicting installed packages of a port =============
943 pkgsys_get_conflicting_installed_pkgs ()
944 {
945         local mode origin tmp_conflicts
946         mode=$1
947         origin=$2
948         tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
949         pkg_info_Eg `pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin"` > $tmp_conflicts || :
950         cat "$tmp_conflicts"
951         [ `wc -l < $tmp_conflicts` -gt 0 ]
952 }       
953
954 # ============= Check whether a package conflicts with a port =============
955 pkgsys_chk_conflict_by_a_pkg ()
956 {
957         local mode origin pkg tmp_conflicts_ptn
958         mode=$1
959         origin=$2
960         pkg=$3
961         tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
962         pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
963                 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
964         echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
965 }       
966
967 # ============= Check whether installed files are lost or broken for a package =============
968 pkgsys_sanitychk_pkgcontents ()
969 {
970         local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
971         pkg=$1
972         var_is_reinstall_encouraged=$2
973         tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
974         pkg_check_sanity "$pkg" > $tmp_sanity || :
975         eval "$var_is_reinstall_encouraged=no"
976         [ `wc -l < $tmp_sanity` -eq 0 ] && return
977         nlines=`wc -l < $tmp_sanity`
978         iline=1
979         _is_reinstall_encouraged=no
980         while [ $iline -le $nlines ]
981         do
982                 src=`sed -n ${iline}p "$tmp_sanity"`
983                 iline=$(($iline+1))
984                 filename=`echo "$src" | cut -d ' ' -f 1`
985                 icol=2
986                 until [ -e "$filename" -o "$filename" = "$src" ]
987                 do
988                         filename="$filename "`echo "$src" | cut -d ' ' -f $icol`
989                 done
990                 [ -e "$filename" ] || continue
991                 if expr "$filename" : '.*/include/.*' > /dev/null
992                 then
993                         _is_reinstall_encouraged=yes
994                         break
995                 fi
996                 filename_esc=`str_escape_regexp "$filename"`
997                 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
998                 then
999                         _is_reinstall_encouraged=yes
1000                         break
1001                 fi
1002                 pkg_owner=`pkg_which "$filename"`
1003                 if [ "$pkg" != "$pkg_owner" ]
1004                 then
1005                         _is_reinstall_encouraged=yes
1006                         break
1007                 fi
1008         done
1009         eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1010         origin=`pkg_info_qo "$pkg"`
1011         if [ $opt_batch_mode = no ]
1012         then
1013                 message_echo "[$pkg ($origin)]"
1014                 sed 's/^/  /' "$tmp_sanity"
1015                 message_echo
1016         else
1017                 pkg_replace=`str_escape_replaceval "$pkg"`
1018                 origin_replace=`str_escape_replaceval "$origin"`
1019                 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1020         fi
1021         return 1
1022 }