Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if another program has a file open

After doing tons of research and nor being able to find a solution to my problem i decided to post here on stackoverflow.

Well my problem is kind of unusual so I guess that's why I wasn't able to find any answer:

I have a program that is recording stuff to a file. Then I have another one that is responsible for transferring that file. Finally I have a third one that gets the file and processes it.

My problem is: The file transfer program needs to send the file while it's still being recorded. The problem is that when the file transfer program reaches end of file on the file doesn't mean that the file actually is complete as it is still being recorded.

It would be nice to have something to check if the recorder has that file still open or if it already closed it to be able to judge if the end of file actually is a real end of file or if there simply aren't further data to be read yet.

Hope you can help me out with this one. Maybe you have another idea on how to solve this problem.

Thank you in advance.

GeKod

like image 544
Helder AC Avatar asked Dec 30 '25 02:12

Helder AC


1 Answers

Simply put - you can't without using filesystem notification mechanisms, windows, linux and osx all have flavors of this. I forget how Windows does it off the top of my head, but linux has 'inotify' and osx has 'knotify'.

The easy way to handle this is, record to a tmp file, when the recording is done then move the file into the 'ready-to-transfer-the-file' directory, if you do this so that the files are on the same filesystem when you do the move it will be atomic and instant ensuring that any time your transfer utility 'sees' a new file, it'll be wholly formed and ready to go.

Or, just have your tmp files have no extension, then when it's done rename the file to an extension that the transfer agent is polling for.

like image 119
synthesizerpatel Avatar answered Dec 31 '25 17:12

synthesizerpatel