Class Tempfile
In: lib/tempfile.rb
Parent: DelegateClass(File)

A class for managing temporary files. This library is written to be thread safe.

Methods

close   close!   delete   length   make_tmpname   new   open   open   path   size   unlink  

Constants

MAX_TRY = 10

Public Class methods

Creates a temporary file of mode 0600 in the temporary directory whose name is basename.pid.n and opens with mode "w+". A Tempfile object works just like a File object.

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)

[Source]

    # File lib/tempfile.rb, line 24
24:   def initialize(basename, tmpdir=Dir::tmpdir)
25:     if $SAFE > 0 and tmpdir.tainted?
26:       tmpdir = '/tmp'
27:     end
28: 
29:     lock = nil
30:     n = failure = 0
31:     
32:     begin
33:       Thread.critical = true
34: 
35:       begin
36:         tmpname = File.join(tmpdir, make_tmpname(basename, n))
37:         lock = tmpname + '.lock'
38:         n += 1
39:       end while @@cleanlist.include?(tmpname) or
40:         File.exist?(lock) or File.exist?(tmpname)
41: 
42:       Dir.mkdir(lock)
43:     rescue
44:       failure += 1
45:       retry if failure < MAX_TRY
46:       raise "cannot generate tempfile `%s'" % tmpname
47:     ensure
48:       Thread.critical = false
49:     end
50: 
51:     @data = [tmpname]
52:     @clean_proc = Tempfile.callback(@data)
53:     ObjectSpace.define_finalizer(self, @clean_proc)
54: 
55:     @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
56:     @tmpname = tmpname
57:     @@cleanlist << @tmpname
58:     @data[1] = @tmpfile
59:     @data[2] = @@cleanlist
60: 
61:     super(@tmpfile)
62: 
63:     # Now we have all the File/IO methods defined, you must not
64:     # carelessly put bare puts(), etc. after this.
65: 
66:     Dir.rmdir(lock)
67:   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.

[Source]

     # File lib/tempfile.rb, line 167
167:     def open(*args)
168:       tempfile = new(*args)
169: 
170:       if block_given?
171:         begin
172:           yield(tempfile)
173:         ensure
174:           tempfile.close
175:         end
176: 
177:         nil
178:       else
179:         tempfile
180:       end
181:     end

Public Instance methods

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.

[Source]

    # File lib/tempfile.rb, line 93
93:   def close(unlink_now=false)
94:     if unlink_now
95:       close!
96:     else
97:       _close
98:     end
99:   end

Closes and unlinks the file.

[Source]

     # File lib/tempfile.rb, line 102
102:   def close!
103:     _close
104:     @clean_proc.call
105:     ObjectSpace.undefine_finalizer(self)
106:   end
delete()

Alias for unlink

length()

Alias for size

Opens or reopens the file with mode "r+".

[Source]

    # File lib/tempfile.rb, line 75
75:   def open
76:     @tmpfile.close if @tmpfile
77:     @tmpfile = File.open(@tmpname, 'r+')
78:     @data[1] = @tmpfile
79:     __setobj__(@tmpfile)
80:   end

Returns the full path name of the temporary file.

[Source]

     # File lib/tempfile.rb, line 126
126:   def path
127:     @tmpname
128:   end

Returns the size of the temporary file. As a side effect, the IO buffer is flushed before determining the size.

[Source]

     # File lib/tempfile.rb, line 132
132:   def size
133:     if @tmpfile
134:       @tmpfile.flush
135:       @tmpfile.stat.size
136:     else
137:       0
138:     end
139:   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.

[Source]

     # File lib/tempfile.rb, line 112
112:   def unlink
113:     # keep this order for thread safeness
114:     begin
115:       File.unlink(@tmpname) if File.exist?(@tmpname)
116:       @@cleanlist.delete(@tmpname)
117:       @data = @tmpname = nil
118:       ObjectSpace.undefine_finalizer(self)
119:     rescue Errno::EACCES
120:       # may not be able to unlink on Windows; just ignore
121:     end
122:   end

Private Instance methods

[Source]

    # File lib/tempfile.rb, line 69
69:   def make_tmpname(basename, n)
70:     sprintf('%s.%d.%d', basename, $$, n)
71:   end

[Validate]