Skip to content

Instantly share code, notes, and snippets.

@headius
Created January 26, 2022 22:02
Show Gist options
  • Save headius/f972fdb91565828c76e78dcf4ae7f5be to your computer and use it in GitHub Desktop.
Save headius/f972fdb91565828c76e78dcf4ae7f5be to your computer and use it in GitHub Desktop.
--- lib/tmpdir.rb 2022-01-26 15:27:27.000000000 -0600
+++ ../jruby/lib/ruby/stdlib/tmpdir.rb 2021-10-27 14:50:07.000000000 -0500
@@ -1,20 +1,20 @@
# frozen_string_literal: true
#
# tmpdir - retrieve temporary directory path
#
# $Id$
#
require 'fileutils'
begin
- require 'etc.so'
+ require 'etc'
rescue LoadError # rescue LoadError for miniruby
end
class Dir
@@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp'
##
# Returns the operating system's temporary file path.
@@ -62,41 +62,41 @@
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
#
# If a block is given,
# it is yielded with the path of the directory.
# The directory and its contents are removed
# using FileUtils.remove_entry before Dir.mktmpdir returns.
# The value of the block is returned.
#
# Dir.mktmpdir {|dir|
# # use the directory...
- # open("#{dir}/foo", "w") { something using the file }
+ # open("#{dir}/foo", "w") { ... }
# }
#
# If a block is not given,
# The path of the directory is returned.
# In this case, Dir.mktmpdir doesn't remove the directory.
#
# dir = Dir.mktmpdir
# begin
# # use the directory...
- # open("#{dir}/foo", "w") { something using the file }
+ # open("#{dir}/foo", "w") { ... }
# ensure
# # remove the directory.
# FileUtils.remove_entry dir
# end
#
def self.mktmpdir(prefix_suffix=nil, *rest, **options)
base = nil
- path = Tmpname.create(prefix_suffix || "d", *rest, **options) {|path, _, _, d|
+ path = Tmpname.create(prefix_suffix || "d", *rest, **options) {|_path, _, _, d|
base = d
- mkdir(path, 0700)
+ mkdir(_path, 0700)
}
if block_given?
begin
yield path.dup
ensure
unless base
stat = File.stat(File.dirname(path))
if stat.world_writable? and !stat.sticky?
raise ArgumentError, "parent directory is world writable but not sticky"
end
@@ -133,21 +133,24 @@
prefix = (String.try_convert(prefix) or
raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
prefix = prefix.delete(UNUSABLE_CHARS)
suffix &&= (String.try_convert(suffix) or
raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
suffix &&= suffix.delete(UNUSABLE_CHARS)
begin
t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\
"#{n ? %[-#{n}] : ''}#{suffix||''}"
- path = File.join(tmpdir, path)
+ # We use the second form here because chdir + ./ files won't open right (http://bugs.jruby.org/3698)
+ # path = File.join(tmpdir, path)
+ path = File.expand_path(path, tmpdir)
+
yield(path, n, opts, origdir)
rescue Errno::EEXIST
n ||= 0
n += 1
retry if !max_try or n < max_try
raise "cannot generate temporary name using `#{basename}' under `#{tmpdir}'"
end
path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment