Skip to content

Instantly share code, notes, and snippets.

@nobrowser
Created August 16, 2015 00:40
Show Gist options
  • Save nobrowser/6a756f610e433ba4a6b3 to your computer and use it in GitHub Desktop.
Save nobrowser/6a756f610e433ba4a6b3 to your computer and use it in GitHub Desktop.
I noticed that temporary file names created by Emacs' make-temp-file functions were a bit too short for comfort, so I wrote my own.
(defun itz-mktemp (prefix &optional dir-flag suffix)
"A better temporary file creator function."
(let ((rg "dd if=/dev/urandom bs=16 count=1 2>/dev/null | md5sum")
(sfx (or suffix ""))
(creator
(if dir-flag #'mkdir
(lambda (f) (write-region "" nil f nil 'quiet nil 'excl))))
(oldmodes (default-file-modes)))
(unwind-protect
(catch 'created
(set-default-file-modes ?\600)
(while t
(let* ((output (shell-command-to-string rg))
(bytes (nth 0 (split-string output "[ \t]+")))
(base (concat prefix bytes sfx))
(path (expand-file-name base temporary-file-directory)))
(condition-case nil
(progn (funcall creator path) (throw 'created path))
(file-already-exists nil)))))
(set-default-file-modes oldmodes))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment