Пример #1
0
        public void Format(DiskDriver disk)
        {
            // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR

            FREE_SECTOR free = new FREE_SECTOR(disk.BytesPerSector);

            for (int i = 0; i < disk.SectorCount; i++)
            {
                disk.WriteSector(i, free.RawBytes);
            }

            // DRIVE_INFO
            DRIVE_INFO drive = new DRIVE_INFO(disk.BytesPerSector, ROOT_DIR_SECTOR);

            disk.WriteSector(DRIVE_INFO_SECTOR, drive.RawBytes);

            // DIR_NODE for root node
            DIR_NODE rootDir = new DIR_NODE(disk.BytesPerSector, ROOT_DATA_SECTOR, FSConstants.PATH_SEPARATOR.ToString(), 0);

            disk.WriteSector(ROOT_DIR_SECTOR, rootDir.RawBytes);

            // DATA_SECTOR for root node
            DATA_SECTOR data = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[] { 0 });

            disk.WriteSector(ROOT_DATA_SECTOR, data.RawBytes);
        }
Пример #2
0
        public void Format(DiskDriver disk)
        {
            // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR

            // Wipe all sectors (replace with "zeroes" are FREE_SECTOR)
            int         bps  = disk.BytesPerSector;
            FREE_SECTOR free = new FREE_SECTOR(bps);

            for (int i = 0; i < disk.SectorCount; i++)
            {
                disk.WriteSector(i, free.RawBytes);
            }

            // Create DRIVE_INFO
            DRIVE_INFO di = new DRIVE_INFO(bps, ROOT_DIR_SECTOR);

            disk.WriteSector(DRIVE_INFO_SECTOR, di.RawBytes);

            // Create and write the DIR_NODE for the root node...
            DIR_NODE dn = new DIR_NODE(bps, ROOT_DATA_SECTOR, FSConstants.ROOT_DIR_NAME, 0);

            disk.WriteSector(ROOT_DIR_SECTOR, dn.RawBytes);

            // ... and an empty DATA_SECTOR
            DATA_SECTOR ds = new DATA_SECTOR(bps, 0, null); // 0 = no next data sector, nul = empty set

            disk.WriteSector(ROOT_DATA_SECTOR, ds.RawBytes);
        }
        public void Format(DiskDriver disk)
        {
            // wipe all sectors of disk and create minimum required DRIVE_INFO, DIR_NODE and DATA_SECTOR

            //how many sectors
            int numSectors = disk.SectorCount;

            FREE_SECTOR freeSector = new FREE_SECTOR(disk.BytesPerSector);

            for (int lba = 0; lba < numSectors; lba++)
            {
                disk.WriteSector(lba, freeSector.RawBytes);
            }

            DRIVE_INFO diSector1 = new DRIVE_INFO(disk.BytesPerSector, ROOT_DIR_SECTOR);

            disk.WriteSector(0, diSector1.RawBytes);

            DIR_NODE rootDirSector = new DIR_NODE(disk.BytesPerSector, ROOT_DATA_SECTOR, FSConstants.ROOT_DIR_NAME, 0);

            disk.WriteSector(ROOT_DIR_SECTOR, rootDirSector.RawBytes);


            DATA_SECTOR rootDataSector = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[DATA_SECTOR.MaxDataLength(disk.BytesPerSector)]);

            disk.WriteSector(ROOT_DATA_SECTOR, rootDataSector.RawBytes);
        }
        public VirtualNode CreateDirectoryNode(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 dir? which sector?
            //find 2 free sectors

            int[] sectors = drive.GetNextFreeSectors(2);
            int   newDirectoryNodeSectorAt = sectors[0];
            int   newDirDataSectorAt       = sectors[1];

            //creat new dir on disk
            DIR_NODE newDirNodeSector = new DIR_NODE(drive.Disk.BytesPerSector, newDirDataSectorAt, name, 0);

            drive.Disk.WriteSector(newDirectoryNodeSectorAt, newDirNodeSector.RawBytes);

            //creat a data sector for dir
            DATA_SECTOR newDirDataSector = new DATA_SECTOR(drive.Disk.BytesPerSector, 0, null);

            drive.Disk.WriteSector(newDirDataSectorAt, newDirDataSector.RawBytes);

            //creat virtual node
            VirtualNode newDirectory = new VirtualNode(drive, newDirectoryNodeSectorAt, newDirNodeSector, this);

            //add dir to parent
            children.Add(name, newDirectory);
            CommitChildren();

            return(newDirectory);
        }
Пример #5
0
        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!");
        }
Пример #6
0
        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);
                }
            }
        }
Пример #7
0
        public void Mount(DiskDriver disk, string mountPoint)
        {
            // for the first mounted drive, expect mountPoint to be named FSConstants.PATH_SEPARATOR as the root
            if (drives.Count == 0 && mountPoint != FSConstants.PATH_SEPARATOR.ToString())
            {
                throw new Exception("Expected first mounted dist to be at root directory!");
            }

            // read drive info from disk, load root node and connect to mountPoint

            DRIVE_INFO   driveInfo = DRIVE_INFO.CreateFromBytes(disk.ReadSector(DRIVE_INFO_SECTOR));
            VirtualDrive drive     = new VirtualDrive(disk, DRIVE_INFO_SECTOR, driveInfo);

            DIR_NODE rootSector = DIR_NODE.CreateFromBytes(disk.ReadSector(ROOT_DIR_SECTOR));

            rootNode = new VirtualNode(drive, ROOT_DIR_SECTOR, rootSector, null);

            drives.Add(mountPoint, drive);
        }
        public void Mount(DiskDriver disk, string mountPoint)
        {
            // read drive info from disk, load root node and connect to mountPoint
            // for the first mounted drive, expect mountPoint to be named FSConstants.ROOT_DIR_NAME as the root

            //read drive info for disk to determin what sector contains its root node
            DRIVE_INFO diSector = DRIVE_INFO.CreateFromBytes(disk.ReadSector(DRIVE_INFO_SECTOR));

            int rootNodeAt = diSector.RootNodeAt;

            VirtualDrive drive = new VirtualDrive(disk, DRIVE_INFO_SECTOR, diSector);

            DIR_NODE rootNodeSector = DIR_NODE.CreateFromBytes(disk.ReadSector(rootNodeAt));

            if (rootNode == null)
            {
                rootNode = new VirtualNode(drive, rootNodeAt, rootNodeSector, null);
            }

            drives.Add(mountPoint, drive);
        }
Пример #9
0
        public void Mount(DiskDriver disk, string mountPoint)
        {
            // read drive info from disk, load root node and connect to mountPoint
            // for the first mounted drive, expect mountPoint to be named "/", FSConstants.ROOT_DIR_NAME, as the root

            try
            {
                // Step 1: Read infor from the disk to understand it's directory structure
                // Read DRIVE_INFO from FRIVE_INFO_SECTOR
                DRIVE_INFO  di = DRIVE_INFO.CreateFromBytes(disk.ReadSector(DRIVE_INFO_SECTOR));
                DIR_NODE    dn = DIR_NODE.CreateFromBytes(disk.ReadSector(di.RootNodeAt));
                DATA_SECTOR ds = DATA_SECTOR.CreateFromBytes(disk.ReadSector(dn.FirstDataAt));


                // Step 2: Join th new disk into the virtual file system structure, at the mount point.
                // Create a VistualDrive for the disk
                VirtualDrive vd = new VirtualDrive(disk, DRIVE_INFO_SECTOR, di);


                if (rootNode == null)
                {
                    // Create a VirtualNode to represent the root dictionaru, at the mount point.
                    // Set the VFS's root node, if this is the first disk to be mounted.
                    rootNode = new VirtualNode(vd, di.RootNodeAt, dn, null);
                }
                else
                {
                    // TODO: Extra Credit:  Handle 2nd dick mounted to exsisting VFS
                    // Create a virtual node for this new disk's root
                    // "join" the new node to the exsisting node structure at the mount point
                }

                // Add a new VirtualDrive to drives dictionary, using te mountPoint as the key
                drives.Add(mountPoint, vd);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to mount disk");
            }
        }
Пример #10
0
        public VirtualNode CreateDirectoryNode(string name)
        {
            // Only create children if we're a directory
            if (!IsDirectory)
            {
                throw new Exception("Must be a directory to create children!");
            }

            // read current list of children
            LoadChildren();

            // allocate a new DIR_NODE and DATA_SECTOR on the disk

            // Find the first two FREE_SECTORs on the disk
            int[] freeSectors = drive.GetNextFreeSectors(2);

            //DIR_NODE
            DIR_NODE dirSector = new DIR_NODE(drive.Disk.BytesPerSector, freeSectors[1], name, 0);

            drive.Disk.WriteSector(freeSectors[0], dirSector.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], dirSector, this);

            // Add it to its parent
            children.Add(name, newNode);

            CommitChildren();

            // Return new node
            return(newNode);
        }