OSDN Git Service

Fixed the wrong implementation of show errormessage.
[portsreinstall/current.git] / lib / libtemp.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Temporary directory and signal trapping -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Variables =============
10 TMPDIR=/tmp     # Temporary directory
11
12 # ============= Default additional operation in a trap process =============
13 temp_terminate_process () { :; }
14
15 # ============= Warn when the temporary database is obsolete =============
16 temp_warn_obsolete_temp_db () { :; }
17
18 # ============= Creation of temporary work directories =============
19 temp_trap_init ()
20 {
21         trap 'errno=$?; temp_warn_obsolete_temp_db >&2 || :; temp_warn_obsolete_temp_db () { :; }; temp_terminate_process $errno >&2 || :; temp_terminate_process () { :; }; rm -rf "${TMPDIR}" 2> /dev/null; [ -n "${DBDIR}" -a x`cat "${DBDIR}/.lock" 2> /dev/null` = x$$ ] && rm -f "${DBDIR}/.lock"; [ $errno -gt 0 -a $errno -ne 130 ] && message_echo "(Exit code: $errno)" >&2; exit $errno' 0 1 2 3 9 15 17 18
22         { until TMPDIR=`mktemp -dq /tmp/"${APPNAME}".XXXXXXXX` ; do : ; done ; }
23         chgrp "`id -gn`" "${TMPDIR}"
24 }
25
26 # ============= Set the signal trap to be friendly for invoking a new version =============
27 temp_trap_for_invoking_new_version ()
28 {
29         [ x`cat "${DBDIR}/.lock" 2> /dev/null` = x$$ ] && rm -f "${DBDIR}/.lock"
30         trap 'errno=$?; rm -rf "${TMPDIR}" 2> /dev/null; [ $errno -gt 0 -a $errno -ne 130 ] && echo "(Error exit by $errno)" >&2; exit $errno' 0 1 2 3 9 15 17 18
31 }
32
33 # ============= Set the message telling the current stage =============
34 temp_set_msg_current_stage ()
35 {
36         echo "$*" > ${TMPDIR}/TEMP::MSG_CURRENT_STAGE
37 }
38
39 # ============= Get the message telling the current stage =============
40 temp_get_msg_current_stage ()
41 {
42         cat "${TMPDIR}/TEMP::MSG_CURRENT_STAGE" 2> /dev/null || :
43 }
44
45 # ============= Reset the termination messages to the common one =============
46 temp_reset_termination_messages_common ()
47 {
48         temp_terminate_process ()
49         {
50                 local errno
51                 errno=${1:-0}
52                 temp_terminate_process_common "$errno"
53         }
54 }