Paul Lutus wrote:
> Eero Saynatkari wrote:
> 
>> Hi!
>> 
>> Can anyone tell me if Ruby's File.ctime can be reliably
>> used to detect if a directory's contents have changed?
>> 
>> I do not need to know of changes to files, just adds and
>> deletes of files to/from the directory itself.
> 
> On Linux, File.ctime(path) can be used on a directory to detect file
> additions or removals, but not changes to existing files.

Excellent, this is just what I needed to know.

> Example code:
> 
> #!/usr/bin/ruby
> 
> path="/temp2"
> 
> ot = File.ctime(path)
> 
> while true
>         nt = File.ctime(path)
>         puts "#{path} changed at #{Time.new}" if nt != ot
>         ot = nt
>         sleep 1
> end
> 
>> In case you are curious, I intend to cache the files under
>> $PATH and only re-access them if a modification has been
>> detected.
> 
> Okay, now you are saying "modification", not addition or removal. On 
> Linux,
> File.ctime won't flag change in the directory when file contents change,
> only additions or removals.

No, I am sorry for mixing terms. I needed to monitor additions
and removals which (in my addled brain are modifications of the
directory 'file' itself).

> BTW File.mtime and File.ctime appear to act the same under Linux.
> 
>> Alternative solutions to this are also welcome.
> 
> You want to monitor a directory of files for changes and act if one or 
> more
> of them has changed, yes? Why not simply collect stats on all the files
> periodically, and compare new times with old for each file?

I have to be assured that my cache is up-to-date, so I
check the ctime each time anything in that directory
is invoked--if there has been a change, I re-cache the
directory.


-- 
Posted via http://www.ruby-forum.com/.