I’m implementing a libfuse filesystem. When a file is opened, I read the file attributes and store them in a hash table keyed with the file handle I generate. This serves two purposes: to maintain a collection of open file handles and to cache the information I retrieve during opening.
Of course, nothing is stopping user code from trying to pass an invalid file handle, i.e. read from a file that’s not open.
There are a number of error codes that I can return from the read
function, but it’s not clear to me which is the one that is expected in such situation.
Advertisement
Answer
As you can see in the POSIX standard, the correct value to return would be EBADF
:
[EBADF] The fildes argument is not a valid file descriptor open for reading.
That said, if user code passes an invalid file handle, the Linux kernel will return EBADF
to the user before you, or fuse, get any say on the matter.