[go-nuts] Temporary files

460 views
Skip to first unread message

Michael Hoisie

unread,
May 5, 2010, 2:32:09 PM5/5/10
to golang-nuts
Does Go have a mechanism to create temporary files? That's the last
piece I need for the http multipart encoding. I remember reading that
someone was working on it.

If nobody has done it, I could create something simple and either 1)
make it a separate package or 2) keep it private in the http package.

- Mike

hotei

unread,
May 5, 2010, 11:03:46 PM5/5/10
to golang-nuts
Your os probably has a mechanism. mktemp is the *nix name. Try man 1
mktemp for the OS level version and man 3 mktemp for the C level
interface. I've also seen that it has a POSIX guideline and another
person says "don't ever use it cause the names it creates are too easy
to guess". If you need it quickly you'll probably be better off to
write your own and keep it private. The man pages will at least give
you an idea on how others have done it before.

Hotei

Chris Wedgwood

unread,
May 6, 2010, 12:25:06 AM5/6/10
to Michael Hoisie, golang-nuts
On Wed, May 05, 2010 at 11:32:09AM -0700, Michael Hoisie wrote:

> Does Go have a mechanism to create temporary files? That's the last
> piece I need for the http multipart encoding. I remember reading
> that someone was working on it.

i have something local here i can clean up and send your way

Marcin 'Qrczak' Kowalczyk

unread,
May 6, 2010, 2:16:12 PM5/6/10
to hotei, golang-nuts
2010/5/6 hotei <ravens...@cox.net>:
> Your os probably has a mechanism.  mktemp is the *nix name.  Try man 1
> mktemp for the OS level version and man 3 mktemp for the C level
> interface.  I've also seen that it has a POSIX guideline and another
> person says "don't ever use it cause the names it creates are too easy
> to guess".

Indeed; its interface has an inherent race condition. In glibc
mkstemp() is a correct alternative with no race (guessability of the
name does not matter for correctness).

--
Marcin Kowalczyk

peterGo

unread,
May 6, 2010, 4:15:42 PM5/6/10
to golang-nuts
Michael,

One design criterion for the mechanism to create temporary files
should be OS portability. Here's the view from Windows.

Creating and Using a Temporary File
http://msdn.microsoft.com/en-us/library/aa363875%28VS.85%29.aspx

Peter

On May 5, 2:32 pm, Michael Hoisie <hoi...@gmail.com> wrote:

Giles Lean

unread,
May 6, 2010, 8:42:12 PM5/6/10
to peterGo, golang-nuts

peterGo <go.pe...@gmail.com> wrote:

> Creating and Using a Temporary File
> http://msdn.microsoft.com/en-us/library/aa363875%28VS.85%29.aspx

An emulation of mkstemp() [ perhaps renamed ] would be my preference,
with the documentation promising that the new file has been created
and cannot be shared.

Having read the Microsoft documentation, it looks like their two
calls (one to get a path name, one to open it) are safe (security
wise) but not so safe functionality wise: someone may create the
file between the two calls. (My Windows programming skills are
rusty, for which I offer no apology.)

An atomic interface is much to be preferred over a two step
"get name, open file" type of deal.

Cheers,

Giles
Reply all
Reply to author
Forward
0 new messages