📄 write.c
字号:
#ifndef _FAT32_DEFINE
#include "FAT32.c"
#endif
#define LAST_CLUSTER 0xFFFFFFFF
//#define EOF 0xFF
#define DISK_FULL 0xFF
CHAR FAT32WriteFile(BYTE fp,CHAR *WriteBuffer,WORD BytesToWrite);
DWORD FreeClus(void);
DWORD ReqstNewClus(OldClus);
BYTE DEleteFile(CHAR *filename);
BYTE CreatFile(CHAR * filename);
CHAR FAT32WriteFile(BYTE fp,CHAR *WriteBuffer,WORD BytesToWrite)
{
WORD WritedBytes;
WORD i,j;
BYTE dir;
bit FILE_END;
while(FCB[fp].LastCluster<LAST_CLUSTER) //find the last cluster
{
FCB[fp].LastCluster=FAT32NextCluster(FCB[fp].LastCluster);
}
while (FILE_END !=1) //find the end of the file
{
FAT32ReadCluster(&FCB[fp].LastCluster);
for(j=0;j<512;j++)
{
if(DiskBuffer[j]==EOF)
{
FCB[fp].ClusSecCnt = i;
FCB[fp].ByteCnt = j;
FILE_END = 1;
break;
}
}
}
do
{
if(FCB[fp].ByteCnt<=512)
{
DiskBuffer[FCB[fp].ByteCnt] = WriteBuffer[WritedBytes]; //ready
if((WritedBytes++)>=BytesToWrite) return 0;
}
else
{
WriteDiskSector(FCB[fp].LastCluster*FAT32.SecPerClus); //write a sector
FCB[fp].ByteCnt=0;
FCB[fp].ClusSecCnt++;
if(FCB[fp].ClusSecCnt>=FAT32.SecPerClus)
{
FCB[fp].LastCluster=ReqstNewClus(FCB[fp].LastCluster);
if(FCB[fp].LastCluster!=0) continue;
}
}
}while(1);
dir = FAT32FindDIREntry(FCB[fp].FileName);
sDIR[dir].FileSize+= BytesToWrite;
FCB[fp].Size += BytesToWrite;
WriteDiskSector(ClusterToSector( FAT32.CurrentDirectory)+FAT32.ClusSecCnt);
}
DWORD FreeClus(void)
{
BYTE i;
for(FAT.Sector=0;FAT.Sector<FAT32.FATSz;FAT.Sector++)
{
ReadDiskSector(FAT32.FATSec+FAT.Sector);
for(i=0;i<128;i++)
{
if(((DWORD *)&DiskBuffer)[i]==0x0000)
{
return (i+128*FAT.Sector);
}
}
}
return DISK_FULL;
}
DWORD ReqstNewClus(OldClus)
{
DWORD NewClus;
NewClus = FreeClus();
if(NewClus == DISK_FULL) return DISK_FULL;
((DWORD *)DiskBuffer)[NewClus%128] = LAST_CLUSTER;
WriteDiskSector(FAT32.FATSec+FAT.Sector); //write the first FAT
WriteDiskSector(FAT32.FATSec+FAT.Sector+FAT32.FATSz); //write the second FAT
if(OldClus)
{
FAT.Sector = OldClus/128;
ReadDiskSector(FAT32.FATSec+FAT.Sector);
((DWORD *)DiskBuffer)[OldClus%128] = NewClus;
WriteDiskSector(FAT32.FATSec+FAT.Sector);
WriteDiskSector(FAT32.FATSec+FAT.Sector+FAT32.FATSz);
}
return NewClus;
}
BYTE DEleteFile(CHAR *filename)
{
BYTE fp;
BYTE dir;
DWORD chain;
fp=FAT32OpenFile(filename);
if(fp==FAT32_MAX_FCB) return (0x00);
/*delete the FAT*/
while(FCB[fp].LastCluster!=LAST_CLUSTER)
{
ReadDiskSector(FAT32.FATSec+FCB[fp].LastCluster/128);
((DWORD *)DiskBuffer)[FCB[fp].LastCluster] = 0x0000;
WriteDiskSector(FAT32.FATSec+FCB[fp].LastCluster%128);
WriteDiskSector(FAT32.FATSec+FCB[fp].LastCluster%128+FAT32.FATSz);
FCB[fp].LastCluster = FAT32NextCluster(FCB[fp].LastCluster);
}
FAT32CloseFile(fp);
/*delete the directory*/
dir = FAT32FindDIREntry(filename);
sDIR[dir].Name[0]=0x00;
chain=FAT32.CurrentDirectory;
WriteDiskSector(ClusterToSector(chain)+FAT32.ClusSecCnt);
}
BYTE CreatFile(CHAR * filename)
{
DWORD FstClus;
DWORD chain;
BYTE i,j;
FstClus = ReqstNewClus(0);
chain=FAT32.CurrentDirectory;
while(chain!=0x0fffffff)
{
FAT32ReadCluster(&chain);
for (i=0;i<16;i++)
{
if (sDIR[i].Name[0]==0xe5||sDIR[i].Name[0]==0x00)
{
for(j=0;j<11;j++)
sDIR[i].Name[j] = filename[j];
sDIR[i].Attr =
sDIR[i].FstClusHI = ((WORD *)&FstClus)[1];
sDIR[i].FstClusLO = ((WORD *)&FstClus)[0];
sDIR[i].FileSize = 0;
return 0;
}
}
}
return DISK_FULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -