⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
📖 第 1 页 / 共 2 页
字号:
				GetFullName(data, chFullName);
				if(SendFile(iSockfd, data, chFullName) == -1)
				{
					return -1;
				}
				break;
			/*客户已收到最后一个数据包*/
			case STATUS_LASTDATA:
				if(1 == GobalInfo.iBackupFlag)
				{
					//备份
					if(BackupFile(data) != 0)
					{
						pthread_mutex_lock(&GobalMutex.AlertMutex);
						Msg("Backup file %s failure", data->chFileName);
						pthread_mutex_unlock(&GobalMutex.AlertMutex);
						return -1;
					}
				}
				else
				{
					//不备份
					if(DeleteFile(data) == -1)
					{
						pthread_mutex_lock(&GobalMutex.AlertMutex);
						Msg("delete file %s failure", data->chFileName);
						pthread_mutex_unlock(&GobalMutex.AlertMutex);
						return -1;
					}
				}
				strcpy(chFileName, data->chFileName);
				if(IsZipFile(chFileName) == 1)
				{
					chFileName[strlen(chFileName)-2] = '\0';
				}
				pthread_mutex_lock(&GobalMutex.LogMutex);
				Log("get %s success", chFileName+1);
				//Log("get %s success %d", chFileName+1, iSockfd);
				pthread_mutex_unlock(&GobalMutex.LogMutex);
				data->iStatus = htonl(STATUS_FINISH);
				if(WriteSockData(iSockfd, data) == -1)
				{
					return -1;
				}
				break;
			/*客户想终止本次会话*/
			case STATUS_BYBY:
				iEnd = 1;
				break;
			/*数据包错误*/
			default:
				pthread_mutex_lock(&GobalMutex.AlertMutex);
				Msg("Read Data Packet Error");
				pthread_mutex_unlock(&GobalMutex.AlertMutex);
				return -1;
		}//end switch(ntohl(data->iStatus))
		if(iEnd)
		{
		   break;
		}
		if(GetSockData(iSockfd, data) == -1)
		{
			return -1;
		}
	}//end while(!iEnd)
	//close(iSockfd);
	//iSockfd = -1;
	return 0;
}

/****************************************************************
**
**	Function:SendFileInfo
**	Purpose:发送文件信息
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int SendFileInfo(int iSockfd, struct S_DataInfo *data)
{
	char chFullName[STRING_LEN];
	long lFileLen = 0;
	long lZipFileLen = 0;
	int iResult;

	if(strlen(data->chFileName) != 0)
	{
		/*断点续传*/
		GetFullName(data, chFullName);
		if(!access(chFullName, F_OK))
		{
			if(IsZipFile(data->chFileName))
			{
				lZipFileLen = GetFileSize(chFullName);
				if(-1 == lZipFileLen)
				{
					pthread_mutex_lock(&GobalMutex.AlertMutex);
					Msg("GetFileSize");
					pthread_mutex_unlock(&GobalMutex.AlertMutex);
				}
				data->lFileLen = htonl(lFileLen);
				data->lZipSize = htonl(lZipFileLen);
				data->iStatus = htonl(STATUS_FILEEXIST);
			}
			else
			{
				lFileLen = GetFileSize(chFullName);
				if(-1 == lFileLen)
				{
					pthread_mutex_lock(&GobalMutex.AlertMutex);
					Msg("GetFileSize");
					pthread_mutex_unlock(&GobalMutex.AlertMutex);
				}
				data -> lFileLen = htonl(lFileLen);
				data -> iStatus = htonl(STATUS_FILEEXIST);
			}
		}
		else
		{
			data->iStatus = htonl(STATUS_NOFILE);
		}
	}
	else
	{
		pthread_mutex_lock(&GobalMutex.SearchFileMutex);
		if(SearchFile(data) == -1)
		{
			pthread_mutex_unlock(&GobalMutex.SearchFileMutex);
			pthread_mutex_lock(&GobalMutex.AlertMutex);
			Msg("SearchFile");
			pthread_mutex_unlock(&GobalMutex.AlertMutex);
			return -1;
		}
		pthread_mutex_unlock(&GobalMutex.SearchFileMutex);
		if(ntohl(data->iStatus) == STATUS_FILEEXIST)
		{
			GetFullName(data, chFullName);
			lFileLen = GetFileSize(chFullName);
			if(-1 == lFileLen)
			{
				pthread_mutex_lock(&GobalMutex.AlertMutex);
				Msg("GetFileSize");
				pthread_mutex_unlock(&GobalMutex.AlertMutex);
			}
			if(COMPRESS == ntohl(data->iCompress))
			{
				iResult = ZipFile(chFullName);
				switch(iResult)
				{
					/*压缩文件错误*/
					case -1:
						return -1;
					/*压缩文件正常,改文件名*/
					case 0:
						strcat(data->chFileName, ".Z");
						strcat(chFullName, ".Z");
						break;
					/*文件已经被压缩,无需改文件名*/
					default:
						break;
				}
				lZipFileLen = GetFileSize(chFullName);
				if(-1 == lZipFileLen)
				{
					pthread_mutex_lock(&GobalMutex.AlertMutex);
					Msg("GetFileSize");
					pthread_mutex_unlock(&GobalMutex.AlertMutex);
				}
				data->lFileLen = htonl(lFileLen);
				data->lZipSize = htonl(lZipFileLen);
				data->iStatus = htonl(STATUS_FILEEXIST);
			}
			else
			{
				data->lFileLen = htonl(lFileLen);
				data->lZipSize = htonl(0);
				data->iStatus = htonl(STATUS_FILEEXIST);
			}
		}
	}
	if(WriteSockData(iSockfd, data) == -1)
	{
		return -1;
	}
	return 0;
}

