Forum: Ruby tempfile.rb and unlink on windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca722d97bf250f7abcb87a05d63ac6c3?d=identicon&s=25 Nicholas Manning (modethree3)
on 2009-05-19 17:01
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/...   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?
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2009-05-19 18:32
(Received via mailing list)
> 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
Ca722d97bf250f7abcb87a05d63ac6c3?d=identicon&s=25 Nicholas Manning (modethree3)
on 2009-05-19 21:16
Daniel Berger 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
This topic is locked and can not be replied to.