mkstemp

Henry Cejtin henry@sourcelight.com
Thu, 21 Jun 2001 16:27:17 -0500


As  to  directories,  only the mdir system call can meke them, and it doesn't
have the fancy options that open has.  On the other hand, mkdir will fail  if
the  file  already  exists, even if it is a symlink to something that doesn't
exist.  Thus there are no race conditions.

As you say, mkstemp is all just using the correct open options,  so  I  would
definitely  do  it  using the posix stuff.  If you need files with particular
suffixes, then you can't use mkstemp(), but you can, of course, get the  same
effect  using  the  correct open modes by hand.  The key point is O_CREAT and
O_EXCL.  They indicate that you want to create the file, and  that  you  want
the  call  to fail if the file exists, even if it is a symlink to a file that
does not exist.  You also have to supply the mode argument, which  should  be
octal  666  (any  one can read and write, no execute), which is automatically
adjusted by the umask removing some of these bits.  If the file is  going  to
be executable you should use octal 777 instead.

The  Linux  manual doesn't seem to spell it out, but the Sun manual indicates
that the file descriptor that mkstemp  returns  is  opened  for  reading  AND
writing.  This makes sense since it is often used as a temporary file between
different  stages  of  the  same  program.   In  this   cake   the   proposed
MLton.TextIO.mkstemp you suggest isn't quite right since you are only getting
the output part.  I don't think that this is a big deal since one can  always
manually do the open calls using the Posix.FileSys has what one needs.