tempfile.nim icon indicating copy to clipboard operation
tempfile.nim copied to clipboard

option for guaranteed auto cleanup using file descriptor trick

Open timotheecour opened this issue 7 years ago • 1 comments

this answer uses a nice trick to guarantee auto cleanup:

tmpfile=$(mktemp /tmp/abc-script.XXXXXX)
exec 3>"$tmpfile"
rm "$tmpfile"
: ...
echo foo >&3

the cleanup would happen even if a crash happens (or, say, a quit):

When it gets closed (which the kernel does automatically when the process exits) the filesystem deletes the file

the same trick could be used (as option!) in tempfile API

timotheecour avatar Nov 30 '18 04:11 timotheecour

thank you!

ba0f3 avatar Nov 30 '18 05:11 ba0f3