Cleanup and reorganization

This commit is contained in:
Simon Rozman 2016-04-01 13:28:05 +02:00
parent 02b080c3ef
commit c53bd0285f
2 changed files with 541 additions and 359 deletions

View File

@ -21,33 +21,106 @@
/// ///
/// Module initializer /// UpdCheck Application
/// ///
class wxUpdCheckInitializer : public wxInitializer class wxUpdCheckApp
{ {
public:
wxUpdCheckApp();
virtual ~wxUpdCheckApp();
///
/// Application's main method
///
/// \returns Return code
///
int Run();
protected: protected:
std::ofstream m_log; ///
/// Downloads repository database (with integrity check).
///
/// \returns Repository database. Use wxDELETE after use.
///
wxXmlDocument* GetCatalogue();
public: ///
wxConfig m_config; /// Parses repository database.
wxString m_path; ///
wxLocale m_locale; /// \returns
/// - true if parsing succeeded
/// - false otherwise
///
bool ParseCatalogue(const wxXmlDocument &doc);
public:
wxUpdCheckInitializer(); ///
/// Stores update package meta-information to cache.
///
/// \returns
/// - true if writing succeeded
/// - false otherwise
///
bool WriteUpdatePackageMeta();
///
/// Retrieves update package meta-information from cache.
///
/// \returns
/// - true if reading succeeded
/// - false otherwise
///
bool ReadUpdatePackageMeta();
///
/// Downloads update package.
///
/// \param[in] fileName File name to save downloaded package to
///
/// \returns
/// - true if downloading succeeded
/// - false otherwise
///
bool DownloadUpdatePackage(const wxString &fileName);
///
/// Launch update.
///
/// \param[in] fileName File name of the update package
///
/// \returns
/// - true if launch succeeded
/// - false otherwise
///
bool LaunchUpdate(const wxString &fileName);
protected:
bool m_ok; ///< Is class initialized correctly
wxConfig m_config; ///< Application configuration
wxString m_path; ///< Working directory (log, downloaded files, etc.)
wxFFile m_log; ///< Log file
wxLocale m_locale; ///< Current locale
wxCryptoSession *m_cs; ///< Cryptographics session
wxCryptoKey *m_ck; ///< Public key for update database integrity validation
wxUint32 m_version; ///< Latest product version available (numerical)
wxString m_versionStr; ///< Latest product version available (string)
wxArrayString m_urls; ///< List of update package file downloads
wxMemoryBuffer m_hash; ///< Update package SHA-1 hash
}; };
wxUpdCheckInitializer::wxUpdCheckInitializer() : //////////////////////////////////////////////////////////////////////////
m_config(wxT(UPDATER_CFG_APPLICATION) wxT("\\Updater"), wxT(UPDATER_CFG_VENDOR)), // wxUpdCheckApp
wxInitializer() //////////////////////////////////////////////////////////////////////////
{
if (wxInitializer::IsOk()) {
// Set desired locale.
wxLanguage language = (wxLanguage)m_config.Read(wxT("Language"), wxLANGUAGE_DEFAULT);
if (wxLocale::IsAvailable(language))
wxVERIFY(m_locale.Init(language));
wxUpdCheckApp::wxUpdCheckApp() :
m_config(wxT(UPDATER_CFG_APPLICATION) wxT("\\Updater"), wxT(UPDATER_CFG_VENDOR)),
m_cs(NULL),
m_ck(NULL),
m_version(0)
{
m_path = wxFileName::GetTempDir(); m_path = wxFileName::GetTempDir();
if (!wxEndsWithPathSeparator(m_path)) if (!wxEndsWithPathSeparator(m_path))
m_path += wxFILE_SEP_PATH; m_path += wxFILE_SEP_PATH;
@ -55,84 +128,128 @@ wxUpdCheckInitializer::wxUpdCheckInitializer() :
if (!wxDirExists(m_path)) if (!wxDirExists(m_path))
wxMkdir(m_path); wxMkdir(m_path);
m_log.open((LPCTSTR)(m_path + wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT(".log")), std::ios_base::out | std::ios_base::trunc); m_log.Open(m_path + wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT(".log"), wxT("wt"));
if (m_log.is_open()) if (m_log.IsOpened())
delete wxLog::SetActiveTarget(new wxLogStream(&m_log)); delete wxLog::SetActiveTarget(new wxLogStderr(m_log.fp()));
}
}
// Set desired locale.
wxLanguage language = (wxLanguage)m_config.Read(wxT("Language"), wxLANGUAGE_DEFAULT);
if (wxLocale::IsAvailable(language))
wxVERIFY(m_locale.Init(language));
/// wxASSERT_MSG(!m_cs, wxT("cryptographics session already initialized"));
/// Main function m_cs = new wxCryptoSessionRSAAES();
/// wxCHECK2_MSG(m_cs, { m_ok = false; return; }, wxT("wxCryptoSessionRSAAES creation failed"));
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
// Initialize wxWidgets.
wxUpdCheckInitializer initializer;
if (!initializer.IsOk())
return -1;
// Create RSA AES cryptographic context.
wxCryptoSessionRSAAES cs;
if (!cs.IsOk())
return -1;
// Import public key. // Import public key.
wxCryptoKey ck;
{
HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(IDR_KEY_PUBLIC), RT_RCDATA); HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(IDR_KEY_PUBLIC), RT_RCDATA);
wxASSERT_MSG(res, wxT("public key not found")); wxCHECK2_MSG(res, { m_ok = false; return; }, wxT("public key not found"));
HGLOBAL res_handle = ::LoadResource(NULL, res); HGLOBAL res_handle = ::LoadResource(NULL, res);
wxASSERT_MSG(res_handle, wxT("loading resource failed")); wxCHECK2_MSG(res_handle, { m_ok = false; return; }, wxT("loading resource failed"));
if (!ck.ImportPublic(cs, ::LockResource(res_handle), ::SizeofResource(NULL, res))) m_ck = new wxCryptoKey();
return -1; wxCHECK2_MSG(m_ck, { m_ok = false; return; }, wxT("wxCryptoKey creation failed"));
wxCHECK2(m_ck->ImportPublic(*m_cs, ::LockResource(res_handle), ::SizeofResource(NULL, res)), { m_ok = false; return; });
} }
long val;
wxDateTime last_checked = initializer.m_config.Read(wxT("LastChecked"), &val) ? wxDateTime((time_t)val) : wxInvalidDateTime; wxUpdCheckApp::~wxUpdCheckApp()
{
if (m_ck) delete m_ck;
if (m_cs) delete m_cs;
}
int wxUpdCheckApp::Run()
{
if (!m_ok)
return -1; // Initialization failed.
// Download update catalogue.
wxXmlDocument *doc = GetCatalogue();
if (doc) {
// Parse the update catalogue to check for an update package availability.
if (ParseCatalogue(*doc)) {
// Save update package meta-information for the next time.
WriteUpdatePackageMeta();
} else {
wxLogStatus(wxT("Update check complete. Your package is up to date."));
return 0;
}
} else {
// Update download catalogue failed, or repository database didn't change. Read a cached version of update package meta-information?
if (!ReadUpdatePackageMeta()) {
// Reset CatalogueLastModified to force update catalogue download next time.
m_config.DeleteEntry(wxT("CatalogueLastModified"));
return 1; // No update package information available.
}
if (m_version <= UPDATER_PRODUCT_VERSION) {
wxLogStatus(wxT("Update check complete. Your package is up to date."));
return 0;
}
}
wxString fileName(m_path);
fileName += wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT("-");
fileName += m_versionStr;
fileName += wxT(".msi");
if (!DownloadUpdatePackage(fileName))
return 2; // Update package not available.
if (!LaunchUpdate(fileName))
return 3; // Update launch failed.
return 0;
}
wxXmlDocument* wxUpdCheckApp::GetCatalogue()
{
wxLogStatus(wxT("Downloading repository catalogue..."));
wxString lastModified;
m_config.Read(wxT("CatalogueLastModified"), &lastModified, wxT(""));
for (const wxChar *server = wxT(UPDATER_HTTP_SERVER); server[0]; server += wcslen(server) + 1) { for (const wxChar *server = wxT(UPDATER_HTTP_SERVER); server[0]; server += wcslen(server) + 1) {
wxXmlDocument doc;
wxLogStatus(wxT("Contacting repository at %s..."), server); wxLogStatus(wxT("Contacting repository at %s..."), server);
// Load repository database. // Load repository database.
wxHTTP http; wxHTTP http;
http.SetHeader(wxS("User-Agent"), wxS("Updater/") wxS(UPDATER_VERSION_STR)); http.SetHeader(wxS("User-Agent"), wxS("Updater/") wxS(UPDATER_VERSION_STR));
if (last_checked.IsValid()) if (!lastModified.IsEmpty())
http.SetHeader(wxS("If-Modified-Since"), last_checked.Format(wxS("%a, %d %b %Y %H:%M:%S %z"))); http.SetHeader(wxS("If-Modified-Since"), lastModified);
if (!http.Connect(server, UPDATER_HTTP_PORT)) { if (!http.Connect(server, UPDATER_HTTP_PORT)) {
wxLogWarning(wxT("Error resolving %s server name."), server); wxLogWarning(wxT("Error resolving %s server name."), server);
continue; continue;
} }
wxInputStream *httpStream = http.GetInputStream(wxS(UPDATER_HTTP_PATH)); wxInputStream *httpStream = http.GetInputStream(wxS(UPDATER_HTTP_PATH));
if (!httpStream) {
if (http.GetResponse() == 304) { if (http.GetResponse() == 304) {
wxLogStatus(wxT("Repository did not change since the last time...")); wxLogStatus(wxT("Repository did not change since the last time."));
return 0; wxDELETE(httpStream);
} return NULL;
} else if (!httpStream) {
wxLogWarning(wxT("Error response received from server %s (port %u) requesting %s."), server, UPDATER_HTTP_PORT, UPDATER_HTTP_PATH); wxLogWarning(wxT("Error response received from server %s (port %u) requesting %s."), server, UPDATER_HTTP_PORT, UPDATER_HTTP_PATH);
continue; continue;
} }
wxLogStatus(wxT("Loading repository catalogue...")); wxXmlDocument *doc = new wxXmlDocument();
if (!doc.Load(*httpStream, "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES)) { if (!doc->Load(*httpStream, "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES)) {
wxLogWarning(wxT("Error loading repository catalogue.")); wxLogWarning(wxT("Error loading repository catalogue."));
wxDELETE(httpStream); wxDELETE(httpStream);
http.Close(); http.Close();
delete doc;
continue; continue;
} }
wxDELETE(httpStream); wxDELETE(httpStream);
m_config.Write(wxT("CatalogueLastModified"), http.GetHeader(wxT("Last-Modified")));
http.Close(); http.Close();
wxLogStatus(wxT("Verifying repository catalogue signature...")); wxLogStatus(wxT("Verifying repository catalogue signature..."));
// Find the (first) signature. // Find the (first) signature.
wxXmlNode *document = doc.GetDocumentNode(); wxXmlNode *document = doc->GetDocumentNode();
std::vector<BYTE> sig; wxMemoryBuffer sig;
for (wxXmlNode *prolog = document->GetChildren(); prolog; prolog = prolog->GetNext()) { for (wxXmlNode *prolog = document->GetChildren(); prolog; prolog = prolog->GetNext()) {
if (prolog->GetType() == wxXML_COMMENT_NODE) { if (prolog->GetType() == wxXML_COMMENT_NODE) {
wxString content = prolog->GetContent(); wxString content = prolog->GetContent();
@ -142,54 +259,66 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
{ {
// Read the signature. // Read the signature.
size_t signature_len = content_len - (_countof(wxS(UPDATER_SIGNATURE_MARK)) - 1); size_t signature_len = content_len - (_countof(wxS(UPDATER_SIGNATURE_MARK)) - 1);
sig.resize(wxBase64DecodedSize(signature_len)); size_t len = wxBase64DecodedSize(signature_len);
size_t res = wxBase64Decode(sig.data(), sig.capacity(), content.Right(signature_len), wxBase64DecodeMode_SkipWS); size_t res = wxBase64Decode(sig.GetWriteBuf(len), len, content.Right(signature_len), wxBase64DecodeMode_SkipWS);
if (res != wxCONV_FAILED) { if (res != wxCONV_FAILED) {
sig.resize(res); sig.SetDataLen(res);
// Remove the signature as it is not a part of the validation check. // Remove the signature as it is not a part of the validation check.
document->RemoveChild(prolog); document->RemoveChild(prolog);
wxDELETE(prolog); wxDELETE(prolog);
break; break;
} else } else
sig.clear(); sig.Clear();
} }
} }
} }
if (sig.empty()) { if (sig.IsEmpty()) {
wxLogWarning(wxT("Signature not found in the repository catalogue.")); wxLogWarning(wxT("Signature not found in the repository catalogue."));
delete doc;
continue; continue;
} }
// Hash the content. // Hash the content.
wxCryptoHashSHA1 ch(cs); wxCryptoHashSHA1 ch(*m_cs);
if (!wxXmlHashNode(ch, document)) if (!wxXmlHashNode(ch, document)) {
delete doc;
continue; continue;
}
// We have the hash, we have the signature, we have the public key. Now verify. // We have the hash, we have the signature, we have the public key. Now verify.
if (!wxCryptoVerifySignature(ch, sig.data(), sig.size(), ck)) { if (!wxCryptoVerifySignature(ch, sig, *m_ck)) {
wxLogWarning(wxT("Repository catalogue signature does not match its content, or signature verification failed.")); wxLogWarning(wxT("Repository catalogue signature does not match its content, or signature verification failed."));
delete doc;
continue; continue;
} }
// The signature is correct. Now parse the file. // Verify document type (root element).
wxLogStatus(wxT("Parsing repository catalogue...")); const wxString &nameRoot = doc->GetRoot()->GetName();
// Start processing the XML file.
wxXmlNode *elRoot = doc.GetRoot();
const wxString &nameRoot = elRoot->GetName();
if (nameRoot != wxT("Packages")) { if (nameRoot != wxT("Packages")) {
wxLogWarning(wxT("Invalid root element in repository catalogue (actual: %s, expected %s)."), nameRoot.c_str(), wxT("Packages")); wxLogWarning(wxT("Invalid root element in repository catalogue (actual: %s, expected %s)."), nameRoot.c_str(), wxT("Packages"));
delete doc;
continue; continue;
} }
// The downloaded repository database passed all checks.
return doc;
}
// No repository database downloaded successfully.
return NULL;
}
bool wxUpdCheckApp::ParseCatalogue(const wxXmlDocument &doc)
{
bool found = false;
wxLogStatus(wxT("Parsing repository catalogue..."));
// Iterate over packages. // Iterate over packages.
wxUint32 versionMax = 0; for (wxXmlNode *elPackage = doc.GetRoot()->GetChildren(); elPackage; elPackage = elPackage->GetNext()) {
wxString versionStrMax;
std::vector<wxString> urlsMax;
std::vector<BYTE> hashMax;
for (wxXmlNode *elPackage = elRoot->GetChildren(); elPackage; elPackage = elPackage->GetNext()) {
if (elPackage->GetType() == wxXML_ELEMENT_NODE && elPackage->GetName() == wxT("Package")) { if (elPackage->GetType() == wxXML_ELEMENT_NODE && elPackage->GetName() == wxT("Package")) {
// Get package version. // Get package version.
wxUint32 version = 0; wxUint32 version = 0;
@ -207,8 +336,8 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
} }
} }
} }
if (version <= UPDATER_PRODUCT_VERSION || version < versionMax) { if (version <= UPDATER_PRODUCT_VERSION || version < m_version) {
// This package is older than currently installed product or the superseeded package found. // This package is older than currently installed product or the superseeded package found already.
continue; continue;
} }
@ -222,9 +351,9 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
#else #else
platformId += wxT("-x86"); platformId += wxT("-x86");
#endif #endif
wxString languageId(initializer.m_locale.GetCanonicalName()); wxString languageId(m_locale.GetCanonicalName());
std::vector<wxString> urls; wxArrayString urls;
std::vector<BYTE> hash; wxMemoryBuffer hash;
for (wxXmlNode *elDownloads = elPackage->GetChildren(); elDownloads; elDownloads = elDownloads->GetNext()) { for (wxXmlNode *elDownloads = elPackage->GetChildren(); elDownloads; elDownloads = elDownloads->GetNext()) {
if (elDownloads->GetType() == wxXML_ELEMENT_NODE && elDownloads->GetName() == wxT("Downloads")) { if (elDownloads->GetType() == wxXML_ELEMENT_NODE && elDownloads->GetName() == wxT("Downloads")) {
for (wxXmlNode *elPlatform = elDownloads->GetChildren(); elPlatform; elPlatform = elPlatform->GetNext()) { for (wxXmlNode *elPlatform = elDownloads->GetChildren(); elPlatform; elPlatform = elPlatform->GetNext()) {
@ -237,16 +366,16 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
if (elLocaleNote->GetType() == wxXML_ELEMENT_NODE) { if (elLocaleNote->GetType() == wxXML_ELEMENT_NODE) {
const wxString &name = elLocaleNote->GetName(); const wxString &name = elLocaleNote->GetName();
if (name == wxT("url")) if (name == wxT("url"))
urls.push_back(elLocaleNote->GetNodeContent()); urls.Add(elLocaleNote->GetNodeContent());
else if (name == wxT("hash")) { else if (name == wxT("hash")) {
// Read the hash. // Read the hash.
wxString content(elLocaleNote->GetNodeContent()); wxString content(elLocaleNote->GetNodeContent());
hash.resize(wxHexDecodedSize(content.length())); size_t len = wxHexDecodedSize(content.length());
size_t res = wxHexDecode(hash.data(), hash.capacity(), content, wxHexDecodeMode_SkipWS); size_t res = wxHexDecode(hash.GetWriteBuf(len), len, content, wxHexDecodeMode_SkipWS);
if (res != wxCONV_FAILED) if (res != wxCONV_FAILED)
hash.resize(res); hash.SetDataLen(res);
else else
hash.clear(); hash.Clear();
} }
} }
} }
@ -257,68 +386,125 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
} }
} }
} }
if (urls.empty() || hash.empty()) { if (urls.empty() || hash.IsEmpty()) {
// This package is for different architecture and/or language. // This package is for different architecture and/or language.
// ... or missing URL and/or hash. // ... or missing URL and/or hash.
continue; continue;
} }
versionMax = version; // Save found package info.
versionStrMax = versionStr; m_version = version;
urlsMax = urls; m_versionStr = versionStr;
hashMax = hash; m_urls = urls;
m_hash = hash;
found = true;
wxLogMessage(wxT("Update package found (version: %s)."), m_versionStr.c_str());
} }
} }
if (versionMax) { return found;
wxLogMessage(wxT("Update package found (version: %s)."), versionStrMax.c_str()); }
wxString fileName(initializer.m_path);
fileName += wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT("-");
fileName += versionStrMax;
fileName += wxT(".msi");
bool isLocal = false; bool wxUpdCheckApp::WriteUpdatePackageMeta()
{
// Concatenate URLs.
wxString urls;
for (size_t i = 0, n = m_urls.GetCount(); i < n; i++) {
if (i) urls += wxT('\n');
urls += m_urls[i];
}
bool result = true;
if (!m_config.Write(wxT("PackageVersion" ), m_version )) result = false;
if (!m_config.Write(wxT("PackageVersionDesc"), m_versionStr )) result = false;
if (!m_config.Write(wxT("PackageURLs" ), urls )) result = false;
if (!m_config.Write(wxT("PackageHash" ), wxHexEncode(m_hash))) result = false;
return result;
}
bool wxUpdCheckApp::ReadUpdatePackageMeta()
{
long lng;
if (m_config.Read(wxT("PackageVersion"), &lng)) {
m_version = lng;
if (m_config.Read(wxT("PackageVersionDesc"), &m_versionStr)) {
wxString str;
if (m_config.Read(wxT("PackageURLs"), &str)) {
// Split URLs
m_urls.Clear();
const wxStringCharType *buf = str;
for (size_t i = 0, j = 0, n = str.Length(); i < n;) {
for (; j < n && buf[j] != wxT('\n'); j++);
m_urls.Add(str.SubString(i, j - 1));
i = ++j;
}
if (m_config.Read(wxT("PackageHash"), &str)) {
// Convert to binary.
size_t len = wxHexDecodedSize(str.length());
size_t res = wxHexDecode(m_hash.GetWriteBuf(len), len, str, wxHexDecodeMode_SkipWS);
if (res != wxCONV_FAILED) {
m_hash.SetDataLen(res);
return true;
} else
m_hash.Clear();
}
}
}
}
wxLogWarning(wxT("Reading update package meta-information from cache failed or incomplete."));
m_version = 0;
m_versionStr.Clear();
m_urls.Clear();
m_hash.Clear();
return false;
}
bool wxUpdCheckApp::DownloadUpdatePackage(const wxString &fileName)
{
if (wxFileExists(fileName)) { if (wxFileExists(fileName)) {
// The update package file already exists. Do the integrity check. // The update package file already exists. Do the integrity check.
std::ifstream file((LPCTSTR)fileName, std::ios_base::in | std::ios_base::binary); wxFFile file(fileName, wxT("rb"));
if (file.is_open()) { if (file.IsOpened()) {
// Calculate file hash. // Calculate file hash.
wxCryptoHashSHA1 ch(cs); wxCryptoHashSHA1 ch(*m_cs);
wxMemoryBuffer buf(4*1024); wxMemoryBuffer buf(4*1024);
char *data = (char*)buf.GetData(); char *data = (char*)buf.GetData();
size_t nBlock = buf.GetBufSize(); size_t nBlock = buf.GetBufSize();
do { while (!file.Eof())
file.read(data, nBlock); ch.Hash(data, file.Read(data, nBlock));
ch.Hash(data, file.gcount());
} while (file.good());
ch.GetValue(buf); ch.GetValue(buf);
if (buf.GetDataLen() == hashMax.size() && if (buf.GetDataLen() == m_hash.GetDataLen() &&
memcmp(buf.GetData(), hashMax.data(), buf.GetDataLen()) == 0) memcmp(buf.GetData(), m_hash.GetData(), buf.GetDataLen()) == 0)
{ {
// Update package file exists and its hash is correct. // Update package file exists and its hash is correct.
wxLogStatus(wxT("Update package file already downloaded.")); wxLogStatus(wxT("Update package file already downloaded."));
isLocal = true; return true;
} }
} }
} }
if (!isLocal) { wxFFile file(fileName, wxT("wb"));
std::ofstream file((LPCTSTR)fileName, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary); if (!file.IsOpened()) {
if (!file.is_open()) {
wxLogError(wxT("Can not open/create %s file for writing."), fileName.c_str()); wxLogError(wxT("Can not open/create %s file for writing."), fileName.c_str());
goto quit; // This condition has no chance to succeed with the next download server. Quit. return false;
} }
// Download update package file. // Download update package file.
for (std::vector<wxString>::const_iterator i = urlsMax.cbegin(), i_end = urlsMax.end(); i != i_end; ++i) { for (size_t i = 0, n = m_urls.GetCount(); i < n; i++) {
wxURL url(*i); wxURL url(m_urls[i]);
if (!url.IsOk()) if (!url.IsOk())
continue; continue;
wxLogStatus(wxT("Downloading update package from %s..."), i->c_str()); wxLogStatus(wxT("Downloading update package from %s..."), m_urls[i].c_str());
wxInputStream *stream = url.GetInputStream(); wxInputStream *stream = url.GetInputStream();
if (!stream) { if (!stream) {
wxLogWarning(wxT("Error response received.")); wxLogWarning(wxT("Error response received."));
@ -326,34 +512,37 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
} }
// Save update package to file, and calculate hash. // Save update package to file, and calculate hash.
wxCryptoHashSHA1 ch(cs); wxCryptoHashSHA1 ch(*m_cs);
wxMemoryBuffer buf(4*1024); wxMemoryBuffer buf(4*1024);
char *data = (char*)buf.GetData(); char *data = (char*)buf.GetData();
size_t nBlock = buf.GetBufSize(); size_t nBlock = buf.GetBufSize();
do { do {
stream->Read(data, nBlock); stream->Read(data, nBlock);
size_t nRead = stream->LastRead(); size_t nRead = stream->LastRead();
if (file.fail()) { file.Write(data, nRead);
if (file.Error()) {
wxLogError(wxT("Can not write to %s file."), fileName.c_str()); wxLogError(wxT("Can not write to %s file."), fileName.c_str());
goto quit; // This condition has no chance to succeed with the next download server. Quit. return false;
} }
file.write(data, nRead);
ch.Hash(data, nRead); ch.Hash(data, nRead);
} while (stream->IsOk()); } while (stream->IsOk());
ch.GetValue(buf); ch.GetValue(buf);
if (buf.GetDataLen() == hashMax.size() && if (buf.GetDataLen() == m_hash.GetDataLen() &&
memcmp(buf.GetData(), hashMax.data(), buf.GetDataLen()) == 0) memcmp(buf.GetData(), m_hash.GetData(), buf.GetDataLen()) == 0)
{ {
// Update package file exists and its hash is correct. // Update package file downloaded and its hash is correct.
isLocal = true; return true;
break;
} else } else
wxLogWarning(wxT("Update package file corrupt.")); wxLogWarning(wxT("Update package file corrupt."));
} }
return false;
} }
if (isLocal) {
bool wxUpdCheckApp::LaunchUpdate(const wxString &fileName)
{
wxLogStatus(wxT("Launching update...")); wxLogStatus(wxT("Launching update..."));
// Headless Install // Headless Install
@ -365,41 +554,37 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In
param += wxT("\""); param += wxT("\"");
// Logging // Logging
wxString fileNameLog(initializer.m_path); wxString fileNameLog(m_path);
fileNameLog += wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT("-msiexec.log"); fileNameLog += wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT("-msiexec.log");
param += wxT(" /l* \""); param += wxT(" /l* \"");
param += fileNameLog; param += fileNameLog;
param += wxT("\""); param += wxT("\"");
if ((int)::ShellExecute(NULL, NULL, wxT("msiexec.exe"), param, NULL, SW_SHOWNORMAL) > 32) { int result = (int)::ShellExecute(NULL, NULL, wxT("msiexec.exe"), param, NULL, SW_SHOWNORMAL);
wxLogStatus(wxT("For further information, see %s file."), fileNameLog.c_str()); if (result > 32) {
break; wxLogStatus(wxT("msiexec.exe launch succeeded. For further information, see %s file."), fileNameLog.c_str());
return true;
} else { } else {
wxLogError(wxT("msiexec.exe launch failed.")); wxLogError(wxT("msiexec.exe launch failed. ShellExecute returned %i."), result);
goto quit; // This condition has no chance to succeed with the next download server. Quit. return false;
}
} else
wxLogWarning(wxT("Update package file download failed."));
} else {
wxLogStatus(wxT("Update check complete. Your package is up to date."));
// Clean-up the downloaded package files to free space.
wxDir dir(initializer.m_path);
if (dir.IsOpened()) {
wxString filename;
for (bool cont = dir.GetFirst(&filename, wxT("Updater-") wxT(UPDATER_CFG_APPLICATION) wxT("-*.msi"), wxDIR_FILES | wxDIR_HIDDEN); cont; cont = dir.GetNext(&filename))
wxRemoveFile(initializer.m_path + filename);
} }
} }
// Save the last check date.
initializer.m_config.Write(wxT("LastChecked"), (long)wxDateTime::GetTimeNow());
return 0; ///
} /// Main function
///
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
quit: // Initialize wxWidgets.
// No success. wxInitializer initializer;
return 1; if (!initializer.IsOk())
return -1;
// Do everything.
wxUpdCheckApp app;
return app.Run();
} }

View File

@ -25,20 +25,17 @@
#include "../include/Updater.h" #include "../include/Updater.h"
#include <wx/app.h> #include <wx/app.h>
#include <wx/arrstr.h>
#include <wx/base64.h> #include <wx/base64.h>
#include <wx/buffer.h>
#include <wx/config.h> #include <wx/config.h>
#include <wx/datetime.h>
#include <wx/dir.h> #include <wx/dir.h>
#include <wx/ffile.h>
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/protocol/http.h> #include <wx/protocol/http.h>
#include <wx/url.h> #include <wx/url.h>
#include <wx/xml/xml.h> #include <wx/xml/xml.h>
#include <wxex/common.h>
#include <wxex/crypto.h> #include <wxex/crypto.h>
#include <wxex/hex.h> #include <wxex/hex.h>
#include <wxex/xml.h> #include <wxex/xml.h>
#include <algorithm>
#include <vector>
#include <fstream>