/****************************************************************
**
**	Function:ProcessPutConnect
**	Purpose:处理Put连接
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int ProcessPutConnect(int iSockfd, struct S_DataInfo *data)
{
	long lFileLen = 0;
	char chFullName[STRING_LEN];
	char chFile[STRING_LEN];
	char chFileName[STRING_LEN];
	int iResult = 0;

	for(;;)
	{
		switch(ntohl(data->iStatus))
		{
			/*发送信息头*/
			case STATUS_FILEINFO:
				lFileLen = 0;
				if(GetFileInfo(iSockfd, data) == -1)
				{
					return -1;
				}
				break;
			/*发送下一块数据块*/
			case STATUS_SENDFILE:
				if(SaveFile(data, data->chRemotePath) == -1)
				{
					return -1;
				}
				lFileLen += ntohl(data->lDataLen);
				data->iStatus = htonl(STATUS_SENDFILE);
				data->lFilePos = htonl(lFileLen);
				if(WriteSockData(iSockfd, data) == -1)
				{
					return -1;
				}
				break;
			/*最后的文件块*/
			case STATUS_LASTDATA:
				if(SaveFile(data, data->chRemotePath) == -1)
				{
					return -1;
				}
				strcpy(chFileName, data->chFileName);

				if(1 == IsZipFile(chFileName))
				{
					chFileName[strlen(chFileName)-2] = '\0';
				}

				data->iStatus = htonl(STATUS_LASTDATA);
				data->lFilePos = htonl(lFileLen);
				if(WriteSockData(iSockfd, data) == -1)
				{
					return -1;
				}
				TempToFile(data->chRemotePath, data->chFileName, data->chRemotePath, chFile);
				strcpy(chFullName, data->chRemotePath);
				FullPath(chFullName);
				strcat(chFullName, chFile);
				if(COMPRESS == ntohl(data->iCompress))
				{
					if(UnZipFile(chFullName) == -1)
					{
						return -1;
					}
				}
				if(1 == GobalInfo.iMoveFlag)
				{
					iResult = FileToTargetPath(data);
					if(-1 == iResult)
					{
						pthread_mutex_lock(&GobalMutex.AlertMutex);
						Msg("File(%s)ToTargetPath(%s) failure", chFileName + 1, data->chRemoteTargetPath);
						pthread_mutex_unlock(&GobalMutex.AlertMutex);
						return -1;
					}
				}
				pthread_mutex_lock(&GobalMutex.LogMutex);
				Log("put %s success", chFileName+1);
				//Log("put %s success %d", chFileName+1, iSockfd);
				pthread_mutex_unlock(&GobalMutex.LogMutex);
				break;
			case STATUS_BYBY:
				//close(iSockfd);
				//iSockfd = -1;
				return 0;
			/*数据包错误*/
			default:
				return -1;
		}
		if(GetSockData(iSockfd, data) == -1)
		{
			return -1;
		}
	}//end for(;;)
}

/****************************************************************
**
**	Function:GetFileInfo
**	Purpose:获取文件信息
**	Input Parammeters:
**	Return:成功返回0,出错返回-1。
**	datetime:
**
*****************************************************************/
int GetFileInfo(int iSockfd, struct S_DataInfo *data)
{
	char chFullName[STRING_LEN];

	GetFullName(data, chFullName);
	data->iStatus = htonl(STATUS_SENDFILE);

	if(WriteSockData(iSockfd, data) == -1)
	{
		return -1;
	}
	return 0;
}

