TIP #210: Add 'tempname' to file 
Author Message
 TIP #210: Add 'tempname' to file

I'd like to take another spin at adding temporary file support to the
[file] command.  I submitted TIP #210 back in August, and we debated
the shortcomings of the tmpnam() function.

I've updated the TIP  at http://www.*-*-*.com/
reflect a better API which should avoid the security flaw and race
condition in tmpnam() calls.  This is based on calling mkstemp(),
which opens the temporary file and returns an open file file pointer.

The new sytax would be

    file tempfile ?template? ?namevar?

It always returns an open file channel to the temporary file.  If the
template string is specified, then characters will be appended to
create the unique filename. If namevar is specified, then the command
will set the variable to the name of the temporary file.  This allows
the user to create temporary files, create temporary files with
specific name patterns, or create temporary files and get back the
filename, which is needed if you're copying things out of a VFS for
loading or execution.

Is this a more reasonable API than the previous file name
specification?  It would be pretty straight forward on POSIX compliant
unices, as they have mkstemp().  But I would entertain any suggestions
for how to implement it on Windows.  I don't see much for temporary
file names in my MSDN docs.

Thanks,
Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA             http://www.*-*-*.com/



Sat, 21 Apr 2007 06:56:52 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> The new sytax would be
>     file tempfile ?template? ?namevar?
> It always returns an open file channel to the temporary file.

yes, that makes sense, imho.

Quote:
> If the
> template string is specified, then characters will be appended to
> create the unique filename.

While this might beat mkstemp()-functionality, I wonder if it would
be useful enough to be able to also have a fixed string *after*
the part that is auto-filled.  E.g. one might want his tempfile
to have a certain filename-extension.  While on unix it would be
possible to rename the open file, other OS's might not allow this.

Quote:
> Is this a more reasonable API than the previous file name
> specification?  It would be pretty straight forward on POSIX compliant
> unices, as they have mkstemp().  But I would entertain any suggestions
> for how to implement it on Windows.  I don't see much for temporary
> file names in my MSDN docs.

As long as there is   open(...,O_CREAT|O_EXCL)  (iirc), that should
be a solid base to roll one's own mkstemp(), but I don't know if
these flags are available for windows, nor if these flags really
suffice for implementing mkstemp()-functionality.


Sun, 22 Apr 2007 02:48:58 GMT  
 TIP #210: Add 'tempname' to file

| It would be pretty straight forward on POSIX compliant unices, as
| they have mkstemp().  But I would entertain any suggestions for how
| to implement it on Windows.  I don't see much for temporary file
| names in my MSDN docs.

Windows has `tmpfile()', which resembles mkstemp() in that it returns
an open FILE* pointer, and tempnam(), which just returns a file name
(possible race condition, just as in Unix tempnam()).

Other than that, there is _O_TEMPORARY for _open(), which deletes the
file when the last file descriptor is closed (somewhat like
open/unlink on Unix).

R'



Sun, 22 Apr 2007 17:52:32 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> I would swap the order of the arguments:
>     file tempfile ?namevar ?template??

Keep open the option of using -options for this sort of thing. :^)

  file tempfile ?-variable namevar? ?-template template?

This would also make the code easy to expand in the future (e.g. if
you want to pass additional flags through to the underlying [open] for
some reason.)

Donal.



Sun, 22 Apr 2007 18:01:56 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> Windows has `tmpfile()', which resembles mkstemp() in that it
returns
> an open FILE* pointer, and tempnam(), which just returns a file name
> (possible race condition, just as in Unix tempnam()).

I saw that.  But tmpfile() only returns an open file pointer with no
hope of ever finding the file name.  And tempnam() returns a file
name, but you have that {*filter*} race condition.  I'm thinking that I
might have to adapt BSD's mkstemp.c code for non-POSIX systems.

But even that doesn't help me with the other issue that has been
niggling at my hindbrain.  It would be really nice if the [file
tempfile] command supported creating files in the system temporary
directory.  There's a fair amount of code in tcllib fileutils for
discovering such a place.  It looks at TMPDIR, TEMP, and TMP
environment variables, then looks in system specific directories, and
finally the current working directory.  Maybe this API could be
extended with a -systemdirectory switch, which would attempt to create
the temp file in system locations.

Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester



Sun, 22 Apr 2007 21:57:58 GMT  
 TIP #210: Add 'tempname' to file

Quote:


>>I would swap the order of the arguments:
>>    file tempfile ?namevar ?template??

> Keep open the option of using -options for this sort of thing. :^)

>   file tempfile ?-variable namevar? ?-template template?

> This would also make the code easy to expand in the future (e.g. if
> you want to pass additional flags through to the underlying [open] for
> some reason.)

> Donal.

I agree hearily with Donal hear, if things are optional it is (imho)
much better to have them named with -option flags, it completely
remvoes the headache of wanting one but not the other, but the order
defined precludes you from doing that. And as Donal has mentioned it
makes future additions easier and more consistent.

Bruce



Sun, 22 Apr 2007 23:59:49 GMT  
 TIP #210: Add 'tempname' to file

| But tmpfile() only returns an open file pointer with no hope of ever
| finding the file name.

After digging around a bit more I stumbled upon GetTempFileName().  It
creates the file under a unique name, so you just would have to reopen
it in the TCL function.  The race condition is not present here, since
the function already created the file.

| It would be really nice if the [file tempfile] command supported
| creating files in the system temporary directory.

IMHO that should be the default operation.  Only if I specify a
different location, the function should use that.

| There's a fair amount of code in tcllib fileutils for discovering
| such a place.

On UNIX I would expect $TMP and /tmp in that order to be searched.
On Windows, there is GetTempPath() (which also uses $TMP, $TEMP and
the `windows system directory').  Don't know for MAC.

R'



Mon, 23 Apr 2007 17:26:15 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> On UNIX I would expect $TMP and /tmp in that order to be
> searched.  On Windows, there is GetTempPath() (which also
> uses $TMP, $TEMP and the `windows system directory').
> Don't know for MAC.

The Tcllib code creates a list of directories, starting with
env(TMPDIR), env(TEMP), and env(TMP).  On windows it appends C:/TEMP
C:/TMP and /TMP, on macs it appends env(TRASH_FOLDER), and on other
systems it appends /tmp, /var/tmp, and /usr/tmp.

There are probably "good" ways to search for a nice directory for temp
files on each type of system.  The implementation should probably do
the best thing on each platform.  I suppose that might even entail
opening a temp file which is deleted on close or process exit, like
tmpfile().  But that might be hard to do in a portable manner.

Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA            http://www.mayo.edu/sppdg/



Wed, 25 Apr 2007 03:54:11 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> The Tcllib code creates a list of directories, starting with
> env(TMPDIR), env(TEMP), and env(TMP).  On windows it appends C:/TEMP
> C:/TMP and /TMP, on macs it appends env(TRASH_FOLDER), and on other
> systems it appends /tmp, /var/tmp, and /usr/tmp.

Well, Windows systems should use the per-user temp directories. It
solves all kinds of problems that UNIX-like temp directories cause, like
people deleting your temp files before they're gone, temp files{*filter*}
around after they're not needed, etc.


Wed, 25 Apr 2007 04:23:58 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> It solves all kinds of problems that UNIX-like temp directories cause, like
> people deleting your temp files before they're gone,

please expand on that one.

properly set up unix-systems have /tmp  with permissions 1777
(or rwxrwsrwt)  which allows all users to place new files, but
prevents users from deleting other users' files (as would
be possible with wrongly-setup 777 (rwxrwxrwx)  /tmp)

Quote:
> temp files{*filter*} around after they're not needed, etc.

In what way would user-specific temp-directories solve this?
Are they auto-deleted on logout? brrr.


Thu, 26 Apr 2007 19:50:46 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> properly set up unix-systems have /tmp  with permissions 1777
> (or rwxrwsrwt)  which allows all users to place new files, but
> prevents users from deleting other users' files (as would
> be possible with wrongly-setup 777 (rwxrwxrwx)  /tmp)

Well, yes, they do *now*. :-) It also lets other people see what temp
files you have, read/write your temp files if you've accidentally left
them readable/writable, lets you encrypt your own directory so the
administrator can't read your temp files, lets you encrypt your own
directory so deleted temp files don't leave goodies around for hackers
who steal the machine to read, and etc.

Quote:
>>temp files{*filter*} around after they're not needed, etc.
> In what way would user-specific temp-directories solve this?

Because a user can clean up his own files without stepping on someone
else. Admittedly, with the proper tools, this can be done on UNIX also,
but I've never seen the proper tools on UNIX.

The point is not "user-specific windows files let you clean up your temp
files better than unix", but that "if you use a global temp dir, Windows
doesn't have built-in the tools to clean it up, like it does for using
the 'proper' temp dir."

Quote:
> Are they auto-deleted on logout? brrr.

I actually worked on a system (CP-V) that had temp files that got
deleted when you logged out, that you couldn't access from any other
login (even another login of the same account) and whose quotas came
from a different bucket. Worked fine.

If logging out shouldn't delete temp files, why should rebooting?



Fri, 27 Apr 2007 01:31:13 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> Well, Windows systems should use the per-user temp directories. It
> solves all kinds of problems that UNIX-like temp directories cause,
like
> people deleting your temp files before they're gone, temp files
{*filter*}
> around after they're not needed, etc.

Can you give me a pointer to the "right" way to access per-user temp
directories?  Is there something in the win32 API?  I poked through my
MSDN a bit, but didn't find much.

Thanks,
Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA             http://www.*-*-*.com/



Sat, 28 Apr 2007 00:04:08 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> Can you give me a pointer to the "right" way to access per-user temp
> directories?  Is there something in the win32 API?  I poked through my
> MSDN a bit, but didn't find much.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwu...

Inside the "Local Settings" subdirectory is a "Temp" folder.

So it would be
%USERPROFILE%\Local Settings\Temp
I would think.

Yeah, you'd think they'd make it a bit easier to track down just where
they want you putting things.

I note that it looks like unless you override it, both TEMP and TMP
environment variables default to that subdirectory in Local Settings, so
it's likely this whole discussion was a waste of time. Oops. :-)



Sat, 28 Apr 2007 02:00:06 GMT  
 TIP #210: Add 'tempname' to file

| http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwu...
|
| Inside the "Local Settings" subdirectory is a "Temp" folder.
|
| So it would be
| %USERPROFILE%\Local Settings\Temp
| I would think.

Please note that this directory might have different names in
localized variants.  My german XP system has no `Local Settings\Temp'
but a `Lokale Einstellungen\Temp'.  At least they kept the `Temp' part :-)

R'



Sat, 28 Apr 2007 05:02:40 GMT  
 TIP #210: Add 'tempname' to file

Quote:

> I'd like to take another spin at adding temporary file support to the
> [file] command.  I submitted TIP #210 back in August, and we debated
> the shortcomings of the tmpnam() function.

> I've updated the TIP  at http://www.tcl.tk/cgi-bin/tct/tip/210.html to
> reflect a better API which should avoid the security flaw and race
> condition in tmpnam() calls.  This is based on calling mkstemp(),
> which opens the temporary file and returns an open file file pointer.

> The new sytax would be

>     file tempfile ?template? ?namevar?

> It always returns an open file channel to the temporary file.  If the
> template string is specified, then characters will be appended to
> create the unique filename. If namevar is specified, then the command
> will set the variable to the name of the temporary file.  This allows
> the user to create temporary files, create temporary files with
> specific name patterns, or create temporary files and get back the
> filename, which is needed if you're copying things out of a VFS for
> loading or execution.

> Is this a more reasonable API than the previous file name
> specification?  It would be pretty straight forward on POSIX compliant
> unices, as they have mkstemp().  But I would entertain any suggestions
> for how to implement it on Windows.  I don't see much for temporary
> file names in my MSDN docs.

I think one of most useful options is missing: creating a temporary
directory. One cannot use this function to do this safely (you would have
to close and delete the file in order to create a directory with the given
name). In my experience, namepatterns usually do not matter: Either it
doesn't matter how the temp file is called, or it has to have a specific
name. The second can only be done safely in a temporary directory. It also
solves the problem where several temporary files with specific names are
needed (e.g. to glue some old app that expects them that way).

Peter

--

Bioinformatics Unit                          Tel. +32 3 820 23 27
Department of Molecular Genetics VIB8        Fax. +32 3 820 25 41
University of Antwerp                http://www.molgen.ua.ac.be/
Universiteitsplein 1                 http://rrna.uia.ac.be/~peter
B-2610 Antwerpen, Belgium

The glass is not half full, nor half empty. The glass is just too big.



Mon, 30 Apr 2007 00:37:36 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. TIP #210: Add 'tempname' Subcommand to 'file'

2. TIP #208: Add a 'chan' Command

3. Wanted: Hints'n'Tips'n'Bits'n'Bobs

4. TIP #39: Add New Standard Tk Option: -component

5. TIP #19: Add a Text Changed Flag to Tk's Text Widget

6. Adding a File to a Procedure's File Schematic

7. TIP #188: Add 'string is wide' to the 'string is' Subcommand

8. stream('file','c','seek ='x) problem

9. TIP #99: Add 'file link' to Tcl

10. TIP #239: Enhance the 'load' Command

11. TIP #80: Additional Options for 'lsearch'

12. TIP #53: Addition of 'assert' Command

 

 
Powered by phpBB® Forum Software