OSDN Git Service

Fixed a bug related to the previous modification.
[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 TEMP_IN_TRAP=no # Whether to be in a error trap process
12
13 # ============= Default additional operation in a trap process =============
14 temp_terminate_process () { :; }
15
16 # ============= Warn when the temporary database is obsolete =============
17 temp_warn_obsolete_temp_db () { :; }
18
19 # ============= Creation of temporary work directories: Trap operation =============
20 temp_trap_init__opetration ()
21 {
22         local errno
23         errno=$1
24         set +x
25         TEMP_IN_TRAP=yes
26         temp_warn_obsolete_temp_db >&2 || :
27         temp_warn_obsolete_temp_db () { :; }
28         temp_terminate_process $errno >&2 || :
29         temp_terminate_process () { :; }
30         rm -rf "${TMPDIR}" 2> /dev/null
31         [ -n "${DBDIR}" -a x`cat "${DBDIR}/.lock" 2> /dev/null` = x$$ ] && rm -f "${DBDIR}/.lock"
32         [ $errno -gt 0 -a $errno -ne 130 -a $opt_batch_mode = no -a $opt_no_opening_message = no ] && echo "(Exit code: $errno)" >&2
33         exit $errno
34 }
35
36 # ============= Creation of temporary work directories =============
37 temp_trap_init ()
38 {
39         trap 'temp_trap_init__opetration $?' 0 1 2 3 9 15 17 18
40         { until TMPDIR=`mktemp -dq /tmp/"${APPNAME}".XXXXXXXX` ; do : ; done ; }
41         chgrp "`id -gn`" "${TMPDIR}"
42 }
43
44 # ============= Set the signal trap to be friendly for invoking a new version: Trap operation =============
45 temp_trap_for_invoking_new_version__opetration ()
46 {
47         local errno
48         errno=$1
49         set +x
50         rm -rf "${TMPDIR}" 2> /dev/null
51         [ $errno -gt 0 -a $errno -ne 130 -a $opt_batch_mode = no -a $opt_no_opening_message = no ] && echo "(Exit code: $errno)" >&2
52         exit $errno
53 }
54
55 # ============= Set the signal trap to be friendly for invoking a new version =============
56 temp_trap_for_invoking_new_version ()
57 {
58         [ x`cat "${DBDIR}/.lock" 2> /dev/null` = x$$ ] && rm -f "${DBDIR}/.lock"
59         trap 'temp_trap_for_invoking_new_version__opetration $?' 0 1 2 3 9 15 17 18
60 }
61
62 # ============= Set the message telling the current stage =============
63 temp_set_msg_current_stage ()
64 {
65         echo "$*" > ${TMPDIR}/TEMP::MSG_CURRENT_STAGE
66 }
67
68 # ============= Get the message telling the current stage =============
69 temp_get_msg_current_stage ()
70 {
71         cat "${TMPDIR}/TEMP::MSG_CURRENT_STAGE" 2> /dev/null || :
72 }
73
74 # ============= Reset the termination messages to the common one =============
75 temp_reset_termination_messages_common ()
76 {
77         temp_terminate_process ()
78         {
79                 local errno
80                 errno=${1:-0}
81                 temp_terminate_process_common "$errno"
82         }
83 }