Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

lfs_file_seek and lfs_file_write succeed even if the file is removed while open #842

Closed
shahawi-sumup opened this issue Jun 15, 2023 · 2 comments
Labels

Comments

@shahawi-sumup
Copy link

Hello,

Let's assume we have 2 thread
Thread1: Opened a file and continuously seek and append data to it.
Thread2: deleted the same file opened and in use by Thread1

I can see that the file is not there on the flash after lfs_remove but functions lfs_file_seek and lfs_file_write doesn't report errors.

#define LFS_VERSION 0x00020004
#define LFS_DISK_VERSION 0x00020000

Could you please help me understand this behavior and if it is fixed in a later version ?
I tried to find a similar issue but couldn't found any.

Thanks in advance :)

@geky geky added the question label Jun 20, 2023
@geky
Copy link
Member

geky commented Jun 20, 2023

Hi @shahawi-sumup,

In general, littlefs tries to match Linux/POSIX behavior where possible, since this is what a lot of developers expect from a filesystem, even if the behavior is sometimes unintuitive.

In this case, the storage for a file is reserved until both:

  1. The file is removed (unlink in Linux/POSIX).
  2. All open file handles are closed.

In this model, it's not really a bug for Thread1 to be able to still write to the file, the data just doesn't go anywhere when closed.

This has some trade offs. On one hand, it can create surprising behavior like this, and may end up with more storage temporarily resrved. On the other hand, it can make some multithreaded cases easier, such as opening a file for reading without worrying about a thread removing the file mid-read.


Sidenote: Reading up on things, it seems there was also a proposal for Linux to add a sort of flinkcall that acted as the inverse of unlink, i.e. relink unlink files back into the filesystem (src).

That proposal was shot down for security reasons. littlefs doesn't really have the same security concerns, so it could in theory be added here, but I don't know if there's much demand for such a function.

@shahawi-sumup
Copy link
Author

Thanks @geky, That answers my question.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants