filezillaapi.cpp

来自「一个支持FTP,SFTP的客户端程序」· C++ 代码 · 共 831 行 · 第 1/2 页

CPP
831
字号
	if (m_pMainThread->IsBusy())
		return FZ_REPLY_BUSY;
	if (nListMode&FZ_LIST_FORCECACHE)
		return FZ_REPLY_ERROR;

	t_command command;
	command.id=FZ_COMMAND_LIST;
	command.path=parent;
	command.param1=dirname;
	command.param4=nListMode;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
}

int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (TransferFile.remotefile=="" || TransferFile.localfile=="" || TransferFile.remotepath.IsEmpty())
		return FZ_REPLY_INVALIDPARAM;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	t_command command;
	command.id=FZ_COMMAND_FILETRANSFER;
	command.transferfile=TransferFile;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
}

int CFileZillaApi::GetCurrentServer(t_server &server)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (m_pMainThread->GetCurrentServer(server))
		return FZ_REPLY_OK;
	else
		return FZ_REPLY_NOTCONNECTED;
}

int CFileZillaApi::CustomCommand(CString CustomCommand)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	t_server server;
	int res=GetCurrentServer(server);
	if (res!=FZ_REPLY_OK)
		return res;
	if (server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
		return FZ_REPLY_NOTSUPPORTED;
	if (CustomCommand=="")
		return FZ_REPLY_INVALIDPARAM;

	t_command command;
	command.id=FZ_COMMAND_CUSTOMCOMMAND;
	command.param1=CustomCommand;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
}

int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	if (FileName=="")
		return FZ_REPLY_INVALIDPARAM;

	CServerPath path2=path;
	if (path2.IsEmpty())
	{
		m_pMainThread->GetCurrentPath(path2);
		if (path2.IsEmpty())
			return FZ_REPLY_INVALIDPARAM;
	}

	t_command command;
	command.id=FZ_COMMAND_DELETE;
	command.param1=FileName;
	command.path=path2;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;

}

int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	if (DirName=="")
		return FZ_REPLY_INVALIDPARAM;

	CServerPath path2=path;
	if (path2.IsEmpty())
	{
		m_pMainThread->GetCurrentPath(path2);
		if (path2.IsEmpty())
			return FZ_REPLY_INVALIDPARAM;
	}

	t_command command;
	command.id=FZ_COMMAND_REMOVEDIR;
	command.param1=DirName;
	command.path=path2;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
	
	return FZ_REPLY_ERROR;
}

int CFileZillaApi::MakeDir(const CServerPath &path)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	if (path.IsEmpty() || !path.HasParent())
		return FZ_REPLY_INVALIDPARAM;

	t_command command;
	command.id=FZ_COMMAND_MAKEDIR;
	command.path=path;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
	
	return FZ_REPLY_ERROR;
}

int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	if (oldName=="" || newName=="")
		return FZ_REPLY_INVALIDPARAM;

	CServerPath path2 = path;
	if (path2.IsEmpty())
	{
		m_pMainThread->GetCurrentPath(path2);
		if (path2.IsEmpty())
			return FZ_REPLY_INVALIDPARAM;
	}

	t_command command;
	command.id = FZ_COMMAND_RENAME;
	command.param1 = oldName;
	command.param2 = newName;
	command.path = path2;
	command.newPath = newPath;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
	
	return FZ_REPLY_ERROR;
}

int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
{
	if (!this || !pData)
		return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;

	if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
		return FZ_REPLY_CRITICALERROR;
	
	if (!m_bInitialized)
	{
		delete pData;
		return FZ_REPLY_NOTINITIALIZED;
	}
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
	{
		delete pData;
		return FZ_REPLY_NOTCONNECTED;
	}

	switch(pData->nRequestType)
	{
	case FZ_ASYNCREQUEST_OVERWRITE:
		break;
	case FZ_ASYNCREQUEST_VERIFYCERT:
		if (!((CVerifyCertRequestData *)pData)->pCertData)
		{
			delete pData;
			return FZ_REPLY_INVALIDPARAM;
		}
		break;
	case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
	case FZ_ASYNCREQUEST_GSS_NEEDUSER:
	case FZ_ASYNCREQUEST_GSS_NEEDPASS:
	case FZ_ASYNCREQUEST_NEWHOSTKEY:
	case FZ_ASYNCREQUEST_CHANGEDHOSTKEY:
		break;
	default:
		delete pData;
		return FZ_REPLY_INVALIDPARAM;
	}
	pData->nRequestResult = nAction;
	if (!m_pMainThread)
	{
		delete pData;
		return FZ_REPLY_NOTINITIALIZED;
	}

	m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY,  (LPARAM)pData);
	
	return FZ_REPLY_OK;
}

