?? backup_disk_sector.cpp
字號:
/********************************************************************
created: 2004/09/27
created: 27:9:2004 22:22
filename: c:\sun\srcwork\tools\cmdtools\backup_disk_sector.cpp
file path: c:\sun\srcwork\tools\cmdtools
file base: backup_disk_sector
file ext: cpp
author: 孫寶建(sunbaojian)
version: 1.0.0
purpose: 備份硬盤分區(qū)表
*********************************************************************/
#include "StdAfx.h"
#ifndef _INCLUDE_BACKUP_DISK_SECTOR_HPP_INCLUDE
#include "backup_disk_sector.hpp"
#endif
SLIB_DEFINE_FILE_NAME_VARIABLE
CBackupDiskSector::~CBackupDiskSector(void)
{
m_pPartitionListFile.close();
CloseAllRestoreFile();
}
void CBackupDiskSector::PrintMsg(const string &s)
{
cout<< s;
}
void CBackupDiskSector::BackupAllDisk(void)
{
}
BOOL CBackupDiskSector::BackupOneDisk(const string & spath)
{
DWORD ret = ERR_NO_ERROR;
m_sPath = spath;
ostringstream ostr;
ostr.exceptions(ios_base::badbit|ios_base::failbit);
if (!m_sPath.empty())
{
ostr<< m_sPath
#ifdef WIN32
<< "\\";
#else
<< '/';
#endif
}
ostr<< 'd' << setw(2) << setfill(_T('0')) << hex << uppercase << m_pInt13->GetActiveDisk() << "_list.txt";
m_pPartitionListFile.open(ostr.str().c_str(), ios_base::out | ios_base::trunc);
m_pPartitionListFile.imbue(locale("C"));
if (m_pPartitionListFile)
{
try
{
PrintMsg("Backup disk partition and BOOT sector ...\n");
BackupPrimerPartition(0);
}
catch(DWORD e)
{
ret = e;
}
catch(string & e)
{
ret = ERR_ERROR;
m_pError->AddError(e.c_str());
}
m_pPartitionListFile.close();
}
else
{
ret = ERR_ERROR;
m_pError->AddError(ERR_FILE_OPEN);
}
PrintMsg("Backup disk partition and BOOT sector OK!\n");
return (ret == ERR_NO_ERROR ? TRUE : FALSE);
}
void CBackupDiskSector::RestoreAllDisk(void)
{
}
BOOL CBackupDiskSector::RestoreOneDisk(const string & fileName)
{
// string name = GetFileName(fileName);
m_sPath = GetFilePath(fileName);
cout<<"path= "<<m_sPath<<endl;
if (CheckRestoreFiles(fileName))
{
DISK_BACKUP_SECTOR partition;
PrintMsg("Start restore sector ...\n");
for (int i=0; i< m_iRestoreFileCount; i++)
{
m_pRestoreIFilePointer[i]->read((char*)&partition, sizeof(DISK_BACKUP_SECTOR));
if (!m_pRestoreIFilePointer[i]->fail())
{
if (partition.SectorPosition != m_SectorPosition[i])
{
PrintMsg("Sector Position is different with original position\n");
}
ostringstream ostr;
ostr<< "Restore sector " <<hex<<uppercase<<showbase << m_SectorPosition[i]
<<" "<<endl;
PrintMsg(ostr.str());
if (!m_pInt13->WriteSector(m_SectorPosition[i], 1, &partition))
return FALSE;
}
else
{
m_pError->AddError(ERR_FILE_READ);
return FALSE;
}
}
PrintMsg("Restore disk partition and BOOT sector OK!!!\n");
return TRUE;
}
else
{
m_pError->AddError("Check Restore Files fail");
return FALSE;
}
}
void CBackupDiskSector::CloseAllRestoreFile(void)
{
for (int i=0; i<c_DISK_BACKUP_MAX_PARTITIONS ;i++)
{
if (m_pRestoreIFilePointer[i] != NULL)
{
m_pRestoreIFilePointer[i]->close();
delete m_pRestoreIFilePointer[i];
m_pRestoreIFilePointer[i] = NULL;
}
}
}
BOOL CBackupDiskSector::CheckRestoreFiles(const string & sfileName)
{
PrintMsg("Checking list files ...\n");
ifstream ifs;
string soneFileName;
string fullName;
ifs.open(sfileName.c_str());
ifs.imbue(locale("C"));
if (ifs)
{
CloseAllRestoreFile();
int i=0;
do
{
char buf[_MAX_PATH];
ifs.getline(buf, _MAX_PATH-1);
istringstream istr(buf);
istr>> soneFileName;
if (istr.fail())
{
continue;
}
if (soneFileName.at(0) == '#')
{
PrintMsg(soneFileName);
PrintMsg("\n");
}
else
{
istr>> hex >>m_SectorPosition[i];
if (istr.fail())
{
CloseAllRestoreFile();
return FALSE;
}
m_pRestoreIFilePointer[i] = new ifstream;
fullName = m_sPath + soneFileName ;
PrintMsg("check file: \"");
PrintMsg(fullName);
PrintMsg("\"\n");
m_pRestoreIFilePointer[i]->open(fullName.c_str(), ios_base::binary);
m_pRestoreIFilePointer[i]->imbue(locale("C"));
if (*m_pRestoreIFilePointer[i])
{
i++;
}
else
{
PrintMsg("Can not open file \"");
PrintMsg(fullName);
PrintMsg("\"\n");
CloseAllRestoreFile();
return FALSE;
}
}
}while(!ifs.eof() && i < c_DISK_BACKUP_MAX_PARTITIONS);
if (i >= c_DISK_BACKUP_MAX_PARTITIONS)
{
PrintMsg("Too many partitions! Restore some!\n");
}
m_iRestoreFileCount = i;
PrintMsg("Check list files OK!!!\n");
return TRUE;
}
else
{
PrintMsg("Can not open file \"");
PrintMsg(sfileName);
PrintMsg("\"\n");
m_pError->AddError(ERR_FILE_OPEN);
}
return FALSE;
}
void CBackupDiskSector::BackupPrimerPartition(UINT64 sectorPosition)
{
DISK_BACKUP_SECTOR partition;
if (m_iPartitionCount >= c_DISK_BACKUP_MAX_PARTITIONS)
{
PrintMsg("Too many partitions! Backup stop!\n");
return ;
}
if (m_pInt13->ReadSector(sectorPosition, 1, &partition))
{
string fileName ;
string fullFileName;
GenFileName(fileName, c_DISK_BACKUP_TYPE_PARTITION);
sprintf(partition.cSectorPositionString, "%08llX", sectorPosition);
strcpy(partition.cFileName, fileName.c_str());
partition.SectorPosition = sectorPosition;
ostringstream ostr;
ostr<<fileName.c_str() << " " << hex << uppercase << sectorPosition << endl;
m_pPartitionListFile<< ostr.str();
PrintMsg("File list ");
PrintMsg(ostr.str());
GenFullFileName(fileName, fullFileName);
WriteFile(fullFileName.c_str(), &partition, sizeof(partition));
for (int i=0; i<4; i++)
{
if (partition.partition.m_Mbr[i].ByPartitionType == 0)
continue;
if (IsContainerPartition(partition.partition.m_Mbr[i].ByPartitionType))
{
if (m_ExtenedPartitionPositition == 0)
{
m_ExtenedPartitionPositition = partition.partition.m_Mbr[i].dwPreservedSector;
BackupExtendedPartition(0);
}
else
{
BackupExtendedPartition(partition.partition.m_Mbr[i].dwPreservedSector);
}
}
else
{
BackupLogicalDisk(partition.partition.m_Mbr[i].dwPreservedSector+sectorPosition);
}
}
}
else
{
throw string("CBackupDiskSector::BackupPrimerPartition:m_pInt13->ReadSector");
}
}
void CBackupDiskSector::BackupExtendedPartition(UINT64 sectorPosition)
{
BackupPrimerPartition(sectorPosition+m_ExtenedPartitionPositition);
}
BOOL CBackupDiskSector::WriteFile(const char * pfileName, void *buf, int len)
{
auto_ptr<ofstream> pofs(new tofstream);
pofs->exceptions(ios_base::badbit|ios_base::failbit);
pofs->open(pfileName, ios_base::out | ios_base::binary | ios_base::trunc);
pofs->imbue(locale("C"));
PrintMsg("Save file ");
PrintMsg(pfileName);
PrintMsg("\n");
//m_pFile->imbue(locale(".936")); //中文中國,用.數字表示區(qū)域名稱
pofs->write((char*)buf, len);
pofs->close();
return pofs->good();
}
void CBackupDiskSector::GenFullFileName(const string & fileName, string & fullName)
{
ostringstream ostr;
ostr.exceptions(ios_base::badbit|ios_base::failbit);
if (!m_sPath.empty())
{
ostr<< m_sPath
#ifdef WIN32
<< "\\";
#else
<< '/';
#endif
}
ostr<< fileName;
fullName = ostr.str();
}
void CBackupDiskSector::GenFileName(string & sfileName, int type)
{
ostringstream ostr;
ostr.exceptions(ios_base::badbit|ios_base::failbit);
ostr<< 'd' << setw(2) << setfill(_T('0')) << hex << uppercase << m_pInt13->GetActiveDisk();
switch (type)
{
case c_DISK_BACKUP_TYPE_PARTITION:
{
ostr<< 'p' << m_iPartitionCount++;
break;
};
case c_DISK_BACKUP_TYPE_BOOT:
{
ostr<< 'b' << m_iBootCount++;
break;
}
default:
throw std::invalid_argument("CBackupDiskSector::GenFileName");
}
ostr << ".dat" ;
sfileName = ostr.str();
}
void CBackupDiskSector::BackupLogicalDisk(UINT64 startSector)
{
DISK_BACKUP_SECTOR partition;
if (m_pInt13->ReadSector(startSector, 1, &partition))
{
string fileName ;
string fullFileName ;
GenFileName(fileName, c_DISK_BACKUP_TYPE_BOOT);
sprintf(partition.cSectorPositionString, "%08llX", startSector);
strcpy(partition.cFileName, fileName.c_str());
partition.SectorPosition = startSector;
GenFullFileName(fileName, fullFileName);
WriteFile(fullFileName.c_str(), &partition, sizeof(partition));
m_pPartitionListFile<< GetFileName(fileName).c_str() << " " << hex << uppercase << startSector << endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -