1 #!/bin/bash 2 # 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 . "$(dirname "$0")/common_minimal.sh" 8 9 # Array of actions that are executed during the clean up process. 10 declare -a cleanup_actions 11 12 # Adds an action to be executed during the clean up process. 13 # Actions are executed in the reverse order of when they were added. 14 # ARGS: ACTION 15 add_cleanup_action() { 16 cleanup_actions[${#cleanup_actions[*]}]=$1 17 } 18 19 # Performs the latest clean up action and removes it from the list. 20 perform_latest_cleanup_action() { 21 local num_actions=${#cleanup_actions[*]} 22 if [ ${num_actions} -gt 0 ]; then 23 eval "${cleanup_actions[$num_actions-1]}" > /dev/null 2>&1 24 unset cleanup_actions[$num_actions-1] 25 fi 26 } 27 28 # Performs clean up by executing actions in the cleanup_actions array in 29 # reversed order. 30 cleanup() { 31 set +e 32 33 while [ ${#cleanup_actions[*]} -gt 0 ]; do 34 perform_latest_cleanup_action 35 done 36 37 set -e 38 } 39 40 # ANSI color codes used when displaying messages. 41 # Taken from src/scripts/common.sh. 42 V_RED="\e[31m" 43 V_YELLOW="\e[33m" 44 V_BOLD_GREEN="\e[1;32m" 45 V_BOLD_RED="\e[1;31m" 46 V_BOLD_YELLOW="\e[1;33m" 47 V_VIDOFF="\e[0m" 48 49 # Prints an informational message. 50 # Taken from src/scripts/common.sh. 51 # Arg: MESSAGE 52 info() { 53 echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}" 54 } 55 56 # Prints a warning message. 57 # Taken from src/scripts/common.sh. 58 # Arg: MESSAGE 59 warn() { 60 echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}" 61 } 62 63 # Prints the specified error and exit the script with an error code. 64 # Taken from src/scripts/common.sh. 65 # Args: MESSAGE 66 error() { 67 echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}" 68 } 69 70 # Prints an error message and exit with an error code. 71 # Taken from src/scripts/common.sh. 72 # Args: MESSAGE 73 die() { 74 error "$1" 75 exit 1 76 } 77 78 # This will override the trap set in common_minmal.sh 79 trap "cleanup" INT TERM EXIT 80 81 add_cleanup_action "cleanup_temps_and_mounts" 82