public void InitializeAsyncLocks() { _lockRequestQueue = new ConcurrentQueue <QueueItem>(); try { FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(_path)); // Set up lock file watcher. Note that depending on how the file is accessed the file watcher may generate multiple events. _lockFileWatcher = new FileSystemWatcher(Path.GetDirectoryName(_path), Path.GetFileName(_path)); _lockFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName; _lockFileWatcher.Changed += OnLockReleasedInternal; _lockFileWatcher.Deleted += OnLockReleasedInternal; _lockFileWatcher.EnableRaisingEvents = true; } catch (UnauthorizedAccessException) { // If not authorized to create the locks directory, do nothing for now. } }
public bool Lock() { try { FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(_path)); _lockStream = FileSystemHelpers.OpenFile(_path, FileMode.Create, FileAccess.Write, FileShare.None); OnLockAcquired(); return(true); } catch (Exception ex) { TraceIfUnknown(ex); return(false); } }
public bool Lock() { try { FileSystemHelpers.EnsureDirectory(_directory); if (Interlocked.Exchange(ref _lockStream, new FileStream(_path, FileMode.Create, FileAccess.Write, FileShare.None)) == null) { TraceLock("Aquired Lock"); _lockStream.WriteByte(0); return(true); } return(false); } catch { TraceLock("Aquire Lock failed"); return(false); } }
public bool Lock(string operationName) { Stream lockStream = null; var ignoreCloseStreamException = false; try { FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(_path)); lockStream = FileSystemHelpers.OpenFile(_path, FileMode.Create, FileAccess.Write, FileShare.Read); WriteLockInfo(operationName, lockStream); OnLockAcquired(); _lockStream = lockStream; lockStream = null; if (_traceLock) { _traceFactory.GetTracer().Trace($"LockFile '{_path}' acquired"); } return(true); } catch (UnauthorizedAccessException) { if (!_ensureLock) { // if it is ReadOnly file system, we will skip the lock // which will enable all read action // for write action, it will fail with UnauthorizedAccessException when perform actual write operation // There is one drawback, previously for write action, even acquire lock will fail with UnauthorizedAccessException, // there will be retry within given timeout. so if exception is temporary, previous`s implementation will still go thru. // While right now will end up failure. But it is a extreme edge case, should be ok to ignore. ignoreCloseStreamException = FileSystemHelpers.IsFileSystemReadOnly(); return(ignoreCloseStreamException); } } catch (IOException ex) { if (!_ensureLock) { // if not enough disk space, no one has the lock. // let the operation thru and fail where it would try to get the file ignoreCloseStreamException = ex.Message.Contains(NotEnoughSpaceText); return(ignoreCloseStreamException); } } catch (Exception ex) { TraceIfUnknown(ex); } finally { if (lockStream != null) { OperationManager.CriticalExecute("LockFile.Lock.Finally", () => lockStream.Close(), _ => !ignoreCloseStreamException); } } return(false); }