Merge pull request #1 from KatolaZ/master

added ztmpd to create temporary directories automatically cleaned up
This commit is contained in:
D.J.R 2016-06-13 16:02:35 +02:00 committed by GitHub
commit 36c81fbbe6
1 changed files with 21 additions and 0 deletions

21
zuper
View File

@ -264,6 +264,17 @@ ztmp() {
tmpfiles+=($ztmpfile)
}
vars+=(ztmpdir)
# ztmpd() fills in $ztmpdir global. Caller must copy that variable as
# it will be overwritten at every call.
ztmpd() {
fn ztmpd
ztmpdir=`mktemp -d`
tmpdirs+=($ztmpdir)
}
# All tempfiles are freed in endgame()
_ztmp_destructor() {
fn _ztmp_destructor
@ -271,10 +282,20 @@ _ztmp_destructor() {
for f in $tmpfiles; do
rm -f "$f"
done
for d in $tmpdirs; do
[[ $d == "" || ! -d $d ]] && continue
pushd $d
[[ `pwd` == "/" ]] && {popd; continue}
popd
rm -rf "$d"
done
tmpfiles=()
tmpdirs=()
}
arrs+=(tmpfiles)
arrs+=(tmpdirs)
destruens+=(_ztmp_destructor)
# }}} Tempfiles