Joseph K. Myers

Tuesday, December 31, 2002

Tmp: Temporary Files

The first thing is that temporary files are eventually for permanent files. The second thing is that temporary files are used so that permanent files do not have to be temporary.

Suppose you have a website. You edit the home page, index.html, and for an hour your website shows odd splotches and blotches as pieces begin to move around and paragraphs start being written, thus perplexing any visitors.

Or, you copy index.html to /tmp/1.html, and you finish working on /tmp/1.html. When you are finished, you move /tmp/1.html to index.html. Hey presto! Your home page is instantly and completely finished, with never a nanosecond of downtime.

But what is a good way, a good standard way, of dealing with temporary files? Some temporary files might be left around, and must be deleted in case the program using them happens to crash. Others are deleted immediately after creation, in order for calculations to be worked out, the solution of which perhaps a simple number, which could not have been found without a large deleted space to spread things out. Some are like cache files, others are like files we edit, in between the moment we begin to change it and the moment we are ready to stop.

I suggest that every program save its edited files (the ones whose unchanged contents will be required later) in its working directory, under a name o.$$; the file will be renamed to replace another file when necessary. I suggest that every program working with files that are to be deleted open them in /tmp/o.$$. $$ is the process id number, and so is unique to each process. If simultaneous temporary files are needed, then o.$$ is changed to o.$$.$n, where $n is an incremented number.