Search code examples
c++linuxd

How to capture and modify read and writes on file using Dlang or cpp


Let's say that another program is to write or read a specific file. When it does that, I need to be able to take that read or write and handle the way I like (for example, program X wants to read a file located at /path/file.txt, but program Y (my program) takes that read "request" and instead gives program X the encryption first 2KiB of another file located at /path/file2). Essentially, any time a specified file is being read or written to, my program will be called and it will handle the read or write request in Dlang or C++. I cannot create a new file system for this :( and it has to at least work with Linux (so anything specific to Linux works). Also, it is crucial that I RESPOND to the read or write and not preprocess the result, sorry this was not clear in the example.


Solution

  • What you need is a seek-able FIFO (named pipe). It has been proposed but as far as I know it has not been implemented yet (check Linux kernel changelogs, maybe it has but I do not know about it).

    As suggested, a new, tiny filesystem is your best option. Luckily it is pretty simple to write one with the FUSE project.