Beispiel #1
0
        /// <summary>
        /// Удаляет указанный файл.
        /// </summary>
        /// <param name="filePath">Путь, указывающий удаляемый файл.</param>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="FileLockedException"></exception>
        /// <exception cref="InvalidPathException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public void DeleteFile(string filePath)
        {
            MethodArgumentValidator.ThrowIfStringIsNullOrEmpty(filePath, "filePath");

            if (_pathBuilder.PointsToRoot(filePath))
            {
                throw new InvalidPathException("Не получилось удалить файл: нельзя удалить корень файловой системы.");
            }

            try
            {
                string fileName;

                var parentFolderResolvingResult = _nodeResolver.ResolveParentFolderNodeByPath(filePath, out fileName);

                ReadOnlyCollection <FileNode> files;
                FolderNode folderToRemoveFileFrom = parentFolderResolvingResult.ResolvedNode;
                FileNode   fileToRemove           = ResolveFileNodeThrowingUserFriendlyErrors(filePath, parentFolderResolvingResult, fileName, "удалить", out files);

                // очистка всех данных.
                using (var dataStream = this.OpenFileForWriting(filePath))
                {
                    dataStream.Truncate();
                }

                // TODO: поглядеть на исключения этого метода.
                _blockReferenceListsEditor.TakeOutABlockFromBlockReferenceList(folderToRemoveFileFrom, fileToRemove.DiskBlockIndex, folderToRemoveFileFrom.FileReferencesStreamDefinition);

                _freeBlockManager.MarkBlockAsFree(fileToRemove.DiskBlockIndex);
                _freeBlockManager.MarkBlockAsFree(fileToRemove.FileContentsStreamDefinition.ContentsBlockIndex);

                _folderEnumeratorRegistry.InvalidateEnumeratorsFor(parentFolderResolvingResult.FoldersPassedWhileResolving);
                _folderEnumeratorRegistry.InvalidateEnumeratorsForFolder(parentFolderResolvingResult.ResolvedNode.Id);
            }
            catch (CannotResolvePathException)
            {
                throw new FileNotFoundException("Не удалось найти файл {0}".FormatWith(filePath));
            }
            catch (InvalidPathException)
            {
                throw new FileNotFoundException("Не удалось найти файл {0}".FormatWith(filePath));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="freeBlockManager"></param>
        /// <param name="blocksToFree"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="BlockNotOccupiedException"></exception>
        public static void MarkBlocksAsFree(this IFreeBlockManager freeBlockManager, IEnumerable <int> blocksToFree)
        {
            if (freeBlockManager == null)
            {
                throw new ArgumentNullException("freeBlockManager");
            }
            if (blocksToFree == null)
            {
                throw new ArgumentNullException("blocksToFree");
            }

            foreach (int blockIndex in blocksToFree)
            {
                freeBlockManager.MarkBlockAsFree(blockIndex);
            }
        }