Ejemplo 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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
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);
                }
            }
        }
Ejemplo n.º 4
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);
        }