correct "mode" to delete windows file

B

bwv549

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.
 
K

Klaus Mueller

bwv549 said:
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
 
D

Daniel Berger

bwv549 said:
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
 
B

bwv549

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!

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top