19 using System.Collections.Generic;
21 namespace Lucene.Net.Store
87 public override Lock MakeLock(System.String lockName)
91 if (internalLockPrefix != null)
92 lockName = internalLockPrefix +
"-" + lockName;
97 public override void ClearLock(System.String lockName)
104 if (System.IO.File.Exists(internalLockDir.FullName))
107 tmpBool = System.IO.Directory.Exists(internalLockDir.FullName);
110 if (internalLockPrefix != null)
112 lockName = internalLockPrefix +
"-" + lockName;
114 System.IO.FileInfo lockFile =
new System.IO.FileInfo(System.IO.Path.Combine(internalLockDir.FullName, lockName));
116 if (System.IO.File.Exists(lockFile.FullName))
119 tmpBool2 = System.IO.Directory.Exists(lockFile.FullName);
121 if (System.IO.File.Exists(lockFile.FullName))
123 System.IO.File.Delete(lockFile.FullName);
126 else if (System.IO.Directory.Exists(lockFile.FullName))
128 System.IO.Directory.Delete(lockFile.FullName);
133 if (tmpBool2 && !tmpBool3)
135 throw new System.IO.IOException(
"Cannot delete " + lockFile);
145 private System.IO.FileStream f;
146 private System.IO.FileStream channel;
147 private bool lock_Renamed;
148 private System.IO.FileInfo path;
149 private System.IO.DirectoryInfo lockDir;
161 private static HashSet<string> LOCK_HELD =
new HashSet<string>();
163 public NativeFSLock(System.IO.DirectoryInfo lockDir, System.String lockFileName)
165 this.lockDir = lockDir;
166 path =
new System.IO.FileInfo(System.IO.Path.Combine(lockDir.FullName, lockFileName));
169 private bool LockExists()
173 return lock_Renamed !=
false;
177 public override bool Obtain()
190 if (System.IO.File.Exists(lockDir.FullName))
193 tmpBool = System.IO.Directory.Exists(lockDir.FullName);
198 System.IO.Directory.CreateDirectory(lockDir.FullName);
202 throw new System.IO.IOException(
"Cannot create directory: " + lockDir.FullName);
205 else if (!System.IO.Directory.Exists(lockDir.FullName))
207 throw new System.IO.IOException(
"Found regular file where directory expected: " + lockDir.FullName);
210 System.String canonicalPath = path.FullName;
212 bool markedHeld =
false;
222 if (LOCK_HELD.Contains(canonicalPath))
233 LOCK_HELD.Add(canonicalPath);
240 f =
new System.IO.FileStream(path.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
242 catch (System.IO.IOException e)
252 catch (System.UnauthorizedAccessException e)
267 lock_Renamed =
false;
270 channel.Lock(0, channel.Length);
273 catch (System.IO.IOException e)
287 catch (System.UnauthorizedAccessException e)
302 if (lock_Renamed ==
false)
333 if (markedHeld && !LockExists())
337 if (LOCK_HELD.Contains(canonicalPath))
339 LOCK_HELD.Remove(canonicalPath);
348 public override void Release()
356 channel.Unlock(0, channel.Length);
360 lock_Renamed =
false;
377 LOCK_HELD.Remove(path.FullName);
383 if (System.IO.File.Exists(path.FullName))
385 System.IO.File.Delete(path.FullName);
388 else if (System.IO.Directory.Exists(path.FullName))
390 System.IO.Directory.Delete(path.FullName);
401 public override bool IsLocked()
413 if (System.IO.File.Exists(path.FullName))
416 tmpBool = System.IO.Directory.Exists(path.FullName);
423 bool obtained = Obtain();
428 catch (System.IO.IOException)
435 public override System.String ToString()
437 return "NativeFSLock@" + path;