DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: A deep dive into file locking with NodeJS

sebmtl profile image
Sebastian Rath Author

But you're approach is pretty good, I like it. As in to use a "pre-staging area" as a safety net

Thread Thread
december1981 profile image
Stephen Brown • Edited on

I meant to add an additional comment - it seems that if you write to a file it can sometimes take a long time to write - imagine a browser is downloading a file - the write stall may depend on the network conditions, and could take as long as you like.

I think your approach of checking for open file handles, combined with the checksum approach I suggested will solve this issue.

So

i) read and checksum original file
ii) check any write handles on original file, as you were doing (fail/abort if any)
iii) copy original file.
iv) compare checksum of copied file with original (fail if mismatch)

These four steps would seem to suffice to me, avoiding race conditions, as well as a partially written, stalling write operations just before the commit operation has begun.

Thread Thread
sebmtl profile image
Sebastian Rath Author

Hey, sorry for the late reply. That's a really good idea you have there, I'll have to let that sink in. In case you're interested, you are more than welcome to contribute your ideas to the project either on GitHub directly or on Discord as well. Door is always open!

Thread Thread
december1981 profile image
Stephen Brown

I will clone and check the project out, it does sound interesting.