diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-07-06 06:32:19 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-07-06 06:32:19 +0000 |
commit | 88f586b8ac7ccc3154753529b99048bf1abb2d9e (patch) | |
tree | 490af2a104a851442cd5157a8bb1371f60a7335e /lib | |
parent | f985b689ca330d73afbd5efa214aae28606b3635 (diff) |
* lib/tempfile.rb: merge from 1.7: do not delete a used tempfile
from children processes.
* lib/tempfile.rb: merge from 1.7: do not change the umask so as
not to affect other threads' file creation. specify a file mode
(0600) when creating a tempfile instead.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1573 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tempfile.rb | 68 |
1 files changed, 33 insertions, 35 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb index ab97f457a8..a0c7f4cf3f 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -4,7 +4,7 @@ # The class for temporary files. # o creates a temporary file, which name is "basename.pid.n" with mode "w+". # o Tempfile objects can be used like IO object. -# o with tmpfile.close(true) created temporary files are removed. +# o with tempfile.close(true) created temporary files are removed. # o created files are also removed on script termination. # o with Tempfile#open, you can reopen the temporary file. # o file mode of the temporary files are 0600. @@ -15,16 +15,19 @@ class Tempfile < SimpleDelegator Max_try = 10 def Tempfile.callback(path, data) + pid = $$ lambda{ - print "removing ", path, "..." if $DEBUG - data[0].close if data[0] - if File.exist?(path) - File.unlink(path) - end - if File.exist?(path + '.lock') - Dir.rmdir(path + '.lock') + if pid == $$ + print "removing ", path, "..." if $DEBUG + data[0].close if data[0] + if File.exist?(path) + File.unlink(path) + end + if File.exist?(path + '.lock') + Dir.rmdir(path + '.lock') + end + print "done\n" if $DEBUG end - print "done\n" if $DEBUG } end @@ -32,36 +35,31 @@ class Tempfile < SimpleDelegator if $SAFE > 0 and tmpdir.tainted? tmpdir = '/tmp' end - umask = File.umask(0177) - begin - n = 0 - while true - begin - tmpname = sprintf('%s/%s%d.%d', tmpdir, basename, $$, n) - lock = tmpname + '.lock' - unless File.exist?(tmpname) or File.exist?(lock) - Dir.mkdir(lock) - break - end - rescue - raise "cannot generate tmpfile `%s'" % tmpname if n >= Max_try - #sleep(1) + n = 0 + while true + begin + tmpname = sprintf('%s/%s%d.%d', tmpdir, basename, $$, n) + lock = tmpname + '.lock' + unless File.exist?(tmpname) or File.exist?(lock) + Dir.mkdir(lock) + break end - n += 1 + rescue + raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try + #sleep(1) end + n += 1 + end - @protect = [] - @clean_files = Tempfile.callback(tmpname, @protect) - ObjectSpace.define_finalizer(self, @clean_files) + @protect = [] + @clean_files = Tempfile.callback(tmpname, @protect) + ObjectSpace.define_finalizer(self, @clean_files) - @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL) - @protect[0] = @tmpfile - @tmpname = tmpname - super(@tmpfile) - Dir.rmdir(lock) - ensure - File.umask(umask) - end + @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) + @protect[0] = @tmpfile + @tmpname = tmpname + super(@tmpfile) + Dir.rmdir(lock) end def Tempfile.open(*args) |