6

This question already has an answer here:

Is there a way that I can have something which appears as a file, but when being read some code executes which fills the content of the file "on the fly"?

Like FIFO/ named pipe, but without the need to start the writing process by hand.

Or like CGI on webservers, but on the local file system.

EDIT:
To guard myself against "why I want this"-doubts:
I want this to be able to have an audiofile whose content is generated on access, and then delivered to the audio player. And it should not be streaming, since when accessed by the audio player the length of the audio should be known.

marked as duplicate by Gilles files Apr 2 '16 at 20:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • A named pipe is the way to go. If you don't want to start the writing process by hand, turn it into a daemon and start it at boot. – Henrik Apr 2 '16 at 10:40
  • 2
    Do you mean something like /dev/random or /dev/zero? – ohno Apr 2 '16 at 10:45
  • @Henrik: I want to start it on demand. – Golar Ramblar Apr 2 '16 at 13:50
  • @ohno: but I want to be able to specify an arpitrary command/ script to generate the content. – Golar Ramblar Apr 2 '16 at 13:51
  • Yes, unix.stackexchange.com/questions/66990/… answers this question for me (scriptfs, or maybe fuseflt can also do the job). – Golar Ramblar Apr 3 '16 at 6:01
8

If you are willing to do some coding, the fanotify system call allows you write a program that will intercept reads on any file beneath some mount point, and substitute or manipulate the data before returning it. See man fanotify for example C code.

Alternatively, you can mount a fuse filesystem, see man fuse, which allows you to implement a filesystem by a userspace program. For example, curlftpfs (see its man page) converts file accesses into curl ftp calls.

  • Is the FA in fanotify for "File Access?" – Aaron Hall Apr 2 '16 at 12:33
  • The FA might be for "file activities", as this phrase is used in the man page, though it is not explicitly defined. – meuh Apr 2 '16 at 12:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.