From: ara.t.howard@... Date: 2006-05-02T03:00:13+09:00 Subject: Re: Generate unique filenames On Tue, 2 May 2006, Bill Guindon wrote: > On 5/1/06, 13 wrote: >> Hi, >> >> Tempfile creates an empty file (in /tmp by default) even if I don't >> write in it. It's not very good IMO. I rather choose some self made >> random number based solution. It would be great if there were some >> Tempfile class method like Tempfile.name(basename, tmpdir=Dir::tmpdir) >> which returns just a name without creating a file. > > It's creating the file immediately to prevent other processes or > threads from creating/accessing the same filename. Depending on what > you're trying to do, that could be very handy. it seems like that, but it does not. File::EXCL is broken on many filesystems and, worse, fails silently. one needs to use a tmpnam algorithm with hostname/pid/time/rand to minimize the chances of dup names or use a form of locking which works on all filesystems. afaik the only portable atomic fs operation is 'link' and, ergo, the locking mechanism must be built on top of this. my lockfile class has an impl if your interested - just wanted to point out that the file creation in tempfile.rb is anything but atomic and fails silently when atomicity fails. fyi. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama