$SAFE=2 and tempfile, tmpdir

artone$B$G$9!#e(B

Ruby 1.8.7-p22e$B$G!“e(B2e$BE@$”$j$^$9!#e(B
1e$B$D$O!"e(Btempfile.rbe$B$G!"e(B$SAFE >=
2e$B$N>l9g$Ke(BTempfile.newe$B$,e(BRuntimeErrore$B$G<:GTe(B
e$B$9$k$N$,0U?^E*$+$I$&$+$N3NG’$G$9!#e(B

C:\home\arton>ruby -rtempfile -e ‘$SAFE=2;Tempfile.new(“test”)’
c:/home/ruby/lib/ruby/1.8/tempfile.rb:52:in initialize': cannot generate tempfi le ’ (RuntimeError)
from -e:1:in `new’
from -e:1

e$B$b$70U?^E*$G$J$1$l$P!"e(B$SAFEe$B$,e(B2e$B$N>l9g$K$Oe([email protected]$7$FM_$7$$$G$9!#e(B
Filee$B%/%i%9$OM?$($i$l$?%U%!%$%kL>$,e(Btainte$B$G$J$1$l$P!"e(B$SAFEe$B$,e(B4e$B$h$j>.$5$1$le(B
e$B$P%U%!%$%k$N:n@.$r5v$7$F$$$^$9$,!"e(BTempfilee$B$N%U%!%$%kL>$O0BA4$J$O$:$J$N$G!“e(B
e$BF1MM$G$”$C$F$[$7$$$H;W$$$^$9!#6qBNE*$K$d$j$?$$$3$H$O!"e(Bopen-urie$B$r;H$C$?e(B
Webe$B%5%$%H$+$i$N%3%s%F%s%DFI$9~$$G$9!#e(B
e$B$J$*!“e(BRuntimeErrore$B$H$J$k860x$O!”%U%!%$%kL>@8@.$N$H$-$K8F$S=P$9e(Bmake_tmpname
e$B$G!"e(B$$e$B$rMxMQ$7$Fe(Bpide$B!Je(B$SAFE=2e$B$Ge(BSecurityErrore$B!K$r<hF@$7$h$&$H$7$F$$$k$?$a!"e(B
get_pide$B$,!"e(B$SAFE>=2e$B$Ge(BSecurityErrore$B$H$7$F$$$k$+$i$G$9!#$3$N$?$a%U%!%$%kL>e(B
e$B$,@8@.$5$l$F$$$J$$>uBV$G%U%!%$%k$r:n@.$7$h$&$H$7$F!"e(BRuntimeErrore$B$H$J$j$^e(B
e$B$9!#e(B

2e$BE@L$O!“e(BWindows
Vistae$B0J9_$Ne(BWindowse$B8GM-$N8=>]$G$9$,!“e(B$SAFE>0e$B$N>l9g$Ne(B
tmpdir.rbe$B$Ne(BDir::tmpdire$B$N5sF0$H$$$&$+@_Dj$,$”$^$j$&$^$/$”$j$^$;$s!#e(B
e$B6qBNE*$K$O!"e(B$SAFE>0e$B$N$?$a!"4D6-JQ?t$r;2>H$;$:$K0BA4$J%F%s%]%i%j%G%#%l%/e(B
e$B%H%j$rF@$h$&$H$$$&0U?^$@$H$O;W$$$^$9$,!"e(B@@systmpdire$B$rMxMQ$7$F$$$^$9!#e(B
e$B$G!"e(B@@systemdire$B$H$7$F!“e(BWin32
APIe$B$G<hF@$7$?e(BWindowse$B%G%#%l%/%H%jFb$Ne(BTempe$B%G%#e(B
e$B%l%/%H%j$r@_Dj$7$F$$$^$9$,!”$3$N%G%#%l%/%H%j$O!"e(BWindows
Vistae$B$G$O4IM}<T8"8B$N$e(B
e$B%"%/%;%92DG=$G!"0lHL$N%f!<%6!<$OFI$
=q$-$G$-$^$;$s!#e(B

C:\home\arton>ruby -rtmpdir -e ‘p Dir::tmpdir’
“C:/Users/arton/AppData/Local/Temp”

C:\home\arton>ruby -rtmpdir -e ‘$SAFE=1; p Dir::tmpdir’
“C:/Windows/Temp”

Windowse$BB&$N0U?^$H!"85$Ne(Btmpdire$B$N0U?^$r!“B:=E$9$k$J$i$P!”%f!<%6!<8GM-$N%Fe(B
e$B%s%]%i%j%G%#%l%/%H%j$r!“e(BWin32
APIe$B$G<hF@$9$k$N$,$”$k$Y$-;Q$G$O$J$$$G$7$ge(B
e$B$&$+!#e(B
e$B$H$$$&$o$1$G!“0J2<$Ke(Btmpdir.rbe$B$N%Q%C%A$rE:IU$7$^$9!#e(B
SHGetFolderPath
APIe$B$G8=:_$N%f!<%6!<$N@lMQ%G%#%l%/%H%j$rF@$k$h$&$K$7$F$$e(B
e$B$^$9!#e(BSHGetFolderPath
APIe$B$,<h$l$J$$>l9ge(B(e$B8E$$e(BWindows)e$B$K$D$$$F$O!”=>Mh$NF0e(B
e$B:n$H$J$k$h$&$K$7$F$$$^$9!#e(B

diff -u tmpdir.rb.org tmpdir.rb
tmpdir.rb.org Sun Jun 29 02:20:44 2008
+++ tmpdir.rb Sun Jun 29 02:47:54 2008
@@ -9,18 +9,25 @@
class Dir

@@systmpdir = ‘/tmp’

  • CSIDL_LOCAL_APPDATA = 0x001c
  • begin
    require ‘Win32API’
    max_pathlen = 260
    windir = ’ '*(max_pathlen+1)
    begin
  •  getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 
    

‘PL’, ‘L’)

  •  getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
    
  •  raise RuntimeError if getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, 
    

windir) != 0

  •  windir = File.expand_path(windir.rstrip)
    
    rescue RuntimeError
  •  getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 
    

‘L’)

  •  begin
    
  •    getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 
    

‘PL’, ‘L’)

  •  rescue RuntimeError
    
  •    getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 
    

‘L’)

  •  end
    
  •  len = getdir.call(windir, windir.size)
    
  •  windir = File.expand_path(windir[0, len])
    
    end
  • len = getdir.call(windir, windir.size)
  • windir = File.expand_path(windir[0, len])
    temp = File.join(windir, ‘temp’)
    @@systmpdir = temp if File.directory?(temp) and
    File.writable?(temp)
    rescue LoadError

e$B%Q%C%A8e$O!"e(B$SAFE=0e$B$N=>Mh$NCM$HF1$8$b$N$K$J$j$^$9$,!"e(BTMPe$B4D6-JQ?t$N@_Dj$Ke(B
e$B$O1F6A$5$l$J$$$3$H$G3NG’$G$-$^$9!#e(B
C:\home\arton\test>set TMP=c:\home\arton\test
C:\home\arton\test>ruby -rtmpdir -e ‘$SAFE=1;p Dir::tmpdir’
“C:/Users/arton/AppData/Local/Temp”

e$B$b$C$H$b!"e(BRuby-1.9.0-01e$B$@$H!"e(BTempfile.newe$B$be(BDir::tmpdire$B$b!"e(B$SAFE>0e$B$G$O!“e(B
e$B$$$:$l$be(BFile.expand_pathe$B$K0z$C$+$+$C$Fe(BSecurityErrore$B$K$J$j$^$9!#e(B
e$B$G$-$l$P!”$3$A$i$bF0$$$FM_$7$$$G$9!#e(B


arton [email protected]