void OpenLog( LPCTSTR lpstrFolder, LPCTSTR lpstrCommandLine)
{
	try
	{
		CString	csFileName,
				csCommand = lpstrCommandLine;
		
		if (bOpened)
			myFile.Close();
		bOpened = FALSE;
		// Check if debug log is required
		csCommand.MakeLower();
		if ((csCommand.Find( _T( "-debug")) == -1) && (csCommand.Find( _T( "/debug")) == -1))
			return;
		csFileName.Format( _T( "%s.log"), lpstrFolder);
		if (!myFile.Open( csFileName, CFile::shareDenyNone|CFile::modeCreate|CFile::modeWrite|CFile::typeText))
			// Unable to open file
			return;
		// Seek to End
		myFile.SeekToEnd();
		bOpened = TRUE;
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		bOpened = FALSE;
	}
}
Esempio n. 2
0
BOOL CPackage::setDone( LPCTSTR lpstrCode, LPCTSTR lpstrOutput)
{
	CStdioFile myFile;
	CString	   csFile, csResult;

	csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_DONE);
	try
	{
		if (!myFile.Open( csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::shareDenyWrite))
			return FALSE;
		// First line is result code
		csResult.Format( _T( "%s\n"), lpstrCode);
		myFile.WriteString( csResult);
		if (lpstrOutput)
			// Following lines are command ouput
			myFile.WriteString( lpstrOutput);
		myFile.Close();
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		DeleteFile( csFile);
		return FALSE;
	}
	return TRUE;
}
Esempio n. 3
0
BOOL CPackage::getDone( CString &csCode, CString &csOutput)
{
	CString csFile, csBuffer;
	CStdioFile myFile;

	csCode = ERR_DONE_FAILED;
	try
	{
		// Open package done file
		csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_DONE);
		if (!myFile.Open( csFile, CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
			return FALSE;
		// Read first line to get result code
		myFile.ReadString( csCode);
		// Following lines are command ouput
		while (myFile.ReadString( csBuffer))
			csOutput.AppendFormat( _T( "%s\n"), csBuffer);
		myFile.Close();
		return TRUE;
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		csCode = ERR_DONE_FAILED;
		return FALSE;
	}
}
Esempio n. 4
0
BOOL CPackage::getDone( CString &csCode)
{
	CString csFile;
	CStdioFile myFile;

	csCode = ERR_DONE_FAILED;
	try
	{
		// Open package done file
		csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_DONE);
		if (!myFile.Open( csFile, CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
			return FALSE;
		// Read only first line to get OCS result code
		myFile.ReadString( csCode);
		myFile.Close();
		return TRUE;
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		csCode = ERR_DONE_FAILED;
		return FALSE;
	}
}
Esempio n. 5
0
BOOL CPackage::createTask()
{
	CString csTask, csBuffer;
	CStdioFile myFile;

	csTask.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_TASK);
	try
	{
		if (!myFile.Open( csTask, CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::shareDenyWrite))
			return FALSE;
		for (UINT uIndex=1; uIndex<=m_uFrags; uIndex++)
		{
			csBuffer.Format( _T( "%s-%u\n"), m_csID, uIndex);
			myFile.WriteString( csBuffer);
		}
		myFile.Close();
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		DeleteFile( csTask);
		return FALSE;
	}
	return TRUE;
}
void CloseLog()
{
	try
	{
		if (bOpened)
			myFile.Close();
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
	}
	bOpened = FALSE;
}
BOOL CCapDownload::checkOcsAgentSetupResult()
{
	CString csFile, csCode = ERR_DONE_FAILED, csID;
	CStdioFile myFile;

	csFile.Format(_T("%s\\%s"), getDownloadFolder(), OCS_AGENT_SETUP_DONE);
	if (!fileExists(csFile))
		// No OCS Agent Setup done file
		return TRUE;
	m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Found OCS Inventory Agent Setup result file <%s>"), csFile);
	// Open OCS Agent Setup done file to read exit code and package ID
	try
	{
		if (!myFile.Open(csFile, CFile::modeRead | CFile::typeText | CFile::shareDenyNone))
			return FALSE;
		// First line contains result code and seconf line contains package ID
		myFile.ReadString(csCode);
		myFile.ReadString(csID);
		myFile.Close();
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed reading OCS Inventory Agent Setup result file <%s>"), csFile);
		return FALSE;
	}
	if (csID.IsEmpty())
	{
		// Upgrading from agent 1.X or previous to 2.0.0.22 ?
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Found result code <%s> for OCS Inventory Agent Setup package but no package ID specified, so remove all packages to avoid running Agent setup in loop !"), csCode);
		COptDownloadPackage::cleanAll();
		return FALSE;
	}
	// All information available => copy result file to Package directory
	csFile.Format(_T("%s\\%s\\%s"), getDownloadFolder(), csID, OCS_DOWNLOAD_DONE);
	if (!CopyFile(myFile.GetFilePath(), csFile, FALSE))
	{
		m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed to copy result code for OCS Inventory Agent Setup package to file <%s>"), csFile);
		return FALSE;
	}
	DeleteFile(myFile.GetFilePath());
	m_pLogger->log(LOG_PRIORITY_NOTICE, _T("DOWNLOAD => Validated result code <%s> for OCS Inventory Agent Setup package <%s>"), csCode, csID);
	return TRUE;
}
Esempio n. 8
0
BOOL CPackage::getFragToDownload( CString &csFragID)
{
	CStdioFile myFile;
	CString csTask;

	csFragID.Empty();
	try
	{
		csTask.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_TASK);
		if (!myFile.Open( csTask, CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
			return FALSE;
		myFile.ReadString( csFragID);
		myFile.Close();
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		return FALSE;
	}
	return TRUE;
}
Esempio n. 9
0
void CGnuTransfers::LoadDownloadParts(CString FilePath, CGnuDownloadShell* pDownload)
{
	CStdioFile PartsFile;

	CString NextLine;
	CString PartsData;
	
	if (PartsFile.Open(FilePath, CFile::modeRead))
	{
		while (PartsFile.ReadString(NextLine))
			PartsData += NextLine + "\n";

		PartsFile.Abort();
	}

	if( PartsData.IsEmpty() )
		return;


	// Load each part
	for(int i = 0; ; i++)
	{
		int CurrentPos = PartsData.Find("[Part " + NumtoStr(i) + "]");

		if(CurrentPos == -1)
			break;

		CurrentPos += 5; // Host in header and value conflict

		if(i < pDownload->m_PartList.size() )
		{
			pDownload->m_PartList[i].BytesCompleted = atoi(GetBackupString("BytesCompleted", CurrentPos, PartsData));
			pDownload->m_PartList[i].Verified       = atoi(GetBackupString("Verified", CurrentPos, PartsData));
			pDownload->m_PartList[i].SourceHostID   = atoi(GetBackupString("SourceHostID", CurrentPos, PartsData));
		}
	}

}
void AddLog(LPCTSTR lpstrLog,...)
{
	if (!bOpened)
		return;
	try
	{
		CString			csBuffer;
		va_list			argList;

		// Format arguments
		va_start(argList, lpstrLog);
		csBuffer.FormatV( lpstrLog, argList);
		va_end( argList);
		// Write string
		myFile.WriteString( csBuffer);
		myFile.Flush();
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
	}
}
Esempio n. 11
0
BOOL CPackage::getExecTry( UINT *puTry)
{
	CString csFile;
	CStdioFile myFile;

	try
	{
		// Open package done file
		csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_EXECUTE_TRY);
		if (!myFile.Open( csFile, CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
			return FALSE;
		// Read only first line to get OCS result code
		myFile.ReadString( csFile);
		*puTry = _ttol( csFile);
		myFile.Close();
		return TRUE;
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		return FALSE;
	}
}
Esempio n. 12
0
BOOL CPackage::setExecTry( UINT uTry)
{
	CStdioFile myFile;
	CString	   csFile;

	csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_EXECUTE_TRY);
	try
	{
		if (!myFile.Open( csFile, CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::shareDenyWrite))
			return FALSE;
		// First line is result code
		csFile.Format( _T( "%u"), uTry);
		myFile.WriteString( csFile);
		myFile.Close();
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		myFile.Abort();
		DeleteFile( csFile);
		return FALSE;
	}
	return TRUE;
}
Esempio n. 13
0
CGnuDownloadShell* CGnuTransfers::LoadDownloadHosts(CString FilePath)
{
	// Check if file already loaded
	for(int i = 0; i < m_DownloadList.size(); i++)
		if( m_DownloadList[i]->m_BackupPath.CompareNoCase(FilePath) == 0 )
			return NULL;


	CStdioFile BackupFile;

	CString NextLine;
	CString Backup;
	
	if (BackupFile.Open(FilePath, CFile::modeRead))
	{
		while (BackupFile.ReadString(NextLine))
			Backup += NextLine + "\n";

		BackupFile.Abort();
	}

	if(Backup.IsEmpty() || Backup.Find("[Download]") == -1)
		return NULL;

	int CurrentPos = Backup.Find("[Download]");

	CGnuDownloadShell* Download = new CGnuDownloadShell(this);
	
	Download->m_ShellStatus		= (CGnuDownloadShell::Status) atoi(GetBackupString("Status", CurrentPos, Backup));
	Download->m_Name			= GetBackupString("Name", CurrentPos, Backup);
	Download->m_FileLength		= _atoi64(GetBackupString("FileLength", CurrentPos, Backup));
	Download->m_PartSize		= atoi(GetBackupString("PartSize", CurrentPos, Backup));
	Download->m_OverrideName	= GetBackupString("OverrideName", CurrentPos, Backup);
	Download->m_OverridePath	= GetBackupString("OverridePath", CurrentPos, Backup);
	Download->m_PartialPath		= GetBackupString("PartialPath", CurrentPos, Backup);
	Download->m_BackupPath  	= FilePath;
	Download->m_Sha1Hash		= GetBackupString("Sha1Hash", CurrentPos, Backup);
	Download->m_Search			= GetBackupString("Search", CurrentPos, Backup);
	Download->m_AvgSpeed		= atoi(GetBackupString("AvgSpeed", CurrentPos, Backup));
	Download->m_HashComputed	= atoi(GetBackupString("HashComputed", CurrentPos, Backup));
	Download->m_HashVerified	= atoi(GetBackupString("HashVerified", CurrentPos, Backup));
	Download->m_FileMoved		= atoi(GetBackupString("FileMoved", CurrentPos, Backup));
	Download->m_ReasonDead		= GetBackupString("ReasonDead", CurrentPos, Backup);
	Download->m_MetaXml         = GetBackupString("Meta", CurrentPos, Backup);

	Download->m_UseProxy		= atoi(GetBackupString("UseProxy", CurrentPos, Backup));
	Download->m_DefaultProxy	= GetBackupString("DefaultProxy", CurrentPos, Backup);

	Download->m_TigerHash		= GetBackupString("TigerHash", CurrentPos, Backup);
	Download->m_TreeSize		= atoi(GetBackupString("TreeSize", CurrentPos, Backup));
	Download->m_TreeRes			= atoi(GetBackupString("TreeRes", CurrentPos, Backup));

	if(Download->m_TreeSize)
	{
		Download->m_TigerTree = new byte[Download->m_TreeSize];
		memset(Download->m_TigerTree, 0, Download->m_TreeSize);
	}

	if(Download->m_TigerTree)
	{
		CString Value = GetBackupString("TigerTree", CurrentPos, Backup);

		int buffPos = 0;
		int dotPos  = Value.Find(".");

		while(dotPos != -1 && buffPos < Download->m_TreeSize)
		{
			DecodeBase32( Value.Mid(dotPos - 39, 39), 39, Download->m_TigerTree + buffPos, Download->m_TreeSize - buffPos );

			buffPos += 24;
			dotPos = Value.Find(".", dotPos + 1);
		}
	}


	Download->Init(Download->m_Name, Download->m_FileLength, HASH_SHA1, Download->m_Sha1Hash);

	
	// Load Host info
	if( !Download->m_FileMoved )
		for(int i = 0; ; i++)
		{
			CurrentPos = Backup.Find("[Host " + NumtoStr(i) + "]");

			if(CurrentPos == -1)
				break;

			CurrentPos += 5; // Host in header and value conflict

			FileSource nResult;
			nResult.Name = GetBackupString("Name", CurrentPos, Backup);
			nResult.NameLower = nResult.Name;
			nResult.NameLower.MakeLower();

			nResult.Sha1Hash	 = GetBackupString("Sha1Hash", CurrentPos, Backup);
			//nResult.BitprintHash = GetBackupString("BitprintHash", CurrentPos, Backup);

			nResult.FileIndex	= atoi(GetBackupString("FileIndex", CurrentPos, Backup));
			nResult.Size		= _atoi64(GetBackupString("Size", CurrentPos, Backup));

			nResult.Address.Host = StrtoIP(GetBackupString("Host", CurrentPos, Backup));
			nResult.Address.Port = atoi(GetBackupString("Port", CurrentPos, Backup));
			nResult.Network      = atoi(GetBackupString("Network", CurrentPos, Backup));
			nResult.HostStr		 = GetBackupString("HostStr", CurrentPos, Backup);
			nResult.Path		 = GetBackupString("Path", CurrentPos, Backup);
			nResult.Speed		 = atoi(GetBackupString("Speed", CurrentPos, Backup));
			nResult.Vendor		 = GetBackupString("Vendor", CurrentPos, Backup);

			nResult.Firewall	= atoi(GetBackupString("Firewall", CurrentPos, Backup)) != 0;
			nResult.OpenSlots	= atoi(GetBackupString("OpenSlots", CurrentPos, Backup)) != 0;
			nResult.Busy		= atoi(GetBackupString("Busy", CurrentPos, Backup)) != 0;
			nResult.Stable		= atoi(GetBackupString("Stable", CurrentPos, Backup)) != 0;
			nResult.ActualSpeed = atoi(GetBackupString("ActualSpeed", CurrentPos, Backup)) != 0;
			nResult.SupportF2F  = atoi(GetBackupString("SupportF2F", CurrentPos, Backup)) != 0;
			DecodeBase16(GetBackupString("PushID", CurrentPos, Backup), 32, (byte*) &nResult.PushID, 16);

			CString Nodes = GetBackupString("Direct", CurrentPos, Backup);
			while(!Nodes.IsEmpty())
				nResult.DirectHubs.push_back( StrtoIPv4(ParseString(Nodes, ',')) );

			nResult.GnuRouteID = 0;
			nResult.Distance = 7;
			//nResult.Icon     = m_pCore->GetIconIndex(nResult.Name);

			Download->AddHost(nResult);
		}

	//Download->m_DoReQuery = true;


	// Add Download to list
	m_DownloadAccess.Lock();
	m_DownloadList.push_back(Download);
	m_DownloadAccess.Unlock();

	TransferLoadMeta();

	if(Download->m_ShellStatus == CGnuDownloadShell::eActive)
		Download->Start();

	return Download;
}