int CFileZillaApi::SetOption(int nOption, int value)
{
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	switch (nOption)
	{
		case FZAPI_OPTION_SHOWHIDDEN:
			m_pMainThread->SetOption(nOption, value);
			break;
		default:
			return FZ_REPLY_INVALIDPARAM;
	}
	return FZ_REPLY_OK;
}

int CFileZillaApi::GetOption(int nOption, int &value)
{
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	switch (nOption)
	{
		case FZAPI_OPTION_SHOWHIDDEN:
			value = m_pMainThread->GetOption(nOption);
			break;
		default:
			return FZ_REPLY_INVALIDPARAM;
	}
	return FZ_REPLY_OK;
}

int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (IsConnected()==FZ_REPLY_NOTCONNECTED)
		return FZ_REPLY_NOTCONNECTED;
	if (IsBusy()==FZ_REPLY_BUSY)
		return FZ_REPLY_BUSY;
	if (FileName=="")
		return FZ_REPLY_INVALIDPARAM;

	t_command command;
	command.id=FZ_COMMAND_CHMOD;
	command.param1=FileName;
	command.param4=nValue;
	command.path=path;
	m_pMainThread->Command(command);
	if (m_hOwnerWnd)
		return FZ_REPLY_WOULDBLOCK;
	else
		return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
}

int CFileZillaApi::SetDebugLevel(int nDebugLevel)
{
	//Check if call allowed
	if (!m_bInitialized)
		return FZ_REPLY_NOTINITIALIZED;
	if (!m_pMainThread->SetDebugLevel(nDebugLevel))
		return FZ_REPLY_ERROR;

	return FZ_REPLY_OK;
}

BOOL CFileZillaApi::DumpDirectoryCache(LPCTSTR pFileName)
{
	CDirectoryCache cache;
	return cache.Dump(pFileName);
}

//CAsyncRequestData derived classes
CAsyncRequestData::CAsyncRequestData()
{
}

CAsyncRequestData::~CAsyncRequestData()
{
}
	
COverwriteRequestData::COverwriteRequestData()
{
	nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
	time1=0;
	time2=0;
	pTransferFile=0;
}

COverwriteRequestData::~COverwriteRequestData()
{
	delete pTransferFile;
	delete time1;
	delete time2;
}

CVerifyCertRequestData::CVerifyCertRequestData()
{
	nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
	pCertData=0;
}

CVerifyCertRequestData::~CVerifyCertRequestData()
{
	delete pCertData;
}

CGssNeedPassRequestData::CGssNeedPassRequestData()
{
	nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
}

CGssNeedPassRequestData::~CGssNeedPassRequestData()
{
}

CGssNeedUserRequestData::CGssNeedUserRequestData()
{
	nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
}

CGssNeedUserRequestData::~CGssNeedUserRequestData()
{
}

CNewHostKeyRequestData::CNewHostKeyRequestData()
{
	nRequestType=FZ_ASYNCREQUEST_NEWHOSTKEY;
}

CNewHostKeyRequestData::~CNewHostKeyRequestData()
{
}

CChangedHostKeyRequestData::CChangedHostKeyRequestData()
{
	nRequestType=FZ_ASYNCREQUEST_CHANGEDHOSTKEY;
}

CChangedHostKeyRequestData::~CChangedHostKeyRequestData()
{
}

BOOL CFileZillaApi::IsValid() const
{
	if (!this)
		return FALSE;
	if (IsBadWritePtr((VOID *)this, sizeof(CFileZillaApi)) )
		return FALSE;
	if (IsBadWritePtr((VOID *)&m_bInitialized, sizeof(BOOL)) ||
		IsBadWritePtr((VOID *)&m_hOwnerWnd, sizeof(HWND)) ||
		IsBadWritePtr((VOID *)&m_nInternalMessageID, sizeof(unsigned int)) ||
		IsBadWritePtr((VOID *)&m_nReplyMessageID, sizeof(unsigned int)) ||
		IsBadWritePtr(m_pMainThread, sizeof(CMainThread)) )
			return FALSE;

	if (!m_pMainThread->IsValid())
		return FALSE;

	return TRUE;
}

⌨️ 快捷键说明

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