1# Copyright 2012 Google Inc. 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: 7# 8# * Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# * Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# * Neither the name of Google Inc. nor the names of its contributors 14# may be used to endorse or promote products derived from this software 15# without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29# \file lib.subr 30# Common initialization and functions for shell scripts. 31 32 33set -e 34 35 36# Directory where the running script lives. 37Lib_DirName="$(dirname ${0})" 38 39 40# Base name of the running script. 41Lib_ProgName="${0##*/}" 42 43 44# Path to the temporary directory for this execution. 45Lib_TempDir= 46 47 48# List of cleanup functions to execute on exit. 49_Lib_Cleanup_Hooks= 50 51 52# Catch unexpected exits and perform the required cleanups. In particular, 53# ensure that the temporary directory in Lib_TempDir, if any, is removed. 54trap 'lib_cleanup ; exit 2' HUP INT QUIT TERM 55 56 57# Prints an informational message. 58# 59# \param ... The message to print. Can be provided as multiple words and, in 60# that case, they are joined together by a single whitespace. 61lib_info() { 62 echo "${Lib_ProgName}: I: $*" 1>&2 63} 64 65 66# Prints a runtime error and exits. 67# 68# \param ... The message to print. Can be provided as multiple words and, in 69# that case, they are joined together by a single whitespace. 70lib_error() { 71 echo "${Lib_ProgName}: E: $*" 1>&2 72 exit 1 73} 74 75 76# Prints a runtime warning. 77# 78# \param ... The message to print. Can be provided as multiple words and, in 79# that case, they are joined together by a single whitespace. 80lib_warning() { 81 echo "${Lib_ProgName}: W: $*" 1>&2 82} 83 84 85# Prints an usage error and exits. 86# 87# \param ... The message to print. Can be provided as multiple words and, in 88# that case, they are joined together by a single whitespace. 89lib_usage_error() { 90 echo "${Lib_ProgName}: E: $*" 1>&2 91 usage "${Lib_ProgName}" 1>&2 92 exit 1 93} 94 95 96# Executes the registered cleanup hooks. 97lib_cleanup() { 98 local hook 99 for hook in ${_Lib_Cleanup_Hooks}; do 100 "${hook}" 101 done 102} 103 104 105# Installs a new cleanup hook. 106# 107# \param ... The names of the cleanup functions to register. 108lib_register_cleanup() { 109 _Lib_Cleanup_Hooks="${_Lib_Cleanup_Hooks} ${*}" 110} 111 112 113# Creates a temporary directory for this execution. 114# 115# The temporary directory is unique to this script and execution. A cleanup 116# hook is installed to delete such directory whenever lib_cleanup is called or 117# when the program abruptly exits. 118# 119# \post Lib_TempDir is set to the path of the created temporary directory. 120lib_init_tempdir() { 121 Lib_TempDir=$(mktemp -d -t "${_Lib_ProgName}.XXXXXX") 122 lib_register_cleanup "lib_clean_tempdir" 123} 124 125 126# Cleanup hook to delete the temporary directory. 127# 128# This operation is idempotent. 129lib_clean_tempdir() { 130 if [ -n "${Lib_TempDir}" ]; then 131 rm -rf "${Lib_TempDir}" 132 Lib_TempDir= 133 fi 134} 135