diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-09-11 08:28:29 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-09-11 08:28:29 +0000 |
commit | 55717aa69b82231d4fcacf2197a63e00348b3b41 (patch) | |
tree | c440c415702767c1d119fa8ed4895ab335225260 | |
parent | d2a68669da4b31f70d99888ef87286c6cc901afb (diff) | |
download | ruby-55717aa69b82231d4fcacf2197a63e00348b3b41.tar.gz |
* 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/trunk@13428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | lib/tempfile.rb | 22 |
2 files changed, 27 insertions, 4 deletions
@@ -1,3 +1,12 @@ +Tue Sep 11 17:28:00 2007 Akinori MUSHA <knu@iDaemons.org> + + * 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] + Tue Sep 11 17:25:59 2007 Akinori MUSHA <knu@iDaemons.org> * lib/shellwords.rb: Add shellescape() and shelljoin(). diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 6a2d560b54..5eb294e390 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -15,9 +15,15 @@ class Tempfile < DelegateClass(File) @@cleanlist = [] @@lock = Mutex.new - # 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'. @@ -65,7 +71,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 |