Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in Node.js if a file is open/being written to?

I found many answers for C++, C#, etc. but haven't yet found one for Node.js. Here is my case.

I've created a module which watches for file/directory changes (new or updated, doesn't care about deleted files) in my FTP server. Once I notice a new file, or an existing one is changed, I get a notification and notify my module B to sync this file to Sourceforge.net. The module works fine, reacts to changes instantly.

But what I'm not satisfied with yet is that when a new file is added, that file receives "modified" event several times depending on the file size: the bigger the size, the more "modified" events it will receive. This is because while the file is being uploaded OS periodically flushes the uploaded buffer to the file system, thus resulting in multiple "modified" fs.watch events.

Question

How can I check if the file is still open so that I know that the "modified" event is happening actually because of the remaining data being flushed, not because the file has been changed multiple times, so I can ignore it?"

Once the file is closed or writing has been stopped, I can then notify my module B to sync to SF.net.

I would appreciate any idea. Thanks.

like image 709
Eye Avatar asked Oct 05 '22 20:10

Eye


1 Answers

You could consider setting a timeout and then re-checking the file to ensure that the size and modified time have not changed, if they haven't changed assume the upload has completed and transfer the file to SF.net.

Another option would be creating a native node module based on your c solutions you found;

Some information;

Getting Started with Node.js Native Modules

But use node-gyp instead of node-waf

https://github.com/TooTallNate/node-gyp

like image 83
Jason Brumwell Avatar answered Oct 10 '22 10:10

Jason Brumwell