예제 #1
0
파일: Fat.cs 프로젝트: riina/fat32lib.NET
        /// <summary>
        /// Reads a <see cref="Fat"/> as specified by a <see cref="BootSector"/>.
        /// </summary>
        /// <param name="bs">the boot sector specifying the <see cref="Fat"/> layout</param>
        /// <param name="fatNr">the number of the <see cref="Fat"/> to read</param>
        /// <returns>the <see cref="Fat"/> that was read</returns>
        /// <exception cref="IOException">IOException on read error</exception>
        /// <exception cref="ArgumentException">ArgumentException if fatNr is greater than
        ///     <see cref="BootSector.GetNrFats"/></exception>
        public static Fat Read(BootSector bs, int fatNr)
        {
            if (fatNr > bs.GetNrFats())
            {
                throw new ArgumentException(
                          "boot sector says there are only " + bs.GetNrFats() +
                          " FATs when reading FAT #" + fatNr);
            }

            var fatOffset = FatUtils.GetFatOffset(bs, fatNr);
            var result    = new Fat(bs, fatOffset);

            result.Read();
            return(result);
        }
예제 #2
0
파일: Fat.cs 프로젝트: riina/fat32lib.NET
        /// <summary>
        /// Creates a new <see cref="Fat"/> as specified by a <see cref="BootSector"/>.
        /// </summary>
        /// <param name="bs">the boot sector specifying the <see cref="Fat"/> layout</param>
        /// <param name="fatNr">the number of the <see cref="Fat"/> to create</param>
        /// <returns>the <see cref="Fat"/> that was created</returns>
        /// <exception cref="IOException">IOException on write error</exception>
        /// <exception cref="ArgumentException">ArgumentException if fatNr is greater than
        ///     <see cref="BootSector.GetNrFats"/></exception>
        public static Fat Create(BootSector bs, int fatNr)
        {
            if (fatNr > bs.GetNrFats())
            {
                throw new ArgumentException(
                          "boot sector says there are only " + bs.GetNrFats() +
                          " FATs when creating FAT #" + fatNr);
            }

            var fatOffset = FatUtils.GetFatOffset(bs, fatNr);
            var result    = new Fat(bs, fatOffset);

            if (bs.GetDataClusterCount() > result.entries.Length)
            {
                throw new IOException("FAT too small for device");
            }

            result.Init(bs.GetMediumDescriptor());
            result.Write();
            return(result);
        }
예제 #3
0
        /// <summary>
        /// Flush all changed structures to the device.
        /// </summary>
        /// <exception cref="IOException">IOException on write error</exception>
        public override void Flush()
        {
            CheckClosed();

            if (bs.IsDirty())
            {
                bs.Write();
            }

            for (var i = 0; i < bs.GetNrFats(); i++)
            {
                fat.WriteCopy(FatUtils.GetFatOffset(bs, i));
            }

            rootDir.Flush();

            if (fsiSector != null)
            {
                fsiSector.SetFreeClusterCount(fat.GetFreeClusterCount());
                fsiSector.SetLastAllocatedCluster(fat.GetLastAllocatedCluster());
                fsiSector.Write();
            }
        }
예제 #4
0
        /// <summary>
        /// Initializes the boot sector and file system for the device. The file
        /// system created by this method will always be in read-write mode.
        /// </summary>
        /// <returns>the file system that was created</returns>
        /// <exception cref="System.IO.IOException">IOException on write error</exception>
        public FatFileSystem Format()
        {
            var sectorSize   = device.GetSectorSize();
            var totalSectors = (int)(device.GetSize() / sectorSize);

            FsInfoSector fsi;
            BootSector   bs;

            if (sectorsPerCluster == 0)
            {
                throw new Exception();
            }

            if (fatType == FatType.BaseFat32)
            {
                bs = new Fat32BootSector(device);
                InitBootSector(bs);

                var f32Bs = (Fat32BootSector)bs;

                f32Bs.SetFsInfoSectorNr(1);

                f32Bs.SetSectorsPerFat(SectorsPerFat(0, totalSectors));
                var rnd = new Random();
                f32Bs.SetFileSystemId(rnd.Next());

                f32Bs.SetVolumeLabel(label);

                /* create FS info sector */
                fsi = FsInfoSector.Create(f32Bs);
            }
            else
            {
                bs = new Fat16BootSector(device);
                InitBootSector(bs);

                var f16Bs = (Fat16BootSector)bs;

                var rootDirEntries = RootDirectorySize(
                    device.GetSectorSize(), totalSectors);

                f16Bs.SetRootDirEntryCount(rootDirEntries);
                f16Bs.SetSectorsPerFat(SectorsPerFat(rootDirEntries, totalSectors));
                if (label != null)
                {
                    f16Bs.SetVolumeLabel(label);
                }
                fsi = null;
            }

            //        bs.write();

            if (fatType == FatType.BaseFat32)
            {
                var f32Bs = (Fat32BootSector)bs;
                /* possibly writes the boot sector copy */
                f32Bs.WriteCopy(device);
            }

            var fat = Fat.Create(bs, 0);

            AbstractDirectory rootDirStore;

            if (fatType == FatType.BaseFat32)
            {
                rootDirStore = ClusterChainDirectory.CreateRoot(fat);
                fsi.SetFreeClusterCount(fat.GetFreeClusterCount());
                fsi.SetLastAllocatedCluster(fat.GetLastAllocatedCluster());
                fsi.Write();
            }
            else
            {
                rootDirStore = Fat16RootDirectory.Create((Fat16BootSector)bs);
            }

            var rootDir =
                new FatLfnDirectory(rootDirStore, fat, false);

            rootDir.Flush();

            for (var i = 0; i < bs.GetNrFats(); i++)
            {
                fat.WriteCopy(FatUtils.GetFatOffset(bs, i));
            }

            bs.Write();

            var fs = FatFileSystem.Read(device, false);

            if (label != null)
            {
                fs.SetVolumeLabel(label);
            }

            fs.Flush();
            return(fs);
        }