Browse Source

added ztmpd to create temporary directories automatically cleaned up
at endgame()

KatolaZ 3 years ago
parent
commit
41e1de2b30
1 changed files with 21 additions and 0 deletions
  1. 21 0
      zuper

+ 21 - 0
zuper

@@ -264,6 +264,17 @@ ztmp() {
264 264
     tmpfiles+=($ztmpfile)
265 265
 }
266 266
 
267
+vars+=(ztmpdir)
268
+# ztmpd() fills in $ztmpdir global. Caller must copy that variable as
269
+# it will be overwritten at every call.
270
+
271
+ztmpd() {
272
+    fn ztmpd
273
+
274
+    ztmpdir=`mktemp -d`
275
+    tmpdirs+=($ztmpdir)
276
+}
277
+
267 278
 # All tempfiles are freed in endgame()
268 279
 _ztmp_destructor() {
269 280
     fn _ztmp_destructor
@@ -271,10 +282,20 @@ _ztmp_destructor() {
271 282
     for f in $tmpfiles; do
272 283
         rm -f "$f"
273 284
     done
285
+    for d in $tmpdirs; do 
286
+        [[ $d == "" || ! -d $d ]] && continue
287
+        pushd $d
288
+        [[ `pwd` == "/" ]] && {popd; continue}
289
+        popd 
290
+        rm -rf "$d"
291
+    done
292
+
274 293
     tmpfiles=()
294
+    tmpdirs=()
275 295
 }
276 296
 
277 297
 arrs+=(tmpfiles)
298
+arrs+=(tmpdirs)
278 299
 destruens+=(_ztmp_destructor)
279 300
 
280 301
 # }}} Tempfiles