Beispiel #1
0
        /// <summary>
        /// 删除实体文件
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="volumeinfo"></param>
        /// <param name="fat"></param>
        /// <returns></returns>
        public static ErrorCode DeleteFile(string drivename, VolumeInfo volumeinfo, FileInfo fat)
        {
            var content = GetList(drivename, fat.Location, volumeinfo);

            UInt32 clustercount = (UInt32)((volumeinfo.SectorCount - 1) / (volumeinfo.Cluster >> 9)); //簇总数
            UInt32 UseBytes     = (clustercount >> 3) + 1;                                            //用多少个字节存储
            UInt32 UseCluster   = UseBytes / volumeinfo.Cluster + 1;                                  //表大小(簇)

            //读取空闲表内容
            byte[] freetable = DiskRW.ReadA(drivename, volumeinfo.FreeTableStart, UseCluster * (UInt32)(volumeinfo.Cluster >> 9));

            //将位置写回空闲表
            foreach (UInt32 block in content)
            {
                Set(block, false, freetable);
            }
            //fat簇写回空闲表;
            Set(TransferInverse(volumeinfo.SectorStart, fat.Location, volumeinfo.Cluster), false, freetable);
            //保存空闲表
            var res = SetFreeBlock(drivename, ref volumeinfo, freetable);

            if (res == ErrorCode.Fail)
            {
                return(ErrorCode.WriteFreeTableFail);
            }
            return(ErrorCode.Success);
        }
Beispiel #2
0
        /// <summary>
        /// 开始格式化 位示图
        /// </summary>
        public void buildA()
        {
            buildHeaderA();
            buildFatHeader();

            //没有错误处理
            DiskRW.write(drivename, firstSector, header);
            DiskRW.write(drivename, transfer(1), fatHead);

            //组织空闲块
            UInt32 clustercount = (UInt32)((length - 1) / (fileUnit >> 9)); //簇总数
            UInt32 UseBytes     = (clustercount >> 3) + 1;                  //用多少个字节存储
            UInt32 UseCluster   = UseBytes / fileUnit + 1;                  //表大小

            byte[] buffer = new byte[UseCluster * fileUnit];

            Set(0, true, buffer);
            Set(1, true, buffer);
            for (uint i = 2; i < UseCluster; i++)
            {
                Set(i, true, buffer);
            }
            DiskRW.write(drivename, transfer(2), buffer);
            return;
        }
Beispiel #3
0
        /// <summary>
        /// 写回空闲表
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="volumeInfo"></param>
        /// <param name="freetable"></param>
        /// <returns></returns>
        public static ErrorCode SetFreeBlock(string drivename, ref VolumeInfo volumeInfo, byte[] freetable)
        {
            var res = DiskRW.write(drivename, volumeInfo.FreeTableStart, freetable);

            if (res)
            {
                return(ErrorCode.Success);
            }
            return(ErrorCode.Fail);
        }
Beispiel #4
0
        /// <summary>
        /// 开始格式化 成组链接
        /// </summary>
        public void build()
        {
            buildHeader();
            buildFatHeader();

            //没有错误处理
            DiskRW.write(drivename, firstSector, header);
            DiskRW.write(drivename, transfer(1), fatHead);

            //组织空闲块
            UInt32 clustercount = (UInt32)((length - 2048) / (fileUnit >> 9)); //簇总数
            UInt16 count        = (UInt16)(fileUnit / 4 - 1);                  //每簇空闲表容量

            //cluster为组长块
            for (UInt32 cluster = 2; cluster < clustercount; cluster += count)
            {
                byte[] buffer = new byte[fileUnit];
                UInt32 i      = 1;//记录当前录到哪一块(组长块偏移) == 该组内目前有的块数+1

                //写入组长块
                UInt32 j = 2;//记录写到了几个32位数
                for (; j < (count + 1); j++, i++)
                {
                    //不能填充完当前块
                    if ((i + cluster) >= clustercount)
                    {
                        buffer[0] = (byte)(i);
                        buffer[1] = (byte)((i) >> 8);
                        buffer[2] = (byte)((i) >> 16);
                        buffer[3] = (byte)((i) >> 24);
                        DiskRW.write(drivename, transfer(cluster), buffer);
                        return;
                    }
                    buffer[j * 4]     = (byte)(i + cluster);
                    buffer[j * 4 + 1] = (byte)((i + cluster) >> 8);
                    buffer[j * 4 + 2] = (byte)((i + cluster) >> 16);
                    buffer[j * 4 + 3] = (byte)((i + cluster) >> 24);
                }
                buffer[0] = (byte)(i);
                buffer[1] = (byte)((i) >> 8);
                buffer[2] = (byte)((i) >> 16);
                buffer[3] = (byte)((i) >> 24);

                //下块地址
                buffer[4] = (byte)(cluster + count);
                buffer[5] = (byte)((cluster + count) >> 8);
                buffer[6] = (byte)((cluster + count) >> 16);
                buffer[7] = (byte)((cluster + count) >> 24);

                DiskRW.write(drivename, transfer(cluster), buffer);
                i = 1;
            }

            return;
        }
Beispiel #5
0
        /// <summary>
        /// 删除目录中某项
        /// </summary>
        /// <param name="drivename">驱动器名</param>
        /// <param name="volumeinfo">卷信息结构体</param>
        /// <param name="cluster">要删的扇区</param>
        /// <param name="dirinfo">目录</param>
        /// <returns></returns>
        public static ErrorCode DeleteInDir(string drivename, VolumeInfo volumeinfo, UInt32 sector, DirectInfo dirinfo)
        {
            var cluster = TransferInverse(volumeinfo.SectorStart, sector, volumeinfo.Cluster);

            byte[] content = DiskRW.ReadA(drivename, dirinfo.Location, (uint)(volumeinfo.Cluster >> 9));

            UInt32 next = mergeByte(content[160], content[161], content[162], content[163]);

            for (UInt16 i = 164; i < content.Length; i += 4)
            {
                UInt32 s1 = mergeByte(content[i], content[i + 1], content[i + 2], content[i + 3]);
                if (s1 == cluster)
                {
                    content[i]     = 0;
                    content[i + 1] = 0;
                    content[i + 2] = 0;
                    content[i + 3] = 0;
                    DiskRW.write(drivename, dirinfo.Location, content);
                    return(ErrorCode.Success);
                }
            }

            //没有下一个索引块,找不到该目录
            if (next == 0)
            {
                return(ErrorCode.NoSuchFile);
            }

            //读取索引表
            content = DiskRW.ReadA(drivename, transfer(volumeinfo.SectorStart, next, volumeinfo.Cluster), (uint)(volumeinfo.Cluster >> 9));
            while (true)
            {
                for (UInt16 i = 4; i < content.Length; i += 4)
                {
                    UInt32 s1 = mergeByte(content[i], content[i + 1], content[i + 2], content[i + 3]);
                    if (s1 == cluster)
                    {
                        content[i]     = 0;
                        content[i + 1] = 0;
                        content[i + 2] = 0;
                        content[i + 3] = 0;
                        DiskRW.write(drivename, next, content);
                        return(ErrorCode.Success);
                    }
                }
                next = mergeByte(content[0], content[1], content[2], content[3]);
                //下簇号 为零表示没有下一簇
                if (next == 0)
                {
                    return(ErrorCode.NoSuchFile);
                }
                content = ReadOut(drivename, volumeinfo, next);
            }
        }
Beispiel #6
0
        public void readPartitionTable()
        {
            currentPartitionList.Clear();
            if (selected.diskname == null || selected.diskname == "")
            {
                return;
            }
            var mbr = DiskRW.read(selected.diskname, 0);

            parse(mbr);
        }
Beispiel #7
0
        public static ErrorCode SetFileInfo(string drivename, FileInfo fat)
        {
            byte[] buffer = DiskRW.read(drivename, fat.Location);

            //name
            for (int i = 0; i < 63; i++)
            {
                buffer[i] = 0;
            }
            byte[] name = Encoding.Unicode.GetBytes(fat.Name);
            name.CopyTo(buffer, 0);

            name = Encoding.Unicode.GetBytes(fat.Extension);
            name.CopyTo(buffer, 64);

            //文件号 权限号 权限标记
            buffer[77] = fat.Type;
            buffer[78] = fat.AccessCode;
            buffer[79] = fat.AccessMode;

            //作者
            name = Encoding.Unicode.GetBytes(fat.Author);
            name.CopyTo(buffer, 80);
            //最后修改人
            name = Encoding.Unicode.GetBytes(fat.LastEditor);
            name.CopyTo(buffer, 112);

            //创建日期
            buffer[144] = (byte)(fat.CreateTime);
            buffer[145] = (byte)(fat.CreateTime >> 8);
            buffer[146] = (byte)(fat.CreateTime >> 16);
            buffer[147] = (byte)(fat.CreateTime >> 24);
            //修改日期
            buffer[148] = (byte)(fat.LastEditTime);
            buffer[149] = (byte)(fat.LastEditTime >> 8);
            buffer[150] = (byte)(fat.LastEditTime >> 16);
            buffer[151] = (byte)(fat.LastEditTime >> 24);

            //尾字节长
            buffer[152] = (byte)fat.Rest;
            buffer[153] = (byte)(fat.Rest >> 8);

            bool res = DiskRW.write(drivename, fat.Location, buffer);

            if (!res)
            {
                return(ErrorCode.Fail);
            }
            return(ErrorCode.Success);
        }
Beispiel #8
0
        /// <summary>
        /// 写入某个簇内容
        /// </summary>
        /// <param name="drivename">驱动器</param>
        /// <param name="volumeinfo">卷信息结构体</param>
        /// <param name="cluster">要写入的簇号</param>
        /// <param name="content">写入内容</param>
        /// <returns></returns>
        public static ErrorCode WriteIn(string drivename, VolumeInfo volumeinfo, UInt32 cluster, byte[] content)
        {
            if (content.Length != volumeinfo.Cluster)
            {
                return(ErrorCode.ErrorClusterLength);
            }
            var res = DiskRW.write(drivename, transfer(volumeinfo.SectorStart, cluster, volumeinfo.Cluster), content);

            if (res)
            {
                return(ErrorCode.Success);
            }
            return(ErrorCode.Fail);
        }
Beispiel #9
0
        /// <summary>
        /// 获取文件内容具体位置(簇号)
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="Location">扇区号</param>
        /// <param name="fileUnit"></param>
        /// <returns>位置表(簇号)</returns>
        public static List <UInt32> GetList(string drivename, UInt32 location, VolumeInfo volumeinfo)
        {
            var res = new List <UInt32>();

            byte[] content = DiskRW.ReadA(drivename, location, (uint)(volumeinfo.Cluster >> 9));

            UInt32 next = mergeByte(content[160], content[161], content[162], content[163]);

            for (UInt16 i = 164; i < content.Length; i += 4)
            {
                UInt32 s1 = mergeByte(content[i], content[i + 1], content[i + 2], content[i + 3]);
                if (s1 == 0)
                {
                    continue;
                }
                res.Add(s1);
            }

            //没有下一个索引块
            if (next == 0)
            {
                return(res);
            }

            //读取索引表
            content = DiskRW.ReadA(drivename, transfer(volumeinfo.SectorStart, next, volumeinfo.Cluster), (uint)(volumeinfo.Cluster >> 9));
            while (true)
            {
                for (UInt16 i = 4; i < content.Length; i += 4)
                {
                    UInt32 s1 = mergeByte(content[i], content[i + 1], content[i + 2], content[i + 3]);
                    if (s1 == 0)
                    {
                        continue;
                    }
                    res.Add(s1);
                }
                next = mergeByte(content[0], content[1], content[2], content[3]);
                //下簇号 为零表示没有下一簇
                if (next == 0)
                {
                    return(res);
                }
                content = ReadOut(drivename, volumeinfo, next);
            }
        }
Beispiel #10
0
        //待优化
        /// <summary>
        /// 全盘擦除
        /// </summary>
        public void erase()
        {
            UInt32 mod64 = selected.sectors - (selected.sectors >> 6);
            UInt32 mod16 = selected.sectors - (selected.sectors >> 4);
            UInt32 i     = 0;

            for (; i < mod64; i += 64)
            {
                DiskRW.write(selected.diskname, i, new byte[32768]);
            }
            for (; i < mod16; i++)
            {
                DiskRW.write(selected.diskname, i, new byte[4096]);
            }
            for (; i < selected.sectors; i++)
            {
                DiskRW.write(selected.diskname, i, new byte[512]);
            }
        }
Beispiel #11
0
        /// <summary>
        /// 获取实体文件信息
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="location"></param>
        /// <param name="fileUnit"></param>
        /// <returns></returns>
        public static FileInfo GetFileInfoA(string drivename, UInt32 location, UInt16 fileUnit)
        {
            byte[] content = DiskRW.ReadA(drivename, location, (uint)(fileUnit >> 9));
            var    file    = new FileInfo();

            byte[] tmp = new byte[64];
            Array.Copy(content, tmp, 64);
            file.Name = Encoding.Unicode.GetString(tmp).Replace("\0", "");

            //后缀名
            tmp = new byte[13];
            Array.Copy(content, 64, tmp, 0, 13);
            file.Extension = Encoding.ASCII.GetString(tmp).Replace("\0", "");

            //类型
            file.Type = content[77];
            //权限号
            file.AccessCode = content[78];
            //权限标记
            file.AccessMode = content[79];
            //作者
            tmp = new byte[32];
            Array.Copy(content, 80, tmp, 0, 32);
            file.Author = Encoding.Unicode.GetString(tmp);
            //最后修改人
            tmp = new byte[32];
            Array.Copy(content, 112, tmp, 0, 32);
            file.LastEditor = Encoding.Unicode.GetString(tmp);
            //创建时间
            file.CreateTime = mergeByte(content[144], content[145], content[146], content[147]);
            //最后修改时间
            file.LastEditTime = mergeByte(content[148], content[149], content[150], content[151]);

            //尾字节
            UInt16 s5 = content[152];
            UInt16 s6 = content[153];

            file.Rest = (UInt16)((s6 << 8) + s5);

            file.Location = location;
            return(file);
        }
Beispiel #12
0
        /// <summary>
        /// 写回卷信息
        /// </summary>
        /// <param name="drivename">驱动器名</param>
        /// <param name="volumeInfo">卷信息结构体</param>
        /// <returns>错误代码</returns>
        static public ErrorCode SetVolumeInfo(string drivename, VolumeInfo volumeInfo)
        {
            var header = new byte[512];

            byte[] name = Encoding.Unicode.GetBytes("新加卷");
            name.CopyTo(header, 0);

            //起始簇扇区
            header[32] = (byte)volumeInfo.SectorStart;
            header[33] = (byte)(volumeInfo.SectorStart >> 8);
            header[34] = (byte)(volumeInfo.SectorStart >> 16);
            header[35] = (byte)(volumeInfo.SectorStart >> 24);

            //分区长度
            header[36] = (byte)(volumeInfo.SectorCount);
            header[37] = (byte)(volumeInfo.SectorCount >> 8);
            header[38] = (byte)(volumeInfo.SectorCount >> 16);
            header[39] = (byte)(volumeInfo.SectorCount >> 24);

            //分配单元大小 512B 的倍数
            header[40] = (byte)(volumeInfo.Cluster >> 9);

            //卷标 预留为空 ascii编码
            header[41] = volumeInfo.VolumeLabel;

            //空闲闲表头
            header[42] = (byte)(volumeInfo.FreeTableStart);
            header[43] = (byte)(volumeInfo.FreeTableStart >> 8);
            header[44] = (byte)(volumeInfo.FreeTableStart >> 16);
            header[45] = (byte)(volumeInfo.FreeTableStart >> 24);

            //加密方式 0 不加密
            header[48] = volumeInfo.EncryptionType;

            bool res = DiskRW.write(drivename, volumeInfo.SectorStart, header);

            if (!res)
            {
                return(ErrorCode.Fail);
            }
            return(ErrorCode.Success);
        }
Beispiel #13
0
        /// <summary>
        /// 获取空闲块 位示图
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="volumeInfo">卷信息的引用</param>
        /// <param name="count">所需空闲块个数</param>
        /// <returns>空闲簇列表</returns>
        public static List <UInt32> GetFreeBlock(string drivename, ref VolumeInfo volumeInfo, UInt32 count)
        {
            UInt32 clustercount = (UInt32)((volumeInfo.SectorCount - 1) / (volumeInfo.Cluster >> 9)); //簇总数
            UInt32 UseBytes     = (clustercount >> 3) + 1;                                            //用多少个字节存储
            UInt32 UseCluster   = UseBytes / volumeInfo.Cluster + 1;                                  //表大小(簇)

            //读取空闲表内容
            byte[] content = DiskRW.ReadA(drivename, volumeInfo.FreeTableStart, UseCluster * (UInt32)(volumeInfo.Cluster >> 9));
            //返回的空闲块号
            List <UInt32> ls = new List <uint>();

            //i为计数,j为簇索引值
            UInt32 i = 0;

            for (UInt32 j = 0; j < clustercount; j++)
            {
                if (!Test(j, content))
                {
                    ls.Add(j);
                    i++;
                }
                if (i == count)
                {
                    break;
                }
            }

            //磁盘已满或没有空间返回null;
            if (ls.Count < i)
            {
                return(null);
            }
            //写空闲表
            foreach (UInt32 item in ls)
            {
                Set(item, true, content);
            }
            //写回磁盘
            DiskRW.write(drivename, volumeInfo.FreeTableStart, content);
            return(ls);
        }
Beispiel #14
0
        /// <summary>
        /// 获取卷信息
        /// </summary>
        /// <param name="drivename">驱动器名</param>
        /// <param name="sectorstart">卷开始扇区</param>
        /// <returns>卷信息结构体</returns>
        static public VolumeInfo GetVolumeInfo(string drivename, UInt32 sectorstart)
        {
            VolumeInfo volumeinfo = new VolumeInfo();

            byte[] content = DiskRW.read(drivename, sectorstart);

            byte[] tmp = new byte[32];
            Array.Copy(content, tmp, 32);
            volumeinfo.VolumeName = System.Text.Encoding.Unicode.GetString(tmp);

            UInt32 s1 = content[32];
            UInt32 s2 = content[33];
            UInt32 s3 = content[34];
            UInt32 s4 = content[35];

            volumeinfo.SectorStart = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;

            s1 = content[36];
            s2 = content[37];
            s3 = content[38];
            s4 = content[39];
            volumeinfo.SectorCount = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;

            volumeinfo.Cluster = (UInt16)(((UInt16)content[40]) << 9);

            volumeinfo.VolumeLabel = content[41];

            s1 = content[42];
            s2 = content[43];
            s3 = content[44];
            s4 = content[45];
            volumeinfo.FreeTableStart = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;

            volumeinfo.EncryptionType = content[48];

            return(volumeinfo);
        }
        void readPartitionTable()
        {
            currentPartitionList.Clear();
            byte[] MBR = DiskRW.read(Data.DriveName, 0);
            for (int i = 0, j = 0x1BE; i < 4; i++, j += 16)
            {
                var part = new Partition();

                //起始扇区偏移
                UInt32 s1   = MBR[8 + j];
                UInt32 s2   = MBR[9 + j];
                UInt32 s3   = MBR[10 + j];
                UInt32 s4   = MBR[11 + j];
                UInt32 temp = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;
                part.StartSector = temp.ToString();
                part.SS          = temp;

                //长度 (扇区)
                s1      = MBR[12 + j];
                s2      = MBR[13 + j];
                s3      = MBR[14 + j];
                s4      = MBR[15 + j];
                temp    = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;
                part.SC = temp;

                //长度为零说明没有这个分区
                if (temp == 0)
                {
                    continue;
                }

                part.SectorCount = temp.ToString();

                currentPartitionList.Add(part);
            }
        }
