mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/tempfile.rb (Tempfile::make_tmpname): Allow to specify a
suffix for a temporary file name. * lib/tempfile.rb (Tempfile::make_tmpname): Make temporary file names less predictable by including a random string. [inspired by: akr] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@13631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1901ca6a17
commit
da7cf16496
2 changed files with 27 additions and 4 deletions
|
@ -13,9 +13,15 @@ class Tempfile < DelegateClass(File)
|
|||
MAX_TRY = 10
|
||||
@@cleanlist = []
|
||||
|
||||
# Creates a temporary file of mode 0600 in the temporary directory
|
||||
# whose name is basename.pid.n and opens with mode "w+". A Tempfile
|
||||
# object works just like a File object.
|
||||
# Creates a temporary file of mode 0600 in the temporary directory,
|
||||
# opens it with mode "w+", and returns a Tempfile object which
|
||||
# represents the created temporary file. A Tempfile object can be
|
||||
# treated just like a normal File object.
|
||||
#
|
||||
# The basename parameter is used to determine the name of a
|
||||
# temporary file. If an Array is given, the first element is used
|
||||
# as prefix string and the second as suffix string, respectively.
|
||||
# Otherwise it is treated as prefix string.
|
||||
#
|
||||
# If tmpdir is omitted, the temporary directory is determined by
|
||||
# Dir::tmpdir provided by 'tmpdir.rb'.
|
||||
|
@ -67,7 +73,15 @@ class Tempfile < DelegateClass(File)
|
|||
end
|
||||
|
||||
def make_tmpname(basename, n)
|
||||
sprintf('%s.%d.%d', basename, $$, n)
|
||||
case basename
|
||||
when Array
|
||||
prefix, suffix = *basename
|
||||
else
|
||||
prefix, suffix = basename, ''
|
||||
end
|
||||
|
||||
t = Time.now.strftime("%Y%m%d")
|
||||
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
|
||||
end
|
||||
private :make_tmpname
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue