Ammar Nofan Faizi (npub1am9…995c) [@LLENN08](https://hiradai.space/@LLENN08 )
(( update on my HTTP server progress ))
I found a new problem with the file table serving.
Case: When I have a ./public/index.html file and it is accessed by the client via HTTP, the web server will open() the file and save its fd (file descriptor) to the table, so next time it's accessed, it can just use the saved fd instead of opening it again.
This mechanism also makes it possible to serve multiple clients that need the same file using only one fd, saving system resources (the offset is managed separately for each client, handle read via pread(2)).
The similar method is also implemented for the mmap() variant of file serving. Except that it doesn't need to call pread(2).
Now the problem is, when the file is deleted from the filesystem (e.g., via "rm ./public/index.html"), the fd in the table is still valid, and the server will continue to serve the deleted file to clients until the fd is closed (e.g., when the server is restarted).
Putting a new file with the same name (e.g., "echo 'new content' > ./public/index.html") will not affect the existing fd, and clients will still get the old deleted file's content.
For now, my workaround for deleted with is calling stat() on the file before serving it, and if stat() fails, I close the fd and remove it from the table, forcing the server to open() the file again on the next access.
But if the file is deleted, then recreated before the stat() call, the server will miss the deletion and continue to serve the old content.
What is the most reliable way to handle this?