Exemplo n.º 1
0
        /// <summary>
        /// Deletes a given folder from the file system.
        /// </summary>
        /// <param name="virtualFolderPath">The qualified path of the folder to be created.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="virtualFolderPath"/>
        /// is a null reference.</exception>
        /// <exception cref="ResourceAccessException">In case of invalid or prohibited
        /// resource access.</exception>
        /// <exception cref="VirtualResourceNotFoundException">If no file exists under the submitted
        /// <paramref name="virtualFolderPath"/>.</exception>
        public override void DeleteFolder(string virtualFolderPath)
        {
            string absolutePath;
            var    folderInfo = GetFolderInfoInternal(virtualFolderPath, true, out absolutePath);

            //do not delete the root
            if (folderInfo.IsRootFolder)
            {
                VfsLog.Warn("Blocked attempt to delete root folder.");
                throw new ResourceAccessException(String.Format("Cannot delete root folder '{0}'", virtualFolderPath));
            }

            DirectoryInfo dir = new DirectoryInfo(absolutePath);

            //do not delete drives
            if (dir.Parent == null)
            {
                VfsLog.Warn("Blocked attempt to delete drive by path [{0}]", virtualFolderPath);
                throw new ResourceAccessException("Cannot delete drive " + virtualFolderPath);
            }

            //delete the folder
            try
            {
                dir.Delete(true);
            }
            catch (Exception e)
            {
                VfsLog.Warn(e, "Error while trying to delete folder '{0}' from file system", virtualFolderPath);
                string msg = String.Format("Could not delete file '{0}'", virtualFolderPath);
                throw new ResourceAccessException(msg);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deletes a given file from the file system.
        /// </summary>
        /// <param name="virtualFilePath">The qualified path of the file to be created.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="virtualFilePath"/>
        /// is a null reference.</exception>
        /// <exception cref="ResourceAccessException">In case of invalid or prohibited
        /// resource access.</exception>
        /// <exception cref="VirtualResourceNotFoundException">If no file exists under the submitted
        /// <paramref name="virtualFilePath"/>.</exception>
        public override void DeleteFile(string virtualFilePath)
        {
            string absolutePath;

            GetFileInfoInternal(virtualFilePath, true, out absolutePath);

            //delete the folder
            try
            {
                File.Delete(absolutePath);
            }
            catch (Exception e)
            {
                VfsLog.Warn(e, "Error while trying to delete file '{0}' from file system", virtualFilePath);
                string msg = String.Format("Could not delete file '{0}'", virtualFilePath);
                throw new ResourceAccessException(msg);
            }
        }
Exemplo n.º 3
0
        private VirtualFolderInfo PerformFolderOperation(string virtualFolderPath, string destinationPath, FileOperation operation)
        {
            if (virtualFolderPath == null)
            {
                throw new ArgumentNullException("virtualFolderPath");
            }
            if (destinationPath == null)
            {
                throw new ArgumentNullException("destinationPath");
            }

            //get folder info for source and destination, thus validating the scope of both
            string absoluteSource;
            var    sourceFolder = GetFolderInfoInternal(virtualFolderPath, true, out absoluteSource);
            string absoluteDestination;

            GetFolderInfoInternal(destinationPath, false, out absoluteDestination);

            string operationName = operation == FileOperation.Move ? "move" : "copy";

            if (sourceFolder.IsRootFolder)
            {
                string msg = String.Format("Cannot {0} root folder (attempted destination: '{1}').", operationName, destinationPath);
                VfsLog.Debug(msg);
                throw new ResourceAccessException(msg);
            }

            if (String.Equals(absoluteSource, absoluteDestination, StringComparison.InvariantCultureIgnoreCase))
            {
                string msg = String.Format("Cannot {0} folder to '{1}' - source and destination are the same.", operationName, destinationPath);
                VfsLog.Debug(msg);
                throw new ResourceAccessException(msg);
            }

            var sourceDir = new DirectoryInfo(absoluteSource);

            if (sourceDir.IsParentOf(absoluteDestination))
            {
                string msg = String.Format("Cannot {0} folder '{1}' to '{2}' - destination folder is a child of the folder.", operationName, virtualFolderPath, destinationPath);
                VfsLog.Debug(msg);
                throw new ResourceAccessException(msg);
            }

            if (Directory.Exists(absoluteDestination))
            {
                string msg = "Cannot {0} folder '{1}' to '{2}' - the destination folder already exists.";
                msg = String.Format(msg, operationName, virtualFolderPath, destinationPath);
                VfsLog.Debug(msg);
                throw new ResourceOverwriteException(msg);
            }


            try
            {
                switch (operation)
                {
                case FileOperation.Move:
                    Directory.Move(absoluteSource, absoluteDestination);
                    break;

                case FileOperation.Copy:
                    PathUtil.CopyDirectory(absoluteSource, absoluteDestination, false);
                    break;

                default:
                    VfsLog.Fatal("Unsupported file operation received: {0}", operation);
                    throw new ArgumentOutOfRangeException("operation");
                }

                return(GetFolderInfo(absoluteDestination));
            }
            catch (Exception e)
            {
                string msg = String.Format("An error occurred while trying to {0} directory '{1}' to '{2}'.",
                                           operationName, virtualFolderPath, destinationPath);
                VfsLog.Warn(e, msg);
                throw new ResourceAccessException(msg, e);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new folder in the file system.
        /// </summary>
        /// <param name="parentFolderPath">The qualified name of the designated parent folder, which
        /// needs to exists, and provide write access.</param>
        /// <param name="folderName">The name of the folder to be created.</param>
        /// <returns>A <see cref="VirtualFileInfo"/> instance which represents
        /// the created folder.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="parentFolderPath"/>
        /// is a null reference.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="folderName"/>
        /// is a null reference.</exception>
        /// <exception cref="ResourceAccessException">In case of invalid or prohibited
        /// resource access.</exception>
        /// <exception cref="VirtualResourceNotFoundException">If no folder exists that
        /// matches the submitted <paramref name="parentFolderPath"/>.</exception>
        /// <exception cref="ResourceOverwriteException">If the folder already exists on the file
        /// system.</exception>
        public override VirtualFolderInfo CreateFolder(string parentFolderPath, string folderName)
        {
            if (parentFolderPath == null)
            {
                throw new ArgumentNullException("parentFolderPath");
            }
            if (folderName == null)
            {
                throw new ArgumentNullException("folderName");
            }

            string absoluteParentPath;
            var    parent = GetFolderInfoInternal(parentFolderPath, true, out absoluteParentPath);

            if (RootDirectory == null && parent.IsRootFolder)
            {
                VfsLog.Debug("Blocked attempt to create a folder '{0}' at system root.", folderName);
                throw new ResourceAccessException("Folders cannot be created at the system root.");
            }

            //create path of the child
            string childPath = PathUtil.GetAbsolutePath(folderName, new DirectoryInfo(absoluteParentPath));

            //make sure the folder name is not a relative path that points outside the scope
            if (RootDirectory != null && !RootDirectory.IsParentOf(childPath))
            {
                string msg = "Blocked attempt to create folder outside of root through with parent '{0}' and folder name '{1}'";
                VfsLog.Warn(msg, absoluteParentPath, folderName);

                throw new ResourceAccessException("Invalid file path: " + folderName);
            }

            var directory = new DirectoryInfo(childPath);

            if (directory.Exists)
            {
                //log and create exception if the directory already exists
                VfsLog.Debug("Blocked attempt to recreate directory '{0}'", directory.FullName);
                string relativePath = PathUtil.GetRelativePath(childPath, RootDirectory);
                string msg          = String.Format("The folder '{0}' already exists.", relativePath);
                throw new ResourceOverwriteException(msg);
            }

            try
            {
                //create directory
                directory.Create();
            }
            catch (Exception e)
            {
                const string msg = "Exception occurred when trying to create new folder '{0}' for parent '{1}'";
                VfsLog.Debug(e, msg, folderName, parent.FullName);

                throw new ResourceAccessException("Could not create folder", e);
            }

            var folder = directory.CreateFolderResourceInfo();

            //adjust and return
            if (UseRelativePaths)
            {
                folder.MakePathsRelativeTo(RootDirectory);
            }
            return(folder);
        }