0

The manual page for lseek() says:

lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a "hole") return null bytes ('\0') until data is actually written into the gap.

At the error section it says:

EINVAL whence is not valid. Or: the resulting file offset would be negative, or beyond the end of a seekable device.

I'm unsure how to interpret the lseek manual page as it doesn't mention MTD.

Assuming that the MTD has been opened with read and write permission, how would lseek() respond when seeking past EOF when whence is SEEK_SET for MTD's?

ZeppRock
  • 988
  • 1
  • 10
  • 22

1 Answers1

1

Don't mix file and device concepts. The manual page is perfectly clear: you can seek past the end of the file, but not to the point that the file would become larger than what the device can host.

Example: you have a 128Mb device with only an empty file in it. You can open the file and seek to any position < 128Mb, because a subsequent write() would make the file grow. But surely you can not seek > 128Mb, because a subsequent write() will surely fail, and this is already known at the time of the seek.

Now, to the MTD device. The MTD is an abstraction layer that maps a (flash) hardware device into a file. Given this, the file exposes both the behaviors of a file and a device, something like a file big exactly as the device it refers to, hence seeking past the end of file equals seeking past the end of the device. In this case another curiosity comes to my mind: what if we try to truncate that file? I suppose to know already the answer...