Skip to content

Commit

Permalink
Perform cleanup action only after executing commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zdyxry committed Jun 27, 2023
1 parent c1cede8 commit 8667191
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
43 changes: 43 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,47 @@ test_untar_D_flag_commit()
fi
}

test_untar_D_flag_commit_without_cleanup()
{
local shell=$1
cp $RESOURCE_DIR/* "$2/"
# Will always commit the result in case of try
if [ "$shell" == "bash" ]; then
$shell gunzip $2/file.txt.gz
else
try_example_dir=$(mktemp -d)
$shell -D $try_example_dir gunzip $2/file.txt.gz
if ! [ -d "$try_example_dir" ]; then
echo "try_example_dir does not exist"
return 1
fi
$shell commit $try_example_dir
if ! [ -d "$try_example_dir" ]; then
echo "try_example_dir does not exist"
return 1
fi
fi
}

test_touch_and_rm_with_cleanup()
{
TMPDIR="/tmp"
local shell=$1
cp $RESOURCE_DIR/* "$2/"
# Will always commit the result in case of try
if [ "$shell" == "bash" ]; then
$shell $MISC_SCRIPT_DIR/touch_echo_and_rm.sh $2/file_1.txt $2/file_2.txt $2/file.txt.gz
else
tmp_file_count1=$(ls $TMPDIR | wc -l)
$shell -y $MISC_SCRIPT_DIR/touch_echo_and_rm.sh $2/file_1.txt $2/file_2.txt $2/file.txt.gz
tmp_file_count2=$(ls $TMPDIR | wc -l)
# We save try_mount_log in /tmp/ dir
if [ $tmp_file_count1 -ne $(($tmp_file_count2 - 1)) ]; then
return 1
fi
fi
}

test_touch_and_rm_no_flag()
{
local shell=$1
Expand Down Expand Up @@ -219,6 +260,8 @@ if [ "$#" -eq 0 ]; then
run_test test_touch_and_rm_no_flag
# run_test test_touch_and_rm_n_flag_commit
run_test test_touch_and_rm_D_flag_commit
run_test test_touch_and_rm_with_cleanup
run_test test_untar_D_flag_commit_without_cleanup

else
for testname in $@
Expand Down
25 changes: 18 additions & 7 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ try() {
export try_mount_log=$(mktemp)

# Cleanup the temporary files
CLEANUP="rm -rf $mount_and_execute $chroot_executable $try_mount_log"
CLEANUP="$mount_and_execute $chroot_executable"
if [ "$SANDBOX_DIR_EXISTS" != "1" ]; then
CLEANUP="$CLEANUP $SANDBOX_DIR"
fi
Expand Down Expand Up @@ -101,8 +101,6 @@ EOF

chmod +x "$mount_and_execute" "$chroot_executable"

trap "$CLEANUP" EXIT

# --mount: mounting and unmounting filesystems will not affect the rest of the system outside the unshare
# --map-root-user: map to the superuser UID and GID in the newly created user namespace.
# --user: the process will have a distinct set of UIDs, GIDs and capabilities.
Expand All @@ -118,24 +116,37 @@ EOF
case "$NO_COMMIT" in
(quiet) ;;
(show) printf "%s\n" "$SANDBOX_DIR";;
(commit) commit "$SANDBOX_DIR";;
(commit) commit "$SANDBOX_DIR"
cleanup $CLEANUP;;
(interactive)
summary "$SANDBOX_DIR" >&2
if [ "$exitcode" -eq 0 ] && [ "$?" -eq 0 ]
if [ "$?" -eq 0 ]
then
echo
read -p "Commit these changes? [y/N] " DO_COMMIT >&2
case "$DO_COMMIT" in
(y|Y|yes|YES) commit "$SANDBOX_DIR";;
(y|Y|yes|YES) commit "$SANDBOX_DIR"
cleanup $CLEANUP;;
(*) printf "Not committing.\n" >&2
echo "$SANDBOX_DIR";;
esac
fi
;;
esac
$CLEANUP
}

################################################################################
# Cleanup temporary files
################################################################################

cleanup() {
if [ -n "$CLEANUP" ]; then
echo "Cleaning up temporary files: $CLEANUP"
rm -rf $CLEANUP
fi
}


################################################################################
# Summarize an overlay
################################################################################
Expand Down

0 comments on commit 8667191

Please sign in to comment.