Tempfile.rb and unlink on windows


#1

PROBLEM

Was setting up a rails projects on a windows machine for and the project
required RubyInline. When running rake db:migrate for the first time
RubyInline creates a temp file to ensure it can have access to something
like /tmp/ruby_inline but causes an error:

rake aborted!
File exists - /tmp/ruby_inline3712-0

The problem here is that on windows, doing something like (in order to
emulate this problem)

ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’

Will not work on Windows (XP Pro).

COMMENTS

Whoever freakin wrote Tempfile#unlink had this little gem in the code:
rescue Errno::EACCES
# may not be able to unlink on Windows; just ignore
So if it can’t delete (unlink) the file it just silently fails - not
cool.

SOLUTION

After reading this post:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/2848 I
applied the following patch and it worked finally (thanks to Florian
Frank who wrote it).

— lib/tempfile.rb 23 Jul 2003 16:37:35 -0000 1.19
+++ lib/tempfile.rb 5 May 2004 23:33:57 -0000
@@ -106,7 +106,10 @@ class Tempfile < SimpleDelegator

file.

def unlink
# keep this order for thread safeness

  • File.unlink(@tmpname) if File.exist?(@tmpname)
  • if File.exist?(@tmpname)
  •  closed? or close
    
  •  File.unlink(@tmpname)
    
  • end
    @@cleanlist.delete(@tmpname) if @@cleanlist
    end
    alias delete unlink

Can anyone provide any insight on this?


#2

required RubyInline. When running rake db:migrate for the first time
ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’

file.

Can anyone provide any insight on this?

Your patch looks good to me. I would submit it on their redmine site:

http://redmine.ruby-lang.org

Regards,

Dan


#3

Daniel B. wrote:

required RubyInline. When running rake db:migrate for the first time
ruby -r tempfile -e ‘Tempfile.new(“foo”).unlink’

file.

Can anyone provide any insight on this?

Your patch looks good to me. I would submit it on their redmine site:

http://redmine.ruby-lang.org

Regards,

Dan

Yeah I will get around to it but it’s just f*cked up that the rescue
clause just silently fails and says in a comment “oh may fail on windows
but who cares” lol