Any nice way to get a temporary directory?

K

Kenneth McDonald

I'd like to be able to obtain a temporary directory in the same way I
can easily obtain a temporary file, but there doesn't seem to be a
function for this. Currently, I'm creating a temp file, getting it's
name, unlinking it, and then creating a dir with the same name. But
this is ugly, and in theory subject to a race condition. Is there a
better way of doing this?

Thanks,
Ken
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

Could you call Dir.mkdir with the result of Dir.tmpdir plus the name of the
directory that you want to create?

Regards,
Craig
 
A

ara.t.howard

I'd like to be able to obtain a temporary directory in the same way
I can easily obtain a temporary file, but there doesn't seem to be a
function for this. Currently, I'm creating a temp file, getting it's
name, unlinking it, and then creating a dir with the same name. But
this is ugly, and in theory subject to a race condition. Is there a
better way of doing this?

Thanks,
Ken



this keeps backwards compact, but also lets you do

Dir.tmpdir do

puts "in tmpdir #{ Dir.pwd }"

end


the tmpdir will be created for you and cleaned up too





class Dir
module Tmpdir
require 'tmpdir'
require 'socket'
require 'fileutils'

unless defined?(Super)
Super = Dir.send:)method, :tmpdir)
class << Dir
remove_method :tmpdir
end
end

class Error < ::StandardError; end

Hostname = Socket.gethostname || 'localhost'
Pid = Process.pid
Ppid = Process.ppid

def tmpdir *args, &block
options = Hash === args.last ? args.pop : {}

dirname = Super.call(*args)

return dirname unless block

turd = options['turd'] || options[:turd]

basename = [
Hostname,
Ppid,
Pid,
Thread.current.object_id.abs,
rand
].join('-')

pathname = File.join dirname, basename

made = false

42.times do
begin
FileUtils.mkdir_p pathname
break(made = true)
rescue Object
sleep rand
:retry
end
end

raise Error, "failed to make tmpdir in #{ dirname.inspect }"
unless made

begin
return Dir.chdir(pathname, &block)
ensure
unless turd
FileUtils.rm_rf(pathname) if made
end
end
end
end

extend Tmpdir
end




a @ http://codeforpeople.com/
 
N

Nobuyoshi Nakada

Hi,

At Fri, 24 Oct 2008 05:24:20 +0900,
Kenneth McDonald wrote in [ruby-talk:318458]:
I'd like to be able to obtain a temporary directory in the same way I
can easily obtain a temporary file, but there doesn't seem to be a
function for this. Currently, I'm creating a temp file, getting it's
name, unlinking it, and then creating a dir with the same name. But
this is ugly, and in theory subject to a race condition. Is there a
better way of doing this?

Dir.mktmpdir is available in lib/tmpdir.rb since 1.8.7.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top