Unique Filename

Unique Filename

Post by Comcas » Tue, 01 Jan 2002 03:05:01



What is the best way to create a unique file name for temporary files?

...Mike

 
 
 

Unique Filename

Post by Michael Heimin » Tue, 01 Jan 2002 03:18:06



Quote:> What is the best way to create a unique file name for temporary
> files?

'date +%s' or using $RANDOM if your shell allows and you create  > 1
file per second or combine both.

Michael Heiming

 
 
 

Unique Filename

Post by Maurice F » Tue, 01 Jan 2002 05:39:03



>What is the best way to create a unique file name for temporary files?

>...Mike

man tempnam

HTH - Maurice

 
 
 

Unique Filename

Post by Erik Max Franci » Tue, 01 Jan 2002 05:32:28



> What is the best way to create a unique file name for temporary files?

man mkstemp

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.

 
 
 

Unique Filename

Post by Comcas » Tue, 01 Jan 2002 11:17:47


Thanks to all who answered. I finally settled on the date/time + $RANDOM.

...Mike


Quote:> What is the best way to create a unique file name for temporary files?

> ...Mike

 
 
 

Unique Filename

Post by Tapani Tarvaine » Tue, 01 Jan 2002 15:44:26



> Thanks to all who answered. I finally settled on the date/time + $RANDOM.

That is not guaranteed to be unique.

In particular, in a security-critical application it can be
relatively easily predicted by a malicious attacker,
and in a heavily-used machine two processes using it might
accidentally hit on the same name ($RANDOM may well be same
for each instance of the process, so if two of them run within
same second...)

That may or may not matter in your case (like, if you are creating the
in a protected directory and you know there can't be two instances at
the same time), but if you are using a public directory like /tmp and
letting others run it, you might wish to do it more carefully.

The multiple-instance problem can be handled simply by adding
process ID ($$) to the string, but it won't help against
malicious attackers.

If your system has a working mktemp you could use it (but be
warned, quite a few mktemp's are broken and vulnerable to
attacks), otherwise here's a sample code that works with
Korn shell:

n=0
set -o noclobber
until MYTMP=/tmp/mytmp$$.$n; >$MYTMP ;do
   n=$(( n+1 ))
done 2>/dev/null
set +o noclobber

Basically that keeps incrementing the suffix number until
it finds a free filename, and then creates it in same atomic
operation, so no other process can intervene.
With POSIX-compliant shells you can achieve the same by
replacing "set -o noclobber" by "set -C" and
"set +o noclobber" by "set +C"; with other shells it may or
may not be possible, depending on availability of an
test-and-create -type atomic operation.

--
Tapani Tarvainen

 
 
 

Unique Filename

Post by Ryan Henness » Fri, 04 Jan 2002 03:59:56



> What is the best way to create a unique file name for temporary files?

> ...Mike

Wait a minute...  How is this to be used?  Do you want a standalone
program that you can run to generate a name only?  Do have an existing
program that needs to use a temp file?

If the latter, why do you even need a file name?  You can use the
standard C function tmpfile() in stdio.h to simply return a handle to a
new, temporary file, which will close and unlink itself upon program
exit.  Check the following snippet:

#include <stdio.h>

int main(void) {
   FILE *temp = tmpfile();
    if (temp) {
       /* Do things with temp */
    }
     return 0;

Quote:}

If the former, use the standard C function tmpnam(), also in stdio.h.  It
is guaranteed to return a unique string that is not the name of an
existing file. If you want a program that will generate a unique filename
for you, go like this:

/* File:  GimmeTmpName.c */
#include <stdio.h>

int main(void) {
   printf("%s", tmpnam(NULL));
   return 0;

Quote:}

Then, when compiled, you can do things like this:
$ ./GimmeTmpName
/var/tmp/aaaeLayCl

$ ps -aef > `./GimmeTmpName`

Hope this helps,
Ryan.

 
 
 

Unique Filename

Post by Comcas » Sun, 06 Jan 2002 14:35:28


Thanks, Tapani. I'm going to file your soultion for later use. A fairly
simple solution works for my current problem since I'm simply creating
temporary files from several cronned scripts which mail statistics to me.
It's in a directory which is used only for this purpose and security is not
an issue. Thanks, again.

...Mike



> > Thanks to all who answered. I finally settled on the date/time +
$RANDOM.

> That is not guaranteed to be unique.

> In particular, in a security-critical application it can be
> relatively easily predicted by a malicious attacker,
> and in a heavily-used machine two processes using it might
> accidentally hit on the same name ($RANDOM may well be same
> for each instance of the process, so if two of them run within
> same second...)

> That may or may not matter in your case (like, if you are creating the
> in a protected directory and you know there can't be two instances at
> the same time), but if you are using a public directory like /tmp and
> letting others run it, you might wish to do it more carefully.

> The multiple-instance problem can be handled simply by adding
> process ID ($$) to the string, but it won't help against
> malicious attackers.

> If your system has a working mktemp you could use it (but be
> warned, quite a few mktemp's are broken and vulnerable to
> attacks), otherwise here's a sample code that works with
> Korn shell:

> n=0
> set -o noclobber
> until MYTMP=/tmp/mytmp$$.$n; >$MYTMP ;do
>    n=$(( n+1 ))
> done 2>/dev/null
> set +o noclobber

> Basically that keeps incrementing the suffix number until
> it finds a free filename, and then creates it in same atomic
> operation, so no other process can intervene.
> With POSIX-compliant shells you can achieve the same by
> replacing "set -o noclobber" by "set -C" and
> "set +o noclobber" by "set +C"; with other shells it may or
> may not be possible, depending on availability of an
> test-and-create -type atomic operation.

> --
> Tapani Tarvainen