Skip to content

Commit

Permalink
Use MRI logic for generating tempfile names in Tempfile.
Browse files Browse the repository at this point in the history
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
headius committed Apr 29, 2013
1 parent f6a9649 commit 607c6d5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 443 deletions.
146 changes: 0 additions & 146 deletions lib/ruby/1.8/tmpdir.rb

This file was deleted.

59 changes: 59 additions & 0 deletions lib/ruby/shared/tempfile.rb
@@ -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
7 changes: 4 additions & 3 deletions lib/ruby/1.9/tmpdir.rb → lib/ruby/shared/tmpdir.rb
Expand Up @@ -11,6 +11,7 @@
end

class Dir
@@using_19 = Hash.respond_to? :try_convert

@@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp'

Expand All @@ -28,13 +29,13 @@ def Dir::tmpdir
# Opening directory is not allowed in Java.
dirs = [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', tmp]
for dir in dirs
if dir and stat = File.stat(dir) and stat.directory? and stat.writable? and !stat.world_writable?
if dir and stat = File.stat(dir) and stat.directory? and stat.writable? and !(@@using_19 && stat.world_writable?)
return File.expand_path(dir)
end
end

# Some OS sets the environment variables to '/tmp', which we may reject.
warn "Unable to find a non world-writable directory for #{__method__}. Consider setting ENV['TMPDIR'], ENV['TMP'] or ENV['TEMP'] to a non world-writable directory."
warn "Unable to find a non world-writable directory for Dir::tmpdir. Consider setting ENV['TMPDIR'], ENV['TMP'] or ENV['TEMP'] to a non world-writable directory."

for dir in dirs
if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
Expand Down Expand Up @@ -128,7 +129,7 @@ def make_tmpname(prefix_suffix, n)
end

def create(basename, *rest)
if opts = Hash.try_convert(rest[-1])
if Hash.respond_to?(:try_convert) && opts = Hash.try_convert(rest[-1])
opts = opts.dup if rest.pop.equal?(opts)
max_try = opts.delete(:max_try)
opts = [opts]
Expand Down
2 changes: 1 addition & 1 deletion src/org/jruby/Ruby.java
Expand Up @@ -1617,7 +1617,7 @@ private void initBuiltins() {
addLazyBuiltin("rbconfig.rb", "rbconfig", "org.jruby.ext.rbconfig.RbConfigLibrary");
addLazyBuiltin("jruby/serialization.rb", "serialization", "org.jruby.ext.jruby.JRubySerializationLibrary");
addLazyBuiltin("ffi-internal.jar", "ffi-internal", "org.jruby.ext.ffi.FFIService");
addLazyBuiltin("tempfile.rb", "tempfile", "org.jruby.ext.tempfile.TempfileLibrary");
addLazyBuiltin("tempfile.jar", "tempfile", "org.jruby.ext.tempfile.TempfileLibrary");
addLazyBuiltin("fcntl.rb", "fcntl", "org.jruby.ext.fcntl.FcntlLibrary");
addLazyBuiltin("rubinius.jar", "rubinius", "org.jruby.ext.rubinius.RubiniusLibrary");
addLazyBuiltin("yecht.jar", "yecht", "YechtService");
Expand Down
67 changes: 0 additions & 67 deletions src/org/jruby/RubyTempfile.java

This file was deleted.

0 comments on commit 607c6d5

Please sign in to comment.