Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use MRI logic for generating tempfile names in Tempfile.
This change does the following: * Adds missing Ruby logic by defining Tempfile.create and including Dir::Tmpname into Tempfile. These changes happen in the new lib/ruby/shared/tempfile.rb. * Invokes #create included from Tmpname to generate the filename, rather than using our own filename logic. This allows overriding both #create and #make_tempname. It also abstracts all name logic to Ruby's tmpdir.rb. * Eliminate unused Reaper logic. * Eliminate unused temp dir finding logic. * Fixes #677
- Loading branch information
Showing
6 changed files
with
122 additions
and
443 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# JRuby 1.7.x and lower implements most of tempfile as a JRuby ext. This | ||
# file simple loads that ext and then adds the additional functions that | ||
# control how tempfile name is generated. | ||
|
||
require 'delegate' # Unused; here for compatibility | ||
require 'tmpdir' | ||
require 'thread' # Unused; here for compatibility | ||
|
||
require 'tempfile.jar' | ||
|
||
class Tempfile | ||
include Dir::Tmpname | ||
end | ||
|
||
# Creates a temporally file as usual File object (not Tempfile). | ||
# It don't use finalizer and delegation. | ||
# | ||
# If no block is given, this is similar to Tempfile.new except | ||
# creating File instead of Tempfile. | ||
# The created file is not removed automatically. | ||
# You should use File.unlink to remove it. | ||
# | ||
# If a block is given, then a File object will be constructed, | ||
# and the block is invoked with the object as the argument. | ||
# The File object will be automatically closed and | ||
# the temporally file is removed after the block terminates. | ||
# The call returns the value of the block. | ||
# | ||
# In any case, all arguments (+*args+) will be treated as Tempfile.new. | ||
# | ||
# Tempfile.create('foo', '/home/temp') do |f| | ||
# ... do something with f ... | ||
# end | ||
# | ||
def Tempfile.create(basename, *rest) | ||
tmpfile = nil | ||
Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts| | ||
mode = File::RDWR|File::CREAT|File::EXCL | ||
perm = 0600 | ||
if opts | ||
mode |= opts.delete(:mode) || 0 | ||
opts[:perm] = perm | ||
perm = nil | ||
else | ||
opts = perm | ||
end | ||
tmpfile = File.open(tmpname, mode, opts) | ||
end | ||
if block_given? | ||
begin | ||
yield tmpfile | ||
ensure | ||
tmpfile.close if !tmpfile.closed? | ||
File.unlink tmpfile | ||
end | ||
else | ||
tmpfile | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.