📄 ffs.c
字号:
*cp++=dir[i]; pFCB->_AvailableBytes=0; pFCB->SubFileSystem=(struct _fs_sFILE *)ffs; pFCB->_NextBytePtr=ffse->data+strlen(dir); pFCB->_BufferBase=(bit8 *)ffse; pFCB->_SubSystemType=FILE_FLASH; pFCB->_FileState=FILE_OPEN|FILE_WRITE|FILE_BIN; return(pFCB); } if (_ffs_GetBit16Value(ffse->flags)&FFS_F_DIRECTORY) inode=_ffs_GetBit16Value(ffse->data+strlen(dir)); else { if (pRestOfFileName) { return(0); } if (wmode) return(0); } }while(pRestOfFileName); } if (ffse==0) return(0); if (!rmode) return(0); pFCB->_AvailableBytes=_ffs_GetBit24Value(ffse->flen); pFCB->SubFileSystem=0; pFCB->_NextBytePtr=ffse->data+strlen(dir); if (_ffs_GetBit16Value(ffse->flags)&FFS_F_BLOCK_ALIGN) { bit8 *newptr; bit32u reducelen,blocksize; reducelen=0; blocksize=FWBGetBlockSize((bit32u)pFCB->_NextBytePtr); newptr=(bit8 *)((((bit32u)pFCB->_NextBytePtr)+(blocksize-1))&(~(blocksize-1))); if (newptr!=pFCB->_NextBytePtr) reducelen=blocksize-(((bit32u)pFCB->_NextBytePtr)&(blocksize-1)); if (pFCB->_AvailableBytes<reducelen) return(0); pFCB->_AvailableBytes-=reducelen; pFCB->_NextBytePtr=newptr; } pFCB->_BufferBase=(bit8 *)ffse; pFCB->_SubSystemType=FILE_FLASH; pFCB->_FileState=FILE_OPEN|FILE_RO|FILE_BIN; return(pFCB); }size_t ffs_fread(void *ptr, size_t size, size_t nitems, FFS_FILE *stream) { bit32 BytesToRead,NumOfBytesRead; bit8 *pTmpBufPtr; pTmpBufPtr=(bit8 *)ptr; if ((stream->_FileState&FILE_OPEN)==0) return(0); if (stream->_SubSystemType!=FILE_FLASH) return(0); if (stream->_AvailableBytes==0) { stream->_FileState|=FILE_EOF; return(0); } BytesToRead=MIN(size*nitems,stream->_AvailableBytes); NumOfBytesRead=BytesToRead; stream->_AvailableBytes-=BytesToRead; while(BytesToRead) { BytesToRead--; *pTmpBufPtr++=*stream->_NextBytePtr++; } return(NumOfBytesRead/size); }void _ffs_xferstream(FFS_FILE *stream,ffs_tEntry *old,ffs_tEntry *new) { bit32u offset; if (stream->_BufferBase!=(bit8 *)old) {sys_printf("Invalid stream change!\n"); while(1) {} } offset=((bit32u)stream->_NextBytePtr)- ((bit32u)stream->_BufferBase); stream->_BufferBase=(bit8 *)new; stream->_NextBytePtr=(bit8 *)new; stream->_NextBytePtr+=offset; }size_t ffs_fwrite(const void *ptr, size_t size, size_t nitems, FFS_FILE *stream) { bit32u BytesToWrite,NumOfBytesWritten,flen,clen,nlen,i1; ffs_tEntry *ffse,*i2; bit8 *pTmpBufPtr; ffse=(ffs_tEntry *)stream->_BufferBase; pTmpBufPtr=(bit8 *)ptr; if ((stream->_FileState&FILE_OPEN)==0) return(0); if (stream->_SubSystemType!=FILE_FLASH) return(0); if ((stream->_FileState&FILE_WRITE)==0) return(0); if ((size*nitems)>(stream->_AvailableBytes)) { clen=((bit32u)stream->_NextBytePtr)- ((bit32u)stream->_BufferBase)+ stream->_AvailableBytes- ((bit32u)ffse->fnlen)-(sizeof(ffs_tEntry)-1); nlen=clen+(size*nitems)-(stream->_AvailableBytes); i1=((bit32u)ffse->fnlen)+(sizeof(ffs_tEntry)-1); i2=sys_realloc(ffse,nlen+i1); if (!i2) return(0); if (ffse!=i2) _ffs_xferstream(stream,ffse,i2); ffse=(ffs_tEntry *)stream->_BufferBase; } BytesToWrite=size*nitems; NumOfBytesWritten=BytesToWrite; while(BytesToWrite) { *stream->_NextBytePtr++=*pTmpBufPtr++; BytesToWrite--; } if (stream->_AvailableBytes>=NumOfBytesWritten) stream->_AvailableBytes-=NumOfBytesWritten; else stream->_AvailableBytes=0; flen=((bit32u)stream->_NextBytePtr)- ((bit32u)stream->_BufferBase)+ stream->_AvailableBytes- ((bit32u)ffse->fnlen)-(sizeof(ffs_tEntry)-1); _ffs_PutBit24Value(ffse->flen,flen); return(NumOfBytesWritten/size); }int ffs_fclose(FFS_FILE *stream) { int i,BytesToWrite; bit32u flen,fnlen,flsize,pad,padindex; bit8 *cp,*dp; ffs_tEntry *ffse,*ffs; ffse=(ffs_tEntry *)stream->_BufferBase; if ((stream->_FileState&FILE_OPEN)==0) return(EOF); if (stream->_SubSystemType!=FILE_FLASH) return(EOF); if (stream->_FileState&FILE_WRITE) { pad=0; _ffs_DumpFlashFileSystemEntry((ffs_tEntry *)stream->_BufferBase,TRUE); flen=_ffs_GetBit24Value(ffse->flen); fnlen=(bit32u)ffse->fnlen; BytesToWrite=flen+fnlen+(sizeof(ffs_tEntry)-1); ffs=(ffs_tEntry *)stream->SubFileSystem; while(_ffs_GetBit16Value(ffs->inode)!=0x0ffff) ffs=(ffs_tEntry *)(((char *)ffs)+(sizeof(ffs_tEntry)-1)+_ffs_GetBit24Value(ffs->flen)+ffs->fnlen); dp=(bit8 *)ffs; padindex=fnlen+(sizeof(ffs_tEntry)-1); if (_ffs_GetBit16Value(ffse->flags)&FFS_F_BLOCK_ALIGN) { bit32u filedatabase,blocksize; filedatabase = ((bit32u)dp)+padindex; blocksize=FWBGetBlockSize(filedatabase); pad=(blocksize-(((bit32u)filedatabase)&(blocksize-1)))&(blocksize-1); _ffs_PutBit24Value(ffse->flen,flen+pad); } flsize=_ffs_GetBit32Value(((char *)stream->SubFileSystem)-4); /*Get Current Flash Disk Size*/ flsize-=10; /*Reduce for FFS header*/ flsize-=(((bit32u)ffs)-((bit32u)stream->SubFileSystem)); /*Reduce for current use*/ flsize-=pad; /*Reduce for pad */ if (BytesToWrite>flsize) { sys_printf("Insufficient Flash Disk Space!\nCompression may be in order.\n"); stream->_FileState=FILE_FREE; sys_free(ffse); return(EOF); } FWBOpen((bit32u)dp); for(cp=((char *)ffse),i=0;i<BytesToWrite;i++) { if (i==padindex) dp+=pad; if(!FWBWriteByte((bit32u)dp++,cp[i])) { stream->_FileState=FILE_FREE; FWBClose(); sys_free(ffse); return(EOF); } } FWBClose(); sys_free(ffse); } stream->_FileState=FILE_FREE; return(0); }int ffs_fseek(FFS_FILE *stream, bit32 offset, int ptrname) { /*Return 0=OK or EOF=FAIL */ bit32u flen,CurOff,oflen,i1,i; ffs_tEntry *ffse,*i2; char *cp; ffse=(ffs_tEntry *)stream->_BufferBase; if ((stream->_FileState&FILE_OPEN)==0) return(EOF); if (stream->_SubSystemType!=FILE_FLASH) return(EOF); if (_ffs_GetBit16Value(ffse->flags)&FFS_F_BLOCK_ALIGN) return(EOF); flen=((bit32u)stream->_NextBytePtr)- ((bit32u)stream->_BufferBase)+ stream->_AvailableBytes- ((bit32u)ffse->fnlen)-(sizeof(ffs_tEntry)-1); oflen=flen; i1=_ffs_GetBit24Value(ffse->flen); if ((flen!=i1)) { sys_printf("fseek(?,%d,%d); ",offset,ptrname); sys_printf("flen=%d, ffse->flen=%d.\n",flen,i1); } CurOff=((bit32u)stream->_NextBytePtr)- ((bit32u)stream->_BufferBase)- ((bit32u)ffse->fnlen)-(sizeof(ffs_tEntry)-1); switch(ptrname) { case FFS_SEEK_SET: if (offset<0) return(EOF); if ((offset>flen)&&((stream->_FileState&FILE_WRITE)==0)) return(EOF); if (offset>flen) { flen=offset; _ffs_PutBit24Value(ffse->flen,flen); } break; case FFS_SEEK_CUR: if ((CurOff+offset)<0) return(EOF); if (((CurOff+offset)>flen)&&((stream->_FileState&FILE_WRITE)==0)) return(EOF); if ((CurOff+offset)>flen) { flen=CurOff+offset; _ffs_PutBit24Value(ffse->flen,flen); } offset+=CurOff; break; case FFS_SEEK_END: if ((flen+offset)<0) return(EOF); if ((offset>0)&&((stream->_FileState&FILE_WRITE)==0)) return(EOF); if (offset>0) { flen+=offset; _ffs_PutBit24Value(ffse->flen,flen); offset=flen; } else offset+=flen; break; default: return(EOF); } if (flen<oflen) {sys_printf("Got real problems, new len=%d, old len=%d",flen,oflen); while(1) {} } if (flen>oflen) { i2=sys_realloc(ffse,i1=(flen+((bit32u)ffse->fnlen)+(sizeof(ffs_tEntry)-1))); if (!i2) return(EOF); for(i=0,cp=(((char *)i2)+oflen+((bit32u)ffse->fnlen)+(sizeof(ffs_tEntry)-1));i<(flen-oflen);i++) *cp++=0xff; if (ffse!=i2) _ffs_xferstream(stream,ffse,i2); ffse=(ffs_tEntry *)stream->_BufferBase; } stream->_NextBytePtr=(bit8 *)(((bit32u)stream->_BufferBase)+ ((bit32u)ffse->fnlen)+ (sizeof(ffs_tEntry)-1)+ offset); stream->_AvailableBytes=flen-offset; return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -