Example #1
0
        public string DeleteTree(string currentDirectory, string directory)
        {
            currentDirectory = NormalizeCurrentDirectory(currentDirectory);
            if (!PathUtils.IsAbsolutePath(directory))
            {
                directory = PathUtils.CombinePath(currentDirectory, directory);
            }

            string[] directoryParts = PathUtils.SplitPath(directory);

            IFileSystemItem currentItem = this.root;

            for (int i = 0; i < directoryParts.Length; i++)
            {
                currentItem = currentItem.ChildItems.FirstOrDefault(item => FileSystemItemNameComparer.Equals(item.Name, directoryParts[i]));
                if (currentItem is null)
                {
                    throw new FileSystemConsoleException("Destination path is not exists.");
                }
            }

            if (currentItem.Kind != FileSystemItemKind.Directory)
            {
                throw new FileSystemConsoleException("Destination path is not a directory.");
            }

            if (currentItem.HasLocks())
            {
                throw new FileSystemConsoleException("Directory or its subdirectories contain one or more locked files.");
            }

            currentItem.Parent.RemoveChildDirectoryWithTree(currentItem.Name);

            return(directory);
        }
Example #2
0
        private void CopyOrMoveInternal(
            string currentDirectory,
            string sourcePath,
            string destPath,
            Action <IFileSystemItem, IFileSystemItem> action)
        {
            currentDirectory = NormalizeCurrentDirectory(currentDirectory);

            if (!PathUtils.IsAbsolutePath(sourcePath))
            {
                sourcePath = PathUtils.CombinePath(currentDirectory, sourcePath);
            }

            if (!PathUtils.IsAbsolutePath(destPath))
            {
                destPath = PathUtils.CombinePath(currentDirectory, destPath);
            }

            string[] sourcePathParts = PathUtils.SplitPath(sourcePath);

            IFileSystemItem sourcePathCurrentItem = this.root;

            for (int i = 0; i < sourcePathParts.Length; i++)
            {
                sourcePathCurrentItem = sourcePathCurrentItem.ChildItems.FirstOrDefault(item => FileSystemItemNameComparer.Equals(item.Name, sourcePathParts[i]));
                if (sourcePathCurrentItem is null)
                {
                    throw new FileSystemConsoleException("Source path is not exists.");
                }
            }

            if (sourcePathCurrentItem.Kind != FileSystemItemKind.Directory && sourcePathCurrentItem.Kind != FileSystemItemKind.File)
            {
                throw new FileSystemConsoleException(Invariant($"'{sourcePath}' is not a directory or a file."));
            }

            if (sourcePathCurrentItem.Kind == FileSystemItemKind.File)
            {
                if (sourcePathCurrentItem.IsLocked)
                {
                    throw new FileSystemConsoleException(Invariant($"File '{sourcePath}' is locked."));
                }
            }

            string[] destPathParts = PathUtils.SplitPath(destPath);

            IFileSystemItem destPathCurrentItem = this.root;

            for (int i = 0; i < destPathParts.Length; i++)
            {
                destPathCurrentItem = destPathCurrentItem.ChildItems.FirstOrDefault(item => FileSystemItemNameComparer.Equals(item.Name, destPathParts[i]));
                if (destPathCurrentItem is null)
                {
                    throw new FileSystemConsoleException("Destination path is not exists.");
                }
            }

            if (destPathCurrentItem.Kind != FileSystemItemKind.Volume && destPathCurrentItem.Kind != FileSystemItemKind.Directory)
            {
                throw new FileSystemConsoleException(Invariant($"'{destPath}' is not a volume or a directory."));
            }

            if (sourcePathCurrentItem == destPathCurrentItem)
            {
                throw new FileSystemConsoleException("Source path and destination path must be not equal.");
            }

            if (sourcePathCurrentItem.Parent == destPathCurrentItem)
            {
                throw new FileSystemConsoleException("Source path cannot be copied or moved to its parent.");
            }

            if (sourcePathCurrentItem.Kind == FileSystemItemKind.Directory)
            {
                IFileSystemItem destPathCurrentItemParent = destPathCurrentItem.Parent;
                while (!(destPathCurrentItemParent is null))
                {
                    if (destPathCurrentItemParent == sourcePathCurrentItem)
                    {
                        throw new FileSystemConsoleException("Source directory cannot be a parent of the dest directory.");
                    }

                    destPathCurrentItemParent = destPathCurrentItemParent.Parent;
                }
            }

            if (sourcePathCurrentItem.HasLocks())
            {
                throw new FileSystemConsoleException("Source path contain one or more locked files and cannot be copied or moved.");
            }

            action(sourcePathCurrentItem, destPathCurrentItem);
        }