- call-with-temporary-filename maker #!optional (prefix temp-file-prefix) #!key (tries 10)procedure
This procedure can be used to perform certain atomic transactions on the file system involving filenames. Some examples:
Linking a file to a fresh backup temp name. Creating and opening an unused, secure temp file. Creating an unused temporary directory.
This procedure uses prefix to generate a series of trial filenames. Prefix is a string, and defaults to the value of invoking temp-file-prefix. File names are generated by concatenating prefix with a varying string.
The maker procedure is called serially on each filename generated. It must return at least one value; it may return multiple values. If the first return value is #f or if maker signals an exception indicating that the file exists, call-with-temporary-filename will loop, generating a new filename and calling maker again. If the first return value is true, the loop is terminated, returning whatever value(s) maker returned.
After a number of unsuccessful trials, call-with-temporary-filename may give up, in which case an exception is signaled or propagated.
To rename a file to a temporary name:
(call-with-temporary-filename (lambda (backup) (create-hard-link old-file backup) backup) ".temp.") ; Keep link in current working directory (delete-file old-file)Recall that this SRFI reports procedure failure by signaling an error. This is critical for this example — the programmer can assume that if the call-with-temporary-filename call returns, it returns successfully. So the following delete-file call can be reliably invoked, safe in the knowledge that the backup link has definitely been established.
To create a unique temporary directory:
(call-with-temporary-filename (lambda (dir) (create-directory dir) dir) "/tmp/tempdir.")Similar operations can be used to generate unique fifos, or to return values other than the new filename (for example, an open port).