public VirtualNode(VirtualDrive drive, int nodeSector, NODE sector, VirtualNode parent) { mDrive = drive; mNodeSector = nodeSector; mSector = sector; mParent = parent; mChildren = null; mBlocks = null; }
public static NODE CreateFromBytes(byte[] raw) { SectorType type = GetTypeFromBytes(raw); NODE result = null; if (type == SectorType.DIR_NODE) { result = DIR_NODE.CreateFromBytes(raw); } else if (type == SectorType.FILE_NODE) { result = FILE_NODE.CreateFromBytes(raw); } else { throw new Exception("Expected a DIR_NODE or FILE_NODE!"); } return(result); }
private VirtualNode CommonMake(string name, bool makeFile = true) { if (mSector.Type != SECTOR.SectorType.DIR_NODE) { throw new Exception($"Cannot create Dir/File under node type " + mSector.Type.ToString()); } LoadChildren(); if (mChildren.ContainsKey(name)) { throw new Exception("Name already in use!"); } int[] nextFreeSectors = mDrive.GetNextFreeSectors(2); int nodeAddr = nextFreeSectors[0]; int dataAddr = nextFreeSectors[1]; NODE newNode = null; if (makeFile) { newNode = new FILE_NODE(mDrive.Disk.BytesPerSector, dataAddr, name, 0); } else { newNode = new DIR_NODE(mDrive.Disk.BytesPerSector, dataAddr, name, 0); } DATA_SECTOR dirdata = new DATA_SECTOR(mDrive.Disk.BytesPerSector, 0, null); mDrive.Disk.WriteSector(nodeAddr, newNode.RawBytes); mDrive.Disk.WriteSector(dataAddr, dirdata.RawBytes); VirtualNode child = new VirtualNode(mDrive, nodeAddr, newNode, this); mChildren[name] = child; CommitChildren(); return(child); }
private void LoadChildren() { if (mChildren == null) { mChildren = new Dictionary <string, VirtualNode>(); DATA_SECTOR dirData = DATA_SECTOR.CreateFromBytes(mDrive.Disk.ReadSector(mSector.FirstDataAt)); int nChildren = (mSector as DIR_NODE).EntryCount; for (int i = 0; i < nChildren; i++) { //fetch address int childNodeAddress = BitConverter.ToInt32(dirData.DataBytes, i * 4); //read in child byte[] childRaw = Drive.Disk.ReadSector(childNodeAddress); NODE childNode = NODE.CreateFromBytes(childRaw); //store the node as virutal node child VirtualNode virtualNode = new VirtualNode(mDrive, childNodeAddress, childNode, this); mChildren[virtualNode.Name] = virtualNode; } } }