Creating a thread-safe temporary file name
Asked Answered
B

4

37

When using Tempfile Ruby is creating a file with a thread-safe and inter-process-safe name. I only need a file name in that way.

I was wondering if there is a more straight forward approach way than:

t = Tempfile.new(['fleischwurst', '.png'])
temp_path = t.path
t.close
t.unlink
Barnum answered 9/12, 2012 at 13:21 Comment(0)
A
62

Dir::Tmpname.create

You could use Dir::Tmpname.create. It figures out what temporary directory to use (unless you pass it a directory). It's a little ugly to use given that it expects a block:

require 'tmpdir'
# => true
Dir::Tmpname.create(['prefix-', '.ext']) {}
# => "/tmp/prefix-20190827-1-87n9iu.ext"
Dir::Tmpname.create(['prefix-', '.ext'], '/my/custom/directory') {}
# => "/my/custom/directory/prefix-20190827-1-11x2u0h.ext"

The block is there for code to test if the file exists and raise an Errno::EEXIST so that a new name can be generated with incrementing value appended on the end.

The Rails Solution

The solution implemented by Ruby on Rails is short and similar to the solution originally implemented in Ruby:

require 'tmpdir'
# => true
File.join(Dir.tmpdir, "YOUR_PREFIX-#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}-YOUR_SUFFIX")
=> "/tmp/YOUR_PREFIX-20190827-1-wyouwg-YOUR_SUFFIX"
File.join(Dir.tmpdir, "YOUR_PREFIX-#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}-YOUR_SUFFIX")
=> "/tmp/YOUR_PREFIX-20190827-1-140far-YOUR_SUFFIX"

Dir::Tmpname.make_tmpname (Ruby 2.5.0 and earlier)

Dir::Tmpname.make_tmpname was removed in Ruby 2.5.0. Prior to Ruby 2.4.4 it could accept a directory path as a prefix, but as of Ruby 2.4.4, directory separators are removed.

Digging in tempfile.rb you'll notice that Tempfile includes Dir::Tmpname. Inside you'll find make_tmpname which does what you ask for.

require 'tmpdir'
# => true
File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname("prefix-", nil))
# => "/tmp/prefix-20190827-1-dfhvld"
File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(["prefix-", ".ext"], nil))
# => "/tmp/prefix-20190827-1-19zjck1.ext"
File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(["prefix-", ".ext"], "suffix"))
# => "/tmp/prefix-20190827-1-f5ipo7-suffix.ext"
Abram answered 9/12, 2012 at 13:35 Comment(8)
Thanks, that's it. Can be used with an array argument as well to preserve the file name extension: Dir::Tmpname.make_tmpname(['a', '.png'], nil)Barnum
@iltempo, you're welcome. I've added your example to the answer.Abram
This is great but nobody has mentioned that you need to require 'tmpdir' for this to work.Trapezium
Dir::Tmpname.make_tmpname was removed in Ruby 2.5Ultan
As of Ruby 2.4.4 this functionality no longer works, as make_tmpname doesn't take a directory anymore, just a filename prefix (directory separator characters are removed)Chauchaucer
is it just me, or is there no ruby docs for the built-in Dir::Tmpname.create? I can't find any!Killdeer
Tempfile.new already uses Dir::Tmpname.create under the hood, so I'm not sure how explicitly using Dir::Tmpname.create gains anything different. The Ruby docs suggest using a mutex to protect the tempfile from multiple threads. Also, @Killdeer here is the source code for that method github.com/ruby/ruby/blob/…Artificial
@chemturion Sometimes you need a temporary file name without the file having already been created. That's why I use this construct.Humpage
U
3

Since Dir::Tmpname.make_tmpname was removed in Ruby 2.5.0, this one falls back to using SecureRandom:

require "tmpdir"

def generate_temp_filename(ext=".png")
  filename = begin
    Dir::Tmpname.make_tmpname(["x", ext], nil)
  rescue NoMethodError
    require "securerandom"
    "#{SecureRandom.urlsafe_base64}#{ext}"
  end
  File.join(Dir.tmpdir, filename)
end
Ultan answered 27/5, 2019 at 18:2 Comment(0)
S
1

Since you only need the filename, what about using the SecureRandom for that:

require 'securerandom'

filename =  "#{SecureRandom.hex(6)}.png" #=> "0f04dd94addf.png"

You can also use SecureRandom.alphanumeric

Surd answered 16/7, 2018 at 23:0 Comment(0)
S
-2

I found the Dir:Tmpname solution did not work for me. When evaluating this:

Dir::Tmpname.make_tmpname "/tmp/blob", nil

Under MRI Ruby 1.9.3p194 I get:

uninitialized constant Dir::Tmpname (NameError)

Under JRuby 1.7.5 (1.9.3p393) I get:

NameError: uninitialized constant Dir::Tmpname

You might try something like this:

def temp_name(file_name='', ext='', dir=nil)
    id   = Thread.current.hash * Time.now.to_i % 2**32
    name = "%s%d.%s" % [file_name, id, ext]
    dir ? File.join(dir, name) : name
end
Silica answered 25/2, 2014 at 21:43 Comment(1)
Before you used Dir::Tempname id you require 'tempfile' require 'tempfile' Dir::Tmpname.make_tmpname "/tmp/blob", nil If you haven't loaded 'Tempfile' then you won't be able to use it's extensions to DirRoyden

© 2022 - 2024 — McMap. All rights reserved.