random thoughts, formed in the twisted mind of a coder... RSS 2.0
# Saturday, August 09, 2008

File-locking on Windows systems is quite easy when it comes to one system controlling all the available storage. But what about when you have SAN storage and you need to mount the same SAN-disk onto multiple fileservers?

The problem is that file-locks are not completely integrated into NTFS. The locks are maintained somewhere in the fileserver's memory. So, when you have one piece of storage mounted on multiple systems, every system keeps track of only their own file-locks, without any knowledge of the locks of the other systems. In such a case clashes can occur frequently, when two separate systems both lock the same file.

Now, if you're writing your own software and this software has to overcome this problem, I have a little trick for you... Create a .lock file. I'll explain. When you need to access (and lock) the file "C:\mydir\myfile.txt" first try to create the file "C:\mydir\myfile.txt.lock". If you fail to create it you know the file is already there on the storage and thus locked by another system). This always works over any number of systems, because it's a file on the storage and not a flag in memory of a fileserver. When two systems try to create a .lock file at the same time, one always fails.

You don't need to write anything to the file. Simply leave it with a length of 0 bytes. When you write bytes to a file it just takes up more time and won't give you any benefits.

Oh, and be sure you have enough rights to create a file on the storage ;) Also, make sure you use the right parameters when creating a file. Make sure an exception is thrown when a .lock file already exists. And please do not use File.Exists() to check for a .lock file, as this to only takes up more time and doesn't give you any benefits because you still have to create the file.

Here is an example in C#:

using System.IO;

string fileName = @"C:\mydir\myfile.txt";

//try to lock the file
FileStream lockFile = null;
try
{
  lockFile = File.Open(fileName+".lock", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException e){;}

//check for a successful lock
if (lockfile == null)
  //file already locked by other process
else
  //you have the lock

//your code here
//...

//unlock
lockFile.Close();
File.Delete(fileName+".lock");
Find more info at:
http://msdn.microsoft.com/en-us/library/y973b725.aspx
http://en.wikipedia.org/wiki/NTFS
Saturday, August 09, 2008 4:31:09 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
FileSystem
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Martijn Thie
Sign In
Statistics
Total Posts: 18
This Year: 0
This Month: 0
This Week: 0
Comments: 161
All Content © 2014, Martijn Thie
DasBlog theme adapted from 'Business' (originally by delarou)