コード例 #1
0
        public void Write(int index, byte[] data)
        {
            if (mSector.Type != SECTOR.SectorType.FILE_NODE)
            {
                throw new Exception("Cannot write data to node type " + mSector.Type.ToString());
            }

            //load blocks
            LoadBlocks();


            // extend file with more sectors if needed
            FILE_NODE fileSector     = mSector as FILE_NODE;
            int       fileFileLength = Math.Max(fileSector.FileSize, index + data.Length);

            VirtualBlock.ExtendBlocks(mDrive, mBlocks, fileSector.FileSize, fileFileLength);

            //write data to the blocks
            VirtualBlock.WriteBlockData(mDrive, mBlocks, index, data);

            //wrte out data
            CommitBlocks();

            // write out new file size to disk
            if (fileFileLength != fileSector.FileSize)
            {
                fileSector.FileSize = fileFileLength;
                Drive.Disk.WriteSector(mNodeSector, fileSector.RawBytes);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Cratez/SimpleFS
        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();
        }
コード例 #3
0
        public static NODE CreateFromBytes(byte[] raw)
        {
            SectorType type   = GetTypeFromBytes(raw);
            NODE       result = null;

            if (type == SectorType.DIR_NODE)
            {
                result = DIR_NODE.CreateFromBytes(raw);
            }
            else if (type == SectorType.FILE_NODE)
            {
                result = FILE_NODE.CreateFromBytes(raw);
            }
            else
            {
                throw new Exception("Expected a DIR_NODE or FILE_NODE!");
            }
            return(result);
        }
コード例 #4
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);
        }