Beispiel #16
0
        /// <summary>
        /// 读取某簇内容
        /// </summary>
        /// <param name="drivename">驱动器</param>
        /// <param name="volumeinfo">卷信息结构体</param>
        /// <param name="cluster">要读取的簇号</param>
        /// <returns>具体内容</returns>
        public static byte[] ReadOut(string drivename, VolumeInfo volumeinfo, UInt32 cluster)
        {
            var res = DiskRW.ReadA(drivename, transfer(volumeinfo.SectorStart, cluster, volumeinfo.Cluster), (UInt32)(volumeinfo.Cluster >> 9));

            return(res);
        }
Beispiel #17
0
        /// <summary>
        /// 从某个FAT节点读取文件信息,返回值要转换方可使用
        /// </summary>
        /// <param name="drivename"></param>
        /// <param name="location">扇区</param>
        /// <param name="fileUnit"></param>
        /// <returns>返回目录信息或者文件信息</returns>
        public static FATInfo GetFileInfo(string drivename, UInt32 location, UInt16 fileUnit)
        {
            byte[] content = DiskRW.ReadA(drivename, location, (uint)(fileUnit >> 9));
            if (content[77] == 0) //目录
            {
                var    dir = new DirectInfo();
                byte[] tmp = new byte[64];
                Array.Copy(content, tmp, 64);
                dir.Name = Encoding.Unicode.GetString(tmp).Replace("\0", "");


                //类型
                dir.Type = content[77];
                //权限号
                dir.AccessCode = content[78];
                //权限标记
                dir.AccessMode = content[79];
                //作者
                tmp = new byte[32];
                Array.Copy(content, 80, tmp, 0, 32);
                dir.Author = Encoding.Unicode.GetString(tmp);
                //最后修改人
                tmp = new byte[32];
                Array.Copy(content, 112, tmp, 0, 32);
                dir.LastEditor = Encoding.Unicode.GetString(tmp);
                //创建时间
                UInt32 s1 = content[144];
                UInt32 s2 = content[145];
                UInt32 s3 = content[146];
                UInt32 s4 = content[147];
                dir.CreateTime = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;
                //最后修改时间
                s1 = content[148];
                s2 = content[149];
                s3 = content[150];
                s4 = content[151];
                dir.LastEditTime = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;

                dir.Location = location;

                return(dir);
            }
            else if (content[77] != 0) //实体文件
            {
                var    file = new FileInfo();
                byte[] tmp  = new byte[64];
                Array.Copy(content, tmp, 64);
                file.Name = Encoding.Unicode.GetString(tmp).Replace("\0", "");

                //后缀名
                tmp = new byte[13];
                Array.Copy(content, 64, tmp, 0, 13);
                file.Extension = Encoding.Unicode.GetString(tmp).Replace("\0", "");

                //类型
                file.Type = content[77];
                //权限号
                file.AccessCode = content[78];
                //权限标记
                file.AccessMode = content[79];
                //作者
                tmp = new byte[32];
                Array.Copy(content, 80, tmp, 0, 32);
                file.Author = Encoding.Unicode.GetString(tmp);
                //最后修改人
                tmp = new byte[32];
                Array.Copy(content, 112, tmp, 0, 32);
                file.LastEditor = Encoding.Unicode.GetString(tmp);
                //创建时间
                UInt32 s1 = content[144];
                UInt32 s2 = content[145];
                UInt32 s3 = content[146];
                UInt32 s4 = content[147];
                file.CreateTime = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;
                //最后修改时间
                s1 = content[148];
                s2 = content[149];
                s3 = content[150];
                s4 = content[151];
                file.LastEditTime = (s4 << 24) + (s3 << 16) + (s2 << 8) + s1;

                //尾字节
                UInt16 s5 = content[152];
                UInt16 s6 = content[153];
                file.Rest = (UInt16)((s6 << 8) + s5);


                file.Location = location;
                return(file);
            }
            return(null);
        }
