/**************************************************************************
- 功能描述:向某一个文件追加数据
- 隶属模块:znFAT文件系统模块
- 函数属性:外部,使用户使用
- 参数说明:pfi:指向FileInfoStruct类型的结构体,用于装载刚创建的文件的信息
len:要追加的数据长度
pbuf:指向数据缓冲区的指针
- 返回说明:成功返回实际写入的数据长度,失败返回0
- 注:追加数据失败很有可能是存储设备已经没有空间了,也就是找不到空闲簇了
**************************************************************************/
#ifdef ZNFAT_ADD_DAT
UINT32 znFAT_Add_Dat(struct FileInfoStruct *pfi,UINT32 len,UINT8 *pbuf)
{
UINT32 i=0,counter=0,iSec,iClu;
UINT32 temp_sub,temp_file_size,new_clu,temp_sec;
struct direntry *prec;
if (len>0)
{
znFAT_ReadSector(pfi->Rec_Sec,znFAT_Buffer);
prec=((struct direntry *)(znFAT_Buffer))+pfi->nRec;
temp_file_size=LE2BE((prec->deFileSize),4);
if (!temp_file_size)
{
if (!(new_clu=znFAT_Find_Free_Clust(0))) return 0;
znFAT_Modify_FAT(new_clu,0x0fffffff);
pfi->FileStartCluster=new_clu;
pfi->FileCurCluster=pfi->FileStartCluster;
pfi->FileSize=0;
pfi->FileCurSector=SOC(pfi->FileCurCluster);
pfi->FileCurPos=0;
pfi->FileCurOffset=0;
znFAT_ReadSector(pfi->Rec_Sec,znFAT_Buffer);
(prec->deHighClust)[0]=(new_clu&0x00ff0000)>>16;
(prec->deHighClust)[1]=(new_clu&0xff000000)>>24;
(prec->deLowCluster)[0]=(new_clu&0x000000ff);
(prec->deLowCluster)[1]=(new_clu&0x0000ff00)>>8;
znFAT_WriteSector(pfi->Rec_Sec,znFAT_Buffer);
}
else
{
if (!(temp_file_size%(pArg->SectorsPerClust*pArg->BytesPerSector))) //在簇的最末尾临界地方,需要寻找新簇
{
znFAT_Seek_File(pfi,pfi->FileSize-1); //@@@待优化
if (!(new_clu=znFAT_Find_Free_Clust(0))) return 0;
znFAT_Modify_FAT(pfi->FileCurCluster,new_clu);
znFAT_Modify_FAT(new_clu,0x0fffffff);
}
znFAT_Seek_File(pfi,pfi->FileSize);
}
//iSec=pfi->FileCurSector;
znFAT_ReadSector(pfi->FileCurSector,znFAT_Buffer);
for (i=pfi->FileCurPos;i<pArg->BytesPerSector;i++)
{
znFAT_Buffer[i]=pbuf[counter];
counter++;
if (counter>=len)
{
iSec=pfi->FileCurSector;
goto end;
}
}
znFAT_WriteSector(pfi->FileCurSector,znFAT_Buffer); //数据接缝,把上次扇区剩余字节填充完
//printf("here1\n");
if ((pfi->FileCurSector-(SOC(pfi->FileCurCluster)))<(pArg->SectorsPerClust-1)) //判断是不是一个簇的最后一个扇区,先将当前簇所有扇区填满
{
for (iSec=pfi->FileCurSector+1;iSec<=(SOC(pfi->FileCurCluster)+pArg->SectorsPerClust-1);iSec++)
{
for (i=0;i<pArg->BytesPerSector;i++)
{
znFAT_Buffer[i]=pbuf[counter];
counter++;
//printf("##-->%d\n$$-->%d\n",counter,pbuf[counter]);
if (counter>=len)
{
goto end;
}
}
znFAT_WriteSector(iSec,znFAT_Buffer);
}
}
//printf("here2\n");
temp_sub=len-counter;
for (iClu=0;iClu<temp_sub/(pArg->SectorsPerClust*pArg->BytesPerSector);iClu++)
{
if (!(new_clu=znFAT_Find_Free_Clust(0))) return 0;
znFAT_Modify_FAT(pfi->FileCurCluster,new_clu);
znFAT_Modify_FAT(new_clu,0x0fffffff);
pfi->FileCurCluster=new_clu;
temp_sec=SOC(new_clu);
for (iSec=temp_sec;iSec<temp_sec+pArg->SectorsPerClust;iSec++)
{
for (i=0;i<pArg->BytesPerSector;i++)
{
znFAT_Buffer[i]=pbuf[counter];
counter++;
}
znFAT_WriteSector(iSec,znFAT_Buffer);
}
}
temp_sub=len-counter;
if (temp_sub)
{
if (!(new_clu=znFAT_Find_Free_Clust(0))) return 0;
znFAT_Modify_FAT(pfi->FileCurCluster,new_clu);
znFAT_Modify_FAT(new_clu,0x0fffffff);
pfi->FileCurCluster=new_clu;
temp_sec=SOC(new_clu);
for (iSec=temp_sec;iSec<temp_sec+temp_sub/pArg->BytesPerSector;iSec++)
{
for (i=0;i<pArg->BytesPerSector;i++)
{
znFAT_Buffer[i]=pbuf[counter];
counter++;
}
znFAT_WriteSector(iSec,znFAT_Buffer);
}
}
temp_sub=len-counter;
if (temp_sub)
{
for (i=0;i<pArg->BytesPerSector;i++)
{
znFAT_Buffer[i]=pbuf[counter];
counter++;
}
znFAT_WriteSector(iSec,znFAT_Buffer);
}
end:
//printf("here!\n");
znFAT_WriteSector(iSec,znFAT_Buffer);
znFAT_ReadSector(pfi->Rec_Sec,znFAT_Buffer);
((((struct direntry *)(znFAT_Buffer))+pfi->nRec)->deFileSize)[0]=((temp_file_size+len)&0x000000ff);
((((struct direntry *)(znFAT_Buffer))+pfi->nRec)->deFileSize)[1]=((temp_file_size+len)&0x0000ff00)>>8;
((((struct direntry *)(znFAT_Buffer))+pfi->nRec)->deFileSize)[2]=((temp_file_size+len)&0x00ff0000)>>16;
((((struct direntry *)(znFAT_Buffer))+pfi->nRec)->deFileSize)[3]=((temp_file_size+len)&0xff000000)>>24;
znFAT_WriteSector(pfi->Rec_Sec,znFAT_Buffer);
//printf("here pass\n");
pfi->FileSize=(temp_file_size+len);
pfi->FileCurSector=(pfi->FileSize%pArg->BytesPerSector)?iSec:iSec+1;
pfi->FileCurPos=pfi->FileSize%pArg->BytesPerSector;
pfi->FileCurOffset=pfi->FileSize;
//printf("==============================================\n");
//printf("FileSize:%d\n",pfi->FileSize);
//printf("FileCurSector:%d\n",pfi->FileCurSector);
// printf("FileCurPos:%d\n",pfi->FileCurPos);
// printf("FileCurOffset:%d\n",pfi->FileCurOffset);
//printf("==============================================\n");
}
znFAT_Find_Free_Clust(1);
//printf("here end\n");
return len;
}
#endif |