4

I've found myself in a couple of situations now where I'd like to have such a "file" that essentially runs a script whenever it's "opened", allowing me to "read" it's output.

For example, I might have a configuration file in some project which I'd like to use locally, but due to differences in my machine I need to run a sed or grep over it to make it work. The project's configuration file is still being maintained, and I'd like those changes to appear in my local configuration. This means doing the simple thing by modifying it then saving it is not ideal since it is easy for it to get stale as the project updates. I'd like the filtering to happen every time I open my local configuration file.

Is there some way for me to achieve this kind of thing? I know that the virtual file system allows you to make some pretty crazy things appear as "files", so I feel like this is not outside the realm of possibility.

5
  • The problem you've got is that you need to write to the file at the same time it is being read. What you would need is a program to open the file in that monitors when your file changes. The main problem is that the file contents will be in memory and if something changes the file on disk then which one is the true, correct version of the file?
    – Kinnectus
    Commented Dec 11, 2014 at 8:37
  • I'm trying to avoid that whole conundrum by never actually having a written, filtered file. Instead the command is run each time I access the "file", and the contents of the "file" is the output of that command, every time. The output of the command never actually hits the disk.
    – LukeGT
    Commented Dec 12, 2014 at 0:42
  • I think it would be easier for your script to run and simply echo out the results to a new file (your new ini for). Your script can sed or grep and you can use the various find and filter tools to make sure your output file is correctly formed.
    – Kinnectus
    Commented Dec 12, 2014 at 7:26
  • I'm aware that I can do that, but that solution requires manual intervention every time the original file changes, making it easy for the filtered version to become stale if I'm not attentive.
    – LukeGT
    Commented Dec 13, 2014 at 22:39
  • 1
    It's certainly not perfect, but the easiest solution would probably be to just set up a cron job that periodically runs whatever filtering you need on the remote file, outputting to your local file. Assuming it's not very complicated or time-intensive filtering it could even be run every minute or something to keep your file fresh.
    – daenris
    Commented Dec 18, 2014 at 16:41

1 Answer 1

4

In theory this is possible using LD_PRELOAD with the desired program.

Write a library to add a wrapper over "open" system call(s) and use the original program (say cat) as LD_PRELOAD=/path/to/library cat.

The overridden_open() code in wrapper library would look similar to following dummy code.

/* This is an illustrative code, and doesn't follow any good coding practice. */
int overridden_open (...)
{
    /* Only do this for the config file. */
    if (strcmp (filename, "/path/to/required/config/file") == 0)
    {
        /* Download a fresh copy, and if successful, overwrite the existing file. */
        if (system ("wget -O /path/to/required/config/file.tmp http://remote.file/url") == 0 && system ("<perform awk/sed/grep operations>") == 0)
        {
            system ("mv /path/to/required/config/file.tmp /path/to/required/config/file");
        }
    }
    return open (...);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .