OSDN Git Service

Version 3.0.3+toward_3.0.4_20131115074959
[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 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         [ "x${DATABASE_IS_OBSOLETE}" = xyes ] || return 0
19         message_echo "WARNING: The Ports tree was updated after construction of the temporary database for ${APPNAME}." >&2
20         message_echo "You should consider executing " >&2
21         message_echo "        ${APPNAME} clean" >&2
22         message_echo "to reset the temporary database unless you have special purposes." >&2
23 }
24
25 # ============= Creation of temporary work directories =============
26 temp_trap_init ()
27 {
28         trap 'errno=$?; temp_warn_obsolete_temp_db; temp_warn_obsolete_temp_db () { :; }; temp_terminate_process; 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 "(Error exit by $errno)" >&2; exit $errno' 0 1 2 3 9 15 17 18
29         { until TMPDIR=`mktemp -dq /tmp/"${APPNAME}".XXXXXXXX` ; do : ; done ; }
30         chgrp "`id -gn`" "${TMPDIR}"
31 }
32
33 # ============= Set the signal trap to be friendly for invoking a new version =============
34 temp_trap_for_invoking_new_version ()
35 {
36         [ x`cat "${DBDIR}/.lock" 2> /dev/null` = x$$ ] && rm -f "${DBDIR}/.lock"
37         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
38 }
39
40 # ============= Set the message telling the current stage =============
41 temp_set_msg_current_stage ()
42 {
43         echo "$*" > ${TMPDIR}/TEMP::MSG_CURRENT_STAGE
44 }
45
46 # ============= Get the message telling the current stage =============
47 temp_get_msg_current_stage ()
48 {
49         cat "${TMPDIR}/TEMP::MSG_CURRENT_STAGE" 2> /dev/null || :
50 }