@@ -1433,7 +1433,35 @@ def rmtree
14331433 # end
14341434 #
14351435 def self . mktmpdir
1436- path = make_tmpdir ( resolve_tmpdir )
1436+ tmpdir = nil
1437+ [ 'TMPDIR' , 'TMP' , 'TEMP' ] . each do |name |
1438+ dir = ENV [ name ] rescue next
1439+ next if dir . nil? || dir . empty?
1440+ dir = File . expand_path ( dir )
1441+ stat = File . stat ( dir ) rescue next
1442+ next unless stat . directory?
1443+ next unless File . writable? ( dir )
1444+ next if stat . world_writable? && !stat . sticky?
1445+ tmpdir = dir
1446+ break
1447+ end
1448+ unless tmpdir
1449+ systmpdir = ( defined? ( Etc . systmpdir ) ? Etc . systmpdir : '/tmp' )
1450+ tmpdir = ( File . directory? ( systmpdir ) && File . writable? ( systmpdir ) ) ? systmpdir : '/tmp'
1451+ end
1452+
1453+ t = Time . now . strftime ( "%Y%m%d" )
1454+ n = nil
1455+ begin
1456+ name = "d#{ t } -#{ $$} -#{ Random . urandom ( 4 ) . unpack1 ( "L" ) . %( 36 **6 ) . to_s ( 36 ) } #{ n ? "-#{ n } " : '' } "
1457+ dir = File . join ( tmpdir , name )
1458+ Dir . mkdir ( dir , 0700 )
1459+ rescue Errno ::EEXIST
1460+ n = ( n || 0 ) + 1
1461+ retry
1462+ end
1463+
1464+ path = new ( dir )
14371465 if block_given?
14381466 begin
14391467 yield path
@@ -1458,38 +1486,6 @@ def remove_entry(path)
14581486 File . unlink ( path )
14591487 end
14601488 end
1461-
1462- def self . resolve_tmpdir
1463- [ 'TMPDIR' , 'TMP' , 'TEMP' ] . each do |name |
1464- dir = ENV [ name ] rescue next
1465- next if dir . nil? || dir . empty?
1466- dir = File . expand_path ( dir )
1467- stat = File . stat ( dir ) rescue next
1468- next unless stat . directory?
1469- next unless File . writable? ( dir )
1470- next if stat . world_writable? && !stat . sticky?
1471- return dir
1472- end
1473- systmpdir = ( defined? ( Etc . systmpdir ) ? Etc . systmpdir : '/tmp' )
1474- return systmpdir if File . directory? ( systmpdir ) && File . writable? ( systmpdir )
1475- '/tmp'
1476- end
1477- private_class_method :resolve_tmpdir
1478-
1479- def self . make_tmpdir ( tmpdir )
1480- t = Time . now . strftime ( "%Y%m%d" )
1481- n = nil
1482- begin
1483- name = "d#{ t } -#{ $$} -#{ Random . urandom ( 4 ) . unpack1 ( "L" ) . %( 36 **6 ) . to_s ( 36 ) } #{ n ? "-#{ n } " : '' } "
1484- path = File . join ( tmpdir , name )
1485- Dir . mkdir ( path , 0700 )
1486- rescue Errno ::EEXIST
1487- n = ( n || 0 ) + 1
1488- retry
1489- end
1490- new ( path )
1491- end
1492- private_class_method :make_tmpdir
14931489end
14941490
14951491class Pathname
0 commit comments