Problem with creating/deleting file in same process (Windows)

Discussion in 'Ruby' started by Mike Grier, Dec 18, 2004.

  1. Mike Grier

    Mike Grier Guest

    I'm new to ruby so perhaps I'm simply doing this wrong, but...

    Has anyone else encountered a problem with attempting to create
    and then delete a file in the same process when using ruby on
    Windows? Below is a test case that illustrates the problem; the
    test_delete_file fails with a permission denied error.

    My ruby version is 'ruby 1.8.2 (2004-07-29) [i386-mswin32]',
    though I've also seen the problem with the cygwin version.

    I'm assuming this test would pass on linux/unix (I unfortunately
    don't have access to one that has ruby loaded). It appears that
    windows is locking the file until the test process ends.
    However, if I create the file in a child process using a system
    call I can delete it in the parent process.

    For my immediate needs I can create the files in a child process,
    perhaps overridng to do it, but does anyone have
    suggestions as to the best way to handle this? Is this a bug in
    the windows version of ruby (i.e. could it be implemented to
    allow the delete in the same process)?

    Thanks in advance,


    #!/usr/bin/env ruby

    require 'fileutils'
    require 'test/unit'
    require 'tmpdir'

    include FileUtils::Verbose

    class TestFileDelete < Test::Unit::TestCase

    TMP_DIR = Dir.tmpdir
    # TMP_DIR = "C:/tmp"

    TEST_DIR = TMP_DIR + "/tdir"
    TEST_FILE_1 = TMP_DIR + "/tfile1"
    TEST_FILE_2 = TMP_DIR + "/tfile2"

    # Clean up the files from the previous run, do this here rather
    # than setup so it only happens once.

    FileUtils.rm_rf(TEST_DIR) if File.exists?(TEST_DIR)
    FileUtils.rm_rf(TEST_FILE_1) if File.exists?(TEST_FILE_1)
    FileUtils.rm_rf(TEST_FILE_2) if File.exists?(TEST_FILE_2)

    def teardown
    # Won't attempt to clean up in same process

    def test_delete_dir
    assert(!File.exists?(TEST_DIR),"Test dir already exists")




    assert(!File.exists?(TEST_DIR),"Unable to delete test dir")

    def test_delete_file
    assert(!File.exists?(TEST_FILE_1),"Test file already exists"),File::CREAT,0666)


    rescue SystemCallError => e
    puts "\nError on attempt: FileUtils.rm_rf (" + e + ")"

    rescue SystemCallError => e
    puts "\nError on attempt: File.delete (" + e + ")"

    system("rm -rf " + TEST_FILE_1);
    rescue SystemCallError => e
    puts "\nError on attempt: system rm -rf (" + e + ")"

    assert(!File.exists?(TEST_FILE_1),"Unable to delete test file")

    def test_delete_file_in_child_process
    assert(!File.exists?(TEST_FILE_2),"Test file already exists")

    system("ruby -e '\"" + TEST_FILE_2 + "\",File::CREAT)'")


    assert(!File.exists?(TEST_FILE_2),"Unable to delete test file")

    Mike Grier, Dec 18, 2004
    1. Advertisements

  2. Mike Grier

    Justin Rudd Guest

    > I'm new to ruby so perhaps I'm simply doing this wrong, but...

    This is more of a Windows thing than a Ruby thing.


    Underneath the covers this opens a handle to the file. Until that
    handle is closed, no one else can do anything to it. The GC will
    eventually get it. But probably not before your call to rm_rf

    > system("ruby -e '\"" + TEST_FILE_2 + "\",File::CREAT)'")

    This works because when the process ends, the Ruby GC is probably
    called. If it isn't, then Windows will clean up the misbehaving

    Justin Rudd
    Justin Rudd, Dec 19, 2004
    1. Advertisements

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.