BOOL COCSInventoryState::WriteToFile(LPCTSTR lpstrFilename, LPCTSTR lpstrSection) { CMarkup myXml; TiXmlElement *pNode, *pTemp; // Load current state file if (myXml.LoadFile( lpstrFilename)) { // File already exists, so remove existing section from XML myXml.ResetPos(); pNode = myXml.FindFirstElem( lpstrSection); while (pNode) { pTemp = pNode; pNode = myXml.FindNextElem( lpstrSection, pTemp); myXml.DeleteElem( pTemp); } } // Add new section myXml.ResetPos(); if (!FormatXML( &myXml, lpstrSection)) return FALSE; // Save state file if (!myXml.SaveFile( lpstrFilename)) return FALSE; return TRUE; }
int COptDownloadPackage::downloadInfoFile() { CConfig *pAgentConfig = getAgentConfig(); CComProvider *pProvider = getComServerProvider(); CServerConfig *pServerConfig = pProvider->newConfig(); CConnexionAbstract *pConnexion = NULL; TCHAR szDummy[_MAX_PATH + 1]; ASSERT(pAgentConfig); ASSERT(pProvider); ASSERT(pServerConfig); // Create provider connection object using default Provider configuration if ((pConnexion = pProvider->newConnexion(pServerConfig)) == NULL) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed creating connection for Communication Provider <%s>"), pAgentConfig->getCommunicationProvider()); pProvider->deleteConfig(pServerConfig); return FALSE; } // Download metadata info file m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Metadata file <info> for package <%s> is located at <%s>"), m_csId, getRemoteMetadataURL()); if (!pConnexion->getFile(getRemoteMetadataURL(), getLocalMetadataFilename())) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Failed to download Metadata file <%s> to <%s>"), getRemoteMetadataURL(), getLocalMetadataFilename()); pProvider->deleteConnexion(pConnexion); pProvider->deleteConfig(pServerConfig); return FALSE; } m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Unloading communication provider")); // Use provider to delete connexion and server config pProvider->deleteConnexion(pConnexion); pProvider->deleteConfig(pServerConfig); // Open metadata file to add fragment location CString csBuffer; CMarkup xml; if (!xml.LoadFile(getLocalMetadataFilename())) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Cannot read or parse Metadata file <%s>"), getLocalMetadataFilename()); return FALSE; } // Add fragment location to meta data xml.FindFirstElem(_T("DOWNLOAD")); xml.SetAttrib(_T("LOC"), m_csRemotePackLoc); // Add package schedule to meta data xml.SetAttrib(_T("SCHEDULE"), m_csSchedule); // Add post execution command to meta data xml.SetAttrib(_T("POSTCMD"), m_csPostCmd); // Write meta data file if (!xml.SaveFile(getLocalMetadataFilename())) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't update Metadata file <%s>"), getLocalMetadataFilename()); return FALSE; } // Compute digest on meta data and add it to Registry if (!fileDigest(getLocalMetadataFilename(), csBuffer)) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't register package <%s> into Registry"), m_csId); DeleteFile(getLocalMetadataFilename()); return FALSE; } if (!regAddPackageDigest(m_csId, csBuffer)) { m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't register package <%s> into Registry"), m_csId); DeleteFile(getLocalMetadataFilename()); return FALSE; } // Now create a timestamp csBuffer.Format(_T("%s\\%s\\%s"), getDownloadFolder(), m_csId, OCS_DOWNLOAD_TIMESTAMP); if (!fileExists(csBuffer)) { _ltot(time(NULL), szDummy, 10); if (!WriteTextToFile(szDummy, csBuffer)) m_pLogger->log(LOG_PRIORITY_ERROR, _T("DOWNLOAD => Can't create timestamp file <%s>"), csBuffer); } else m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Timestamp file <%s> already exists"), csBuffer); m_pLogger->log(LOG_PRIORITY_DEBUG, _T("DOWNLOAD => Retrieve info file...OK (pack %s)"), m_csId); return TRUE; }