private void LoadChildren() { //test if we already have everything in cache? if (children != null) { return; } //create empty cache children = new Dictionary <string, VirtualNode>(); //read this dir's data sector DATA_SECTOR dataSector = DATA_SECTOR.CreateFromBytes(drive.Disk.ReadSector(DataSectorAt)); //create virtual nodes for each child and add to children dictionary byte[] data = dataSector.DataBytes; for (int i = 0; i < ChildCount; i++) { int childAt = BitConverter.ToInt32(data, i * 4); NODE childNodeSector = NODE.CreateFromBytes(drive.Disk.ReadSector(childAt)); VirtualNode childNode = new VirtualNode(drive, childAt, childNodeSector, this); children.Add(childNode.Name, childNode); } }
private List <VirtualBlock> blocks; // cache of file blocks public VirtualNode(VirtualDrive drive, int nodeSector, NODE sector, VirtualNode parent) { this.drive = drive; this.nodeSector = nodeSector; this.sector = sector; this.parent = parent; this.children = null; // initially empty cache this.blocks = null; // initially empty cache }
private VirtualNode CreateNode(string name, SECTOR.SectorType type) { // Create a new file, both on disk and in memory // type may be DIR_NODE or FILE_NODE if (type != SECTOR.SectorType.DIR_NODE && type != SECTOR.SectorType.FILE_NODE) { throw new Exception("Illegal type, CreateNode only take DIR_NODE and FILE_NODE types!"); } // Get 2 free sectors // First Sector: NODE, containing metadata // Second Sector: DATA_SECTOR, containing the new node's data int[] freeSectors = drive.GetNextFreeSectors(2); if (freeSectors == null || freeSectors.Length != 2) { throw new Exception("Can't find 2 free sectors for a new " + (type == SECTOR.SectorType.DIR_NODE ? "directory" : "file") + "!"); } int newNodeAt = freeSectors[0]; int newDataSectorAt = freeSectors[1]; // Create the node sector on disk, initially empty int bps = drive.Disk.BytesPerSector; NODE newNode = (type == SECTOR.SectorType.DIR_NODE ? new DIR_NODE(bps, newDataSectorAt, name, 0) as NODE : new FILE_NODE(bps, newDataSectorAt, name, 0) as NODE); //Create the DATA_SECTOR sector on disk for the new directory // initially empty data sector for this new directory DATA_SECTOR dataSector = new DATA_SECTOR(bps, 0, null); // Write sectors to disk drive.Disk.WriteSector(newNodeAt, newNode.RawBytes); drive.Disk.WriteSector(newDataSectorAt, dataSector.RawBytes); // Create a new VirtualNode instance VirtualNode newVirtualNode = new VirtualNode(drive, newNodeAt, newNode, this); // Add this to the in-memory cache of this directory's children LoadChildren(); children.Add(name, newVirtualNode); CommitChildren(); // Return the new VirtualNode instace return(newVirtualNode); }
private void LoadChildren() { if (children == null) { children = new Dictionary <string, VirtualNode>(); DATA_SECTOR data = DATA_SECTOR.CreateFromBytes(drive.Disk.ReadSector(sector.FirstDataAt)); for (int i = 0; i < ChildCount; i++) { int childAddress = BitConverter.ToInt32(data.DataBytes, i * 4); NODE childSector = NODE.CreateFromBytes(drive.Disk.ReadSector(childAddress)); VirtualNode vn = new VirtualNode(drive, childAddress, childSector, this); children.Add(childSector.Name, vn); } } }