2 # ==============================================================================
3 # portsreinstall library script
4 # - Reinstallation processes -
5 # Copyright (C) 2013-2015 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
9 # ============= Variables =============
10 # The following variables are available only within reinstall_exec and functions invoked by it.
11 REINSTALL_PKGTAG= # Tag indicating version changes by reinstallation of the current port
12 REINSTALL_ORIGPKGTAG= # Tag indicating the port origin and version changes by reinstallation of the current port
13 REINSTALL_CURRENTPKG= # Currently installed package name of the current port
14 REINSTALL_ORIGIN= # Port origin of the current port
15 REINSTALL_ORIGIN_REGEXP= # Port origin of the current port where control characters of extended regular expression are escaped
16 REINSTALL_DBNODE_DIR= # Path of the "requires" section of the temporary database for the current port
17 REINSTALL_IS_CURRENTPKG_LATEST= # Currently installed package name of the current port
18 REINSTALL_DBSUFFIX= # Database suffix for the evaluation method of dependencies
20 # ============= Skip reinstallation by showing messages =============
25 message_echo "($message)"
26 message_target_relations "$REINSTALL_ORIGIN"
28 fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
31 # ============= Check whether a package is forbidden due to conflicts =============
32 reinstall_quick_chk_forbidden_conflicts ()
34 local pkg pkg_regexp_esc
36 pkg_regexp_esc=`str_escape_regexp "$pkg"`
37 grep -qE "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts"
40 # ============= Check whether a package is forbidden due to conflicts =============
41 reinstall_chk_forbidden_conflicts ()
43 local pkg tmp_forbidden pkg_regexp_esc
45 tmp_forbidden=${TMPDIR}/reinstall_chk_forbidden_conflicts:forbidden
46 pkg_regexp_esc=`str_escape_regexp "$pkg"`
47 grep -E "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts" > $tmp_forbidden 2> /dev/null || return
48 pkg_info_e `cut -d : -f 2 "$tmp_forbidden"`
51 # ============= Restore a package if it is temporarily deinstalled =============
52 reinstall_restore_if_temporarily_deinstalled ()
55 [ -n "$REINSTALL_CURRENTPKG" ] && return
56 backup_pkg=`cat "$REINSTALL_DBNODE_DIR/backedup_pkgfile" 2> /dev/null` || return 0
57 [ -z "$backup_pkg" -o ! -e "$backup_pkg" ] && return
58 pkg=`pkgsys_pkgarc_to_pkgname "$backup_pkg"`
59 if reinstall_chk_forbidden_conflicts "$pkg"
61 message_echo "INFO: This port is temporarily deinstalled but avoided because it conflicts with installed packages."
64 message_echo "INFO: This port is temporarily deinstalled, so restoring from the backup."
65 pkg_add_fF "$backup_pkg" && return
66 message_echo "WARNING: Failed to restore the backup, but continuing anyway." >&2
69 # ============= Skip reinstallation by showing messages if an origin is in a list =============
70 reinstall_skip_if_in_a_list ()
72 local message list mode origin_regexp_esc
76 origin_regexp_esc=`str_escape_regexp "$REINSTALL_ORIGIN"`
77 grep -q -E "^$origin_regexp_esc$" "${DBDIR}/$list" 2> /dev/null || return
78 [ "x$mode" = xrestore ] && reinstall_restore_if_temporarily_deinstalled
79 reinstall_skip "$message" || :
82 # ============= Get the make arguments =============
83 reinstall_setup_make_args ()
88 for key in LOCALBASE LINUXBASE PORT_DBDIR PORTSDIR DISTDIR PACKAGES PKGREPOSITORY
92 [ $opt_avoid_vulner = yes ] || echo 'DISABLE_VULNERABILITIES=yes'
96 [ $opt_batch_ports_only = yes ] && echo 'BATCH=yes'
97 [ $opt_interactive_ports_only = yes ] && echo 'INTERACTIVE=yes'
100 if [ $opt_apply_default_config = yes ]
102 pkgsys_is_dialog4ports_used && echo 'NO_DIALOG=yes'
104 cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MARG.conf" 2> /dev/null || :
108 # ============= Get the make environment variables =============
109 reinstall_setup_make_envs ()
111 cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MENV.conf" 2> /dev/null | tr '\n' ' '
114 # ============= Execute make command without restricting for BATCH or INTERACTIVE ports =============
115 reinstall_make_anymode ()
117 local MAKE_ARGS MAKE_ENVS
118 MAKE_ARGS=`reinstall_setup_make_args anymode`
119 MAKE_ENVS=`reinstall_setup_make_envs`
120 env $MAKE_ENVS make -C "${PORTSDIR}/$REINSTALL_ORIGIN" "$@" $MAKE_ARGS
123 # ============= Execute make command =============
126 local MAKE_ARGS MAKE_ENVS
127 MAKE_ARGS=`reinstall_setup_make_args`
128 MAKE_ENVS=`reinstall_setup_make_envs`
129 env $MAKE_ENVS make -C "${PORTSDIR}/$REINSTALL_ORIGIN" "$@" $MAKE_ARGS
132 # ============= Error process during reinstallation =============
138 database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
139 message_echo "ERROR: In $position." >&2
140 message_echo "The port/package in concern is $REINSTALL_ORIGPKGTAG." >&2
141 [ -n "$msg" ] && message_echo "($msg)" >&2
143 [ -d "${DBDIR}/notes/$REINSTALL_ORIGIN" ] || mkdir -p "${DBDIR}/notes/$REINSTALL_ORIGIN"
144 echo "$position" > ${DBDIR}/notes/$REINSTALL_ORIGIN/note_failtre
145 database_record_failure "$REINSTALL_ORIGIN" noclean
146 fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
147 message_report_failure_for_a_port "$REINSTALL_ORIGIN"
150 # ============= Check the latest stage for a port =============
151 reinstall_chk_stage ()
155 [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag" ]
158 # ============= Check completion of a stage for a port =============
159 reinstall_chk_stage_complete ()
163 [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag" ]
166 # ============= Register the latest stage for a port =============
167 reinstall_register_stage ()
169 local stagetag parentdir
171 parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN
172 [ -d "$parentdir" ] || mkdir -p "$parentdir"
173 touch "$parentdir/$stagetag"
176 # ============= Register completion of a stage for a port =============
177 reinstall_register_stage_complete ()
179 local stagetag parentdir
181 parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete
182 [ -d "$parentdir" ] || mkdir -p "$parentdir"
183 touch "$parentdir/$stagetag"
186 # ============= Deregister the latest stage for a port =============
187 reinstall_deregister_stage ()
191 rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag"
194 # ============= Deregister completion of a stage for a port =============
195 reinstall_deregister_stage_complete ()
199 rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag"
202 # ============= Back up and delete conflicts =============
203 # Package names of conflicts are given via stdin.
204 reinstall_backup_and_delete_conflicts ()
206 local pkg origin origin_regexp_esc backup_pkgdir backup_pkg
207 message_echo "INFO: Deinstalling conflicting packages for $REINSTALL_ORIGPKGTAG."
210 origin=`pkg_info_qo "$pkg"`
211 message_echo "INFO: Backing up and deleting a conflict, $origin ($pkg)."
212 origin_regexp_esc=`str_escape_regexp "$origin"`
213 if [ -d "${DBDIR}/requires/$origin" ]
215 backup_pkgdir=${DBDIR}/backup_packages
217 backup_pkgdir=${PKGREPOSITORY}
219 [ -d "$backup_pkgdir" ] || mkdir -p "$backup_pkgdir"
220 if backup_pkg=`pkgsys_get_backup_pkg "$origin"`
222 message_echo "INFO: backup package already exists as $backup_pkg"
223 elif ! backup_pkg=`pkgsys_create_backup_pkg "$pkg" "$backup_pkgdir"`
225 message_echo "WARNING: Failed to create the backup package, the conflict is kept installed." >&2
228 grep -v -E "^${origin_regexp_esc}[[:space:]]" "${DBDIR}/deleted_conflicts" \
229 > ${DBDIR}/deleted_conflicts.tmp 2> /dev/null || :
230 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
231 printf '%s\t%s\n' "$origin" "$pkg" >> ${DBDIR}/deleted_conflicts
232 pkg_delete_f "$pkg" || \
234 message_echo "WARNING: Failed to deinstall $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
237 cat "${DBDIR}/deleted_conflicts" 2> /dev/null | sort -u > ${DBDIR}/deleted_conflicts.tmp
238 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
241 # ============= Back up and delete remaining actual install conflicts =============
242 reinstall_backup_and_delete_remaining_install_conflicts ()
244 local stagedir tmp_conflicts
245 tmp_conflicts=${TMPDIR}/reinstall_backup_and_delete_remaining_install_conflicts
246 message_echo "(Checking installation conflict...)"
247 stagedir=`database_query_get_makevar_val "$REINSTALL_ORIGIN" STAGEDIR`
252 ) | sed 's|^\.||' | while read filepath
254 [ ! -e "$filepath" ] && continue
255 pkg=`pkg_which "$filepath" || :`
256 [ -z "$pkg" ] && continue
257 origin=`pkg_info_qo "$pkg" || :`
258 [ x"$origin" = x"$REINSTALL_ORIGIN" ] || echo "$pkg"
260 pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN"
261 } | sort -u > $tmp_conflicts
262 while read pkg_conflict
264 fileedit_add_a_line_if_new "$pkg_conflict:$REINSTALL_NEWPKGNAME" \
265 "${DBDIR}/forbidden_conflicts"
266 done < $tmp_conflicts
267 reinstall_backup_and_delete_conflicts < $tmp_conflicts
270 # ============= Restoration of backed up conflicts =============
271 reinstall_restore_conflicts ()
273 local origin_current tmpsrc
275 [ $opt_dry_run = yes ] && return
276 [ -e "${DBDIR}/deleted_conflicts" ] || return 0
277 tmpsrc=${TMPDIR}/reinstall_restore_conflicts::deleted_conflicts
278 cp "${DBDIR}/deleted_conflicts" "$tmpsrc"
279 while read origin pkg
281 pkg_regexp_esc=`str_escape_regexp "$pkg"`
282 origin_orig=`echo "$origin" \
283 | sed -E -f "${DBDIR}/REVERSE_REPLACE.complete_sed_pattern"`
284 [ "x$origin_orig" = "x$origin" ] && origin_orig=
285 origin_replace=`echo "$origin" \
286 | sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern"`
287 [ "x$origin_replace" = "x$origin" ] && origin_replace=
288 if [ -n "$origin_current" -a "x$origin" = "x$origin_current" ] || \
289 pkg_info_e "$pkg" || pkg_info_eO "$origin" || \
290 { [ -n "$origin_orig" ] && pkg_info_eO "$origin_orig"; } || \
291 { [ -n "$origin_replace" ] && pkg_info_eO "$origin_replace"; }
293 is_to_dereg_from_list=yes
295 is_to_dereg_from_list=no
297 if pkgsys_chk_conflict_by_a_pkg install "$REINSTALL_ORIGIN" "$pkg"
299 fileedit_add_a_line_if_new "$pkg:$REINSTALL_NEWPKGNAME" "${DBDIR}/forbidden_conflicts"
301 elif reinstall_chk_forbidden_conflicts "$pkg"
303 message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it conflicts with installed packages."
308 if [ $is_to_dereg_from_list = yes ]
310 grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
311 > ${DBDIR}/deleted_conflicts.tmp || :
312 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
315 [ $is_skipped = yes ] && continue
316 origin_regexp_esc=`str_escape_regexp "$origin"`
317 origin_replace_regexp_esc=`str_escape_regexp "$origin_replace"`
318 if grep -q -E -e "^$origin_regexp_esc$" -e "^$origin_replace_regexp_esc$" "${DBDIR}/taboo.all.list" 2> /dev/null
320 message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it is taboo."
323 message_echo "INFO: Restoring a conflict, $origin ($pkg)."
324 if ! backup_pkg=`pkgsys_get_backup_pkg "$origin"`
326 message_echo "WARNING: No backup exists, gave up." >&2
329 if pkg_add_fF "$backup_pkg"
331 grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
332 > ${DBDIR}/deleted_conflicts.tmp || :
333 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
335 message_echo "WARNING: Failed to restore. Note that your system may experience troubles by this error." >&2
340 # ============= Check whether the all requirements are installed =============
341 reinstall_are_requirements_ready ()
343 [ -e "$REINSTALL_DBNODE_DIR/requirements.all.direct" ] || return 0
346 pkg_info_eO "$origin" || return 1
347 done < $REINSTALL_DBNODE_DIR/requirements.all.direct
350 # ============= Get the all requirements ready for a port by restarting them if deinstalled =============
351 reinstall_chk_and_restore_requirements ()
353 local tmp_restore tmp_isfailed
354 tmp_restore=${TMPDIR}/reinstall_setup_requirements:restore
355 tmp_isfailed=${TMPDIR}/reinstall_setup_requirements:isfailed
356 cp /dev/null "$tmp_restore"
357 rm -f "$tmp_isfailed"
358 cat "$REINSTALL_DBNODE_DIR/requirements.all.direct" 2> /dev/null \
361 pkg_info_eO "$origin" && continue
362 origin_regexp=`str_escape_regexp "$origin"`
363 if grep -q -E "^$origin_regexp$" "${DBDIR}/taboo.all.list" 2> /dev/null
365 message_echo "INFO: Restoration of a requirement [$origin] is avoided because it is set taboo."
366 touch "$tmp_isfailed"
369 origin_orig=`echo "$origin" \
370 | sed -E -f "${DBDIR}/REVERSE_REPLACE.complete_sed_pattern"`
371 if [ "x$origin_orig" != "x$origin" ]
373 pkg_info_eO "$origin_orig" && continue
374 origin_orig_regexp=`str_escape_regexp "$origin_orig"`
375 if grep -q -E "^$origin_orig_regexp$" "${DBDIR}/taboo.all.list" 2> /dev/null
377 message_echo "INFO: Restoration of a requirement [$origin_orig] is avoided because it is set taboo."
378 touch "$tmp_isfailed"
382 if ! pkgarc=`pkgsys_get_backup_pkg "$origin"` && \
383 ! pkgarc=`pkgsys_get_backup_pkg "$origin_orig"`
385 if grep -q -E "^$origin_regexp$" "${DBDIR}/failed.list" 2> /dev/null
387 touch "$tmp_isfailed"
392 printf '%s\t%s\n' "$origin_orig" "$pkgarc" >> $tmp_restore
394 [ -e "$tmp_isfailed" ] && return 1
395 while read origin pkgarc
397 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
398 pkg_regexp_esc=`str_escape_regexp "$pkg"`
399 if reinstall_chk_forbidden_conflicts "$pkg"
401 message_echo "INFO: Restoration of a requirement [$origin ($pkg)] is avoided because it conflicts with installed packages."
404 message_echo "INFO: Restoring a backed-up requirement [$origin ($pkg)]."
405 if pkg_add_fF "$pkgarc"
407 grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
408 > ${DBDIR}/deleted_conflicts.tmp || :
409 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
411 message_echo "WARNING: Failed to restore by the backed-up package." >&2
418 # ============= Back-up of the currently installed package =============
419 reinstall_pkg_backup ()
422 reinstall_chk_stage_complete PKG_BACKUP && return
423 pkg=`echo "$REINSTALL_CURRENTPKG" | tr ' ' '\n' | grep -v '^$' | tail -n 1`
426 message_echo "-- (Creating temporary backup package for $REINSTALL_ORIGPKGTAG)"
427 if backup_pkg=`pkgsys_create_backup_pkg "$pkg" "${DBDIR}/backup_packages"`
429 echo "$pkg" > $REINSTALL_DBNODE_DIR/backedup_version
430 echo "$backup_pkg" > $REINSTALL_DBNODE_DIR/backedup_pkgfile
432 message_echo "WARNING: Failed to create the backup package, but ignored by hoping success." >&2
436 reinstall_register_stage_complete PKG_BACKUP
439 # ============= Deinstallation of the currently installed package =============
440 reinstall_deinstall ()
442 [ -n "$REINSTALL_CURRENTPKG" ] || return 0
443 reinstall_chk_stage_complete DEINSTALL && return
444 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
446 pkg_delete_f "$REINSTALL_CURRENTPKG" || \
448 message_echo "WARNING: Failed to deinstall $REINSTALL_CURRENTPKG by $PKGSYS_CMD_PKG_DELETE." >&2
451 echo "$REINSTALL_CURRENTPKG" | tr ' ' '\n' | grep -v '^$' | while read pkg
453 pkg_delete_f "$pkg" || \
455 message_echo "WARNING: Failed to deinstall $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
459 message_echo "-- (Trying to deinstall by ports to make sure. This usually ends up with warnings.)"
460 reinstall_make deinstall || \
462 message_echo "WARNING: Failed to deinstall $REINSTALL_CURRENTPKG by make deinstall." >&2
464 reinstall_register_stage_complete DEINSTALL
467 # ============= Recovery after failure of installation of the new package =============
468 reinstall_failed_install_recover ()
470 local backedup_version origin_regexp_esc backup_pkg
471 reinstall_chk_stage_complete FAILED_INSTALL.RECOVER && return
472 message_echo "INFO: Trying to deinstall the failed/terminated installation (Ignore failures)."
473 if [ -n "$REINSTALL_CURRENTPKG" ]
475 pkg_delete_f "$REINSTALL_CURRENTPKG" || :
477 message_echo "INFO: Trying to deinstall by ports to make sure (This usually ends up with warnings)."
478 reinstall_make deinstall || :
479 backedup_version=`cat "$REINSTALL_DBNODE_DIR/backedup_version" 2> /dev/null || :`
480 origin_regexp_esc=`str_escape_regexp "$REINSTALL_ORIGIN"`
481 if grep -q -E "^$origin_regexp_esc$" "${DBDIR}/taboo.all.list" 2> /dev/null
483 message_echo "INFO: Restoration of the backup of $backedup_version is avoided because it is taboo."
484 elif [ -n "$backedup_version" ]
486 if reinstall_chk_forbidden_conflicts "$backedup_version"
488 message_echo "INFO: Restoration of the backup of $backedup_version, is avoided because it conflicts with installed packages."
490 message_echo "INFO: Restoring the backup of $backedup_version."
491 backup_pkg=`cat "$REINSTALL_DBNODE_DIR/backedup_pkgfile" 2> /dev/null || :`
492 if [ -z "$backup_pkg" -o ! -e "$backup_pkg" ]
494 message_echo "WARNING: No backup exists, gave up." >&2
495 elif ! pkg_add_fF "$backup_pkg"
497 message_echo "WARNING: Failed to restore $backedup_version. Note that your system may experience troubles by this error." >&2
501 reinstall_register_stage_complete FAILED_INSTALL.RECOVER
504 # ============= Report an installation success to the all dependents =============
505 reinstall_tell_update_to_depandents ()
507 local tag level dbsuffix
508 pkgsys_is_pkgtool "$REINSTALL_ORIGIN" && return
509 reinstall_chk_stage_complete TELL_UPDATE_TO_DEPANDENTSL && return
510 for tag in all run build none
512 for level in full direct
514 dbsuffix=${tag}.${level}
515 cat "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix" "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix.orig" 2> /dev/null \
517 | while read origin_dependent
519 [ -d "${DBDIR}/requires/$origin_dependent" ] || continue
520 touch "${DBDIR}/requires/$origin_dependent/need_reinstall_due_to_upgraded_requirements.$dbsuffix"
521 fileedit_rm_a_line "$origin_dependent" \
522 "${DBDIR}/success.$dbsuffix.list"
523 fileedit_rm_a_line "$origin_dependent" \
524 "${DBDIR}/todo_after_requirements_succeed.$dbsuffix.list"
528 reinstall_register_stage_complete TELL_UPDATE_TO_DEPANDENTS
531 # ============= Closing operations after an installation success =============
532 reinstall_closing_operations_after_successful_install ()
534 local tag level initial_orig
535 reinstall_chk_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL && return
536 database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
537 database_build_update_pkgtag "$REINSTALL_ORIGIN"
538 for tag in all run build none
540 for level in full direct
542 rm -f "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${tag}.${level}"
543 [ -e "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" ] || continue
544 cp "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" \
545 "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}.previous"
548 pkgsys_delete_backup_pkg "$REINSTALL_ORIGIN"
549 initial_orig=`cat "$REINSTALL_DBNODE_DIR/initial_orig" 2> /dev/null` || :
550 [ -n "$initial_orig" ] && pkgsys_delete_backup_pkg "$initial_orig"
551 rm -f "$REINSTALL_DBNODE_DIR/backedup_pkgfile"
552 reinstall_deregister_stage in_postinstall
553 database_record_success "$REINSTALL_ORIGIN"
554 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
555 message_echo "===> Done successfully"
556 reinstall_register_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL
559 # ============= Reinstallation for an origin =============
564 REINSTALL_ORIGIN_REGEXP=`str_escape_regexp "$REINSTALL_ORIGIN"`
565 REINSTALL_DBNODE_DIR=${DBDIR}/requires/$REINSTALL_ORIGIN
566 REINSTALL_CURRENTPKG=`database_build_update_pkgname "$REINSTALL_ORIGIN" | tr '\n' ' ' | sed 's/ *$//'`
567 REINSTALL_IS_CURRENTPKG_LATEST=no
568 database_build_is_currentpkg_latest "$REINSTALL_ORIGIN" && REINSTALL_IS_CURRENTPKG_LATEST=yes
569 database_build_update_pkgtag "$REINSTALL_ORIGIN"
570 REINSTALL_PKGTAG=`cat "$REINSTALL_DBNODE_DIR/pkgtag"`
571 REINSTALL_ORIGPKGTAG="$REINSTALL_ORIGIN ($REINSTALL_PKGTAG)"
572 REINSTALL_DBSUFFIX=`options_get_dependency_type`.`options_get_dependency_level`
573 REINSTALL_NEWPKGNAME=`database_build_get_new_pkgname "$REINSTALL_ORIGIN"`
574 message_stage_title "$PROGRAM_STEP_COUNTER $REINSTALL_ORIGPKGTAG"
575 database_query_get_target_attributes currentorigin "$REINSTALL_ORIGIN"
576 if [ -z "${currentorigin_is_all}" -a -z "${currentorigin_is_relevant}" ]
578 reinstall_skip 'Skipped because being irrelevant'
581 reinstall_skip_if_in_a_list 'Skipped because being a leaf port' leaf_ports_to_delete && return
582 if expr "$REINSTALL_CURRENTPKG" : '.* .*' > /dev/null
584 message_echo "WARNING: Multiple packages are registered for this port. Reinstallation is needed to fix it." >&2
585 elif grep -q -E "^$REINSTALL_ORIGIN_REGEXP$" "${DBDIR}/damaged_package" 2> /dev/null
587 message_echo "WARNING: Installed files have invalid checksums for this port. Reinstallation is needed to fix it." >&2
589 if [ -e "${DBDIR}/target_all" ]
591 if [ $REINSTALL_IS_CURRENTPKG_LATEST = yes ]
593 reinstall_restore_if_temporarily_deinstalled
594 reinstall_skip 'Skipped because being already latest' "$REINSTALL_ORIGIN"
597 elif [ ! -e "$REINSTALL_DBNODE_DIR/conf_updated" ]
599 if [ -e "$nodedir/installed_by_pkg" ] && database_query_is_default_conf "$REINSTALL_ORIGIN" quiet
601 reinstall_restore_if_temporarily_deinstalled
602 reinstall_skip 'Skipped because already upgraded with a prebuilt package'
605 reinstall_skip_if_in_a_list 'Skipped because the reinstallation has been already completed' \
606 "success.${REINSTALL_DBSUFFIX}.list" restore && return
607 if [ $opt_skip_unchanged = yes ]
609 if [ ! -e "$REINSTALL_DBNODE_DIR/necessary_upgrade.${REINSTALL_DBSUFFIX}" ]
611 reinstall_restore_if_temporarily_deinstalled
612 if [ -e "$REINSTALL_DBNODE_DIR/installed_version" ]
614 reinstall_skip 'Skipped because being already latest as well as the all requirements'
616 reinstall_skip 'Skipped because being an only-build-time dependency of already latest packages'
621 if [ \( $opt_skip_unchanged = no -a -e "$REINSTALL_DBNODE_DIR/succeeded_once" \) \
622 -o \( $opt_skip_unchanged = yes -a $REINSTALL_IS_CURRENTPKG_LATEST = yes \) ]
624 if [ ! -e "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${REINSTALL_DBSUFFIX}" ]
626 if ! database_query_is_necessary_upgrade "$REINSTALL_ORIGIN"
628 reinstall_restore_if_temporarily_deinstalled
629 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
630 "${DBDIR}/todo_after_requirements_succeed.${REINSTALL_DBSUFFIX}.list"
631 reinstall_skip 'Skipped because being already latest or failed as well as the all requirements'
637 reinstall_skip_if_in_a_list 'Marked to be manually-done' manually_done.list restore && return
639 reinstall_skip_if_in_a_list 'Skipped because being a hold package' conf/HOLD:PORTS.parsed restore && return
640 if database_query_is_a_port_suppressed "$REINSTALL_ORIGIN"
642 reinstall_restore_if_temporarily_deinstalled
643 reinstall_skip 'Skipped because being suppressed'
646 reinstall_skip_if_in_a_list 'Ignored because being taboo' taboo.all.list && return
647 if ! reinstall_are_requirements_ready && ! reinstall_chk_and_restore_requirements
649 reinstall_restore_if_temporarily_deinstalled
650 for tag in all run build none
652 for level in full direct
654 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
655 "${DBDIR}/todo_after_requirements_succeed.${tag}.${level}.list"
658 reinstall_skip 'Skipped because the requirements cannot be ready'
661 if [ -e "$REINSTALL_DBNODE_DIR/installed_version" ]
664 instdesc='a reinstallation'
667 instdesc='an installation'
669 temp_set_msg_current_stage "$instdesc process for $REINSTALL_ORIGPKGTAG $PROGRAM_STEP_COUNTER"
670 message_target_relations "$REINSTALL_ORIGIN"
671 message_echo "------- Starting $instdesc process --------"
672 if [ $opt_dry_run = yes ]
675 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
678 if [ $opt_inst_by_pkg_if_can = yes ] && database_query_is_default_conf "$REINSTALL_ORIGIN"
680 message_echo "INFO: The configuration of this port is default, so use of a prebuilt package is attempted."
681 if reinstall_chk_stage in_bypkg
683 message_echo "(Restarting the previously terminated (re)installation-by-package process...)"
685 reinstall_register_stage in_bypkg
687 if ! reinstall_chk_stage_complete INSTALL_BY_PKG
689 if ! reinstall_chk_stage FAILED_INSTALL_BY_PKG
691 if [ "x$PKGSYS_USE_PKGNG" = xyes ] \
692 && pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
694 if reinstall_deinstall && pkg_is_tool_available && pkg_info_e "$REINSTALL_NEWPKGNAME"
696 reinstall_register_stage_complete INSTALL_BY_PKG
698 reinstall_register_stage FAILED_INSTALL_BY_PKG
701 func_pkg_inst_remote_fetch=pkg_inst_remote_fetch
702 func_pkg_inst_remote=pkg_inst_remote
703 if [ $opt_use_legacy_pkg_for_missing_pkgng = yes ]
705 func_pkg_inst_remote_fetch=pkg_inst_remote_wild_fetch
706 func_pkg_inst_remote=pkg_inst_remote_wild
708 if $func_pkg_inst_remote_fetch "$REINSTALL_NEWPKGNAME"
710 pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" \
711 | reinstall_backup_and_delete_conflicts
712 reinstall_pkg_backup || :
713 reinstall_register_stage in_add_pkg
715 if $func_pkg_inst_remote "$REINSTALL_NEWPKGNAME"
717 reinstall_register_stage_complete INSTALL_BY_PKG
719 reinstall_register_stage FAILED_INSTALL_BY_PKG
721 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
723 pkg_is_tool_available || pkg_rescue_tools
726 reinstall_register_stage FAILED_INSTALL_BY_PKG
730 if reinstall_chk_stage FAILED_IN_ADD_PKG && reinstall_chk_stage in_add_pkg
732 reinstall_failed_install_recover
735 if reinstall_chk_stage_complete INSTALL_BY_PKG
737 touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
738 touch "$REINSTALL_DBNODE_DIR/installed_by_pkg"
739 reinstall_deregister_stage in_bypkg
740 reinstall_tell_update_to_depandents
741 reinstall_closing_operations_after_successful_install
745 reinstall_deregister_stage in_bypkg
746 message_echo "WARNING: (Re)installation-by-package is unsuccessful, so retrying by using port." >&2
748 if reinstall_chk_stage in_prebuild
750 message_echo "(Restarting the previously terminated pre-build process...)"
752 reinstall_restore_conflicts
753 reinstall_register_stage in_prebuild
755 if [ -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf" ] && ! reinstall_chk_stage_complete BEFOREBUILD
757 message_echo "-- BEFOREBUILD operations (start)"
758 sh -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf" || \
760 reinstall_restore_if_temporarily_deinstalled
761 reinstall_errproc 'BEFOREBUILD operations'
762 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
765 message_echo "-- BEFOREBUILD operations (end)"
766 reinstall_register_stage_complete BEFOREBUILD
768 if ! reinstall_chk_stage_complete CLEAN_BEFORE_BUILD
770 reinstall_make_anymode clean || \
772 reinstall_restore_if_temporarily_deinstalled
773 reinstall_errproc 'clean before build'
774 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
778 reinstall_register_stage_complete CLEAN_BEFORE_BUILD
780 if ! reinstall_chk_stage_complete FETCH
782 if ! reinstall_chk_stage FAILED_FETCH
784 reinstall_make_anymode checksum || \
785 reinstall_register_stage FAILED_FETCH
787 if reinstall_chk_stage FAILED_FETCH
789 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_1
791 message_echo "INFO: Refetching distfiles for $REINSTALL_ORIGPKGTAG."
793 reinstall_make_anymode fetch FETCH_ARGS=-Ap &&
794 reinstall_make_anymode checksum
795 } || reinstall_register_stage FAILED_REFETCH_1
796 reinstall_register_stage_complete FAILED_FETCH.RETRIAL_1
798 if reinstall_chk_stage FAILED_REFETCH_1
800 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
802 message_echo "INFO: Cleaning distfiles for the second refetch for $REINSTALL_ORIGPKGTAG."
803 reinstall_make_anymode distclean || :
804 reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
806 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2
808 message_echo "INFO: Refetching distfiles as the second retrial for $REINSTALL_ORIGPKGTAG."
809 if ! reinstall_make_anymode fetch FETCH_ARGS=-Ap
811 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
812 reinstall_restore_if_temporarily_deinstalled
813 reinstall_errproc 'fetch'
814 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
817 reinstall_make_anymode checksum || \
819 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
820 reinstall_restore_if_temporarily_deinstalled
821 reinstall_errproc 'checksum'
822 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
825 reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2
829 reinstall_register_stage_complete FETCH
831 reinstall_deregister_stage in_prebuild
832 if [ $opt_batch_ports_only = yes -o $opt_interactive_ports_only = yes ]
835 case `database_query_get_makevar_val "$REINSTALL_ORIGIN" IS_INTERACTIVE` in
836 yes) msg_is_interactive='interactive'
837 [ $opt_batch_ports_only = yes ] && to_skip=yes
839 '') msg_is_interactive='not interactive'
840 [ $opt_interactive_ports_only = yes ] && to_skip=yes
843 if [ $to_skip = yes ]
845 reinstall_restore_if_temporarily_deinstalled
846 database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
847 message_echo "INFO: Further processes for this port are skipped because it is $msg_is_interactive."
849 fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
850 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
854 if reinstall_chk_stage in_build
856 message_echo "(Restarting the previously terminated build process...)"
857 flag_restarted_build=yes
859 reinstall_register_stage in_build
860 flag_restarted_build=no
862 if ! reinstall_chk_stage_complete BUILD
864 pkgsys_get_conflicting_installed_pkgs build "$REINSTALL_ORIGIN" \
865 | reinstall_backup_and_delete_conflicts
866 if ! reinstall_make build
868 [ $flag_restarted_build = yes ] && message_echo "INFO: This failure may be due to restarting from a terminated build."
869 is_build_env_modified=no
870 if pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" \
871 > ${TMPDIR}/reinstall_exec::conflicts_install
873 message_echo "INFO: Install-only conflicts are deinstalled for retrial because they may have effects on build."
874 reinstall_backup_and_delete_conflicts < ${TMPDIR}/reinstall_exec::conflicts_install
875 is_build_env_modified=yes
877 if pkg_info_eO "$REINSTALL_ORIGIN"
879 message_echo "INFO: The currently installed package for this port is deinstalled for retrial because it may have effects on build."
880 reinstall_pkg_backup || :
882 is_build_env_modified=yes
884 if [ $flag_restarted_build = no -a $is_build_env_modified = no ]
886 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
888 pkg_is_tool_available || pkg_rescue_tools
890 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
891 reinstall_restore_if_temporarily_deinstalled
892 reinstall_errproc 'build'
893 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
896 reinstall_deregister_stage_complete CLEAN_BEFORE_BUILD
897 message_echo "INFO: Retrying the build process after cleaning for $REINSTALL_ORIGPKGTAG."
898 reinstall_make clean || \
900 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
902 pkg_is_tool_available || pkg_rescue_tools
904 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
905 reinstall_restore_if_temporarily_deinstalled
906 reinstall_errproc 'build and clean after build failure'
907 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
910 reinstall_register_stage_complete CLEAN_BEFORE_BUILD
911 reinstall_make build MAKE_JOBS_UNSAFE=yes || \
913 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
915 pkg_is_tool_available || pkg_rescue_tools
917 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
918 reinstall_restore_if_temporarily_deinstalled
919 reinstall_errproc 'retrial of build after failure'
920 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
924 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
926 pkg_is_tool_available || pkg_rescue_tools
928 reinstall_register_stage_complete BUILD
930 reinstall_deregister_stage in_build
931 if reinstall_chk_stage in_stage
933 message_echo "(Restarting the previously terminated staging process...)"
935 reinstall_register_stage in_stage
937 if ! reinstall_chk_stage_complete STAGE
939 if ! reinstall_make stage
941 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
942 reinstall_restore_if_temporarily_deinstalled
943 reinstall_errproc 'STAGE operations'
944 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
947 reinstall_register_stage_complete STAGE
949 reinstall_deregister_stage in_stage
950 if reinstall_chk_stage in_install
952 message_echo "(Restarting the previously terminated installation process...)"
954 reinstall_register_stage in_install
956 reinstall_pkg_backup || :
957 if [ -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf" ] && ! reinstall_chk_stage_complete BEFOREDEINSTALL
959 if [ -n "$REINSTALL_CURRENTPKG" ]
961 message_echo "-- BEFOREDEINSTALL operations (start)"
962 sh -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf" || \
964 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
965 reinstall_restore_if_temporarily_deinstalled
966 reinstall_errproc 'BEFOREDEINSTALL operations'
967 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
970 message_echo "-- BEFOREDEINSTALL operations (end)"
972 reinstall_register_stage_complete BEFOREDEINSTALL
974 if ! reinstall_chk_stage_complete INSTALL
976 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
978 reinstall_backup_and_delete_remaining_install_conflicts
982 reinstall_backup_and_delete_remaining_install_conflicts
984 if ! reinstall_chk_stage FAILED_INSTALL
986 if reinstall_make $insttarget || \
988 message_echo "INFO: Cleaning up for retrial."
989 reinstall_make deinstall \
990 || message_echo "WARNING: Continuing by hoping a success." >&2
991 message_echo "INFO: Retrying the installation."
992 reinstall_make $insttarget MAKE_JOBS_UNSAFE=yes
995 touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
996 reinstall_register_stage_complete INSTALL
998 reinstall_register_stage FAILED_INSTALL
1001 if reinstall_chk_stage FAILED_INSTALL
1003 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1005 pkg_is_tool_available || pkg_rescue_tools
1007 reinstall_failed_install_recover
1009 if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && \
1010 ! reinstall_chk_stage_complete FAILED_INSTALL.AFTERINSTALL
1012 if [ -n "$REINSTALL_CURRENTPKG" ]
1014 message_echo "-- AFTERINSTALL operations (start)"
1015 sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" || \
1017 message_echo "WARNING: Failed in AFTERINSTALL operations." >&2
1018 message_echo "---- (The process is continued anyway)"
1020 message_echo "-- AFTERINSTALL operations (end)"
1022 reinstall_register_stage_complete FAILED_INSTALL.AFTERINSTALL
1024 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1025 reinstall_restore_if_temporarily_deinstalled
1026 reinstall_errproc 'install'
1027 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1031 reinstall_restore_if_temporarily_deinstalled
1032 if ! reinstall_chk_stage_complete UPDATE_CONFLICTS
1034 pkg_info_qL "$REINSTALL_CURRENTPKG" | str_escape_regexp_filter | sed 's/^/^/; s/$/$/' \
1035 > ${TMPDIR}/previously_installed_files.grep_pattern
1036 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1037 | while read origin_bak pkgpath_bak
1039 pkg_bak=`pkgsys_pkgarc_to_pkgname "$pkgpath_bak"`
1040 if [ "$origin_bak" = "$REINSTALL_ORIGIN" ]
1042 [ "$pkg_bak" = "$REINSTALL_NEWPKGNAME" ] && continue
1043 elif reinstall_quick_chk_forbidden_conflicts "$pkg_bak"
1046 elif ! pkgsys_chk_match_to_restored_files_by_backup_pkg "$origin_bak" "${TMPDIR}/previously_installed_files.grep_pattern"
1050 fileedit_add_a_line_if_new "$pkg_bak:$REINSTALL_NEWPKGNAME" \
1051 "${DBDIR}/forbidden_conflicts"
1053 reinstall_register_stage_complete UPDATE_CONFLICTS
1055 if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && ! reinstall_chk_stage_complete AFTERINSTALL
1057 message_echo "-- AFTERINSTALL operations (start)"
1058 sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" || \
1060 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1061 reinstall_errproc 'AFTERINSTALL operations'
1062 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1065 message_echo "-- AFTERINSTALL operations (end)"
1066 reinstall_register_stage_complete AFTERINSTALL
1068 reinstall_deregister_stage in_install
1069 reinstall_register_stage in_postinstall
1070 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1071 if ! reinstall_chk_stage_complete CLEAN_AFTER_INSTALL
1073 reinstall_make clean || \
1075 message_echo "WARNING: Failed to clean $REINSTALL_ORIGPKGTAG." >&2
1077 reinstall_register_stage_complete CLEAN_AFTER_INSTALL
1079 reinstall_tell_update_to_depandents
1080 reinstall_closing_operations_after_successful_install