From e7844991a0ba555cc0062614dae09c65212f59a1 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Tue, 26 Apr 2016 14:35:02 -0500 Subject: [PATCH] use subshell; use /tmp for file --- snap-pac | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/snap-pac b/snap-pac index 12de205..8484392 100755 --- a/snap-pac +++ b/snap-pac @@ -27,19 +27,26 @@ if [[ $EUID -ne 0 ]]; then exit 1 fi -if [[ "$1" != "pre" ]] && [[ "$1" != "post" ]]; then +if [[ $# -ne 1 ]]; then + echo "ERROR: Only one argument should be passed to this script." + exit 1 +fi + +if [[ $1 != "pre" ]] && [[ $1 != "post" ]]; then echo "ERROR: First argument should either be 'pre' or 'post'." exit 1 fi declare -r pre_or_post=$1 -declare -r prefile_prefix="/usr/share/libalpm/hooks.bin/snap-pac/.pre" +declare -r prefile_prefix="/tmp/snap-pac-pre" declare -r pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))" declare -r snapper_config_dir="/etc/snapper/configs" -declare -r configurations="$(find $snapper_config_dir/* -printf '%f\t')" +declare -r configurations="$(find $snapper_config_dir/* -printf '%f\t\0')" for x in $configurations; do + ( + source $snapper_config_dir/$x if [[ $x == "root" ]]; then @@ -48,33 +55,36 @@ for x in $configurations; do take_snapshot=${PACMAN_PRE_POST:-"no"} fi - if [[ $take_snapshot == "yes" ]]; then + [[ $take_snapshot != "yes" ]] && exit 0 + + cleanupalgo=${PACMAN_CLEANUP_ALGORITHM:-"number"} - cleanupalgo=${PACMAN_CLEANUP_ALGORITHM:-"number"} + if [[ "$pre_or_post" == "pre" ]]; then - if [[ "$pre_or_post" == "pre" ]]; then + description=${PACMAN_PRE_DESCRIPTION:-"$pacman_cmd"} + snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --print-number --description "$description" > $prefile_prefix"_"$x + exit 0 - description=${PACMAN_PRE_DESCRIPTION:-"$pacman_cmd"} - snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --print-number --description "$description" > $prefile_prefix"_"$x + elif [[ "$pre_or_post" == "post" ]]; then - elif [[ "$pre_or_post" == "post" ]]; then + if [ -f $prefile_prefix"_"$x ]; then - if [ -f $prefile_prefix"_"$x ]; then + description=${PACMAN_POST_DESCRIPTION:-"$pacman_cmd"} + snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --pre-number $(< $prefile_prefix"_"$x) --description "$description" + rm $prefile_prefix"_"$x + exit 0 - description=${PACMAN_POST_DESCRIPTION:-"$pacman_cmd"} - snapper --config $x create --type $pre_or_post --cleanup-algorithm $cleanupalgo --pre-number $(cat $prefile_prefix"_"$x) --description "$description" - rm $prefile_prefix"_"$x + else - else - - echo "WARNING: $prefile_prefix"_"$x does not exist, so no post snapshot will be taken. If you are initially installing snap-pac, this is normal." - - fi + echo "WARNING: $prefile_prefix"_"$x does not exist, so no post snapshot will be taken. If you are initially installing snap-pac, this is normal." + exit 1 fi fi + ) & + done -exit +exit 0