Ejemplo n.º 1
0
        public String ReadFile(int rootBeginAddr)
        {
            List <String> ls = new List <String>();
            BitArray      rootFCBArray = GetBitArray(rootBeginAddr, 128);
            FCB           rootFCB = new FCB(rootFCBArray, rootBeginAddr);
            BitArray      blockArray = GetBitArray(rootFCB.indexBlock * blockSize, blockSize);
            IndexBlock    indexBlock = new IndexBlock(blockArray);
            int           i, j;

            while (indexBlock.IsFull())
            {
                for (i = 0; i < 31; ++i)
                {
                    int curBlock = indexBlock.indexes[i];
                    ls.Add(GetString(curBlock * 512, 64));
                }
                int nextBlock = indexBlock.indexes[31];
                for (i = 0; i < blockSize; ++i)
                {
                    blockArray[i] = disk[i + nextBlock * blockSize];
                }
                indexBlock.ChangeBlock(blockArray);
            }
            for (i = 0; i < indexBlock.endIndex - 1; ++i)
            {
                int curBlock = indexBlock.indexes[i];
                ls.Add(GetString(curBlock * 512, 64));
            }

            String s = GetString(indexBlock.indexes[i] * blockSize, rootFCB.endOffset);
            int    totLen = ls.Count * 64 + s.Length;

            char[] t = new char[totLen];
            j = 0;
            i = 0;
            foreach (String str in ls)
            {
                for (j = 0; j < 64; ++j)
                {
                    t[i] = str[j];
                    i++;
                }
            }
            for (j = 0; j < s.Length; ++j)
            {
                t[i] = s[j];
                i++;
            }
            return(new String(t));
        }
Ejemplo n.º 2
0
        public bool CreateFile(int rootBeginAddr, String fileName, bool isFolder)
        {
            if (GetInt(16, 21) == 0)
            {
                return(false);
            }
            if (CountFreeBlock() < 2)
            {
                return(false);
            }
            int nextFCB = GetNextFreeFCB();

            if (nextFCB == 0)
            {
                return(false);             //无空闲FCB,创建失败
            }
            int nextBlock = GetNextFreeBlock();

            if (nextBlock == 0)
            {
                return(false);               //无空闲块,创建失败
            }
            SetString(nextFCB, fileName);
            disk[nextFCB + 96] = isFolder;
            SetInt(nextBlock, nextFCB + 97, 16);

            int temp = nextBlock;

            nextBlock = GetNextFreeBlock();
            if (nextBlock == 0)
            {
                return(false);               //无空闲块,创建失败
            }
            SetInt(nextBlock, temp * blockSize, 16);
            BitArray childBeginAddr = new BitArray(32, false);
            BitArray chFCB          = GetBitArray(nextFCB, 128);
            FCB      t = new FCB(chFCB, nextFCB);
            int      i;

            for (i = 0; i < 21; ++i)
            {
                int val = 1 << i;
                childBeginAddr[20 - i] = ((nextFCB & val) == val);
            }
            return(WriteFile(rootBeginAddr, childBeginAddr));
        }
Ejemplo n.º 3
0
        public ArrayList GetAllChildType(int rootBeginAddr)
        {
            ArrayList allChild = new ArrayList();

            BitArray   rootFCBArray = GetBitArray(rootBeginAddr, 128);
            FCB        rootFCB = new FCB(rootFCBArray, rootBeginAddr);
            BitArray   blockArray = GetBitArray(rootFCB.indexBlock * blockSize, blockSize);
            IndexBlock indexBlock = new IndexBlock(blockArray);
            int        i, j;

            while (indexBlock.IsFull())
            {
                for (i = 0; i < 31; ++i)
                {
                    int curBlock = indexBlock.indexes[i];
                    for (j = 0; j < 16; ++j)
                    {
                        allChild.Add(disk[GetInt(curBlock * blockSize + 32 * j, 21) + 96]);
                    }
                }
                int nextBlock = indexBlock.indexes[31];
                for (i = 0; i < blockSize; ++i)
                {
                    blockArray[i] = disk[i + nextBlock * blockSize];
                }
                indexBlock.ChangeBlock(blockArray);
            }
            for (i = 0; i < indexBlock.endIndex - 1; ++i)
            {
                int curBlock = indexBlock.indexes[i];
                for (j = 0; j < 16; ++j)
                {
                    allChild.Add(disk[GetInt(curBlock * blockSize + 32 * j, 21) + 96]);
                }
            }
            for (j = 0; j * 32 < rootFCB.endOffset; ++j)
            {
                allChild.Add(disk[GetInt(indexBlock.indexes[i] * blockSize + 32 * j, 21) + 96]);
            }
            return(allChild);
        }
