* lib/tempfile.rb: define parameters appropriately and some

refactoring.

* lib/tmpdir.rb:   ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
glass 2014-09-20 17:35:06 +00:00
parent 997d5acdc7
commit 09e91be9ab
3 changed files with 24 additions and 41 deletions

View File

@ -1,3 +1,10 @@
Sat Sep 20 03:00:26 2014 Masaki Matsushita <glass.saga@gmail.com>
* lib/tempfile.rb: define parameters appropriately and some
refactoring.
* lib/tmpdir.rb: ditto.
Sat Sep 20 23:58:21 2014 Tanaka Akira <akr@fsij.org> Sat Sep 20 23:58:21 2014 Tanaka Akira <akr@fsij.org>
* enum.c (enum_chunk): Deprecate the state management. * enum.c (enum_chunk): Deprecate the state management.

View File

@ -78,8 +78,6 @@ require 'tmpdir'
# same Tempfile object from multiple threads then you should protect it with a # same Tempfile object from multiple threads then you should protect it with a
# mutex. # mutex.
class Tempfile < DelegateClass(File) class Tempfile < DelegateClass(File)
include Dir::Tmpname
# call-seq: # call-seq:
# new(basename, [tmpdir = Dir.tmpdir], [options]) # new(basename, [tmpdir = Dir.tmpdir], [options])
# #
@ -124,7 +122,7 @@ class Tempfile < DelegateClass(File)
# #
# If Tempfile.new cannot find a unique filename within a limited # If Tempfile.new cannot find a unique filename within a limited
# number of tries, then it will raise an exception. # number of tries, then it will raise an exception.
def initialize(basename, *rest) def initialize(basename, tmpdir=nil, mode: 0, **opts)
if block_given? if block_given?
warn "Tempfile.new doesn't call the given block." warn "Tempfile.new doesn't call the given block."
end end
@ -132,20 +130,13 @@ class Tempfile < DelegateClass(File)
@clean_proc = Remover.new(@data) @clean_proc = Remover.new(@data)
ObjectSpace.define_finalizer(self, @clean_proc) ObjectSpace.define_finalizer(self, @clean_proc)
::Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts| ::Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
mode = File::RDWR|File::CREAT|File::EXCL mode |= File::RDWR|File::CREAT|File::EXCL
perm = 0600 opts[:perm] = 0600
if opts
mode |= opts.delete(:mode) || 0
opts[:perm] = perm
perm = nil
else
opts = perm
end
@data[1] = @tmpfile = File.open(tmpname, mode, opts) @data[1] = @tmpfile = File.open(tmpname, mode, opts)
@data[0] = @tmpname = tmpname @data[0] = @tmpname = tmpname
@mode = mode & ~(File::CREAT|File::EXCL) @mode = mode & ~(File::CREAT|File::EXCL)
perm or opts.freeze opts.freeze
@opts = opts @opts = opts
end end
@ -278,7 +269,7 @@ class Tempfile < DelegateClass(File)
def call(*args) def call(*args)
return if @pid != $$ return if @pid != $$
path, tmpfile = *@data path, tmpfile = @data
STDERR.print "removing ", path, "..." if $DEBUG STDERR.print "removing ", path, "..." if $DEBUG
@ -356,18 +347,11 @@ end
# ... do something with f ... # ... do something with f ...
# end # end
# #
def Tempfile.create(basename, *rest) def Tempfile.create(basename, tmpdir=nil, mode: 0, **opts)
tmpfile = nil tmpfile = nil
Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts| Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
mode = File::RDWR|File::CREAT|File::EXCL mode |= File::RDWR|File::CREAT|File::EXCL
perm = 0600 opts[:perm] = 0600
if opts
mode |= opts.delete(:mode) || 0
opts[:perm] = perm
perm = nil
else
opts = perm
end
tmpfile = File.open(tmpname, mode, opts) tmpfile = File.open(tmpname, mode, opts)
end end
if block_given? if block_given?

View File

@ -17,12 +17,12 @@ class Dir
## ##
# Returns the operating system's temporary file path. # Returns the operating system's temporary file path.
def Dir::tmpdir def self.tmpdir
if $SAFE > 0 if $SAFE > 0
@@systmpdir @@systmpdir
else else
tmp = nil tmp = nil
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'] [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'].each do |dir|
next if !dir next if !dir
dir = File.expand_path(dir) dir = File.expand_path(dir)
if stat = File.stat(dir) and stat.directory? and stat.writable? and if stat = File.stat(dir) and stat.directory? and stat.writable? and
@ -31,7 +31,7 @@ class Dir
break break
end rescue nil end rescue nil
end end
raise ArgumentError, "could not find a temporary directory" if !tmp raise ArgumentError, "could not find a temporary directory" unless tmp
tmp tmp
end end
end end
@ -81,8 +81,8 @@ class Dir
# FileUtils.remove_entry dir # FileUtils.remove_entry dir
# end # end
# #
def Dir.mktmpdir(prefix_suffix=nil, *rest) def Dir.mktmpdir(prefix_suffix="d", *rest)
path = Tmpname.create(prefix_suffix || "d", *rest) {|n| mkdir(n, 0700)} path = Tmpname.create(prefix_suffix, *rest) {|n| mkdir(n, 0700)}
if block_given? if block_given?
begin begin
yield path yield path
@ -122,15 +122,7 @@ class Dir
path << suffix path << suffix
end end
def create(basename, *rest) def create(basename, tmpdir=nil, max_try: nil, **opts)
if opts = Hash.try_convert(rest[-1])
opts = opts.dup if rest.pop.equal?(opts)
max_try = opts.delete(:max_try)
opts = [opts]
else
opts = []
end
tmpdir, = *rest
if $SAFE > 0 and tmpdir.tainted? if $SAFE > 0 and tmpdir.tainted?
tmpdir = '/tmp' tmpdir = '/tmp'
else else
@ -139,7 +131,7 @@ class Dir
n = nil n = nil
begin begin
path = File.join(tmpdir, make_tmpname(basename, n)) path = File.join(tmpdir, make_tmpname(basename, n))
yield(path, n, *opts) yield(path, n, opts)
rescue Errno::EEXIST rescue Errno::EEXIST
n ||= 0 n ||= 0
n += 1 n += 1