]> granicus.if.org Git - icinga2/blobdiff - icinga-installer/icinga-installer.cpp
Merge pull request #5855 from Icinga/fix/vs2017
[icinga2] / icinga-installer / icinga-installer.cpp
index d301af592d0fb5da349fc10538a03edc27df07fe..438ddc37db7eba7ddc1a0f8299293ef382ba918d 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
+ * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
@@ -25,6 +25,8 @@
 #include <shlwapi.h>
 #include <shellapi.h>
 #include <shlobj.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 static std::string GetIcingaInstallPath(void)
 {
@@ -41,6 +43,7 @@ static std::string GetIcingaInstallPath(void)
        return szFileName;
 }
 
+
 static bool ExecuteCommand(const std::string& app, const std::string& arguments)
 {
        SHELLEXECUTEINFO sei = {};
@@ -94,14 +97,6 @@ static bool PathExists(const std::string& path)
        return (_stat(path.c_str(), &statbuf) >= 0);
 }
 
-static void CopyFile(const std::string& source, const std::string& target)
-{
-       std::ifstream ifs(source.c_str(), std::ios::binary);
-       std::ofstream ofs(target.c_str(), std::ios::binary | std::ios::trunc);
-
-       ofs << ifs.rdbuf();
-}
-
 static std::string GetIcingaDataPath(void)
 {
        char path[MAX_PATH];
@@ -124,24 +119,12 @@ static void MkDirP(const std::string& path)
                pos = path.find_first_of("/\\", pos + 1);
 
                std::string spath = path.substr(0, pos + 1);
-               struct stat statbuf;
-               if (stat(spath.c_str(), &statbuf) < 0 && errno == ENOENT)
+               struct _stat statbuf;
+               if (_stat(spath.c_str(), &statbuf) < 0 && errno == ENOENT)
                        MkDir(path.substr(0, pos));
        }
 }
 
-static void CopyConfigFile(const std::string& installDir, const std::string& sourceConfigPath, size_t skelPrefixLength)
-{
-       std::string relativeConfigPath = sourceConfigPath.substr(skelPrefixLength);
-
-       std::string targetConfigPath = installDir + relativeConfigPath;
-
-       if (!PathExists(targetConfigPath)) {
-               MkDirP(DirName(targetConfigPath));
-               CopyFile(sourceConfigPath, targetConfigPath);
-       }
-}
-
 static std::string GetNSISInstallPath(void)
 {
        HKEY hKey;
@@ -162,28 +145,32 @@ static std::string GetNSISInstallPath(void)
        return "";
 }
 
-static void CollectPaths(std::vector<std::string>& paths, const std::string& path)
+static bool CopyDirectory(const std::string& source, const std::string& destination)
 {
-       paths.push_back(path);
+       // SHFileOperation requires file names to be terminated with two \0s
+       std::string tmpSource = source + std::string(1, '\0');
+       std::string tmpDestination = destination + std::string(1, '\0');
+
+       SHFILEOPSTRUCT fop;
+       fop.wFunc = FO_COPY;
+       fop.pFrom = tmpSource.c_str();
+       fop.pTo = tmpDestination.c_str();
+       fop.fFlags = FOF_NO_UI;
+
+       return (SHFileOperation(&fop) == 0);
 }
 
-static bool MoveDirectory(const std::string& source, const std::string& destination)
+static bool DeleteDirectory(const std::string& dir)
 {
-       if (!MoveFileEx(source.c_str(), destination.c_str(), MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
-               // SHFileOperation requires file names to be terminated with two \0s
-               std::string tmpSource = source + std::string(1, '\0');
-               std::string tmpDestination = destination + std::string(1, '\0');
+       // SHFileOperation requires file names to be terminated with two \0s
+       std::string tmpDir = dir + std::string(1, '\0');
 
-               SHFILEOPSTRUCT fop;
-               fop.wFunc = FO_COPY;
-               fop.pFrom = tmpSource.c_str();
-               fop.pTo = tmpDestination.c_str();
-               fop.fFlags = FOF_NO_UI;
-               if (SHFileOperation(&fop) != 0)
-                       return false;
-       }
+       SHFILEOPSTRUCT fop;
+       fop.wFunc = FO_DELETE;
+       fop.pFrom = tmpDir.c_str();
+       fop.fFlags = FOF_NO_UI;
 
-       return true;
+       return (SHFileOperation(&fop) == 0);
 }
 
 static int UpgradeNSIS(void)
@@ -198,27 +185,42 @@ static int UpgradeNSIS(void)
        if (!PathExists(uninstallerPath))
                return 0;
 
-       ExecuteCommand(uninstallerPath, "/S _?=" + installPath);
+       std::string dataPath = GetIcingaDataPath();
 
-       _unlink(uninstallerPath.c_str());
+       if (dataPath.empty())
+               return 1;
 
-       std::string dataPath = GetIcingaDataPath();
+       bool moveUserData = !PathExists(dataPath);
 
        /* perform open heart surgery on the user's data dirs - yay */
-       if (!PathExists(dataPath)) {
+       if (moveUserData) {
                MkDir(dataPath.c_str());
 
                std::string oldNameEtc = installPath + "\\etc";
                std::string newNameEtc = dataPath + "\\etc";
-               if (!MoveDirectory(oldNameEtc, newNameEtc))
+               if (!CopyDirectory(oldNameEtc, newNameEtc))
                        return 1;
 
                std::string oldNameVar = installPath + "\\var";
                std::string newNameVar = dataPath + "\\var";
-               if (!MoveDirectory(oldNameVar, newNameVar))
+               if (!CopyDirectory(oldNameVar, newNameVar))
                        return 1;
+       }
+
+       ExecuteCommand(uninstallerPath, "/S _?=" + installPath);
+
+       _unlink(uninstallerPath.c_str());
 
-               _unlink(installPath.c_str());
+       if (moveUserData) {
+               std::string oldNameEtc = installPath + "\\etc";
+               if (!DeleteDirectory(oldNameEtc))
+                       return 1;
+
+               std::string oldNameVar = installPath + "\\var";
+               if (!DeleteDirectory(oldNameVar))
+                       return 1;
+
+               _rmdir(installPath.c_str());
        }       
 
        return 0;
@@ -244,12 +246,12 @@ static int InstallIcinga(void)
 
                MkDirP(dataDir + "/etc/icinga2/pki");
                MkDirP(dataDir + "/var/cache/icinga2");
-               MkDirP(dataDir + "/var/lib/icinga2/pki");
+               MkDirP(dataDir + "/var/lib/icinga2/certs");
+               MkDirP(dataDir + "/var/lib/icinga2/certificate-requests");
                MkDirP(dataDir + "/var/lib/icinga2/agent/inventory");
                MkDirP(dataDir + "/var/lib/icinga2/api/config");
                MkDirP(dataDir + "/var/lib/icinga2/api/log");
                MkDirP(dataDir + "/var/lib/icinga2/api/zones");
-               MkDirP(dataDir + "/var/lib/icinga2/api/zones");
                MkDirP(dataDir + "/var/log/icinga2/compat/archive");
                MkDirP(dataDir + "/var/log/icinga2/crash");
                MkDirP(dataDir + "/var/run/icinga2/cmd");
@@ -280,7 +282,6 @@ int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
        CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
 
        //AllocConsole();
-
        int rc;
 
        if (strcmp(lpCmdLine, "install") == 0) {