/****************************************************************
**
**	Function:SaveFile
**	Purpose:把数据存放到文件中
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int SaveFile(struct S_DataInfo *data, char *chLocalDir)
{
	char chFullName[STRING_LEN];
	FILE *fp = NULL;
	long lFileLen;
	long lWriteLen;
	lFileLen = 0;
	if(chLocalDir[strlen(chLocalDir)-1] == '/')
	{
		sprintf(chFullName, "%s%s\0", chLocalDir, data->chFileName);
	}
	else
	{
		sprintf(chFullName,"%s/%s\0", chLocalDir, data->chFileName);
	}
    if(access(chFullName, F_OK) == -1)
    {
		fp = fopen(chFullName, "wb+");
	}
    else
    {
		fp = fopen(chFullName, "rb+");
	}
	if(fp == NULL)
	{
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("Write File %s", chFullName);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		return -1;
	}
	if(ntohl(data->lFilePos) != 0)
	{
		if(fseek(fp, ntohl(data->lFilePos), SEEK_SET))
		{
			pthread_mutex_lock(&GobalMutex.AlertMutex);
			Msg("fseek File %s", chFullName);
			pthread_mutex_unlock(&GobalMutex.AlertMutex);
			fclose(fp);
			return -1;
		}
	}
	lWriteLen = fwrite(data->chData, sizeof(char), ntohl(data->lDataLen), fp);
	if(lWriteLen < ntohl(data->lDataLen))
	{
		lFileLen == -1;
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("Save file %s error", chFullName);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		fclose(fp);
		return -1;
	}
	fclose(fp);
	return lFileLen;
}

/****************************************************************
**
**	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], chDescFullName[STRING_LEN];
	char chTempFile[STRING_LEN];

	strcpy(chSrcFullName, chSrcPath);
	FullPath(chSrcFullName);
	strcat(chSrcFullName, chSrcFile);

	strcpy(chDescFullName, chDescPath);
	FullPath(chDescFullName);
	sprintf(chTempFile, "~%s\0", chSrcFile);
	strcat(chDescFullName,chTempFile);
	strcpy(chDestFile, chTempFile);
	if(rename(chSrcFullName, chDescFullName)==-1)
	{
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("Rename file %s to %s failure",chSrcFullName,chDescFullName);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		return -1;
	}
	return 0;
}

/****************************************************************
**
**	Function:TempToFile
**	Purpose:将临时文件改成正常的文件
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/

int TempToFile(const char *chSrcPath, const char *chSrcFile, const char *chDescPath, char *chDestFile)
{

	char chSrcFullName[STRING_LEN], chDescFullName[STRING_LEN];
	char chFile[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)
	{
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("Rename file %s to %s failure", chSrcFullName, chDescFullName);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		return -1;
	}
	return 0;
}

/****************************************************************
**
**	Function:FiltToTargetPath
**	Purpose:将临时目录中的文件移动到目标目录中
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int FileToTargetPath(struct S_DataInfo *data)
{
	char chFileName[STRING_LEN];
	char chFullTempPath[STRING_LEN];
	char chFullTargPath[STRING_LEN];
	char chFullTempFileName[STRING_LEN];
	char chFullTargFileName[STRING_LEN];

	memset(chFileName, 0, sizeof(chFileName));
	strcpy(chFileName, data->chFileName);
	if(COMPRESS == ntohl(data->iCompress))
	{
		chFileName[strlen(chFileName) - 2] = '\0';
	}
	strcpy(chFullTempPath, data->chRemotePath);
	FullPath(chFullTempPath);
	strcpy(chFullTargPath, data->chRemoteTargetPath);
	FullPath(chFullTargPath);

	sprintf(chFullTempFileName, "%s%s", chFullTempPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);
	sprintf(chFullTargFileName, "%s%s", chFullTargPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);

	int iResult = 0;

	iResult = rename(chFullTempFileName, chFullTargFileName);

	if(iResult < 0)
	{
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("rename(): %s", sys_errlist[errno]);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		return -1;
	}
	return 0;
}

int BackupFile(struct S_DataInfo *data)
{
	char chFileName[STRING_LEN];
	char chFullTempPath[STRING_LEN];
	char chFullTargPath[STRING_LEN];
	char chFullTempFileName[STRING_LEN];
	char chFullTargFileName[STRING_LEN];

	memset(chFileName, 0, sizeof(chFileName));
	strcpy(chFileName, data->chFileName);
	if(COMPRESS == ntohl(data->iCompress))
	{
		chFileName[strlen(chFileName) - 2] = '\0';
	}
	strcpy(chFullTempPath, data->chRemotePath);
	FullPath(chFullTempPath);
	strcpy(chFullTargPath, data->chRemoteBackupPath);
	FullPath(chFullTargPath);

	sprintf(chFullTempFileName, "%s%s", chFullTempPath, data->chFileName);
	sprintf(chFullTargFileName, "%s%s", chFullTargPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);

	int iResult = 0;

	iResult = rename(chFullTempFileName, chFullTargFileName);

	if(iResult < 0)
	{
		pthread_mutex_lock(&GobalMutex.AlertMutex);
		Msg("rename(): %s", sys_errlist[errno]);
		pthread_mutex_unlock(&GobalMutex.AlertMutex);
		return -1;
	}
	return 0;
}

//-------------------------------------The End------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -