📄 common.cc
字号:
if(0 == fp)
{
sprintf(chAlertMsg, "%s error!", data->chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(FOPEN_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
data->iStatus = htonl(STATUS_ERROR);
gv_TranMonitor.AddRate(sizeof(S_DataInfo));
return WriteSockData(iSockfd, data);
}
if(fseek(fp, ntohl(data->lFilePos), SEEK_SET) != 0)
{
sprintf(chAlertMsg, "%s error!", data->chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(FSEEK_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
data->iStatus = htonl(STATUS_ERROR);
fclose(fp);
gv_TranMonitor.AddRate(sizeof(S_DataInfo));
return WriteSockData(iSockfd, data);
}
data->iStatus = htonl(STATUS_SENDFILE);
memset(data->chData, 0, lDataLen);
lReadLen = fread(data->chData, sizeof(char), lDataLen-1, fp);
if(lReadLen < lDataLen-1)
{
/*这是最后的文件块*/
data->iStatus = htonl(STATUS_LASTDATA);
}
fclose(fp);
data->lDataLen = htonl(lReadLen);
gv_TranMonitor.AddRate(sizeof(S_DataInfo));
return WriteSockData(iSockfd, data);
}
/****************************************************************
**
** Function:ZipFile
** Purpose:压缩文件
** Input Parammeters:
** Return:成功返回0,失败返回-1。
** datetime:
**
*****************************************************************/
int ZipFile(char *chFileName)
{
char chShellCmd[STRING_LEN];
char chAlertMsg[STRING_LEN];
if(IsZipFile(chFileName))
{
return 1;
}
sprintf(chShellCmd, "compress -f %s", chFileName);
if(system(chShellCmd) == -1)
{
sprintf(chAlertMsg, "%s error!",chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(ZIPFILE_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
return -1;
}
return 0;
}
/****************************************************************
**
** Function:UnZipFile
** Purpose:解压文件
** Input Parammeters:
** Return:成功返回0,失败返回-1。
** datetime:
**
*****************************************************************/
int UnZipFile(char *chFileName)
{
char chShellCmd[STRING_LEN];
char chAlertMsg[STRING_LEN];
sprintf(chShellCmd, "uncompress -f %s", chFileName);
if(system(chShellCmd) == -1)
{
sprintf(chAlertMsg, "%s error!", chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(UNZIPFILE_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
return -1;
}
return 0;
}
/****************************************************************
**
** Function:IsZipFile
** Purpose:判断文件是否是压缩文件
** Input Parammeters:
** Return:是则返回1,否则返回0。
** datetime:
**
*****************************************************************/
/*因为不知到Lempel-Zev algorithm文件头,所以只用文件名判断*/
int IsZipFile(char *chFileName)
{
int iFileNameLen;
iFileNameLen = strlen(chFileName);
if(chFileName[iFileNameLen-1] == 'Z' && chFileName[iFileNameLen-2] == '.')
{
/*已压缩*/
return 1;
}
return 0;
}
/****************************************************************
**
** Function:SaveFile
** Purpose:把数据存放到文件中
** Input Parammeters:
** Return:
** datetime:
**
*****************************************************************/
int SaveFile(struct S_DataInfo *data, char *chLocalDir)
{
char chFullName[STRING_LEN];
char chAlertMsg[STRING_LEN];
FILE *fp = NULL;
long lFileLen;
long lWriteLen;
lFileLen = 0;
if(chLocalDir[strlen(chLocalDir)-1] == '/')
{
sprintf(chFullName, "%s%s", chLocalDir, data->chFileName);
}
else
{
sprintf(chFullName, "%s/%s", chLocalDir, data->chFileName);
}
if(access(chFullName, F_OK) == -1)
{
fp = fopen(chFullName, "wb+");
}
else
{
fp = fopen(chFullName, "rb+");
}
if(fp == 0)
{
sprintf(chAlertMsg, "%s error!", data->chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(FOPEN_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
return -1;
}
if(ntohl(data->lFilePos) != 0)
{
if(fseek(fp, ntohl(data->lFilePos), SEEK_SET))
{
sprintf(chAlertMsg, "%s error!", data->chFileName);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(FSEEK_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
data->iStatus = htonl(STATUS_ERROR);
fclose(fp);
return -1;
}
}
lWriteLen = fwrite(data->chData, sizeof(char), ntohl(data->lDataLen), fp);
if(lWriteLen < ntohl(data->lDataLen))
{
sprintf(chAlertMsg, "%s error!", data->chFileName);
lFileLen == -1;
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(FWRITE_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
/****************************************************************
**
** Function:CheckData
** Purpose:校验数据
** Input Parammeters:
** Return:正确返回0,错误返回-1。
** datetime:
**
*****************************************************************/
int CheckData(struct S_DataInfo *data)
{
long lValue = 0;
lValue += ntohl(data->iTransferType);
lValue += ntohl(data->lFilePos);
lValue += ntohl(data->iCompress);
lValue += ntohl(data->iStatus);
lValue += ntohl(data->lFileLen);
lValue += ntohl(data->lZipSize);
lValue += ntohl(data->lDataLen);
if(lValue == ntohl(data->lCheckValue))
{
/*校验值正确*/
return 0;
}
else
{
/*校验值错误*/
return -1;
}
}
/****************************************************************
**
** Function:SetCheckValue
** Purpose:设置校验位
** Input Parammeters:
** Return:
** datetime:
**
*****************************************************************/
int SetCheckValue(struct S_DataInfo *data)
{
long lValue = 0;
lValue += ntohl(data->iTransferType);
lValue += ntohl(data->lFilePos);
lValue += ntohl(data->iCompress);
lValue += ntohl(data->iStatus);
lValue += ntohl(data->lFileLen);
lValue += ntohl(data->lZipSize);
lValue += ntohl(data->lDataLen);
data->lCheckValue = htonl(lValue);
return 0;
}
/****************************************************************
**
** Function:FileToTemp
** Purpose:将要传输的文件改成临时文件
** Input Parammeters:
** Return:
** datetime:
**
*****************************************************************/
int FileToTemp(char *chSrcPath, char *chSrcFile, char *chDescPath, char *chDestFile)
{
char chSrcFullName[STRING_LEN];
char chDescFullName[STRING_LEN];
char chTempFile[STRING_LEN];
char chAlertMsg[STRING_LEN];
memset(chSrcFullName, 0, sizeof(chSrcFullName));
memset(chDescFullName, 0, sizeof(chDescFullName));
memset(chTempFile, 0, sizeof(chTempFile));
memset(chAlertMsg, 0, sizeof(chAlertMsg));
strcpy(chSrcFullName, chSrcPath);
FullPath(chSrcFullName);
strcat(chSrcFullName, chSrcFile);
strcpy(chDescFullName, chDescPath);
FullPath(chDescFullName);
sprintf(chTempFile, "~%s", chSrcFile);
strcat(chDescFullName, chTempFile);
strcpy(chDestFile, chTempFile);
if(rename(chSrcFullName, chDescFullName) == -1)
{
sprintf(chAlertMsg, "%s error!", chSrcFile);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(RENAME_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
return -1;
}
return 0;
}
/****************************************************************
**
** Function:TempToFile
** Purpose:将临时文件改成正常的文件
** Input Parammeters:
** Return:
** datetime:
**
*****************************************************************/
int TempToFile(char *chSrcPath, char *chSrcFile, char * chDescPath, char *chDestFile)
{
char chSrcFullName[STRING_LEN],chDescFullName[STRING_LEN];
char chFile[STRING_LEN];
char chAlertMsg[STRING_LEN];
strcpy(chSrcFullName,chSrcPath);
FullPath(chSrcFullName);
strcat(chSrcFullName,chSrcFile);
strcpy(chDescFullName,chDescPath);
FullPath(chDescFullName);
strcpy(chFile,chSrcFile+1);
strcat(chDescFullName,chFile);
strcpy(chDestFile,chFile);
if(rename(chSrcFullName,chDescFullName)==-1)
{
sprintf(chAlertMsg, "%s error!", chSrcFile);
pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
WriteAlert(RENAME_ERROR, chAlertMsg, ALERT_LEVEL_3);
pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
return -1;
}
return 0;
}
//---------------------------------- THE END ----------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -