From: =?ISO-8859-1?Q?Une_B=E9vue?= on
i'd like to know how to test if a file is open from another app ?

i've read http://www.ruby-forum.com/topic/144114

and have tested with :
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
and
MacRuby version 0.5 (ruby 1.9.0) [universal-darwin10.0, x86_64]

the following :
f = File.new("/Users/yt/dev/Signature/signatures.txt")
puts "f.flock(File::LOCK_EX) : #{f.flock(File::LOCK_EX)}"
puts "f.flock(File::LOCK_UN) : #{f.flock(File::LOCK_UN)}"
puts "f.flock(File::LOCK_EX | File::LOCK_NB) : #{f.flock(File::LOCK_EX |
File::LOCK_NB)}"

giving the same result in both cases :
f.flock(File::LOCK_EX) : 0
f.flock(File::LOCK_UN) : 0
f.flock(File::LOCK_EX | File::LOCK_NB) : 0


--
� L'essence m�me du g�nie, c'est de mettre en pratique
les id�es les plus simples. �
(Charles Peguy)
From: Brian Candler on
The traditional Unix utilities to solve this problem are "lsof" and
"fuser". I don't know if OSX has either of these as standard, but they
are probably available as ports.

Locking won't help unless the other process has taken a lock on the
file.
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
> The traditional Unix utilities to solve this problem are "lsof" and
> "fuser". I don't know if OSX has either of these as standard, but they
> are probably available as ports.

Or it might be "fstat", given that OSX is BSD-derived.
http://netbsd.gw.com/cgi-bin/man-cgi?fstat++NetBSD-4.0
--
Posted via http://www.ruby-forum.com/.

From: Giampiero Zanchi on
You could use exceptions
try to read from file; if it is closed, then an exception will be
raised; then you can do something, for instance set a boolean and return
it...

def is_open(a_file)
begin
...read from file...
rescue
...if file is not open then do something...
end
end
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
Giampiero Zanchi wrote:
> You could use exceptions

I thought the OP was interested in whether *another* process had the
same file open.

If you just want to test whether a Ruby File object is closed, there is
a 'closed?' method.

irb(main):002:0> f = File.open("/etc/passwd")
=> #<File:/etc/passwd>
irb(main):003:0> f.closed?
=> false
irb(main):004:0> f.close
=> nil
irb(main):005:0> f.closed?
=> true
--
Posted via http://www.ruby-forum.com/.