Beispiel #18
0
        /// <summary>
        /// 向某个空文件添加内容块列表
        /// </summary>
        /// <param name="drivename">驱动器</param>
        /// <param name="fat">实体文件FAT</param>
        /// <param name="blocks">内容块</param>
        /// <returns></returns>
        private static ErrorCode AppendBlock(string drivename, VolumeInfo volumeinfo, FileInfo fat, List <UInt32> blocks)
        {
            var    content = new byte[volumeinfo.Cluster];
            UInt16 i1      = (ushort)(volumeinfo.Cluster - 163);//fat簇内可存放多少簇索引

            ///<summary>
            ///总共两种情况
            ///1.fat表即可放入所有索引
            ///2.fat表和额外的索引表放入
            ///</summary>

            //第一种情况 已测试
            if (blocks.Count <= i1)
            {
                //下一索引表为0
                content[160] = 0;
                content[161] = 0;
                content[162] = 0;
                content[163] = 0;

                //i是list的索引,j是content的索引
                for (int i = 0, j = 164; j < volumeinfo.Cluster; j += 4)
                {
                    content[j]     = (byte)blocks[i];
                    content[j + 1] = (byte)(blocks[i] >> 8);
                    content[j + 2] = (byte)(blocks[i] >> 16);
                    content[j + 3] = (byte)(blocks[i] >> 24);

                    i++;
                    if (i == blocks.Count)
                    {
                        break;                   //写完
                    }
                }
                var res = DiskRW.write(drivename, fat.Location, content);
                if (res == false)
                {
                    return(ErrorCode.Fail);
                }
                return(ErrorCode.Success);
            }

            //第二种情况
            int i2 = 0;//list索引

            for (int j = 164; j < volumeinfo.Cluster; i2++, j += 4)
            {
                content[j]     = (byte)blocks[i2];
                content[j + 1] = (byte)(blocks[i2] >> 8);
                content[j + 2] = (byte)(blocks[i2] >> 16);
                content[j + 3] = (byte)(blocks[i2] >> 24);
            }
            //获取的空闲块列表
            var blocklist = GetFreeBlock(drivename, ref volumeinfo, 1);

            content[160] = (byte)blocklist[0];
            content[161] = (byte)(blocklist[0] >> 8);
            content[162] = (byte)(blocklist[0] >> 16);
            content[163] = (byte)(blocklist[0] >> 24);

            DiskRW.write(drivename, fat.Location, content);

            //var blocklist = GetFreeBlock(drivename, ref volumeinfo, 1);
            while (true)
            {
                content = new byte[volumeinfo.Cluster];
                for (int j = 4; j < volumeinfo.Cluster; j += 4)
                {
                    content[j]     = (byte)blocks[i2];
                    content[j + 1] = (byte)(blocks[i2] >> 8);
                    content[j + 2] = (byte)(blocks[i2] >> 16);
                    content[j + 3] = (byte)(blocks[i2] >> 24);

                    i2++;
                    if (i2 == blocks.Count)
                    {
                        break;
                    }
                }
                if (i2 == blocks.Count)
                {
                    break;
                }

                var tmp = blocklist[0];//暂存目前块地址
                blocklist  = GetFreeBlock(drivename, ref volumeinfo, 1);
                content[0] = (byte)blocklist[0];
                content[1] = (byte)(blocklist[0] >> 8);
                content[2] = (byte)(blocklist[0] >> 16);
                content[3] = (byte)(blocklist[0] >> 24);
                WriteIn(drivename, volumeinfo, tmp, content);
            }

            WriteIn(drivename, volumeinfo, blocklist[0], content);
            return(ErrorCode.Success);
        }
