Create Temporary file with EXTENSION

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Create Temporary file with EXTENSION

Sravan Kumar Reddy Javaji
Hello Everyone,

As per the requirement, I want to create a temporary file with extension.

I know that os.tmpname create a temporary file, but there is no way to provide extension to it.

Could someone please let me know how to provide extension to the temporary file name.

Thanks for your help.

-
Regards,
Sravan
Reply | Threaded
Open this post in threaded view
|

Re: Create Temporary file with EXTENSION

彭 书呆
在 2015/4/22 10:01, Sravan Kumar Reddy Javaji 写道:

> Hello Everyone,
>
> As per the requirement, I want to create a temporary file with extension.
>
> I know that os.tmpname create a temporary file, but there is no way to provide extension to it.
>
> Could someone please let me know how to provide extension to the temporary file name.
>
> Thanks for your help.
>
> -
> Regards,
> Sravan

when I use tmpname, I'm expecting a filename which is guaranteed to be unique and that I have
proper privilege to use that file. but I don't know any means to provide some kind of `template'
so that tmpname would give a filename in special format. One can concatenate the name returned by
tmpname with a special suffix, but in theory he lose the guarantees provided by tmpname.

if I want create temporary files with special names, I would usually simply do:

```
local dirname = os.tmpname()
lfs.mkdir(dirname)
local myfile = io.open(dirname .. "/file-with-special-name.specialsuffix", "wb")
--[[ blahblah ]]
```

NOTE: on a windows platform, use ``os.getenv("TMP") .. os.tmpname()`` instead.

--
the nerdy Peng / 书呆彭 / Sent from Thunderbird



Reply | Threaded
Open this post in threaded view
|

Re: Create Temporary file with EXTENSION

steve donovan
On Wed, Apr 22, 2015 at 6:03 AM, 书呆彭 <[hidden email]> wrote:
> NOTE: on a windows platform, use ``os.getenv("TMP") .. os.tmpname()``
> instead.

This is an important gotcha!

Reply | Threaded
Open this post in threaded view
|

Re: Create Temporary file with EXTENSION

Sravan Kumar Reddy Javaji
In reply to this post by 彭 书呆
Thanks for your reply.

Actually, os.tmpname() creates a file with that name. So, in that case if I try to create the directory i.e., os.tmpname() as the file already exists with the same name of file, I think ubuntu mkdir command works.

Probably after calling os.tmpname(), we may have to call os.remove to delete the created file and we may have to create the directory. But is it really a reliable solution?

-
Regards,
Sravan


On Tue, Apr 21, 2015 at 9:03 PM, 书呆彭 <[hidden email]> wrote:
在 2015/4/22 10:01, Sravan Kumar Reddy Javaji 写道:
Hello Everyone,

As per the requirement, I want to create a temporary file with extension.

I know that os.tmpname create a temporary file, but there is no way to provide extension to it.

Could someone please let me know how to provide extension to the temporary file name.

Thanks for your help.

-
Regards,
Sravan

when I use tmpname, I'm expecting a filename which is guaranteed to be unique and that I have
proper privilege to use that file. but I don't know any means to provide some kind of `template'
so that tmpname would give a filename in special format. One can concatenate the name returned by
tmpname with a special suffix, but in theory he lose the guarantees provided by tmpname.

if I want create temporary files with special names, I would usually simply do:

```
local dirname = os.tmpname()
lfs.mkdir(dirname)
local myfile = io.open(dirname .. "/file-with-special-name.specialsuffix", "wb")
--[[ blahblah ]]
```

NOTE: on a windows platform, use ``os.getenv("TMP") .. os.tmpname()`` instead.

--
the nerdy Peng / 书呆彭 / Sent from Thunderbird




Reply | Threaded
Open this post in threaded view
|

Re: Create Temporary file with EXTENSION

Sravan Kumar Reddy Javaji
In reply to this post by steve donovan
Yes, I will note down that :)

Thanks.. Could you please check my previous email and resolve my new query regarding the tempname()

Appreciate your help!

On Tue, Apr 21, 2015 at 10:58 PM, steve donovan <[hidden email]> wrote:
On Wed, Apr 22, 2015 at 6:03 AM, 书呆彭 <[hidden email]> wrote:
> NOTE: on a windows platform, use ``os.getenv("TMP") .. os.tmpname()``
> instead.

This is an important gotcha!


Reply | Threaded
Open this post in threaded view
|

Re: Create Temporary file with EXTENSION

Tom N Harris
In reply to this post by 彭 书呆
On Wednesday, April 22, 2015 12:03:44 PM 书呆彭 wrote:
> NOTE: on a windows platform, use ``os.getenv("TMP") .. os.tmpname()``
> instead.

Don't you mean ``os.getenv("TEMP)``? [1]

If you are working in a modern BSD-like environment, see if mkstemps [2] is
available. This is the only temporary name function I know of that allows you
to control the suffix. mkstemp is exposed by Luaposix [3] but it requires the
template to end with "XXXXXX". Similarly, Windows has GetTempFileName [4] but
always adds ".TMP".

If you must name temporary files a particular way, you'll have to roll your own
generator using a sufficiently unpredictable source of names (UUIDs are good
enough, usually) and the POSIX exclusive create flags for security.

[1] http://blogs.msdn.com/b/oldnewthing/archive/2015/04/17/10608077.aspx
[2] http://man.cx/mkstemps
[3] http://luaposix.github.io/luaposix/modules/posix.stdlib.html#mkstemp
[4] https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).aspx

--
tom <[hidden email]>