Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Callback on file access

by tomazos (Deacon)
on Dec 13, 2009 at 16:01 UTC ( #812594=perlquestion: print w/replies, xml ) Need Help??

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

Suppose I provide a Perl function:

    sub fileread($) { my ($relpath) = @_; ... }

and I have a directory path:

    my $dir = '/path/to/some/dir';

and then I want to execute a system command which will start a subprocess and wait for it to complete:


I want to set it up so that whenever a file within $dir (or any of its subdirectories) is accessed by the subprocess, the fileread function is called with the relative path of the file that was accessed. For example if the subprocess reads /path/to/some/dir/foo/bar, than fileread("foo/bar") should be called. If there is also some way of determining the type of access it made (read, write, delete, create, etc), even better.

Anyone know of any CPAN modules that could help? I am working on Linux BTW if there is no cross-platform solution and only a Linux-specific solution? Approximate solutions are also welcome.

Thanks in advance, Andrew.

Replies are listed 'Best First'.
Re: Callback on file access
by Corion (Patriarch) on Dec 13, 2009 at 16:19 UTC

    If you know what file your subprogram will try to read on, you can create a pipe via mkpipe and feed the program from that pipe. If you don't know what files the subprogram will try to read from, I fear you'll have to go the route of a user-installable filesystem via FUSE, or by going the roundabout way of installing Samba, Samba callbacks and then mounting the Samba share into Linux.

    A second approach could be to override the system calls if the subprogram loads the C library dynamically and redirect the system calls into your program, much like FUSE, but completely in user space.

    Neither of the solution is an in-process solution, unfortunately.

      What about Linux::Inotify2 and friends? Why wouldn't something like that work or be appropriate for this problem? -Andrew.

        Ah - a good approach - I thought the callback was supposed to supply data, but it seems that it's enough to just register the access. Then, potentially Inotify or truss or strace might produce the appropriate list of system calls as well.

Re: Callback on file access
by zentara (Archbishop) on Dec 14, 2009 at 11:51 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://812594]
Approved by Perlbotics
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-12-10 07:44 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (39 votes). Check out past polls.