~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/tempfile.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
# tempfile - manipulates temporary files
3
3
#
4
 
# $Id: tempfile.rb 31768 2011-05-28 23:31:24Z yugui $
 
4
# $Id: tempfile.rb 33089 2011-08-26 23:54:49Z drbrain $
5
5
#
6
6
 
7
7
require 'delegate'
39
39
# that's it's unnecessary to explicitly delete a Tempfile after use, though
40
40
# it's good practice to do so: not explicitly deleting unused Tempfiles can
41
41
# potentially leave behind large amounts of tempfiles on the filesystem
42
 
# until they're garbage collected. The existance of these temp files can make
 
42
# until they're garbage collected. The existence of these temp files can make
43
43
# it harder to determine a new Tempfile filename.
44
44
#
45
45
# Therefore, one should always call #unlink or close in an ensure block, like
132
132
    ObjectSpace.define_finalizer(self, @clean_proc)
133
133
 
134
134
    create(basename, *rest) do |tmpname, n, opts|
135
 
      lock = tmpname + '.lock'
136
135
      mode = File::RDWR|File::CREAT|File::EXCL
137
136
      perm = 0600
138
137
      if opts
142
141
      else
143
142
        opts = perm
144
143
      end
145
 
      self.class.mkdir(lock)
146
 
      begin
 
144
      self.class.locking(tmpname) do
147
145
        @data[1] = @tmpfile = File.open(tmpname, mode, opts)
148
146
        @data[0] = @tmpname = tmpname
149
 
      ensure
150
 
        self.class.rmdir(lock)
151
147
      end
152
148
      @mode = mode & ~(File::CREAT|File::EXCL)
153
149
      perm or opts.freeze
165
161
    __setobj__(@tmpfile)
166
162
  end
167
163
 
168
 
  def _close    # :nodoc:
 
164
  def _close    # :nodoc:
169
165
    @tmpfile.close if @tmpfile
170
166
    @tmpfile = nil
171
167
    @data[1] = nil if @data
316
312
      tempfile = new(*args)
317
313
 
318
314
      if block_given?
319
 
        begin
320
 
          yield(tempfile)
321
 
        ensure
322
 
          tempfile.close
323
 
        end
 
315
        begin
 
316
          yield(tempfile)
 
317
        ensure
 
318
          tempfile.close
 
319
        end
324
320
      else
325
 
        tempfile
 
321
        tempfile
326
322
      end
327
323
    end
328
324
 
 
325
    # :stopdoc:
 
326
 
 
327
    # yields with locking for +tmpname+ and returns the result of the
 
328
    # block.
 
329
    def locking(tmpname)
 
330
      lock = tmpname + '.lock'
 
331
      mkdir(lock)
 
332
      yield
 
333
    ensure
 
334
      rmdir(lock) if lock
 
335
    end
 
336
 
329
337
    def mkdir(*args)
330
338
      Dir.mkdir(*args)
331
339
    end
 
340
 
332
341
    def rmdir(*args)
333
342
      Dir.rmdir(*args)
334
343
    end