Exemplo n.º 1
0
 public void WriteINode(System.IO.FileStream fs, INode iNode, long offset)
 {
     fs.Position = offset;
     FSPartsWriter.WriteINode(fs, iNode);
 }
Exemplo n.º 2
0
        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);
            }
        }