public void MakeSureWriteReadCycleWorksOnFreeSpaceMap()
        {
            Stream stream = new FileStream(@"e:\diskTests.tst", FileMode.Create);
            // new MemoryStream();

            var disk = VirtualDisk.CreateFormattingTheStream(stream, 2048, 15000 * 2048);

            const int numberOfBlocksToMap = 150000;
            var       mapBytes            = new byte[numberOfBlocksToMap];

            var bitArray = new BitArray(mapBytes);

            bitArray.Length = numberOfBlocksToMap;

            var freeBlockManager = new FreeBlockManagerBitArrayBased(bitArray, 22, numberOfBlocksToMap);

            freeBlockManager.AcquireFreeBlocks(7000);
            freeBlockManager.AcquireFreeBlocks(24);

            var bitmapStore = new FreeSpaceBitmapStore(disk, VirtualDiskFormatter.FreeSpaceStartingBlockIndex);

            var bytes = new byte[SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(numberOfBlocksToMap, Constants.NumberOfBitsInByte)];

            bitArray.CopyTo(bytes, 0);
            bitmapStore.WriteMap(bytes, numberOfBlocksToMap);

            int bitmapSize;
            var bytesRead = bitmapStore.ReadMap(out bitmapSize);

            Assert.AreEqual(numberOfBlocksToMap, bitmapSize);
            CollectionAssert.AreEqual(bytes, bytesRead);
        }
        public void Format(IVirtualDisk virtualDisk, FileSystemNodeStorage fileSystemNodeStorage)
        {
            var numberOfBlocksToMap = virtualDisk.NumberOfBlocks - FileSystemHeaderReservedBlocks;

            int numberOfBytesNeededToStoreTheMap =
                SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(numberOfBlocksToMap, Constants.NumberOfBitsInByte);

            int numberOfBlocksNeededToStoreTheMap =
                SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData(numberOfBytesNeededToStoreTheMap,
                                                                               virtualDisk.BlockSizeInBytes);

            numberOfBlocksToMap -= numberOfBlocksNeededToStoreTheMap;

            var freeSpaceBitmap = new BitArray(numberOfBytesNeededToStoreTheMap);

            freeSpaceBitmap.Length = numberOfBlocksToMap;

            var store = new FreeSpaceBitmapStore(virtualDisk, VirtualDiskFormatter.FreeSpaceStartingBlockIndex);

            var freeBlockManagerBitArrayBased =
                new FreeBlockManagerBitArrayBased(freeSpaceBitmap, FreeSpaceStartingBlockIndex + numberOfBlocksNeededToStoreTheMap, numberOfBlocksToMap);

            var freeBlockManager = new FreeBlockManagerDiskWriting(store, freeBlockManagerBitArrayBased);

            var freeBlocks = freeBlockManager.AcquireFreeBlocks(3);

            int rootBlockIndex            = freeBlocks[0];
            int rootFileReferencesBlock   = freeBlocks[1];
            int rootFolderReferencesBlock = freeBlocks[2];

            var fileContentsStreamDefinition   = new DataStreamDefinition(rootFileReferencesBlock, 0);
            var folderContentsStreamDefinition = new DataStreamDefinition(rootFolderReferencesBlock, 0);

            var rootNode = new FolderNode("root", Guid.NewGuid(), rootBlockIndex, 0, fileContentsStreamDefinition, folderContentsStreamDefinition, DateTime.UtcNow, Guid.NewGuid());

            fileSystemNodeStorage.WriteNode(rootNode);

            fileSystemNodeStorage.WriteFileSystemHeader(new FileSystemHeader(rootBlockIndex, new Version(1, 0, 0, 0)), FileSystemHeaderBlockIndex);
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="disk"></param>
        /// <param name="namesComparer"></param>
        /// <param name="nodeResolver"></param>
        /// <param name="pathBuilder"></param>
        /// <param name="namesValidator"></param>
        /// <param name="pathValidator"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InconsistentDataDetectedException"></exception>
        internal static VirtualFileSystem CreateFromDisk(IVirtualDisk disk, IEqualityComparer <string> namesComparer, NodeResolver nodeResolver, PathBuilder pathBuilder, IFileSystemArtifactNamesValidator namesValidator, IPathValidator pathValidator)
        {
            if (disk == null)
            {
                throw new ArgumentNullException("disk");
            }
            if (namesComparer == null)
            {
                throw new ArgumentNullException("namesComparer");
            }
            if (nodeResolver == null)
            {
                throw new ArgumentNullException("nodeResolver");
            }
            if (pathBuilder == null)
            {
                throw new ArgumentNullException("pathBuilder");
            }
            if (namesValidator == null)
            {
                throw new ArgumentNullException("namesValidator");
            }
            if (pathValidator == null)
            {
                throw new ArgumentNullException("pathValidator");
            }

            VirtualFileSystemInfo fileSystemInfo;
            var fileSystemNodeStorage = new FileSystemNodeStorage(disk);

            const int headerBlockIndex = VirtualDiskFormatter.FileSystemHeaderBlockIndex;
            const int freeBlockBitmapStartingBlockIndex = VirtualDiskFormatter.FreeSpaceStartingBlockIndex;

            var header = fileSystemNodeStorage.ReadFileSystemHeader(headerBlockIndex);

            fileSystemInfo = new VirtualFileSystemInfo(header.Version, disk.BlockSizeInBytes, header.RootBlockOffset, freeBlockBitmapStartingBlockIndex);

            var freeSpaceBitmapStore = new FreeSpaceBitmapStore(disk, VirtualDiskFormatter.FreeSpaceStartingBlockIndex);

            int bitmapSize;
            var freeSpaceMap = freeSpaceBitmapStore.ReadMap(out bitmapSize);

            var freeSpaceBitArray = new BitArray(freeSpaceMap)
            {
                Length = bitmapSize
            };

            var freeBlockManagerBitArrayBased = new FreeBlockManagerBitArrayBased(freeSpaceBitArray,
                                                                                  fileSystemInfo.FirstNonReservedDiskBlockIndex,
                                                                                  bitmapSize);

            IFreeBlockManager freeBlockManager = new FreeBlockManagerDiskWriting(freeSpaceBitmapStore, freeBlockManagerBitArrayBased);

            IFolderEnumeratorRegistry folderEnumeratorRegistry = new FolderEnumeratorRegistry();

            IFileSystemObjectLockingManager lockingManager = new FileSystemObjectLockingManager();

            var blockReferenceEditor = new BlockReferenceListsEditor(disk, freeBlockManager, fileSystemNodeStorage);

            //Note: много общих коллабораторов у трех классов. Недорефакторено.

            var fileManager   = new FileManager(disk, fileSystemNodeStorage, namesComparer, nodeResolver, freeBlockManager, folderEnumeratorRegistry, lockingManager, blockReferenceEditor, pathBuilder, namesValidator, pathValidator);
            var folderManager = new FolderManager(fileSystemNodeStorage, namesComparer, nodeResolver, freeBlockManager, folderEnumeratorRegistry, lockingManager, blockReferenceEditor, pathBuilder, namesValidator, pathValidator);

            return(new VirtualFileSystem(disk, fileSystemInfo, fileSystemNodeStorage, namesComparer, nodeResolver, freeBlockManager, folderEnumeratorRegistry, blockReferenceEditor, pathBuilder, namesValidator, pathValidator, fileManager, folderManager));
        }