Ejemplo n.º 4
0
        public void DeleteFile(int rootBeginAddr)
        {
            bool isFolder = disk[rootBeginAddr + 96];
            int  i;

            if (isFolder)
            {
                BitArray   rootFCBArray = GetBitArray(rootBeginAddr, 128);
                FCB        rootFCB      = new FCB(rootFCBArray, rootBeginAddr);
                BitArray   blockArray   = GetBitArray(rootFCB.indexBlock * blockSize, blockSize);
                IndexBlock indexBlock   = new IndexBlock(blockArray);
                indexBlock.num = rootFCB.indexBlock;
                int j;
                while (indexBlock.IsFull())
                {
                    for (i = 0; i < 31; ++i)
                    {
                        int curBlock = indexBlock.indexes[i];
                        for (j = 0; j < 16; ++j)
                        {
                            DeleteFile(GetInt(curBlock * blockSize + 32 * j, 21));
                        }
                    }
                    int nextBlock = indexBlock.indexes[31];
                    for (i = 0; i < 512; ++i)
                    {
                        disk[indexBlock.num * blockSize + i] = false;//将数据擦除
                    }
                    SetInt(GetInt(0, 16), indexBlock.num * blockSize, 16);
                    SetInt(indexBlock.num, 0, 16);
                    SetInt(GetInt(256, 16) + 1, 256, 16);
                    for (i = 0; i < blockSize; ++i)
                    {
                        blockArray[i] = disk[i + nextBlock * blockSize];
                    }
                    indexBlock.ChangeBlock(blockArray);
                    indexBlock.num = nextBlock;
                }
                for (i = 0; i < indexBlock.endIndex - 1; ++i)
                {
                    int curBlock = indexBlock.indexes[i];
                    for (j = 0; j < 16; ++j)
                    {
                        DeleteFile(GetInt(curBlock * blockSize + 32 * j, 21));
                    }
                }
                for (j = 0; j * 32 < rootFCB.endOffset; ++j)
                {
                    DeleteFile(GetInt(indexBlock.indexes[i] * blockSize + 32 * j, 21));
                }
                for (i = 0; i < 512; ++i)
                {
                    disk[indexBlock.num * blockSize + i] = false;//将数据擦除
                }
                SetInt(GetInt(0, 16), indexBlock.num * blockSize, 16);
                SetInt(indexBlock.num, 0, 16);
                SetInt(GetInt(256, 16) + 1, 256, 16);
            }
            else
            {
                BitArray   rootFCBArray = GetBitArray(rootBeginAddr, 128);
                FCB        rootFCB      = new FCB(rootFCBArray, rootBeginAddr);
                BitArray   blockArray   = GetBitArray(rootFCB.indexBlock * blockSize, blockSize);
                IndexBlock indexBlock   = new IndexBlock(blockArray);
                indexBlock.num = rootFCB.indexBlock;
                int j;
                while (indexBlock.IsFull())
                {
                    for (i = 0; i < 31; ++i)
                    {
                        int curBlock = indexBlock.indexes[i];
                        for (j = 0; j < 512; ++j)
                        {
                            disk[curBlock * blockSize + j] = false;//将数据擦除
                        }
                        SetInt(GetInt(0, 16), curBlock * blockSize, 16);
                        SetInt(curBlock, 0, 16);
                        SetInt(GetInt(256, 16) + 1, 256, 16);
                    }
                    int nextBlock = indexBlock.indexes[31];
                    for (i = 0; i < 512; ++i)
                    {
                        disk[indexBlock.num * blockSize + i] = false;//将数据擦除
                    }
                    SetInt(GetInt(0, 16), indexBlock.num * blockSize, 16);
                    SetInt(indexBlock.num, 0, 16);
                    SetInt(GetInt(256, 16) + 1, 256, 16);
                    for (i = 0; i < blockSize; ++i)
                    {
                        blockArray[i] = disk[i + nextBlock * blockSize];
                    }
                    indexBlock.ChangeBlock(blockArray);
                    indexBlock.num = nextBlock;
                }
                for (i = 0; i < indexBlock.endIndex; ++i)
                {
                    int curBlock = indexBlock.indexes[i];
                    for (j = 0; j < 512; ++j)
                    {
                        disk[curBlock * blockSize + j] = false;//将数据擦除
                    }
                    SetInt(GetInt(0, 16), curBlock * blockSize, 16);
                    SetInt(curBlock, 0, 16);
                    SetInt(GetInt(256, 16) + 1, 256, 16);
                }
                for (j = 0; j < 512; ++j)
                {
                    disk[indexBlock.num * blockSize + j] = false;//将数据擦除
                }
                SetInt(GetInt(0, 16), indexBlock.num * blockSize, 16);
                SetInt(indexBlock.num, 0, 16);
                SetInt(GetInt(256, 16) + 1, 256, 16);
            }
            for (i = 0; i < 128; ++i)
            {
                disk[rootBeginAddr + i] = false;//擦除PCB
            }
            SetInt(GetInt(16, 21), rootBeginAddr, 21);
            SetInt(rootBeginAddr, 16, 21);
        }
