bool CWpf::WpfOpen(const char * strPathName,__int64 iOffset,DWORD dwOpenType)
{
if(strPathName == NULL || dwOpenType == 0)
{
m_eWrLastError = EWR_PARA_ERROR;
return false;
}
if(m_bSaving)
{
m_eWrLastError = EWR_FILE_SAVING;
return false;
}
//先关闭当前打开的wpf文件
WpfClose();
m_strWpfDir = strPathName;
replace(m_strWpfDir.begin(),m_strWpfDir.end(),'/','\\');
size_t iPos = m_strWpfDir.find_last_of("\\");
if (iPos != string::npos)
{
m_strWpfFileName = m_strWpfDir.substr(iPos + 1,m_strWpfDir.length() - iPos - 1);
m_strWpfDir = m_strWpfDir.substr(0,iPos);
}
//打开或创建文件
errno_t err = 0;
if (dwOpenType & EWOT_RDWR)
{
err = _sopen_s(&(m_iFileHandle[EFT_MAIN]), strPathName, _O_BINARY | _O_RDWR , _SH_DENYWR, _S_IREAD | _S_IWRITE );
dwOpenType |= EWOT_LISTDIR;
}
else
{
err = _sopen_s(&(m_iFileHandle[EFT_MAIN]), strPathName, _O_BINARY | _O_RDONLY , /*_SH_DENYWR*/_SH_DENYNO, _S_IREAD);
if (dwOpenType & EWOT_READONLY)
{
dwOpenType |= EWOT_LISTHASH;
}
}
if(err != 0)
{
m_eWrLastError = EWR_FILE_OPEN_FAIL;
return false;
}
for (int i = EFT_THREAD ; i < EFT_NUM; i ++)
{
err = _sopen_s(&(m_iFileHandle[i]), strPathName, _O_BINARY | _O_RDONLY , _SH_DENYNO, _S_IREAD);
if(err != 0)
{
m_eWrLastError = EWR_FILE_OPEN_FAIL;
return false;
}
}
m_iWpfStartPos = iOffset;
m_WpfHeader.dwMagic = 0;//防止有些文件长度不够sizeof(S_WpfHeader),取到默认值
m_WpfHeader_Bak.dwMagic = 0;//防止有些文件长度不够sizeof(S_WpfHeader),取到默认值
m_WpfHeader.bySavingHeader = TRUE;//防止有些文件长度不够sizeof(S_WpfHeader),取到默认值
m_WpfHeader_Bak.bySavingHeader = TRUE;//防止有些文件长度不够sizeof(S_WpfHeader),取到默认值
//读入文件头
_lseeki64(m_iFileHandle[EFT_MAIN],m_iWpfStartPos,SEEK_SET);
_read(m_iFileHandle[EFT_MAIN],&m_WpfHeader,sizeof(S_WpfHeader));
//读入文件头备份
_lseeki64(m_iFileHandle[EFT_MAIN],m_iWpfStartPos + sizeof(S_WpfHeader),SEEK_SET);
_read(m_iFileHandle[EFT_MAIN],&m_WpfHeader_Bak,sizeof(S_WpfHeader));
if(m_WpfHeader.bySavingHeader && m_WpfHeader_Bak.bySavingHeader)//该文件在创建的时候出现异常,如断点,程序崩溃等情况,没有正常写入头
{
return false;
}
//如果上次文件头没有正确保存,读取必份的文件
if (m_WpfHeader.bySavingHeader)
{
m_WpfHeader = m_WpfHeader_Bak;
m_bModifyed = true;//原来没有保存好,用了备份的,那么这次即使没有修改也要重新保存
}
m_WpfHeader_Disk = m_WpfHeader;
if(m_WpfHeader.dwMagic != WPF_MAGIC)
{
m_eWrLastError = EWR_NOT_WPF_FILE;
WpfClose();
return false;
}
m_WpfHeader.bySavingHeader = FALSE;//理论上不可能出现文件头及备份文件头m_WpfHeader.bySavingHeader同时为TRUE,还是清一下标记
m_dwOpenType = dwOpenType;
//如果以写方式打开要求读入空闲块描述信息
if ((m_dwOpenType & EWOT_RDWR) && m_WpfHeader.dwBlankBlockSize > 0)
{
PBlankBlock pBlock = new BlankBlock[m_WpfHeader.dwBlankBlockSize];
_lseeki64(m_iFileHandle[EFT_MAIN],m_iWpfStartPos + m_WpfHeader.iBlankBlockPos,SEEK_SET);
_read(m_iFileHandle[EFT_MAIN],pBlock,m_WpfHeader.dwBlankBlockSize * sizeof(BlankBlock));
PBlankBlock p = pBlock;
PBlankBlockNode pList = NULL,pList2 = NULL;
pList = new BlankBlockNode();
pList->blankBlock.dwStart = p->dwStart;
pList->blankBlock.dwSize = p->dwSize;
pList->pParent = NULL;
pList->pNext = NULL;
m_pBlankBlockList = pList;
pList2 = pList;
p ++;
for (DWORD i = 1; i < m_WpfHeader.dwBlankBlockSize; i++,p++)
{
pList = new BlankBlockNode();
pList->blankBlock.dwStart = p->dwStart;
pList->blankBlock.dwSize = p->dwSize;
pList->pParent = pList2;
pList2->pNext = pList;
pList2 = pList;
}
pList2->pNext = NULL;
SAFE_DELETE_ARRAY(pBlock);
}
//载入文件分配表
return WpfLoadFat();
}
声明:本文系互联网搜索而收集整理,不以盈利性为目的,文字、图文资料源于互联网且共享于互联网。
如有侵权,请联系 yao4fvip#qq.com (#改@) 删除。
如有侵权,请联系 yao4fvip#qq.com (#改@) 删除。