Module: FakeFS::Dir::Tmpname
- Defined in:
- lib/fakefs/dir.rb
Overview
Tmpname module
Class Method Summary collapse
Class Method Details
.create(basename, *rest) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/fakefs/dir.rb', line 173 def create(basename, *rest) 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 && tmpdir.tainted? tmpdir = '/tmp' else tmpdir ||= self.tmpdir end n = nil begin path = File.join(tmpdir, make_tmpname(basename, n)) yield(path, n, *opts) rescue Errno::EEXIST n ||= 0 n += 1 retry if !max_try || n < max_try raise "cannot generate temporary name using `#{basename}' " \ "under `#{tmpdir}'" end path end |
.make_tmpname(prefix_suffix, n) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/fakefs/dir.rb', line 155 def make_tmpname(prefix_suffix, n) case prefix_suffix when String prefix = prefix_suffix suffix = '' when Array prefix = prefix_suffix[0] suffix = prefix_suffix[1] else fail ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" end t = Time.now.strftime('%Y%m%d') path = "#{prefix}#{t}-#{$PID}-#{rand(0x100000000).to_s(36)}" path << "-#{n}" if n path << suffix end |