Beispiel #19
0
        /// <summary>
        /// 添加文件fat,文件为空
        /// </summary>
        /// <param name="current">当前目录信息</param>
        /// <param name="drivename">驱动器名</param>
        /// <param name="volumeinfo">当前卷信息</param>
        /// <returns>新文件簇号</returns>
        public static UInt32 AddDir(DirectInfo current, string drivename, VolumeInfo volumeinfo)
        {
            byte[] buffer = new byte[volumeinfo.Cluster];
            var    ls     = GetFreeBlock(drivename, ref volumeinfo, 1);//获取存放fat的簇

            //向现在目录加项
            var currentDir = DiskRW.ReadA(drivename, current.Location, (UInt16)(volumeinfo.Cluster >> 9));

            //检索当前的fat表
            for (int i = 164; i < volumeinfo.Cluster; i += 4)
            {
                if (mergeByte(currentDir[i], currentDir[i + 1], currentDir[i + 2], currentDir[i + 3]) == 0)
                {
                    currentDir[i]     = (byte)ls[0];
                    currentDir[i + 1] = (byte)(ls[0] >> 8);
                    currentDir[i + 2] = (byte)(ls[0] >> 16);
                    currentDir[i + 3] = (byte)(ls[0] >> 24);

                    //写回磁盘
                    DiskRW.write(drivename, current.Location, currentDir);
                    return(ls[0]);
                }
            }

            //fat满并且没有下个簇存放
            if (mergeByte(currentDir[160], currentDir[161], currentDir[162], currentDir[163]) == 0)
            {
                //添加下一索引块
                byte[] buffer2 = new byte[volumeinfo.Cluster];//写入内容缓存
                var    l2      = GetFreeBlock(drivename, ref volumeinfo, 1);
                //写入上个的索引表
                currentDir[160] = (byte)l2[0];
                currentDir[161] = (byte)(l2[0] >> 8);
                currentDir[162] = (byte)(l2[0] >> 16);
                currentDir[163] = (byte)(l2[0] >> 24);
                //下个
                buffer2[4] = (byte)ls[0];
                buffer2[5] = (byte)(ls[0] >> 8);
                buffer2[6] = (byte)(ls[0] >> 16);
                buffer2[7] = (byte)(ls[0] >> 24);
                //写入磁盘
                WriteIn(drivename, volumeinfo, l2[0], buffer2);
                DiskRW.write(drivename, current.Location, currentDir);
                return(ls[0]);
            }

            //寻找下一个索引表
            currentDir = ReadOut(drivename, volumeinfo, mergeByte(currentDir[160], currentDir[161], currentDir[162], currentDir[163]));
            while (true)
            {
                for (int i = 4; i < volumeinfo.Cluster; i += 4)
                {
                    if (mergeByte(currentDir[i], currentDir[i + 1], currentDir[i + 2], currentDir[i + 3]) == 0)
                    {
                        currentDir[i]     = (byte)ls[0];
                        currentDir[i + 1] = (byte)(ls[0] >> 8);
                        currentDir[i + 2] = (byte)(ls[0] >> 16);
                        currentDir[i + 3] = (byte)(ls[0] >> 24);
                        //写回磁盘
                        DiskRW.write(drivename, current.Location, currentDir);
                        return(ls[0]);
                    }
                    continue;
                }
                //索引表满并且没有下个簇存放
                if (mergeByte(currentDir[0], currentDir[1], currentDir[2], currentDir[3]) == 0)
                {
                    //添加下一索引块
                    byte[] buffer2 = new byte[volumeinfo.Cluster];//写入内容缓存
                    var    l2      = GetFreeBlock(drivename, ref volumeinfo, 1);
                    //写入上个的索引表
                    currentDir[0] = (byte)l2[0];
                    currentDir[1] = (byte)(l2[0] >> 8);
                    currentDir[2] = (byte)(l2[0] >> 16);
                    currentDir[3] = (byte)(l2[0] >> 24);
                    //下个
                    buffer2[4] = (byte)ls[0];
                    buffer2[5] = (byte)(ls[0] >> 8);
                    buffer2[6] = (byte)(ls[0] >> 16);
                    buffer2[7] = (byte)(ls[0] >> 24);
                    //写入磁盘
                    WriteIn(drivename, volumeinfo, l2[0], buffer2);
                    DiskRW.write(drivename, current.Location, currentDir);
                    return(ls[0]);
                }
                currentDir = ReadOut(drivename, volumeinfo, mergeByte(currentDir[0], currentDir[1], currentDir[2], currentDir[3]));
            }
        }