tempfile.rb and unlink on windows

Discussion in 'Ruby' started by Nicholas Manning, May 19, 2009.

  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?
    --
    Posted via http://www.ruby-forum.com/.
    Nicholas Manning, May 19, 2009
    #1
    1. Advertising

  2. > -----Original Message-----
    > From: [mailto:]
    > 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
    Daniel Berger, May 19, 2009
    #2
    1. Advertising

  3. 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
    --
    Posted via http://www.ruby-forum.com/.
    Nicholas Manning, May 19, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Coffee Pot

    How can I unlink/delete an open file in Windows?

    Coffee Pot, Oct 18, 2008, in forum: C Programming
    Replies:
    12
    Views:
    697
    CBFalconer
    Oct 20, 2008
  2. Thomas Jollans

    Re: os.unlink on Windows

    Thomas Jollans, Aug 7, 2010, in forum: Python
    Replies:
    5
    Views:
    825
    Lawrence D'Oliveiro
    Aug 9, 2010
  3. Replies:
    4
    Views:
    210
    Paul Battley
    Jul 6, 2007
  4. Replies:
    12
    Views:
    486
  5. Alex van der Spek

    Difference between tempfile and spooled tempfile?

    Alex van der Spek, Apr 5, 2012, in forum: Python
    Replies:
    2
    Views:
    350
    Steve Howell
    Apr 5, 2012
Loading...

Share This Page