//Returnes Dictionary with pair: block address - byte arr //I dont use real file data. Instead I just fill the blocks for file data with '1' public static Dictionary <int, byte[]> GetDataArr(short[] freeBlocks, int blockSize) { var dataToWrite = new Dictionary <int, byte[]>(); const int di_addr_size = 13, startDataBlocks = di_addr_size - 1; var addressBlocks = BlocksHandler.BlocksForAddress(freeBlocks.Length, blockSize); int blockIndex = startDataBlocks + addressBlocks; for (int i = 0; i < startDataBlocks; ++i) { dataToWrite.Add(freeBlocks[i], WriteBlock(new byte[blockSize])); } //Writes addresses into the following blocks for (int i = startDataBlocks; i < startDataBlocks + addressBlocks; ++i) { dataToWrite.Add(freeBlocks[i], WriteAddrBlock(new byte[blockSize], freeBlocks, blockIndex)); blockIndex += blockSize / 2; } //Writes data into the following blocks for (int i = startDataBlocks + addressBlocks; i < freeBlocks.Length; ++i) { dataToWrite.Add(freeBlocks[i], WriteBlock(new byte[blockSize])); } return(dataToWrite); }
private bool WriteFile(FileDataStorage storage, INode iNode) { using (System.IO.FileStream fs = System.IO.File.OpenRead(Path), temp = new System.IO.FileStream(fs.Name + ".temp", System.IO.FileMode.Create)) { if (iNode.Size / 1024 > 8240) { throw new Exception("File size is too big"); } temp.SetLength(63774720); int nodeNum = DataExtractor.GetINodeNum(storage.INodeMap); FSPartsWriter.WriteSuperblock(temp, storage.Superblock); ByteWriter.CopyData(fs, temp, temp.Position, GetBlockEnd(storage.Superblock.ClusterSize, temp.Position), storage.Superblock.ClusterSize); var blocks = BlocksHandler.GetBlocksArr(storage.Bitmap, iNode.Size, storage.Superblock.ClusterSize); for (int i = 0; i < blocks.Length; ++i) { storage.Bitmap.BitmapValue = BitWorker.TurnBitOn(storage.Bitmap.BitmapValue, blocks[i]); } FSPartsWriter.WriteBitmap(temp, storage.Bitmap); ByteWriter.CopyData(fs, temp, temp.Position, GetBlockEnd((Superblock.UsedBlock + Bitmap.UsedBlock) * storage.Superblock.ClusterSize, temp.Position), storage.Superblock.ClusterSize); storage.INodeMap.BitmapValue = BitWorker.TurnBitOn(storage.INodeMap.BitmapValue, nodeNum); FSPartsWriter.WriteBitmap(temp, storage.INodeMap); ByteWriter.CopyData(fs, temp, temp.Position, GetBlockEnd((Superblock.UsedBlock + Bitmap.UsedBlock + Bitmap.UsedBlock) * storage.Superblock.ClusterSize, temp.Position), storage.Superblock.ClusterSize); var dataDict = BlocksHandler.GetDataArr(blocks, storage.Superblock.ClusterSize); List <int> dataKeys = new List <int>(dataDict.Keys); for (int index = 0; index < iNode.Di_addr.Length && index < dataDict.Count; ++index) { iNode.Di_addr[index] = (short)dataKeys[index]; //To change short for int cause data can be lost } long bytesNum = (nodeNum - 1) * INode.Offset; //How many bytes method need to skip ByteWriter.CopyBytes(fs, temp, temp.Position, bytesNum, storage.Superblock.ClusterSize); FSPartsWriter.WriteINode(temp, iNode); ByteWriter.CopyData(fs, temp, temp.Position, (Superblock.UsedBlock + Bitmap.UsedBlock + Bitmap.UsedBlock + storage.NodeBlocksOffset) * storage.Superblock.ClusterSize - 1, storage.Superblock.ClusterSize); for (int i = 0; i < storage.Bitmap.BitmapValue.Length * 8; ++i) { if (dataKeys.Contains(i)) { ByteWriter.WriteBlock(temp, storage.Superblock.ClusterSize, dataDict[i]); } else { ByteWriter.CopyBytes(fs, temp, temp.Position, storage.Superblock.ClusterSize, storage.Superblock.ClusterSize); } } return(true); } }