Skip to content

Commit

Permalink
Auto cleanup temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
zdyxry committed Jun 25, 2023
1 parent b2df6b6 commit 587cd2c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ TRY_VERSION="0.1.0"
try() {
START_DIR="$PWD"

[ "$SANDBOX_DIR" ] || SANDBOX_DIR=$(mktemp -d)
if [ -d "$SANDBOX_DIR" ]; then
SANDBOX_DIR_EXISTS=1
else
SANDBOX_DIR=$(mktemp -d)
fi
export SANDBOX_DIR
mkdir -p "$SANDBOX_DIR/upperdir" "$SANDBOX_DIR/workdir" "$SANDBOX_DIR/temproot"

Expand All @@ -39,6 +43,13 @@ try() {
mount_and_execute=$(mktemp)
export chroot_executable=$(mktemp)
export try_mount_log=$(mktemp)

# Cleanup the temporary files
CLEANUP="rm -rf $mount_and_execute $chroot_executable $try_mount_log"
if [ "$SANDBOX_DIR_EXISTS" != "1" ]; then
CLEANUP="$CLEANUP $SANDBOX_DIR"
fi

cat >"$mount_and_execute" <<"EOF"
#!/bin/sh
Expand Down Expand Up @@ -77,7 +88,6 @@ if ! [ -f "$SANDBOX_DIR/temproot/$chroot_executable" ]; then
cp $chroot_executable "$SANDBOX_DIR/temproot/$chroot_executable"
fi
unshare --root="$SANDBOX_DIR/temproot" /bin/bash "$chroot_executable"
EOF

Expand All @@ -91,6 +101,8 @@ 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 @@ -109,7 +121,7 @@ EOF
(commit) commit "$SANDBOX_DIR";;
(interactive)
summary "$SANDBOX_DIR" >&2
if [ "$?" -eq 0 ]
if [ "$exitcode" -eq 0 ] && [ "$?" -eq 0 ]
then
echo
read -p "Commit these changes? [y/N] " DO_COMMIT >&2
Expand All @@ -121,6 +133,7 @@ EOF
fi
;;
esac
$CLEANUP
}

################################################################################
Expand Down Expand Up @@ -278,3 +291,4 @@ case "$1" in
esac

exit $exitcode

0 comments on commit 587cd2c

Please sign in to comment.