OSDN Git Service

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