Ejemplo n.º 5
0
        public bool WriteFile(int rootBeginAddr, BitArray dataBitset)
        {
            int        i;
            BitArray   rootFCBArray = GetBitArray(rootBeginAddr, 128);
            FCB        rootFCB      = new FCB(rootFCBArray, rootBeginAddr);
            BitArray   blockArray   = GetBitArray(rootFCB.indexBlock * blockSize, blockSize);
            IndexBlock indexBlock   = new IndexBlock(blockArray);

            indexBlock.num = rootFCB.indexBlock;
            while (indexBlock.IsFull())
            {
                int nextBlock = indexBlock.indexes[31];
                for (i = 0; i < blockSize; ++i)
                {
                    blockArray[i] = disk[i + nextBlock * blockSize];
                }
                indexBlock.ChangeBlock(blockArray);
                indexBlock.num = nextBlock;
            }
            int endAddr = rootFCB.endOffset + indexBlock.indexes[indexBlock.endIndex - 1] * blockSize;

            for (i = 0; i < dataBitset.Length; ++i)
            {
                disk[endAddr] = dataBitset[i];
                if (endAddr % blockSize == (blockSize - 1)) //当前块被写满
                {
                    if (indexBlock.endIndex == 31)          //索引块被写满
                    {
                        int nextBlock = GetNextFreeBlock();
                        if (nextBlock == 0)
                        {
                            return(false);               //磁盘已满,写入失败
                        }
                        SetInt(nextBlock, indexBlock.num * blockSize + 16 * 31, 16);
                        int j;
                        for (j = 0; j < blockSize; ++j)
                        {
                            blockArray[j] = disk[j + indexBlock.num * blockSize];
                        }
                        int temp = nextBlock;
                        nextBlock = GetNextFreeBlock();
                        SetInt(nextBlock, temp * blockSize, 16);

                        indexBlock.ChangeBlock(blockArray);
                        indexBlock.num      = temp;
                        endAddr             = nextBlock * blockSize;
                        indexBlock.endIndex = 1;
                    }
                    else
                    {
                        int nextBlock = GetNextFreeBlock();
                        if (nextBlock == 0)
                        {
                            return(false);               //磁盘已满,写入失败
                        }
                        SetInt(nextBlock, indexBlock.num * blockSize + indexBlock.endIndex * 16, 16);
                        indexBlock.endIndex++;
                        endAddr = nextBlock * blockSize;
                    }
                }
                else
                {
                    endAddr++;
                }
            }
            int offset = endAddr % blockSize;

            SetInt(offset, rootBeginAddr + 113, 9);
            return(true);
        }