Exemplo n.º 1
0
 public VirtualBlock(VirtualDrive drive, int sectorAddress, DATA_SECTOR sector, bool dirty = false)
 {
     this.drive         = drive;
     this.sector        = sector;
     this.sectorAddress = sectorAddress;
     this.dirty         = dirty;
 }
Exemplo n.º 2
0
 public VirtualDrive(DiskDriver disk, int driveInfoSector, DRIVE_INFO sector)
 {
     mDisk               = disk;
     mDriveInfoSector    = driveInfoSector;
     mBytesPerDataSector = DATA_SECTOR.MaxDataLength(disk.BytesPerSector);
     mSector             = sector;
 }
Exemplo n.º 3
0
        public void Format(DiskDriver disk)
        {
            //wipe the disk
            FREE_SECTOR freeSector = new FREE_SECTOR(disk.BytesPerSector);

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

            //format it
            DRIVE_INFO driveInfo = new DRIVE_INFO(disk.BytesPerSector, Constants.ROOT_DIR_SECTOR);

            disk.WriteSector(Constants.DRIVE_INFO_SECTOR, driveInfo.RawBytes);


            DIR_NODE rootNode = new DIR_NODE(disk.BytesPerSector, Constants.ROOT_DATA_SECTOR, "/", 0);

            disk.WriteSector(Constants.ROOT_DIR_SECTOR, rootNode.RawBytes);


            DATA_SECTOR rootData = new DATA_SECTOR(disk.BytesPerSector, 0, new byte[1]);

            disk.WriteSector(Constants.ROOT_DATA_SECTOR, rootData.RawBytes);
        }
Exemplo n.º 4
0
        private void CommitChildren()
        {
            DATA_SECTOR dirData = DATA_SECTOR.CreateFromBytes(mDrive.Disk.ReadSector(mSector.FirstDataAt));

            byte[] data = null;
            if (mChildren.Count > 0)
            {
                //get data for children
                data = new byte[mChildren.Count * 4];
                int byteIndex = 0;
                foreach (VirtualNode child in mChildren.Values)
                {
                    //copy child address
                    BitConverter.GetBytes(child.mNodeSector).CopyTo(data, byteIndex);
                    byteIndex += 4;
                }
            }

            //add to dirData, write back to disk
            dirData.DataBytes = data;
            mDrive.Disk.WriteSector(mSector.FirstDataAt, dirData.RawBytes);

            //write entry count to disk
            (mSector as DIR_NODE).EntryCount = mChildren.Count;
            mDrive.Disk.WriteSector(mNodeSector, mSector.RawBytes);
        }
Exemplo n.º 5
0
        static void TestPhysicalFileSystem()
        {
            SlowDisk disk = new SlowDisk(1);

            disk.TurnOn();

            int SECTOR_SIZE = disk.BytesPerSector;

            //free sector
            {
                FREE_SECTOR freeWrite = new FREE_SECTOR(disk.BytesPerSector);
                disk.WriteSector(0, freeWrite.RawBytes);
                FREE_SECTOR freeRead = FREE_SECTOR.CreateFromBytes(disk.ReadSector(0));
                CheckSectorBytes(freeWrite, freeRead);
            }

            //drive info
            {
                DRIVE_INFO driveWrite = new DRIVE_INFO(disk.BytesPerSector, 101);
                disk.WriteSector(1, driveWrite.RawBytes);
                DRIVE_INFO driveRead = DRIVE_INFO.CreateFromBytes(disk.ReadSector(1));
                CheckSectorBytes(driveWrite, driveRead);
            }

            //dir node
            {
                DIR_NODE dirWrite = new DIR_NODE(disk.BytesPerSector, 103, "dir1", 10);
                disk.WriteSector(2, dirWrite.RawBytes);
                DIR_NODE dirRead = DIR_NODE.CreateFromBytes(disk.ReadSector(2));
                CheckSectorBytes(dirWrite, dirRead);
            }

            //file node
            {
                FILE_NODE fileWrite = new FILE_NODE(disk.BytesPerSector, 104, "file1", 100);
                disk.WriteSector(3, fileWrite.RawBytes);
                FILE_NODE fileRead = FILE_NODE.CreateFromBytes(disk.ReadSector(3));
                CheckSectorBytes(fileWrite, fileRead);
            }

            //data sector
            {
                byte[] testData = new byte[DATA_SECTOR.MaxDataLength(disk.BytesPerSector)];
                for (int i = 0; i < testData.Length; i++)
                {
                    testData[i] = (byte)(i + 1);
                }

                DATA_SECTOR dataWrite = new DATA_SECTOR(disk.BytesPerSector, 105, testData);
                disk.WriteSector(4, dataWrite.RawBytes);
                DATA_SECTOR dataRead = DATA_SECTOR.CreateFromBytes(disk.ReadSector(4));
                CheckSectorBytes(dataWrite, dataRead);
            }

            disk.TurnOff();
        }
