bakaid commented on a change in pull request #709: MINIFICPP-1088 - clean up 
minifiexe and MINIFI_HOME logic
URL: https://github.com/apache/nifi-minifi-cpp/pull/709#discussion_r366867563
 
 

 ##########
 File path: main/MiNiFiMain.cpp
 ##########
 @@ -142,94 +159,39 @@ int main(int argc, char **argv) {
 #ifdef WIN32
        if (!SetConsoleCtrlHandler(consoleSignalHandler, TRUE)) {
                logger->log_error("Cannot install signal handler");
-               std::cerr << "Cannot install signal handler" << std::endl;
-               return 1;
+               return -1;
        }
 
        if (signal(SIGINT, sigHandler) == SIG_ERR || signal(SIGTERM, 
sigHandler) == SIG_ERR ) {
-               std::cerr << "Cannot install signal handler" << std::endl;
+               logger->log_error("Cannot install signal handler");
                return -1;
        }
 #ifdef SIGBREAK
        if (signal(SIGBREAK, sigHandler) == SIG_ERR) {
-               std::cerr << "Cannot install signal handler" << std::endl;
+               logger->log_error("Cannot install signal handler");
                return -1;
        }
 #endif
 #else
        if (signal(SIGINT, sigHandler) == SIG_ERR || signal(SIGTERM, 
sigHandler) == SIG_ERR || signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
-               std::cerr << "Cannot install signal handler" << std::endl;
+    logger->log_error("Cannot install signal handler");
                return -1;
        }
 #endif
-       // assumes POSIX compliant environment
-       std::string minifiHome;
-       if (const char *env_p = std::getenv(MINIFI_HOME_ENV_KEY)) {
-               minifiHome = env_p;
-               logger->log_info("Using MINIFI_HOME=%s from environment.", 
minifiHome);
-       }
-       else {
-               logger->log_info("MINIFI_HOME is not set; determining based on 
environment.");
-               char *path = nullptr;
-               char full_path[PATH_MAX];
-#ifndef WIN32
-               path = realpath(argv[0], full_path);
-#else
-               path = nullptr;
-#endif
-
-               if (path != nullptr) {
-                       std::string minifiHomePath(path);
-                       if (minifiHomePath.find_last_of("/\\") != 
std::string::npos) {
-                               minifiHomePath = minifiHomePath.substr(0, 
minifiHomePath.find_last_of("/\\"));  //Remove /minifi from path
-                               minifiHome = minifiHomePath.substr(0, 
minifiHomePath.find_last_of("/\\"));    //Remove /bin from path
-                       }
-               }
-
-               // attempt to use cwd as MINIFI_HOME
-               if (minifiHome.empty() || !validHome(minifiHome)) {
-                       char cwd[PATH_MAX];
-#ifdef WIN32
-                       _getcwd(cwd, PATH_MAX);
-                       auto handle = GetModuleHandle(0);
-                       GetModuleFileNameA(NULL, cwd, sizeof(cwd));
-                       std::string fullPath = cwd;
-                       std::string minifiFileName, minifiPath;
-                       
minifi::utils::file::PathUtils::getFileNameAndPath(fullPath, minifiPath, 
minifiFileName);
-                       if (utils::StringUtils::endsWith(minifiPath, "bin")) {
-                               minifiHome = minifiPath.substr(0, 
minifiPath.size()-3);
-                       }
-                       else {
-                               minifiHome = minifiPath;
-                       }
-               
-#else
-                       getcwd(cwd, PATH_MAX);
-                       minifiHome = cwd;
-#endif
-                       
-               }
-
 
-               logger->log_debug("Setting %s to %s", MINIFI_HOME_ENV_KEY, 
minifiHome);
-#ifdef WIN32
-               SetEnvironmentVariable(MINIFI_HOME_ENV_KEY, minifiHome.c_str());
-#else
-               setenv(MINIFI_HOME_ENV_KEY, minifiHome.c_str(), 0);
-#endif
+       // Determine MINIFI_HOME
+  const std::string minifiHome = determineMinifiHome(logger);
+       if (minifiHome.empty()) {
+         // determineMinifiHome already logged everything we need
+         return -1;
        }
 
-       if (!validHome(minifiHome)) {
-               minifiHome = minifiHome.substr(0, 
minifiHome.find_last_of("/\\"));    //Remove /bin from path
-               if (!validHome(minifiHome)) {
-                       logger->log_error("No valid MINIFI_HOME could be 
inferred. "
-                               "Please set MINIFI_HOME or run minifi from a 
valid location. minifiHome is %s", minifiHome);
-                       return -1;
-               }
+       // chdir to MINIFI_HOME
+       if 
(!utils::Environment::setCurrentWorkingDirectory(minifiHome.c_str())) {
 
 Review comment:
   This will solve the problem of relative paths being relative to the cwd, 
instead of MINIFI_HOME. A lot of code has been written under the assumption 
that this is true, but with the Windows service case it is explicitly not (the 
default working directory is C:\Windows\System32

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to