Files
coco df489d5640 a
2026-07-03 16:05:30 +08:00

155 lines
5.2 KiB
C++

// MyCopyFile.cpp: implementation of the CMyCopyFile class.
//
//////////////////////////////////////////////////////////////////////
#include "geomative.h"
#include "MyCopyFile.h"
#include "DetcGD10Dev.h"
#include "FileOperTools.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
FILE* CMyCopyFile::g_pCopyFileLog = NULL;
CRITICAL_SECTION* CMyCopyFile::g_pLogCriticSec = NULL;
extern SYSTEMTIME g_sysCurTime;
extern BOOL g_bCancelCopyFile;
extern int g_iUILanguage;
CMyCopyFile::CMyCopyFile()
{
if (NULL == g_pCopyFileLog)
{
g_pCopyFileLog = fopen("LOG\\transfer_file.txt","ab+");
if (NULL == g_pCopyFileLog)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("打开转发文件日志失败"));
else
MessageBoxEx(NULL, _T("Open transfer file log failed!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
}
if (NULL == g_pLogCriticSec)
{
g_pLogCriticSec = new CRITICAL_SECTION;
if (NULL == g_pLogCriticSec)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("创建临界区失败!"));
else
MessageBoxEx(NULL, _T("Create CRITICAL_SECTION failed!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
else
InitializeCriticalSection(g_pLogCriticSec);
}
memset(&m_liTotalFileSize, 0, sizeof(m_liTotalFileSize));
memset(&m_liTransferedBytes, 0, sizeof(m_liTransferedBytes));
m_iErrorCode = -1;
m_bIsCancelCopy = FALSE;
}
CMyCopyFile::~CMyCopyFile()
{
}
void CMyCopyFile::Initialize()
{
memset(&m_liTotalFileSize, 0, sizeof(m_liTotalFileSize));
memset(&m_liTransferedBytes, 0, sizeof(m_liTransferedBytes));
m_iErrorCode = -1;
}
void CMyCopyFile::GetTransferInfo(DWORD &dwTransferedBytes, DWORD &dwTotalFileSize)
{
dwTransferedBytes = m_liTransferedBytes.LowPart;
dwTotalFileSize = m_liTotalFileSize.LowPart;
}
bool CMyCopyFile::TransferFile(CString strSrcDir, CString strDstDir, CString strFileName)
{
CString strText = _T("");
CString strDstFilePath = _T("");
strDstFilePath.Format(_T("%s\\%s"),strDstDir,strFileName);
DeleteFile(strDstFilePath);
CString strSrcFilePath = _T("");
strSrcFilePath.Format(_T("%s\\%s"),strSrcDir,strFileName);
if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(strSrcFilePath))
{
strText.Empty();
strText.Format(_T("Get SrcFile Attributes failed, filepath = %s"),strSrcFilePath);
PrintLog(strText);
m_iErrorCode = EN_GET_SRCATTRI_FAILED;
return false;
}
m_bIsCancelCopy = FALSE;
if (0 == CopyFileEx(strSrcFilePath,strDstFilePath,CopyProgressInfo,(LPVOID)this,
&m_bIsCancelCopy,COPY_FILE_FAIL_IF_EXISTS))
{
m_iErrorCode = GetLastError();
strText.Empty();
if (ERROR_REQUEST_ABORTED == m_iErrorCode)
strText.Format(_T("copy file failed because of progress_routine cancel copy operation,srcfile = %s,dstfile = %s"),
strSrcFilePath,strDstFilePath);
else
strText.Format(_T("copy file failed, errorno = %d, srcfile = %s, dstfile = %s"),
m_iErrorCode, strSrcFilePath, strDstFilePath);
PrintLog(strText);
return false;
}
return true;
}
void CMyCopyFile::PrintLog(const CString& strLog)
{
if (NULL == g_pCopyFileLog)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("无法写入transfer_log,日志文件点为空"));
else
MessageBoxEx(NULL, _T("Can't write transfer_log, log File point is NULL"), _T("LOG_ERROR"), MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return;
}
if (NULL == g_pLogCriticSec)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("不能写transfer_log,日志临界切片点为空"));
else
MessageBoxEx(NULL, _T("Can't write transfer_log, log critical section point is NULL"), _T("LOG_ERROR"), MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return;
}
EnterCriticalSection(g_pLogCriticSec);
CString strOutPut = _T("");
strOutPut.Format(_T("%04d-%02d-%02d %02d:%02d:%02d.%03d %s \r\n"),g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay,
g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond, g_sysCurTime.wMilliseconds, strLog);
fwrite(strOutPut, 1, strOutPut.GetLength(), g_pCopyFileLog);
fflush(g_pCopyFileLog);
LeaveCriticalSection(g_pLogCriticSec);
}
DWORD CALLBACK CMyCopyFile::CopyProgressInfo(LARGE_INTEGER iTotalFileSize, LARGE_INTEGER iTotalBytesTransferred, LARGE_INTEGER iStreamSize,
LARGE_INTEGER iStreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason,
HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData)
{
CMyCopyFile* pCopyFile = (CMyCopyFile*)lpData;
pCopyFile->m_liTotalFileSize = iTotalFileSize;
pCopyFile->m_liTransferedBytes = iTotalBytesTransferred;
//如果此时发现USB设备被拔出,则取消传输
if (FALSE == CDetcGD10Dev::GetInstance()->IsGD10DevConnect())
{
CString strLog = _T("");
strLog.Format(_T("[CopyFile][%d]Usb device was pull out,copy file was canceled!"), __LINE__);
CFileOperTools::GetInstance()->WriteComLog(strLog);
return PROGRESS_CANCEL;
}
return PROGRESS_CONTINUE;
}