Lua os.tempname

1.3k views Asked by At

The file names returned by the Lua os.tempname function are weak in two respects

  1. Unlike most other languages it is not possible to define a custom prefix for the file name
  2. The _6char suffix that is generated can, with brute force but nevertheless, be guessed

I have at times used a, salted, MD5 of the current epoch time + a random number. About the only reason why that "feels" like a better solution is the length of the file name string.

Is there a way to generate better ( = longer?) file names?

1

There are 1 answers

0
dlask On BEST ANSWER
  1. The purpose of this Lua function is only to provide a unique name.
  2. There are not many options in the standard Lua library.

In other words, either you use what's available in Lua, or you write your own function. However, even if you use hashes, random numbers, and so on, you are still dealing with probability: Such file could exist there since the uniqueness is not guaranteed by the OS in this case.

By the way, consider use of io.tmpfile instead of os.tmpname.