Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.1.6
    • Fix Version/s: None
    • Component/s: Core Classes/Modules
    • Labels:
      None
    • Environment:
      JRuby 1.1.6, OS X, Java 1.5
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      Somehow, someway, the File.chmod method is failing when interacting with Dir.chdir and Test::Unit This sample program demonstrates the problem:

      # test.rb
      #
      # Create this file as 'test/test.rb'
      # Run from the parent directory, e.g. jruby test/test.rb
      #
      require 'test/unit'
      
      class TC_Writable < Test::Unit::TestCase
         def setup
            Dir.chdir('test') # Comment this out and it works
            @file = 'foo.doc'
            File.open(@file, 'w'){}
         end
      
         def test_filetest
            File.chmod(0644, @file)
            assert_equal(true, File.writable?(@file))
      
            File.chmod(0444, @file)
            assert_equal(false, File.writable?(@file))
         end
      
         def teardown
            File.delete(@file) if File.exists?(@file)
            @file = nil
         end
      end
      

      When I run standalone programs, however, it works fine. This leads me to believe there's some sort of interaction going on with Test::Unit, but I couldn' tell you what.

      Regards,

      Dan

        Activity

        Hide
        Daniel Berger added a comment -

        BTW, here's my output:

        $ jruby test/test.rb 
        Loaded suite test/test
        Started
        F
        Finished in 0.113602 seconds.
        
          1) Failure:
        test_filetest(TC_Writable) [test/test.rb:21]:
        <false> expected but was
        <true>.
        
        1 tests, 2 assertions, 1 failures, 0 errors
        
        Show
        Daniel Berger added a comment - BTW, here's my output: $ jruby test/test.rb Loaded suite test/test Started F Finished in 0.113602 seconds. 1) Failure: test_filetest(TC_Writable) [test/test.rb:21]: <false> expected but was <true>. 1 tests, 2 assertions, 1 failures, 0 errors
        Hide
        Charles Oliver Nutter added a comment -

        I think this is related to a known issue with chdir: we can't do it safely, so we fake it. For chdir, POSIX allows only a per-process cwd. So whenever you change it, all threads in a given process see that change. If other threads depend on cwd not changing, they break in nasty ways. In JRuby's case, we can't actually call the POSIX chdir because it would change it for the entire JVM. That would at least not be polite and at most crash other applications running in the same JVM. So we fake it, and try to juggle paths whenever we make calls that are based on cwd. In this case, we're probably not juggling the path enough so that a native chmod call can succeed.

        The best workaround is not to use chdir at all. Slightly less great would be to ensure you're using full paths, which we may chdir correctly. I agree that it's a rather cumbersome state of affairs, but without chdir being per-thread at the POSIX level no implementation can support it completely and reliably. Don't use it.

        Show
        Charles Oliver Nutter added a comment - I think this is related to a known issue with chdir: we can't do it safely, so we fake it. For chdir, POSIX allows only a per-process cwd. So whenever you change it, all threads in a given process see that change. If other threads depend on cwd not changing, they break in nasty ways. In JRuby's case, we can't actually call the POSIX chdir because it would change it for the entire JVM. That would at least not be polite and at most crash other applications running in the same JVM. So we fake it, and try to juggle paths whenever we make calls that are based on cwd. In this case, we're probably not juggling the path enough so that a native chmod call can succeed. The best workaround is not to use chdir at all. Slightly less great would be to ensure you're using full paths, which we may chdir correctly. I agree that it's a rather cumbersome state of affairs, but without chdir being per-thread at the POSIX level no implementation can support it completely and reliably. Don't use it.
        Hide
        Thomas E Enebo added a comment -

        In this case couldn't we check to see what the faked out chdir is and use that for our FileTest methods? It is true we cannot fake chdir all the time, but these should be ok to do. Actually all File/Dir-based methods should be doing this? We probably need to do same resolution as JRubyFile (or perhaps even just use it before calling posix function).

        Show
        Thomas E Enebo added a comment - In this case couldn't we check to see what the faked out chdir is and use that for our FileTest methods? It is true we cannot fake chdir all the time, but these should be ok to do. Actually all File/Dir-based methods should be doing this? We probably need to do same resolution as JRubyFile (or perhaps even just use it before calling posix function).
        Hide
        Charles Oliver Nutter added a comment -

        We probably could, but I think we're fighting a losing battle trying to propagate chdir across all calls. No reason not to fix obvious places, of course.

        Show
        Charles Oliver Nutter added a comment - We probably could, but I think we're fighting a losing battle trying to propagate chdir across all calls. No reason not to fix obvious places, of course.
        Hide
        Logan Barnett added a comment -

        I was able to workaround this issue myself by calling File.expand_path on the path I was passing into chmod (I think Charlie said this same/similar thing to some effect).

        Show
        Logan Barnett added a comment - I was able to workaround this issue myself by calling File.expand_path on the path I was passing into chmod (I think Charlie said this same/similar thing to some effect).
        Hide
        Charles Oliver Nutter added a comment -

        Working on master as of now. Not sure when it was fixed, but it's fixed.

        Show
        Charles Oliver Nutter added a comment - Working on master as of now. Not sure when it was fixed, but it's fixed.

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Daniel Berger
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: