Esempio n. 1
0
        public int[] GetNextFreeSectors(int count)
        {
            // find count available free sectors on the disk and return their addresses
            if (count <= 0)
            {
                throw new Exception("Hey! No Negative!");
            }

            int[] result = new int[count];

            // Itterate over all sectors, starting at the beginning, until we find count FREE_SECTORS
            int found = 0;

            for (int lba = 0; found < count && lba < disk.SectorCount; lba++)
            {
                //byte[] bytes = disk.ReadSector(lba);
                if (SECTOR.GetTypeFromBytes(disk.ReadSector(lba)) == SECTOR.SectorType.FREE_SECTOR)
                {
                    result[found++] = lba;
                }
            }

            // If we didn't find enough free sectors, just return null
            if (found < count)
            {
                return(null);
            }

            return(result);
        }
        public int[] GetNextFreeSectors(int count)
        {
            // find count available free sectors on the disk and return their addresses
            //if not enough free sectors found throw exception
            int[] result = new int[count];
            count--;

            for (int lba = 0; lba < disk.SectorCount && count >= 0; lba++)
            {
                byte[] raw = disk.ReadSector(lba);

                if (SECTOR.GetTypeFromBytes(raw) == SECTOR.SectorType.FREE_SECTOR)
                {
                    result[count] = lba;
                    count--;
                }
            }

            if (count >= 0)
            {
                throw new Exception("can't find enough free sectors");
            }

            return(result);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        static bool TestSector(DiskDriver disk, int lba, byte[] testdata)
        {
            disk.WriteSector(lba, testdata);
            byte[] s       = disk.ReadSector(lba);
            bool   success = Compare(testdata, s);

            Console.WriteLine("Compare " + success.ToString());

            return(success);
        }
        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);
        }
Esempio n. 6
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");
            }
        }
Esempio n. 7
0
        public void Mount(DiskDriver disk, string mountPoint)
        {
            if (mDrives.Count == 0 && mountPoint.Length != 1 && mountPoint[0] != '/')
            {
                throw new Exception("First disk must be mounted at the root");
            }

            //add virtual drive
            DRIVE_INFO   driveInfo = DRIVE_INFO.CreateFromBytes(disk.ReadSector(Constants.DRIVE_INFO_SECTOR));
            VirtualDrive newDrive  = new VirtualDrive(disk, Constants.DRIVE_INFO_SECTOR, driveInfo);

            mDrives.Add(mountPoint, newDrive);

            //add root dir, make node, add it
            int         rootNodeAt      = driveInfo.RootNodeAt;
            DIR_NODE    newDriveRootDir = DIR_NODE.CreateFromBytes(disk.ReadSector(rootNodeAt));
            VirtualNode virtualNode     = new VirtualNode(newDrive, rootNodeAt, newDriveRootDir, null);

            if (mDrives.Count == 1)
            {
                mRootNode = virtualNode;
            }
        }
Esempio n. 8
0
        public int[] GetNextFreeSectors(int count)
        {
            // find count available free sectors on the disk and return their addresses

            int[] result = new int[count];

            int foundIndex = 0;

            for (int address = 0; address < disk.SectorCount && foundIndex < count; address++)
            {
                byte[] raw = disk.ReadSector(address);
                if (SECTOR.GetTypeFromBytes(raw) == SECTOR.SectorType.FREE_SECTOR)
                {
                    result[foundIndex++] = address;
                }
            }

            return(result);
        }
Esempio n. 9
0
        public int[] GetNextFreeSectors(int count)
        {
            //array for our free sectors...
            int[] freeSectors = new int[count];

            //search fof free sectors...
            for (int i = 0; i < mDisk.SectorCount; i++)
            {
                byte[] raw = mDisk.ReadSector(i);
                if (SECTOR.GetTypeFromBytes(raw) == SECTOR.SectorType.FREE_SECTOR)
                {
                    //add the free sector
                    freeSectors[--count] = i;

                    //enough?
                    if (count <= 0)
                    {
                        return(freeSectors);
                    }
                }
            }
            throw new Exception("Disk is full!");
        }