/// <summary>
 /// Sets the information that the vItem was changed. If SetLastAccess=true, the information that the vItem was accessed is also set.
 /// </summary>
 /// <param name="vItem"></param>
 /// <param name="setLastAccess"></param>
 public static void SetLastWrite(IFuserFilesystemItem vItem, bool setLastAccess)
 {
     try {
         if (vItem == null)
         {
             return;
         }
         if (vItem.Filelock == null)
         {
             return;
         }
         FileLastAccessControl flac = vItem.Filelock.getLastAccessControl;
         if (flac == null)
         {
             return;
         }
         lock (flac) {
             flac.SetLastWrite();
             if (setLastAccess)
             {
                 flac.SetLastAccess();
             }
         }
     } catch {
         return;
     }
 }
        /// <summary>
        /// Converts a passed FileItem into a file information structure. Exceptions can be thrown.
        /// </summary>
        /// <param name="vItem"></param>
        /// <returns></returns>
        public static FuserFileInformation convertFileInformation(IFuserFilesystemItem vItem)
        {
            FuserFileInformation fileInfo = new FuserFileInformation();

            convertFileInformation(vItem, fileInfo);
            return(fileInfo);
        }
Example #3
0
        private Win32Returncode pLastErrorCode; // always returns an error code that best fits.

        public PathResolver(IFuserFilesystemDirectory root, string path)
        {
            this.pPath          = null;
            this.pFileitem      = null;
            this.pPathInvalid   = true;
            this.pLastErrorCode = Win32Returncode.DEFAULT_UNKNOWN_ERROR;

            resolve(root, path);
        }
        /// <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));
        }
Example #5
0
        public void MoveTo(IFuserFilesystemItem item, IFuserFilesystemDirectory destination, string newname)
        {
            if (item is SimRoot)
            {
                throw new Exception("Access error");
            }
            if (item is SimFile)
            {
                SimFile vFile = (SimFile)item;
                if (vFile.Name != newname)
                {
                    vFile.SetNewName(newname);
                }
            }

            if (item is SimSubdirectory)
            {
                SimSubdirectory vDir = (SimSubdirectory)item;
                if (vDir.Name != newname)
                {
                    vDir.SetNewName(newname);
                }
            }

            if (destination == null)
            {
                return;
            }



            if (destination is SimSubdirectory)
            {
                SimSubdirectory vDstDir = (SimSubdirectory)destination;
                directoryContent.Remove(item);
                vDstDir.AddItem(item);
                return;
            }

            if (destination is SimRoot)
            {
                SimRoot vDstRoot = (SimRoot)destination;
                directoryContent.Remove(item);
                vDstRoot.AddItem(item);
                return;
            }



            throw new Exception("Mode not supported");
        }
Example #6
0
 public void Delete(IFuserFilesystemItem item)
 {
     if (!directoryContent.Remove(item))
     {
         throw new Exception("File Not found");
     }
     else
     {
         if (item is SimFile)
         {
             SimFile vFile = (SimFile)item;
             vFile.destroy();
         }
     }
 }
        public FileHandler(string filename)
        {
            this.pFilename  = filename;
            this.curItem    = null;
            this.curDir     = null;
            this.UseOPLocks = true;// by default, OPLocks must be checked

            this.pIsOpenRead  = false;
            this.pIsOpenWrite = false;
            this.pData        = null;

            this.pLocks = new List <FileLockItem>();
            this.pLocks.Clear();

            this.pFileLastAccess = new FileLastAccessControl();
        }
        /// <summary>
        /// Converts a passed FileItem into a file information structure. Exceptions can be thrown.
        /// </summary>
        /// <param name="FuserFileItem"></param>
        /// <returns></returns>
        public static void convertFileInformation(IFuserFilesystemItem vItem, FuserFileInformation overwriteFileInfo)
        {
            lock (vItem) {
                overwriteFileInfo.Filename       = vItem.Name;
                overwriteFileInfo.CreationTime   = vItem.CreationTime;
                overwriteFileInfo.LastAccessTime = vItem.LastAccessTime;
                overwriteFileInfo.LastWriteTime  = vItem.LastWriteTime;

                overwriteFileInfo.Attributes = 0;
                if (vItem.isArchive)
                {
                    overwriteFileInfo.Attributes |= FileAttributes.Archive;
                }
                if (vItem.isReadOnly)
                {
                    overwriteFileInfo.Attributes |= FileAttributes.ReadOnly;
                }
                if (vItem.isHidden)
                {
                    overwriteFileInfo.Attributes |= FileAttributes.Hidden;
                }
                if (vItem.isSystem)
                {
                    overwriteFileInfo.Attributes |= FileAttributes.System;
                }


                if (vItem is IFuserFilesystemDirectory)
                {
                    overwriteFileInfo.Attributes |= FileAttributes.Directory;
                    overwriteFileInfo.Length      = 0;
                }
                else
                {
                    if (overwriteFileInfo.Attributes == 0)
                    {
                        overwriteFileInfo.Attributes = FileAttributes.Normal;
                    }

                    IFuserFilesystemFile file = (IFuserFilesystemFile)vItem;
                    overwriteFileInfo.Length = file.Length;
                }
            }
        }
        /// <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>
        /// Release a filelock
        /// </summary>
        /// <param name="fileHandle"></param>
        /// <param name="item"></param>
        public static void FilelockRelease(FileHandler fileHandle, IFuserFilesystemItem item)
        {
            try {
                if (fileHandle == null || item == null)
                {
                    return;
                }

                FileLockManager lockMgr = item.Filelock;
                FileLockItem[]  locks   = null;


                lock (fileHandle) {
                    locks = fileHandle.GetFileLockList();
                }

                if (locks != null)
                {
                    IFuserFilesystemDirectory dirItem = null;
                    if (item is IFuserFilesystemDirectory)
                    {
                        dirItem = (IFuserFilesystemDirectory)item;
                    }


                    lock (lockMgr) {
                        foreach (FileLockItem lItem in locks)
                        {
                            lockMgr.UnregisterLock(lItem);
                            if (dirItem != null)
                            {
                                ReleaseBlockDeletePermission(dirItem, lItem);
                            }
                        }
                    }
                }
            } catch {
                return;
            }
        }
Example #11
0
 public bool DeletionAllow(IFuserFilesystemItem item)
 {
     return(true);
 }
        /// <summary>
        /// Checks whether the specified area has been locked by another file handle.
        /// </summary>
        /// <param name="fileHandle"></param>
        /// <param name="item"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool FileOPLockCheck(FileHandler fileHandle, IFuserFilesystemItem item, long offset, long length)
        {
            try {
                if (fileHandle == null)
                {
                    return(false);
                }
                if (!fileHandle.OPLockActive)
                {
                    return(true); // do not check any locks, according to the filehandle locks are not possible.
                }

                if (item == null)
                {
                    return(false);
                }
                if (offset < 0)
                {
                    return(true);
                }
                if (length <= 0)
                {
                    return(true);
                }


                FileLockManager lockMgr     = item.Filelock;
                FileLockItem[]  locksHandle = null; // list of locks from this handle.
                FileLockItem[]  locksFile   = null; // list of locks from this file.


                lock (fileHandle) {
                    locksHandle = fileHandle.GetFileLockList();
                }

                lock (lockMgr) {
                    locksFile = lockMgr.GetFileLockList();
                }

                if (locksFile == null)
                {
                    return(true);
                }

                List <FileLockItem> locks = new List <FileLockItem>(locksFile);
                if (locksHandle != null)
                {
                    foreach (FileLockItem lItem in locksHandle)
                    {
                        locks.Remove(lItem);
                    }
                }

                foreach (FileLockItem lItem in locks)
                {
                    if (lItem.IsOpLock)
                    {
                        if (!lItem.CheckOPLockRange(offset, length))
                        {
                            return(false);// violation occurred!
                        }
                    }
                }
                return(true);
            } catch {
                return(false);
            }
        }
Example #13
0
 public void AddItem(IFuserFilesystemItem n)
 {
     directoryContent.Add(n);
 }
Example #14
0
        private void resolve(IFuserFilesystemDirectory root, string path)
        {
            FuserPathResolveResult vpr = null;

            try {
                vpr        = ResolvePath(root, path);
                this.pPath = vpr;

                if (vpr.returncode != Win32Returncode.SUCCESS)
                {
                    this.pPathInvalid   = true;
                    this.pLastErrorCode = vpr.returncode;
                    return;
                }
            } catch {
                // should never occur
                this.pPathInvalid   = true;
                this.pLastErrorCode = Win32Returncode.ERROR_PATH_NOT_FOUND;
                this.pPath          = null;
                return;
            }

            if (vpr != null)
            {
                if (vpr.HasError)
                {
                    vpr = null;
                }
            }

            if (vpr != null)
            {
                if (vpr.returncode != Win32Returncode.SUCCESS)
                {
                    vpr = null;
                }
            }


            if (vpr == null)
            {
                this.pPathInvalid   = true;
                this.pLastErrorCode = Win32Returncode.ERROR_PATH_NOT_FOUND;
                this.pPath          = null;
            }
            else
            {
                this.pPathInvalid   = false;
                this.pLastErrorCode = Win32Returncode.DEFAULT_UNKNOWN_ERROR;
                this.pFileitem      = null;

                if (vpr.currentDirectory == root && vpr.itemname == "")
                {
                    this.pFileitem = root;
                }
                else
                {
                    if (vpr.itemname == "")
                    {
                        // path not found
                        this.pLastErrorCode = Win32Returncode.ERROR_PATH_NOT_FOUND;
                        this.pPathInvalid   = true;
                    }
                    else
                    {
                        try {
                            lock (vpr.currentDirectory) {
                                this.pFileitem = vpr.currentDirectory.GetItem(vpr.itemname);
                            }
                        } catch {
                            this.pFileitem = null;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Writes the LastAccessed and LastModified information, if they have been changed, to the file. For files, the archive attribute is also set if necessary.
        /// </summary>
        /// <param name="vItem"></param>
        public static void WriteLastWriteAndAccess(IFuserFilesystemItem vItem)
        {
            try {
                bool     las   = false;
                bool     lws   = false;
                bool     IsDir = false;
                DateTime lat   = DateTime.MinValue;
                DateTime lwt   = DateTime.MinValue;

                if (vItem == null)
                {
                    return;
                }
                if (vItem.Filelock == null)
                {
                    return;
                }
                IsDir = (vItem is IFuserFilesystemDirectory);

                FileLastAccessControl flac = vItem.Filelock.getLastAccessControl;
                if (flac == null)
                {
                    return;
                }
                lock (flac) {
                    las = flac.IsLastAccessSet;
                    lws = flac.IsLastWriteSet;

                    if (las)
                    {
                        lat = flac.LastAccessTime;
                    }
                    if (lws)
                    {
                        lwt = flac.LastWriteTime;
                    }

                    if (las || lws)
                    {
                        flac.Rest(); // if there are any changes, reset them all.
                    }
                }

                if (!las && !lws)
                {
                    return; // nothing to do
                }
                lock (vItem) {
                    if (las)
                    {
                        vItem.LastAccessTime = lat;
                    }

                    if (lws)
                    {
                        vItem.LastWriteTime = lwt;
                        if (!IsDir)
                        {
                            vItem.isArchive = true;
                        }
                    }
                }
            } catch {
                return;
            }
        }
Example #16
0
 public void MoveTo(IFuserFilesystemItem item, IFuserFilesystemDirectory destination, string newname)
 {
     throw new NotImplementedException();
 }
Example #17
0
 public void Delete(IFuserFilesystemItem item)
 {
     throw new NotImplementedException();
 }
Example #18
0
 public void DebugAddEntry(IFuserFilesystemItem n)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Checks whether the selected item can be deleted. Only the FileLocks are taken into account. Required for Move and Delete
        /// </summary>
        /// <param name="fileHandle"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool DeletionAllow(FileHandler fileHandle, IFuserFilesystemItem item)
        {
            try {
                if (fileHandle == null)
                {
                    return(false);
                }
                if (item == null)
                {
                    return(false);
                }

                List <FileLockItem> locks;
                FileLockManager     lockMgr          = item.Filelock;
                FileLockItem[]      locksHandle      = null; // list with locks for the current fileHandle
                FileLockItem[]      locksFile        = null; // list with locks for the current file
                FileLockItem[]      locksBlockDelete = null; // list with deletion-locks for the current file

                lock (fileHandle) {
                    locksHandle = fileHandle.GetFileLockList();
                }

                lock (lockMgr) {
                    locksFile        = lockMgr.GetFileLockList();
                    locksBlockDelete = lockMgr.GetBlockDeletePermissionList();
                }

                if (locksFile == null)
                {
                    return(true); // no locks found, file not in use
                }


                // check: deletion block
                if (locksBlockDelete != null)
                {
                    locks = new List <FileLockItem>(locksBlockDelete);
                    if (locksHandle != null)
                    {
                        foreach (FileLockItem lItem in locksHandle)
                        {
                            locks.Remove(lItem);
                        }
                    }
                    if (locks.Count != 0)
                    {
                        return(false);// deletion was blocked
                    }
                }
                // check: deletion block


                locks = new List <FileLockItem>(locksFile);
                if (locksHandle != null)
                {
                    foreach (FileLockItem lItem in locksHandle)
                    {
                        locks.Remove(lItem);
                    }
                }

                bool del = false;
                foreach (FileLockItem lck in locks)
                {
                    if (!lck.Share.HasFlag(FileShare.Delete))
                    {
                        del = true;
                    }
                }

                if (del == false)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            } catch {
                return(false);
            }
        }