OSDN Git Service

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