Task

Create a temporary file, securely and exclusively (opening it such that there are no possible race conditions).

It's fine assuming local filesystem semantics (NFS or other networking filesystems can have signficantly more complicated semantics for satisfying the "no race conditions" criteria).

The function should automatically resolve name collisions and should only fail in cases where permission is denied, the filesystem is read-only or full, or similar conditions exist (returning an error or raising an exception as appropriate to the language/environment).




/* NetRexx */
options replace format comments java crossref symbols binary
 
runSample(arg)
return
 
-- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
method makeTempFile(prefix = String, suffix = String null, startDir = String null) -
  public static signals IOException returns File
  if startDir \= null then fStartDir = File(startDir)
  else                     fStartDir = null
  ff = File.createTempFile(prefix, suffix, fStartDir)
  ff.deleteOnExit() -- make sure the file is deleted at termination
  return ff
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
  do
    tempFiles = [ -
      makeTempFile('rexx'), -
      makeTempFile('rexx', '.rex'), -
      makeTempFile('rexx', null, './tmp') -
    ]
    loop fFile over tempFiles
      fName = fFile.getCanonicalPath()
      say 'Temporary file:' fName
      end fFile
  catch ex = IOException
    ex.printStackTrace()
  end
  return
 