public VirtualNode CreateFileNode(string name) { if (!IsDirectory) { throw new Exception("Must be a directory to create children!"); } // read current list of children LoadChildren(); // Find the first two FREE_SECTORs on the disk int[] freeSectors = drive.GetNextFreeSectors(2); // allocate a new FILE_NODE and DATA_SECTOR on the disk //FILE_NODE FILE_NODE fileSector = new FILE_NODE(drive.Disk.BytesPerSector, freeSectors[1], name, 0); drive.Disk.WriteSector(freeSectors[0], fileSector.RawBytes); //DATA_SECTOR DATA_SECTOR dataSector = new DATA_SECTOR(drive.Disk.BytesPerSector, 0, new byte[] { 0 }); drive.Disk.WriteSector(freeSectors[1], dataSector.RawBytes); // Create a new virtual node VirtualNode newNode = new VirtualNode(drive, freeSectors[0], fileSector, this); // Add it to its parent children.Add(name, newNode); CommitChildren(); return(newNode); }
public VirtualNode CreateFileNode(string name) { LoadChildren(); //insure child cached is full if (children.ContainsKey(name)) { throw new Exception("Another child with that name already exists"); } //where do we store new file? which sector? //find 2 free sectors int[] sectors = drive.GetNextFreeSectors(2); int newFileNodeSectorAt = sectors[0]; int newFileDataSectorAt = sectors[1]; //creat new on disk FILE_NODE newFileNodeSector = new FILE_NODE(drive.Disk.BytesPerSector, newFileDataSectorAt, name, 0); drive.Disk.WriteSector(newFileNodeSectorAt, newFileNodeSector.RawBytes); //creat a data sector for dir DATA_SECTOR newFileDataSector = new DATA_SECTOR(drive.Disk.BytesPerSector, 0, null); drive.Disk.WriteSector(newFileDataSectorAt, newFileDataSector.RawBytes); //creat virtual node VirtualNode newFile = new VirtualNode(drive, newFileNodeSectorAt, newFileNodeSector, this); //add dir to parent children.Add(name, newFile); CommitChildren(); return(newFile); }
public static NODE CreateFromBytes(byte[] raw) { switch (GetTypeFromBytes(raw)) { case SectorType.DIR_NODE: return(DIR_NODE.CreateFromBytes(raw)); case SectorType.FILE_NODE: return(FILE_NODE.CreateFromBytes(raw)); } throw new Exception("Expected a DIR_NODE or FILE_NODE!"); }
private void LoadChildren() { // Ensure that the children cache is correctly put in memory (i.e. reflects what's on the disk) // Assume if the cache exsists, it is correct at the moment // So, we need to call CommitChildren() if (children == null) { // Create the cache itself children = new Dictionary <string, VirtualNode>(); // Read the list of children for this directory from disk // Instantiate a VirtualNode for each [child?] and add them to the children cache. // Read the data sector for this directory DATA_SECTOR dataSector = DATA_SECTOR.CreateFromBytes(drive.Disk.ReadSector(sector.FirstDataAt)); // Extract the list of children from the sata sector byte[] rawList = dataSector.DataBytes; // Foreach child in the list... for (int i = 0; i < ChildCount; i++) { // Getthe child's sector address int childSectorAt = BitConverter.ToInt32(rawList, i * 4); // Read its sector from disk // Check if it's a file or directory byte[] childNodeBytes = drive.Disk.ReadSector(childSectorAt); NODE childSector; if (SECTOR.GetTypeFromBytes(childNodeBytes) == SECTOR.SectorType.DIR_NODE) { childSector = DIR_NODE.CreateFromBytes(drive.Disk.ReadSector(childSectorAt)); } else if (SECTOR.GetTypeFromBytes(childNodeBytes) == SECTOR.SectorType.FILE_NODE) { childSector = FILE_NODE.CreateFromBytes(drive.Disk.ReadSector(childSectorAt)); } else { throw new Exception("Unexpected sector type whe reading directory's children!"); } // Construct a VirtualNode VirtualNode childNode = new VirtualNode(drive, childSectorAt, childSector, this); // Add the VirtualNode to the children cache children.Add(childNode.Name, childNode); } } }