private void GetDiskFileAllocationTable() { DiskStream.Seek(DiskBootSector.FATOffset(), SeekOrigin.Begin); byte[] tempFAT = new byte[DiskBootSector.SizeOfFAT() * DiskBootSector.GetBytesPerSector()]; DiskStream.Read(tempFAT, 0, tempFAT.Length); FAT_TABLE = new FileAllocationTable(tempFAT, DiskBootSector); }
private bool NewFile(File _file) { var file = _file; var name = ((FatFileMetada)file.Metadata).FileName; var size = ((FatFileMetada)file.Metadata).FileSize; var ext = "txt"; var numberCluster = (int)Math.Ceiling((decimal)size / Settings.ClusterSize); var enoughCluster = CheckIfEnoughCluster(numberCluster); if (!enoughCluster) { MessageBox.Show("Disk space full"); return(false); } //Update root directory { var needUpdate = RootDirectory.NeedUpdateRootDirectory(); if (needUpdate) { bool spaceDiskOk = AddRootDirectory(); if (!spaceDiskOk) { return(false); } } } //Allocate cluster and create file { var index = -1; if ((index = name.LastIndexOf(".", StringComparison.Ordinal)) > 0) { index++; ext = name.Substring(index, name.Length - index); if (ext.Equals("")) { ext = "txt"; } } ((FatFileMetada)file.Metadata).CreatedTime = DateTime.Now; ((FatFileMetada)file.Metadata).WriteTime = DateTime.Now; ((FatFileMetada)file.Metadata).Extension = ext; ((FatFileMetada)file.Metadata).AccessDate = DateTime.Now; ((FatFileMetada)file.Metadata).FileCreationTime = (byte)random.Next(10, 300); ((FatFileMetada)file.Metadata).WriteDate = DateTime.Now; ((FatFileMetada)file.Metadata).CreatedDate = DateTime.Now; ((FatFileMetada)file.Metadata).Attribut = EnumFileAttribut.Archive; ((FatFileMetada)file.Metadata).Reserved = 0; AllocateCluster(file, name, size); FileAllocationTable.AddFileInCluster(file); Files.Add(file); } return(true); }
public FatFileSystem() { Name = "FAT"; Structure = new FatStructureFileSystem(); FileAllocationTable = new FileAllocationTable(this); RootDirectory = new RootDirectory(Clusters, Files); Initialize(); }
public void ModifySelectedFile() { if (SelectedFile == null) { return; } var oldSize = ((FatFileMetada)SelectedFile.Metadata).FileSize; var fileWindow = new Windows.FileWindow(SelectedFile) { ConfirmContent = "Modify", Owner = Application.Current.MainWindow }; fileWindow.ShowDialog(); if (!fileWindow.IsConfirmed) { return; } var newFile = fileWindow.File; ((FatFileMetada)newFile.Metadata).WriteDate = new DateTime().Date; ((FatFileMetada)newFile.Metadata).WriteTime = DateTime.Now; ((FatFileMetada)newFile.Metadata).AccessDate = DateTime.Now; //Clear selection var newSize = ((FatFileMetada)newFile.Metadata).FileSize; if (newSize > oldSize) { var numberNewCluster = (int)Math.Ceiling((decimal)newSize / Settings.ClusterSize) - newFile.NumberClusterUse; newFile.IncreaseFileSize(oldSize); FileAllocationTable.IncreaseClusterFile(numberNewCluster, newFile); } else if (newSize < oldSize) //Smaller size { FileAllocationTable.DecreaseClusterFile(oldSize, newFile); newFile.DecreaseSizeFile(oldSize); } //Reselect SelectClustersByFile(newFile, true); }
public void obtenerArchivo(BootSector BS, FileStream stream, FileAllocationTable FAT, String path) { List <byte[]> bytes = new List <byte[]>(); MemoryStream ms = new MemoryStream(); byte[] archivo = null; int Cluster = Entry.GetFirstCluster(); while (Cluster != -1) { //Leer archivo byte[] tempByte = new byte[BS.GetBytesPerSector()]; stream.Seek(BS.SectorOffsetForCluster(Cluster), SeekOrigin.Begin); stream.Read(tempByte, 0, BS.GetBytesPerSector()); bytes.Add(tempByte); //Obtener el siguiente cluster Cluster = FAT.nextClusterInChain(Cluster); } if (bytes.Count > 0) { foreach (byte[] b in bytes) { ms.Write(b, 0, b.Length); } ms.Seek(0, SeekOrigin.Begin); archivo = new byte[Entry.FileSize]; ms.Read(archivo, 0, archivo.Length); Console.WriteLine(ms.Length); } if (archivo != null) { File.WriteAllBytes(path + Entry.NombreDeDir, archivo); MessageBox.Show("Archivo(s) extraido(s) exitosamente!", "Extraccion de Archivo(s)", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Archivo devuelto en cero!", "Error al extraer archivo", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Formatear() { try { if (DiskStream != null) { //Nulificar archivo DiskStream.Seek(0, SeekOrigin.Begin); DiskStream.SetLength(0); DiskStream.Flush(); DiskStream.SetLength(DiskSize); DiskStream.Flush(); //Escribir el BootSector BootSector BootSector = new BootSector((int)DiskStream.Length, NewsectorPorCluster, NewVolumeLabel); BootSector.WriteBootSector(DiskStream); //Escribir FSInfoSector FSInfoSector FSInfoSector = new FSInfoSector(); FSInfoSector.WriteInformationSector(DiskStream); //Inicializar Tabla FAT - Offset 32 despues de los sectores reservados FileAllocationTable NewFAT = new FileAllocationTable(BootSector); NewFAT.WriteNewFAT(DiskStream); FSInfoSector.UpdateFreeClusters(1, BootSector); FSInfoSector.UpdateLastAllocatedCluster(2); FSInfoSector.WriteInformationSector(DiskStream); MessageBox.Show("La imagen de disco ha sido formateada!", "Formato Completo", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void creardisco_Click(object sender, EventArgs e) { SaveFileDialog NewFileDialog = new SaveFileDialog(); if (NewFileDialog.ShowDialog() == DialogResult.OK) { if (!File.Exists(NewFileDialog.FileName)) { FileStream fs = new FileStream(NewFileDialog.FileName, FileMode.CreateNew); fs.Seek(1024 * 1024 * 1024, SeekOrigin.Begin); fs.WriteByte(0); fs.Close(); MasterBootRecord tablembr = new MasterBootRecord(); tablembr.DatosMBR(); using (BinaryWriter stream = new BinaryWriter(File.Open(NewFileDialog.FileName, FileMode.Open))) { stream.BaseStream.Position = 0; stream.Write(tablembr.JumpIns, 0, tablembr.JumpIns.Length); stream.Write(tablembr.OEMname, 0, tablembr.OEMname.Length); stream.Write(tablembr.BytesPerSector); stream.Write(tablembr.SectorsPerCluster); stream.Write(tablembr.ReservedSectors); stream.Write(tablembr.NumeroDeFATS); stream.Write(tablembr.RootEntryCount); stream.Write(tablembr.SmallSectors); stream.Write(tablembr.MediaDescriptor); stream.Write(tablembr.SectorPerFATS); stream.Write(tablembr.SectorPerTrack); stream.Write(tablembr.NumeroHeads); stream.Write(tablembr.HiddenSectors); stream.Write(tablembr.LargeSectors); stream.Write(tablembr.PhysicalDrive); stream.Write(tablembr.Reservado); stream.Write(tablembr.ExtBootSignature); stream.Write(tablembr.Serial); stream.Write(tablembr.VolumeLabel); stream.Write(tablembr.FileSystemType); stream.Write(tablembr.BootCode, 0, tablembr.BootCode.Length); stream.Write(tablembr.EndOfSector); FileAllocationTable[] FAT = new FileAllocationTable[65536]; for (int i = 0; i < 65536; i++) { FAT[i] = new FileAllocationTable(); if (i >= 17) { FAT[i].ClusterStart(); } else { FAT[i].ClusterReserved(); } } for (int a = 0; a < 2; a++) { foreach (FileAllocationTable fen in FAT) { stream.Write(fen.entrada); } } for (int i = 0; i < 512; i++) { DirectoryEntry EmptyDir = new DirectoryEntry(); stream.Write(EmptyDir.DIR_NAME); stream.Write(EmptyDir.DIR_EXT); stream.Write(EmptyDir.DIR_ATTR); stream.Write(EmptyDir.DIR_NTRES); stream.Write(EmptyDir.DIR_CrtTimeTenth); stream.Write(EmptyDir.DIR_CrtTime); stream.Write(EmptyDir.DIR_CrtDate); stream.Write(EmptyDir.DIR_LstAccDate); stream.Write(EmptyDir.DIR_FstClustHI); stream.Write(EmptyDir.DIR_WrtTime); stream.Write(EmptyDir.DIR_WrtDate); stream.Write(EmptyDir.DIR_FstClustLO); stream.Write(EmptyDir.DIR_FileSize); } } Vars.Default = Path.GetFullPath(NewFileDialog.FileName); string rutaIndex = Vars.Default.Substring(0, Vars.Default.Length - 4) + ".index"; using (FileStream fi = new FileStream(rutaIndex, FileMode.CreateNew)) { fi.Seek(0, SeekOrigin.Begin); fi.WriteByte(0); } Vars.Index = rutaIndex; Main.Disk = Path.GetFullPath(NewFileDialog.FileName); MessageBox.Show("Disco Creado Exitosamente!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("Error en el archivo!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void Formatear(FileStream stream) { try { byte SectoresPorCluster = 0x00; //Obtener el numero de sectores por cluster switch (tamanoSectoresPorCluster.SelectedIndex) { case 0: //1 sector por cluster = Clusters de 512 bytes SectoresPorCluster = 0x01; break; case 1: //2 sectores por cluster = Clusters de 1 KiB SectoresPorCluster = 0x02; break; case 2: //4 sectores por cluster = Clusters de 2 KiB SectoresPorCluster = 0x04; break; case 3: //8 sectores por cluster = Clusters de 4 KiB SectoresPorCluster = 0x08; break; case 4: //16 sectores por cluster = Clusters de 8 KiB SectoresPorCluster = 0x10; break; case 5: //32 sectores por cluster = Clusters de 16KiB SectoresPorCluster = 0x20; break; default: SectoresPorCluster = 0x01; break; } //Escribir el BootSector BootSector BootSector = new BootSector((int)stream.Length, SectoresPorCluster, txtVolLabel.Text); BootSector.WriteBootSector(stream); //Escribir FSInfoSector FSInfoSector FSInfoSector = new FSInfoSector(); FSInfoSector.WriteInformationSector(stream); //Inicializar Tabla FAT - Offset 32, despues de los sectores reservados FileAllocationTable NewFAT = new FileAllocationTable(BootSector); NewFAT.WriteNewFAT(stream); FSInfoSector.UpdateFreeClusters(1, BootSector); FSInfoSector.UpdateLastAllocatedCluster(2); FSInfoSector.WriteInformationSector(stream); MessageBox.Show("La imagen de disco ha sido creada y formateada!", "Formato Completo", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }