8

Does Linux provide a system call which can create a "view" of a limited byte range of a backing file? I'm envisioning something that for example would act on an open file descriptor and either modify it or generate a new file descriptor where file offsets are relative to the beginning of the range and end at the end of the range.

The use-case would be to limit a non-cooperating subprocess to accessing only a particular portion of an input file.

llasram
  • 183

1 Answers1

12

One way of doing this is to use a loop device. This approach does have two requirements which may make it less useful: you need to be root to set it up, and the non-cooperating subprocess must be able to write to a block device. Oh, and it doesn’t deal with conflicting changes.

To set the loop device up, run

losetup -o 1024 --sizelimit 2048 --show -f yourfile

replacing 1024, 2048 and yourfile with appropriate values — -o specifies the start offset, --sizelimit the size (counting from the offset). Note that sizelimit has to be a multiple of 512.

This will output the name of the loop device which has been set up; adjust the permissions as necessary, and give it to your non-cooperating sub-process. When you no longer need the device, delete it with

losetup -d /dev/loopN

replacing N as appropriate.

Stephen Kitt
  • 434,908
  • Hmm, what do you mean with conflicting changes? Do you mean writes that come through the loop device not being in sync with direct writes to the backing file? – ilkkachu Feb 27 '19 at 18:43
  • Not 100% what I wanted, but in the absence of anything closer I'll take it. Thanks! – llasram Feb 27 '19 at 19:32
  • @ilkkachu yes, I mean that there needs to be some “agreement” between the processes about who is allowed to write. (It can be simple enough — if the “parent” process doesn’t write while the loop device is set up for the non-cooperating subprocess, everything will be fine.) – Stephen Kitt Feb 27 '19 at 19:33
  • @llasram right, I know this isn’t perfect; the FIFO approach in Gilles’ link is interesting to look at too. – Stephen Kitt Feb 27 '19 at 19:34