Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Problem with creating/deleting file in same process (Windows)

Reply
Thread Tools

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

 
 
Mike Grier
Guest
Posts: n/a
 
      12-18-2004
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 File.new 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,

Mike


#!/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
end

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

Dir.mkdir(TEST_DIR)

assert(File.exists?(TEST_DIR))

FileUtils.rm_rf(TEST_DIR)

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

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

File.new(TEST_FILE_1,File::CREAT,0666)

assert(File.exists?(TEST_FILE_1))

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

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

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

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

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

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

File.delete(TEST_FILE_2)

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

end



 
Reply With Quote
 
 
 
 
Justin Rudd
Guest
Posts: n/a
 
      12-19-2004
> 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.

> File.new(TEST_FILE_1,File::CREAT,0666)


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 'File.new(\"" + 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
process.

--
Justin Rudd
http://seagecko.org/thoughts/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Launching a process (exe) using the same process id as the launcher franckspike C++ 1 07-03-2008 10:30 PM
Cannot delete a file the process cannot access the file because it is being used by another process ASp.net 2 ary ASP .Net 2 11-29-2006 04:30 AM
Cannot delete a file the process cannot access the file because it is being used by another process ASp.net 2 ary ASP .Net 0 11-28-2006 09:13 AM
delete file failed, The process cannot access the file because it is being used by another process guoqi zheng ASP .Net 3 04-13-2005 06:17 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57