[Insight-developers] portable temporary file name generator function?
kent williams
norman-k-williams at uiowa.edu
Thu Jul 30 10:20:22 EDT 2009
I¹m sure that would be fine; the problem is that with respect to provable
correctness and security there is no perfect way. I don¹t know if GUIDs are
more or less unique than the concatenation of two 8 random integers. My
thought with this code was that if you seed the random number generator with
your process ID, you¹ll be using a number sequence that is locally unique.
The perfect temporary file name generator would
1. Work with some sort of per-user preference for a preferred temporary
directory.
2. Work atomically, i.e. create a temporary file and open it.
3. Return the filename, not just the open file handle.
4. Be thread safe.
My code assumes that /tmp is writable, and won¹t run out of space, and is
not atomic, nor thread safe. For the specific program I¹m using it in,
that¹s kind of OK for now.
On 7/30/09 6:38 AM, "Luis Ibanez" <luis.ibanez at kitware.com> wrote:
> Kent, Mathieu,
>
> Could we use the UUID functionalities of GDCM
> in order to generate a unique filename ?
>
>
> My 2 cents,
>
>
> Luis
>
>
> --------------------------------------------------
> On Wed, Jul 29, 2009 at 5:48 PM, kent williams <norman-k-williams at uiowa.edu>
> wrote:
>> Yeah, you found the conundrum. If you can't come up with a temporary name
>> and create the file in one atomic operation, you're theoretically up against
>> a race condition -- another process could create that file between choosing
>> the filename and opening it.
>>
>> My 'solution' is robust enough for our purposes, though it's neither
>> thread-safe nor secure. It also does nothing to check for things like /tmp
>> filling up or being unwritable. And it will fail on Windows as it stands
>> unless you've made a '/tmp' directory on every drive. But I will worry about
>> that if we ever deploy on Windows.
>>
>> namespace {
>>
>> vnl_random randgen;
>> bool randomized(false);
>> // TODO
>> std::string NewTempName()
>> {
>> #if !defined(_WIN32)
>> if(!randomized)
>> {
>> randomized = true;
>> randgen.reseed(static_cast<unsigned long>(getpid()));
>> }
>> #endif
>> // it needs to be an image name. None of the standard library functions I
>> know
>> // about generate unique filenames with suffixes.
>> for(;;)
>> {
>> std::ostringstream strvalue;
>> strvalue << "/tmp/";
>> strvalue << randgen.lrand32();
>> strvalue << randgen.lrand32();
>> strvalue << ".nii.gz";
>> if(!vtksys::SystemTools::FileExists(strvalue.str().c_str(),true))
>> {
>> return strvalue.str().c_str();
>> }
>> }
>> }
>>
>>
>> On 7/29/09 4:26 PM, "Mathieu Malaterre" <mathieu.malaterre at gmail.com> wrote:
>>
>>> > On Tue, Jul 28, 2009 at 8:46 PM, kent
>>> > williams<norman-k-williams at uiowa.edu> wrote:
>>>> >> I have need to generate some temporary filenames, in order to write out
>>>> >> images for external programs to use as inputs.
>>>> >>
>>>> >> I notice there's no temporary file name generator in
>>>> itksys::SystemTools.
>>>> >>
>>>> >> Shouldn't there be?
>>>> >>
>>>> >> I'd can write one, no doubt, but don't have Windows builds to test it
>>>> on.
>>>> >>
>>>> >> Is there one buried somewhere in CMake?
>>> >
>>> > The only one I found out to be working on linux and win32 was tempnam:
>>> >
>>> > char *tempnam(const char *dir, const char *pfx);
>>> >
>>> > Most documentation will tell you this is a bad idea anyway and should
>>> > instead request a file (without knowing the actual filename) from the
>>> > system (eg: tmpfile).
>>> >
>>> >
>>> > HTH
>>
>> _______________________________________________
>> Powered by www.kitware.com <http://www.kitware.com>
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-developers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20090730/7ca7aded/attachment.htm>
More information about the Insight-developers
mailing list