Reputation: 43179
I am running into an issue as follows in my C++ application running on Centos 5:
On machine A there is an NFS mounted drive from machine C that contains a file:
/nfs/mounted/drive/path/directory/file
My application renames the directory from machine A:
mv /nfs/mounted/drive/path/directory /nfs/mounted/drive/path/directory.old
The application on machine A then ssh(s) over to machine B to run an application I created which starts by first checking for the file's existence:
/nfs/mounted/drive/path/directory/file
The file is found to be present (it is a race condition and machine B is still seeing the old state) and therefore machine B proceeds as if the file is present, but then machine B subsequently fails when it goes to open the file and it is not really there.
I tried to resolve this by calling sync()
in my application on machine B before it checks for the file. I also tried doing echo 2 > /proc/sys/vm/drop_caches
but neither of these caused my application on machine B to immediately see the current state of the file being gone.
What can I do in my C++ application on machine B before it checks for the file to ensure the data is the most up to date status? I do not want to explicitly test the file in question by opening it, but instead I wish to cause the disk to be in current state. I would like to do this forced sync in a relatively fast manner from C++, as in taking less than around a quarter of a second elapsed time.
Upvotes: 0
Views: 1856
Reputation: 4423
NFS has a file handle per file system object. As you rename a directory, the filemhandle did not change and lookup gives you positive result. To avoid such situation better to create a new directory, move all files and directories from original directory and remove the source directory. By this you will invalidate the old file handle.
Upvotes: 1