Exemplo n.º 6
0
        public void Delete()
        {
            if (mParent == null)
            {
                throw new Exception("Cannot delete root node");
            }

            //we need to do cleanup if we are a dir
            if (IsDirectory)
            {
                LoadChildren();
                foreach (var child in mChildren)
                {
                    //recursive delete call to free
                    child.Value.Delete();
                }
            }

            mParent.LoadChildren();
            mParent.mChildren.Remove(this.Name);
            mParent.CommitChildren();

            //free sector to wipe with


            FREE_SECTOR freeSector = new FREE_SECTOR(mDrive.Disk.BytesPerSector);
            //start at first data
            int sectorIndex = mSector.FirstDataAt;

            while (sectorIndex != 0)
            {
                //store what will be next sector...
                int nextsector = 0;

                //get sector
                DATA_SECTOR currentSector = DATA_SECTOR.CreateFromBytes(mDrive.Disk.ReadSector(sectorIndex));
                nextsector = currentSector.NextSectorAt;

                //wipe sector
                mDrive.Disk.WriteSector(sectorIndex, freeSector.RawBytes);

                //point to next sector
                sectorIndex = nextsector;
            }
            ;

            //wipe main sector
            mDrive.Disk.WriteSector(mNodeSector, freeSector.RawBytes);
        }
Exemplo n.º 7
0
        private void LoadBlocks()
        {
            if (mBlocks == null)
            {
                //make list
                mBlocks = new List <VirtualBlock>();

                //load all blocks into memory
                int currentSector = mSector.FirstDataAt;
                do
                {
                    DATA_SECTOR dataSector = DATA_SECTOR.CreateFromBytes(mDrive.Disk.ReadSector(currentSector));
                    mBlocks.Add(new VirtualBlock(mDrive, currentSector, dataSector, false));
                    currentSector = dataSector.NextSectorAt;
                }while (currentSector != 0);
            }
        }
Exemplo n.º 8
0
        public static void ExtendBlocks(VirtualDrive drive, List <VirtualBlock> blocks, int initialFileLength, int finalFileLength)
        {
            int initialSectorcount = BlocksNeeded(drive, initialFileLength);
            int finalSectorcount   = BlocksNeeded(drive, finalFileLength);

            if (finalSectorcount > initialSectorcount)
            {
                int[] freeSectors = drive.GetNextFreeSectors(finalSectorcount - initialSectorcount);


                DATA_SECTOR dataSector = blocks.Last().Sector;
                foreach (int freeSector in freeSectors)
                {
                    int currentSector = dataSector.NextSectorAt = freeSector;
                    dataSector = new DATA_SECTOR(drive.Disk.BytesPerSector, 0, null);
                    blocks.Add(new VirtualBlock(drive, currentSector, dataSector, true));
                }
            }
        }
Exemplo n.º 9
0
        private VirtualNode CommonMake(string name, bool makeFile = true)
        {
            if (mSector.Type != SECTOR.SectorType.DIR_NODE)
            {
                throw new Exception($"Cannot create Dir/File under node type " + mSector.Type.ToString());
            }
            LoadChildren();

            if (mChildren.ContainsKey(name))
            {
                throw new Exception("Name already in use!");
            }

            int[] nextFreeSectors = mDrive.GetNextFreeSectors(2);
            int   nodeAddr        = nextFreeSectors[0];
            int   dataAddr        = nextFreeSectors[1];

            NODE newNode = null;

            if (makeFile)
            {
                newNode = new FILE_NODE(mDrive.Disk.BytesPerSector, dataAddr, name, 0);
            }
            else
            {
                newNode = new DIR_NODE(mDrive.Disk.BytesPerSector, dataAddr, name, 0);
            }

            DATA_SECTOR dirdata = new DATA_SECTOR(mDrive.Disk.BytesPerSector, 0, null);

            mDrive.Disk.WriteSector(nodeAddr, newNode.RawBytes);
            mDrive.Disk.WriteSector(dataAddr, dirdata.RawBytes);

            VirtualNode child = new VirtualNode(mDrive, nodeAddr, newNode, this);

            mChildren[name] = child;
            CommitChildren();
            return(child);
        }
Exemplo n.º 10
0
        private void LoadChildren()
        {
            if (mChildren == null)
            {
                mChildren = new Dictionary <string, VirtualNode>();

                DATA_SECTOR dirData = DATA_SECTOR.CreateFromBytes(mDrive.Disk.ReadSector(mSector.FirstDataAt));

                int nChildren = (mSector as DIR_NODE).EntryCount;
                for (int i = 0; i < nChildren; i++)
                {
                    //fetch address
                    int childNodeAddress = BitConverter.ToInt32(dirData.DataBytes, i * 4);

                    //read in child
                    byte[] childRaw  = Drive.Disk.ReadSector(childNodeAddress);
                    NODE   childNode = NODE.CreateFromBytes(childRaw);

                    //store the node as virutal node child
                    VirtualNode virtualNode = new VirtualNode(mDrive, childNodeAddress, childNode, this);
                    mChildren[virtualNode.Name] = virtualNode;
                }
            }
        }