Helder AC
Helder AC

Reputation: 376

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

Upvotes: 3

Views: 811

Answers (5)

synthesizerpatel
synthesizerpatel

Reputation: 28056

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.

Upvotes: 1

hxc
hxc

Reputation: 89

I think an advisory lock will help. Since if one using the file which another program is working on it, the one will get blocked or get an error. but if you access it in force,the action is Okey, but the result is unpredictable, In order to maintain the consistency, all of the processes who want to access the file should obey the advisory lock rule. I think that will work.

When the file is closed then the lock is freed too.Other processes can try to hold the file.

Upvotes: 0

Hemant Metalia
Hemant Metalia

Reputation: 30688

you can check if file is open or not as following

FILE_NAME="filename"
FILE_OPEN=`lsof | grep $FILE_NAME`

// if [ -z $FILE_NAME ] ;then
   // "File NOT open"
// else
    // "File Open"

refer http://linux.about.com/library/cmd/blcmdl8_lsof.htm

Upvotes: 0

Sangeeth Saravanaraj
Sangeeth Saravanaraj

Reputation: 16627

There is no functions/libraries available in C to do this. But a simple alternative is to rename the file once an activity is over. For example, recorder can open the file with name - file.record and once done with recording, it can rename the file.record to file.transfer and the transfer program should look for file.transfer to transfer and once the transfer is done, it can rename the file to file.read and the reader can read that and finally rename it to file.done!

Upvotes: 0

nishant
nishant

Reputation: 1

Have you considered using stream interface between the recorder program and the one that grabs the recorded data/file? If you have access to a stream interface (say an OS/stack service) which also provides a reliable end of stream signal/primitive you could consider that to replace the file interface.

Upvotes: 0

Related Questions