📄 flashdsk-080.cpp
字号:
else //磁盘已经初始化,初始化磁盘镜像系统区
{
//置磁盘未格式化标志
_IsDiskFormat=0;
sysOffset = dwSysPhSecNum*sizeof(TMapPhSector);
for(i=0;i<phsecnum;i++)
{
//设置磁盘镜像区物理扇区控制信息
if(pFlashSec[i].dwMode == PSM_SYSTEM)
{
//该物理扇区在内存磁盘镜像中的偏移量
pMapSysSec[phs].pOffset = sysOffset+pDiskMap;
//该物理扇区的长度
//pMapSysSec[phs].dwSize = pFlashSec[i].dwSize;
/*定义系统扇区大小 (ZQZ 2002/12/11)*/
pMapSysSec[phs].dwSize = SYS_SECT_SIZE;
//该系统文件区的物理扇区编号
pMapSysSec[phs].nSector = i;
//设置该扇区未改变标志
pMapSysSec[phs].wStatus = MS_CHANGE;
sysOffset += pFlashSec[i].dwSize;
phs++;
}
}
}
pCurrentDir = pMapDir;
DiskInit = TRUE;
return 0;
}
DWORD create_f(char *szFileName,DWORD expand_unit, DWORD mode)
{
char path[30];
char fname[20];
TDirectory *pEntry;
DWORD fat,i;
// Not case sensitive
ToLower(szFileName);
// Find whether the file has existed, if find exit.
pEntry = GetDirEntry(szFileName);
if(pEntry != NULL)
return 3;
// TO find directory entry. Depart full filename to pathname and filename.
// If pathname is null, set the directory to current directory, else find
// directory entry according to path.
SeparatePath(szFileName,path,fname);
if(path[0] != 0)
pEntry = GetDirEntry(path);
else
pEntry = pCurrentDir;
if( pEntry==NULL )
return 4;
// Get a blank logic block.
fat=AllocFreeBlock();
if(fat == 0xFFFFFFFF)
return 1;
// Find a new directory entry for this file.
for( i=0; i<DIR_ENTRY_NUM; i++)
{
if( pEntry[i].wType==0xFFFF )
{
pEntry[i].wType = FT_FILE;
pEntry[i].dwMode = mode;
pEntry[i].dwMagic = 0;
pEntry[i].dwOffset = fat;
pEntry[i].dwLength = 0;
FileName(fname,pEntry[i].szName,pEntry[i].szSuffix);
//ReadAbsTime(&pEntry[i].stModify);
SetMapChangeFlag(&pEntry[i]);
//WriteBlock2Disk((BYTE*)(&pEntry[i])-(DWORD)pMapDesp,&pEntry[i],sizeof(TDirectory));
return 0;
}
}
return 2;
}
//打开一个文件
DWORD open_f(DWORD *hFile,char *name,DWORD mode)
{
TOpenFile *pFile;
TDirectory *pEntry;
DWORD fat,tfat,nfat;
DWORD len;
// Not case sensitive.
ToLower(name);
// Get directory entry.
pEntry = GetDirEntry(name);
if(pEntry == NULL)
{
DebugInfo('O','E','1');
return 1;
}
// Whether the directory entry belong to file.
if(pEntry->wType != FT_FILE)
{
DebugInfo('O','E','2');
return 2;
}
// Get Opened file entry
pFile = (TOpenFile*)FreeFilePool.RemoveHead();
if(pFile == NULL)
{
DebugInfo('O','E','3');
return 3;
}
// If file is opened by overlay mode, remove old file first.
if( mode == FD_OPEN_OVERLAY )
{
// Get a new logic block.
nfat = AllocFreeBlock();
if( nfat==0xFFFFFFFF )
return 4;
// Get the first logic block
fat = pEntry->dwOffset;
// Release the logic block of this file.
len = 0;
while( ( fat!=FAT_FILEEND ) && ( len<pEntry->dwLength ) )
{
// Get the flag ( the flag maybe FAT_FILEEND or the number of next
// logic block ) in FAT table and clear it to FAT_REMOVE.
tfat = pMapFat[fat];
pMapFat[fat] = FAT_REMOVE;
fat = tfat;
len += LOGIC_SECTOR_SIZE;
}
// Reset directory entry
pEntry->dwMagic = 0;
pEntry->dwOffset= nfat;
pEntry->dwLength= 0;
}
// File opened file descriptor.
pFile->pEntry = pEntry;
pFile->dwMagic = OPEN_FILE;
//ydd
if( mode == FD_OPEN_MODIFY )
pFile->dwMode = mode;
else
pFile->dwMode = pEntry->dwMode;
pFile->dwTask = GetAppID(0);
pFile->dwFileLength = pEntry->dwLength;
pFile->dwFileFat = pEntry->dwOffset;
pFile->dwReadOffset = 0;
pFile->dwWriteOffset= 0;
pFile->WriteCache.Init();
// return opened file descriptor.
*hFile = (DWORD)pFile;
DebugInfo('O','O','K');
return 0;
}
//读文件
DWORD read_f(DWORD hFile,void *buf,DWORD count,DWORD *retval)
{
DWORD nread=0;
BYTE* pBuf = (BYTE*)buf;
TOpenFile *pFile = (TOpenFile*)hFile;
DWORD status;
DWORD offset=0;
DWORD num=0;
if( buf==NULL || count==0 || retval==NULL )
return 5;
if(pFile->dwMagic != OPEN_FILE)
return 1;
if(pFile->dwReadOffset >= pFile->dwFileLength)
return 2;
if(pFile->dwFileLength == 0)
return 3;
// Calucate the maxinum number of characters can be read.
if(count+pFile->dwReadOffset >= pFile->dwFileLength)
count = pFile->dwFileLength-pFile->dwReadOffset;
while(1)
{
LoadFatInfo(pFile);
for(int i=0;i<8;i++)
{
offset = pFile->dwReadOffset % LOGIC_SECTOR_SIZE;
num = LOGIC_SECTOR_SIZE-offset;
if(count-nread < num)
num = count-nread;
if(pFile->aAddrIndex[i] == NULL)
{
*retval=nread;
return 4;
}
memcpy(pBuf+nread,pFile->aAddrIndex[i]+offset,num);
nread += num;
pFile->dwReadOffset += num;
//nread = ReadFromSector(i,count);
if(nread >= count)
{
*retval=nread;
return 0;
}
}
}
return 0;
}
DWORD write_f(DWORD hFile,void *buf,DWORD count)
{
BYTE* pBuf ;
TWriteCache *pwrite;
DWORD sector;
DWORD block;
DWORD writenum;
DWORD num = count;
BYTE *pwritebuf=(PBYTE)buf;
TOpenFile *pfile = (TOpenFile*)hFile;
BYTE *wptr = (BYTE*)buf;
//文件句柄非法
if(pfile->dwMagic != OPEN_FILE)
{
DebugInfo('W','E','1');
return 1;
}
if(count == 0)
{
DebugInfo('W','E','2');
return 2;
}
//如果写文件CACHE为空
if(pfile->WriteCache.IsEmpty())
{
//计算当前写文件指针所在的逻辑扇区号
sector = SearchFat(pfile->dwFileFat,pfile->dwWriteOffset);
pwrite = (TWriteCache*)WriteCachePool.RemoveHead();
ASSERT(pwrite != NULL);
pfile->WriteCache.AddTail(&pwrite->Node);
pwrite->dwSector = sector;
pwrite->dwLength = 0;
}
//block = (count+LOGIC_SECTOR_SIZE-1)/LOGIC_SECTOR_SIZE;
//取写CACHE链表的未节点
//pwrite = (TWriteCache*)pfile->wlWriteLink.GetTail();
for(;;)//int i=0;i<block;i++)
{
pwrite = (TWriteCache*)pfile->WriteCache.GetTail();
writenum = Write2Cache(pfile,pwritebuf,num);
#if (__RAM_FILE == NO)
Cache2Disk(pfile); //5.16
#endif
num -= writenum;
pwritebuf += writenum;
if(num == 0)
break;
if((sector=AllocFreeBlock(pwrite->dwSector))==0xFFFFFFFF)
{
/*磁盘满没有空闲逻辑块,返回*/
return 4;
}
/*从写CACHE中分配内存块*/
pwrite = (TWriteCache*)WriteCachePool.RemoveHead();
//ASSERT(pwrite != NULL);
pfile->WriteCache.AddTail(&pwrite->Node);
pwrite->dwSector = sector;
pwrite->dwLength = 0;
}
DebugInfo('W','O','K');
return 0;
}
DWORD close_f(DWORD hFile)
{
TOpenFile *pFile = (TOpenFile*)hFile;
TWriteCache *pwrite;
if(pFile->dwMagic != OPEN_FILE)
return 1;
// 如果写CACHE不空,把CACHE中的数据写到磁盘
Cache2Disk(pFile);
// Reset the file length.
pFile->pEntry->dwLength = pFile->dwFileLength;
// Release the opened file descriptor.
FreeFilePool.AddTail(&pFile->Node);
// Write system area to flash.
Map2Disk(); //deleted by zqz 2002/12/12
return 0;
}
DWORD lseek_f(DWORD hFile,DWORD position,long offset,DWORD *oldptr)
{
TOpenFile *pFile = (TOpenFile *)hFile;
DWORD status = 0;
if(pFile->dwMagic != OPEN_FILE)
{
//LogError("SeekFile",FILE_LINE,"hFile Invalidate");
return 1;
}
switch(position)
{
case SEEK_BEGIN:
if((offset < 0)||(offset > pFile->dwFileLength))
status = 2;
else
{
*oldptr = pFile->dwReadOffset;
pFile->dwReadOffset = offset;
pFile->dwWriteOffset = offset;
status = 0;
}
break;
case SEEK_CURRENT:
if(offset + pFile->dwReadOffset > pFile->dwFileLength)
status = 2;
else
{
*oldptr = pFile->dwReadOffset;
pFile->dwReadOffset += offset;
pFile->dwWriteOffset += offset;
status = 0;
}
break;
case SEEK_END:
if((offset >0)||(offset+pFile->dwFileLength<0))
status = 2;
else
{
*oldptr = pFile->dwReadOffset;
pFile->dwReadOffset = offset + pFile->dwFileLength;
pFile->dwWriteOffset = pFile->dwReadOffset;
status = 0;
}
break;
default:
status =3;
break;
}
return status;
}
DWORD move_f(char *szOldName,char *szNewName)
{
TDirectory *pEntry;
TDirectory *pNewEntry;
char path[30];
char name[20];
// Not case sensitive.
ToLower(szOldName);
ToLower(szNewName);
// Get file entry
pEntry = GetDirEntry(szOldName);
if(pEntry == NULL)
{
//LogError("MoveFile",FILE_LINE,"no file:%s",szOldName);
return 1;
}
// Get new path entry
SeparatePath(szNewName,path,name);
pNewEntry = GetDirEntry(path);
if(pNewEntry == NULL)
return 2;
// Move file descriptor to old path entry to new path entry.
for(int i=0;i<DIR_ENTRY_NUM;i++)
{
//找到空的文件目录入口
if(pNewEntry[i].wType == 0xFFFF)
{
pNewEntry[i].wType = FT_FILE;
FileName(name,pNewEntry[i].szName,pNewEntry[i].szSuffix);
//ReadAbsTime(&pEntry[i].stModify);
pNewEntry[i].dwMode = pEntry->dwMode;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -