rootcho has asked for the wisdom of the Perl Monks concerning the following question:

hi, Is there a way to intercept file access calls ?
Some way to catch open()? or something like that ?

What I have is a modules which access/modify config files in many dirs. The problem is that the path to the files is hardcoded
What I will do is go over all the modules and change in any place I see suspicious code to get the the path from function I write, so I can modify it to whatever I want...but still after this I can't be 100% sure i'll find them all (i haven't written the code).
So I was thinking of a way to intercept these accesses and fix the code. The problem is that it is running system, I mean I need to run both scenarios together for some time to find the offending code.
I was thinking of using lsof and inotify, but both of these doesn't work. Because the new structure will include symlinks (both scenarios at the same time), for which inotify can't detect accesses/changes.
And I want to detect in what part of the code this happens so i can fix it
So if it is perl solution I can print caller() info and find it easy ?
Any ideas


UPDATE:
Probably this was the thing I was searching for :
http://search.cpan.org/~ctweten/ex-override-1.01/override.pm
I will need this for some time and then will revert back to the original open()
Unless there is more elegant way

Replies are listed 'Best First'.
Re: Intercepting IO file access
by blahblahblah (Priest) on Jul 28, 2007 at 02:53 UTC
    I've never tried the ex::override module mentioned in your update, but I can point you to a similar thing I did a while ago using the subs pragma. After a few rounds of improvements from the great folks here, it worked pretty well. There were a couple of subtle things about open's behavior that I missed initially:

    Re: anonymous filehandle for overridden open sub helped me get it right for undefined lexical filehandles

    Re: Re: use subs enlightened me somewhat on the difference between lists and arrays, or why I couldn't just pass @_ on to the CORE::open function.

    I wouldn't at all be surprised if there are more bugs in my open sub, but my program's use of open is limited to some very simple cases and my sub seems to handle them well enough.

    Joe