Search code examples
fusevfs

FUSE: Multiple opens on the same file


Does the OS/VFS/FUSE-layer manage the semantics of multiple handles to the same file, or is that something that the driver has to arbitrate?


Solution

  • Short: If you want to disallow that, you have to handle it in the driver.

    Long: I did not find any indication in the POSIX error codes of open() that would prevent having multiple handles for the same file in the same process. Wikipedia states that this is fine:

    The same file may be opened simultaneously by several processes, and even by the same process (resulting in several file descriptors for the same file) depending on the file organization and filesystem.

    FUSE in it's documentation does not condemn it either; it often just proxies the semantics.

    To try it, I opened the same file in Python twice, and got two different file descriptors.

    In [1]: fd1 = open("./resting.org")
    
    In [2]: fd2 = open("./resting.org")
    
    In [3]: fd1.fileno()
    Out[3]: 5
    
    In [4]: fd2.fileno()
    Out[4]: 6
    

    So, you have to prevent it yourself, might stay POSIX compliant, since it's unspecified, but may violate an assumption some unknowing programmer made.