tempfile.rb and unlink on windows

N

Nicholas Manning

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?
 
D

Daniel Berger

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]
Sent: Tuesday, May 19, 2009 9:02 AM
To: ruby-talk ML
Subject: tempfile.rb and unlink on windows

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?

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

http://redmine.ruby-lang.org

Regards,

Dan
 
N

Nicholas Manning

Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top