public void AddFileLock(FileLockItem lockItem)
 {
     if (lockItem == null)
     {
         return;
     }
     else
     {
         this.pLocks.Add(lockItem);
     }
 }
 public void RemoveFileLock(FileLockItem lockItem)
 {
     if (lockItem == null)
     {
         return;
     }
     else
     {
         this.pLocks.Remove(lockItem);
     }
 }
        /// <summary>
        /// Sets the specified accesses and shares to the file. If false is returned, the lock cannot be set.
        /// </summary>
        /// <param name="fileHandle"></param>
        /// <param name="item"></param>
        /// <param name="access"></param>
        /// <param name="share"></param>
        /// <returns></returns>
        public static bool FilelockSet(FileHandler fileHandle, IFuserFilesystemItem item, FuserFileAccess access, FileShare share)
        {
            FileLockItem lItem = null;

            try {
                lItem = new FileLockItem((item is IFuserFilesystemDirectory), (access == FuserFileAccess.Read || access == FuserFileAccess.ReadWrite), (access == FuserFileAccess.Write || access == FuserFileAccess.ReadWrite), share);
            } catch {
                return(false);
            }
            return(FilelockSet(fileHandle, item, lItem));
        }
        private bool CheckShareOPLockIsPermit(FileLockItem otherRule)
        {
            if (this.pIsOpLock != otherRule.IsOpLock)
            {
                return(true); // OPlock must always be valid with another rule, otherwise no OPLocks can be registered.
            }
            if (!this.pIsOpLock)
            {
                return(true); // Should never occur anyway.
            }

            return(CheckOPLockRange(otherRule.OPLockOffset, otherRule.OPLockLength));
        }
        public bool RegisterLock(FileLockItem flock)
        {
            bool isDir = flock.IsDirectory;

            foreach (FileLockItem el in this.fLocks)
            {
                if (el.IsDirectory != flock.IsDirectory)
                {
                    // Locks for directories may only be compared with those of directories.
                    return(false);
                }
                if (!flock.CheckShareRuleIsPermit(el))
                {
                    return(false); // Access violation found
                }
            }
            this.fLocks.Add(flock);
            return(true);
        }
 public void UnregisterLock(FileLockItem flock)
 {
     this.fLocks.Remove(flock);
 }
 public void ReleaseBlockDeletePermission(FileLockItem flock)
 {
     this.BlockDeletePermissionList.Remove(flock);
 }
 public void RegisterBlockDeletePermission(FileLockItem flock)
 {
     this.BlockDeletePermissionList.Add(flock);
 }
        public bool CheckShareRuleIsPermit(FileLockItem otherRule)
        {
            if (pIsDirectory != otherRule.IsDirectory)
            {
                return(false);
            }
            if (this.pIsDirectory)
            {
                return(true); // As long as CreateDirectory and OpenDirectory do not return correct FileShare/FileAccess values, this workaround must remain active.
            }

            if (this.pIsOpLock || otherRule.IsOpLock)
            {
                return(CheckShareOPLockIsPermit(otherRule));
            }

            FileShare shareO = otherRule.Share;

            bool shareReadT  = false;
            bool shareWriteT = false;

            bool shareReadO  = false;
            bool shareWriteO = false;

            if (pShare.HasFlag(FileShare.Read))
            {
                shareReadT = true;
            }
            if (pShare.HasFlag(FileShare.Write))
            {
                shareWriteT = true;
            }
            if (pShare.HasFlag(FileShare.ReadWrite))
            {
                shareReadT = true; shareWriteT = true;
            }

            if (shareO.HasFlag(FileShare.Read))
            {
                shareReadO = true;
            }
            if (shareO.HasFlag(FileShare.Write))
            {
                shareWriteO = true;
            }
            if (shareO.HasFlag(FileShare.ReadWrite))
            {
                shareReadO = true; shareWriteO = true;
            }

            if (shareReadT == false && shareWriteT == false)
            {
                if (otherRule.AccessModeWrite || otherRule.AccessModeRead)
                {
                    return(false); // Authorised Share: none
                }
            }
            if (shareReadO == false && shareWriteO == false)
            {
                if (this.AccessModeWrite || this.AccessModeRead)
                {
                    return(false); // Authorised Share: none
                }
            }


            if (shareReadT == true && shareWriteT == false)
            {
                if (otherRule.AccessModeWrite == true)
                {
                    return(false);
                }
            }
            if (shareReadO == true && shareWriteO == false)
            {
                if (this.AccessModeWrite == true)
                {
                    return(false);
                }
            }


            if (shareReadT == false && shareWriteT == true)
            {
                if (otherRule.AccessModeRead == true)
                {
                    return(false);
                }
            }
            if (shareReadO == false && shareWriteO == true)
            {
                if (this.AccessModeRead == true)
                {
                    return(false);
                }
            }


            if (shareReadT == true && shareWriteT == true)
            {
            }
            if (shareReadO == true && shareWriteO == true)
            {
            }


            return(true);
        }
        /// <summary>
        /// Sets the specified FileLock on the file and saves it in the file handle. Returns whether the setting is successful;
        /// if False is returned, the file is already blocked by another process.
        /// </summary>
        /// <param name="fileHandle"></param>
        /// <param name="fsItem"></param>
        /// <param name="LockItem"></param>
        /// <returns></returns>
        public static bool FilelockSet(FileHandler fileHandle, IFuserFilesystemItem fsItem, FileLockItem LockItem)
        {
            if (LockItem == null || fileHandle == null || fsItem == null)
            {
                return(false);
            }


            try {
                bool            ret     = false;
                FileLockManager lockMgr = fsItem.Filelock;

                if (lockMgr == null)
                {
                    return(false);
                }

                lock (lockMgr) {
                    ret = lockMgr.RegisterLock(LockItem);
                }

                if (ret && fsItem is IFuserFilesystemDirectory && LockItem.IsDirectory && !LockItem.IsOpLock && !LockItem.Share.HasFlag(FileShare.Delete))
                {
                    RegisterBlockDeletePermission((IFuserFilesystemDirectory)fsItem, LockItem);
                }


                if (ret)
                {
                    lock (fileHandle) {
                        fileHandle.AddFileLock(LockItem);
                    }
                }
                else
                {
                    return(false); // access violation occurred
                }
            } catch {
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Removes the lock for deleting directories for all directories (recursive up to the root).
        /// </summary>
        /// <param name="blockDirectory"></param>
        /// <param name="flock"></param>
        private static void ReleaseBlockDeletePermission(IFuserFilesystemDirectory blockDirectory, FileLockItem flock)
        {
            if (blockDirectory == null)
            {
                return; // Abort criterion for recursive method.
            }
            if (flock == null)
            {
                return;
            }

            try {
                FileLockManager lockMgr = blockDirectory.Filelock;
                if (lockMgr == null)
                {
                    return;
                }

                lock (lockMgr) {
                    lockMgr.ReleaseBlockDeletePermission(flock);
                }
            } catch {
                return;
            }

            ReleaseBlockDeletePermission(blockDirectory.Parent, flock);  // recursively reset to root for all
        }