[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

correct "mode" to delete windows file

bwv549

5/4/2006 9:15:00 PM

What is the correct mode to set so that I can delete a windows (ruby
native win32) file? ( e.g., File.chmod(<mode>, filename) ) Does the
containing directory require a certain mode?

A file that is created by a separate executable defies my attempts to
delete it inside a script. I can create files and delete them, but not
this tenacious guy. When I run:

puts "MODE: " + File.stat(file).mode.to_s
p File.stat(file)

I get this output:

MODE: 33188
#<File::Stat dev=0x2, ino=0, mode=0100644, nlink=1, uid=0, gid=0,
.... >

Why would File.stat(file).mode give different output (33188) than
printing the stat object (where the mode=0100644)?

Thanks a bunch.

3 Answers

Klaus Mueller

5/4/2006 9:29:00 PM

0

bwv549 wrote:
>
> Why would File.stat(file).mode give different output (33188) than
> printing the stat object (where the mode=0100644)?

0100644 is octal (leading zero) and represents 33188 in decimal.

Klaus

Daniel Berger

5/5/2006 3:30:00 AM

0


bwv549 wrote:
> What is the correct mode to set so that I can delete a windows (ruby
> native win32) file? ( e.g., File.chmod(<mode>, filename) ) Does the
> containing directory require a certain mode?
>
> A file that is created by a separate executable defies my attempts to
> delete it inside a script. I can create files and delete them, but not
> this tenacious guy.

<snip>

The most probable explanation is that there's a handle open on that
file somewhere and/or some other process has ahold of it.

Regards,

Dan

bwv549

5/5/2006 9:15:00 PM

0

After some more tests and head pounding, this seems to be the case. I
was using this expression to test file equivalency:

assert_equal( File.open(file).read, File.open(other).read )

This does not close the file handles! Linux apparently doesn't mind
unlinking an open file handle, Windows does.

Another expression I often use does not close the file handle either:

File.open(file).each do |line|
# do something with line
end
# file handle NOT closed!

As I generally code on linux, I've never been alerted to this problem
before. I'd bet I'm not the first person to be caught out doing this.

Below is a script to more fully demonstrate the phenomenon:
----------------------------------------
#!/usr/bin/ruby -w

require 'fileutils'

test = "testing"
fh = File.open(test, "w")
fh.print "hello"
fh.close

test2 = "other"
FileUtils.copy test, test2

if File.open(test){|fh| fh.read } == "hello" &&
File.open(test2){|fh| fh.read } == File.open(test){|fh| fh.read }
puts "equivalent"
end

if File.open(test).read == "hello" &&
File.open(test2).read == File.open(test).read
puts "also equivalent, but you now have two open file handles!"
end

File.unlink test
File.unlink test2

## On windows (output): ->
# equivalent
# also equivalent, but you now have two open file handles!
# ./test.rb:23:in `unlink': Permission denied - testing (Errno::EACCES)
# from ./test.rb:23
## On linux (output):->
# equivalent
# also equivalent, but you now have two open file handles!

----------------------------------------