Class | Tempfile |
In: |
lib/tempfile.rb
|
Parent: | DelegateClass(File) |
A class for managing temporary files. This library is written to be thread safe.
MAX_TRY | = | 10 |
Creates a temporary file of mode 0600 in the temporary directory, opens it with mode "w+", and returns a Tempfile object which represents the created temporary file. A Tempfile object can be treated just like a normal File object.
The basename parameter is used to determine the name of a temporary file. If an Array is given, the first element is used as prefix string and the second as suffix string, respectively. Otherwise it is treated as prefix string.
If tmpdir is omitted, the temporary directory is determined by Dir::tmpdir provided by ‘tmpdir.rb’. When $SAFE > 0 and the given tmpdir is tainted, it uses /tmp. (Note that ENV values are tainted by default)
# File lib/tempfile.rb, line 30 30: def initialize(basename, tmpdir=Dir::tmpdir) 31: if $SAFE > 0 and tmpdir.tainted? 32: tmpdir = '/tmp' 33: end 34: 35: lock = nil 36: n = failure = 0 37: 38: begin 39: Thread.critical = true 40: 41: begin 42: tmpname = File.join(tmpdir, make_tmpname(basename, n)) 43: lock = tmpname + '.lock' 44: n += 1 45: end while @@cleanlist.include?(tmpname) or 46: File.exist?(lock) or File.exist?(tmpname) 47: 48: Dir.mkdir(lock) 49: rescue 50: failure += 1 51: retry if failure < MAX_TRY 52: raise "cannot generate tempfile `%s'" % tmpname 53: ensure 54: Thread.critical = false 55: end 56: 57: @data = [tmpname] 58: @clean_proc = Tempfile.callback(@data) 59: ObjectSpace.define_finalizer(self, @clean_proc) 60: 61: @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) 62: @tmpname = tmpname 63: @@cleanlist << @tmpname 64: @data[1] = @tmpfile 65: @data[2] = @@cleanlist 66: 67: super(@tmpfile) 68: 69: # Now we have all the File/IO methods defined, you must not 70: # carelessly put bare puts(), etc. after this. 71: 72: Dir.rmdir(lock) 73: end
If no block is given, this is a synonym for new().
If a block is given, it will be passed tempfile as an argument, and the tempfile will automatically be closed when the block terminates. In this case, open() returns nil.
# File lib/tempfile.rb, line 183 183: def open(*args) 184: tempfile = new(*args) 185: 186: if block_given? 187: begin 188: yield(tempfile) 189: ensure 190: tempfile.close 191: end 192: 193: nil 194: else 195: tempfile 196: end 197: end
Closes the file. If the optional flag is true, unlinks the file after closing.
If you don‘t explicitly unlink the temporary file, the removal will be delayed until the object is finalized.
# File lib/tempfile.rb, line 108 108: def close(unlink_now=false) 109: if unlink_now 110: close! 111: else 112: _close 113: end 114: end
Closes and unlinks the file.
# File lib/tempfile.rb, line 117 117: def close! 118: _close 119: @clean_proc.call 120: ObjectSpace.undefine_finalizer(self) 121: @data = @tmpname = nil 122: end
Opens or reopens the file with mode "r+".
# File lib/tempfile.rb, line 89 89: def open 90: @tmpfile.close if @tmpfile 91: @tmpfile = File.open(@tmpname, 'r+') 92: @data[1] = @tmpfile 93: __setobj__(@tmpfile) 94: end
Unlinks the file. On UNIX-like systems, it is often a good idea to unlink a temporary file immediately after creating and opening it, because it leaves other programs zero chance to access the file.
# File lib/tempfile.rb, line 128 128: def unlink 129: # keep this order for thread safeness 130: begin 131: File.unlink(@tmpname) if File.exist?(@tmpname) 132: @@cleanlist.delete(@tmpname) 133: @data = @tmpname = nil 134: ObjectSpace.undefine_finalizer(self) 135: rescue Errno::EACCES 136: # may not be able to unlink on Windows; just ignore 137: end 138: end
# File lib/tempfile.rb, line 75 75: def make_tmpname(basename, n) 76: case basename 77: when Array 78: prefix, suffix = *basename 79: else 80: prefix, suffix = basename, '' 81: end 82: 83: t = Time.now.strftime("%Y%m%d") 84: path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}" 85: end