예제 #1
0
파일: LockFile.cs 프로젝트: sathirayee/kudu
        public bool Lock()
        {
            Stream lockStream = null;

            try
            {
                FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(_path));

                lockStream = FileSystemHelpers.OpenFile(_path, FileMode.Create, FileAccess.Write, FileShare.Read);

                WriteLockInfo(lockStream);

                OnLockAcquired();

                _lockStream = lockStream;
                lockStream  = null;

                return(true);
            }
            catch (Exception ex)
            {
                TraceIfUnknown(ex);

                if (lockStream != null)
                {
                    lockStream.Close();
                }

                return(false);
            }
        }
예제 #2
0
        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;

                _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);
        }
예제 #3
0
파일: LockFile.cs 프로젝트: veteranlu/kudu
        public bool Lock()
        {
            Stream lockStream = null;

            try
            {
                FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(_path));

                lockStream = FileSystemHelpers.OpenFile(_path, FileMode.Create, FileAccess.Write, FileShare.Read);

                WriteLockInfo(lockStream);

                OnLockAcquired();

                _lockStream = lockStream;
                lockStream  = null;

                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                // 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 extreem edge case, should be ok to ignore.
                try
                {
                    return(FileSystemHelpers.IsFileSystemReadOnly());
                }
                catch
                {
                    if (lockStream != null)
                    {
                        lockStream.Close();
                    }
                    return(false);
                }
            }
            catch (Exception ex)
            {
                TraceIfUnknown(ex);

                if (lockStream != null)
                {
                    lockStream.Close();
                }

                return(false);
            }
        }
예제 #4
0
        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);
            }
        }