Bladeren bron

HPCC-16867 Fix ecl-bundle temporary directory creation on Windows

Signed-off-by: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 8 jaren geleden
bovenliggende
commit
8c49b08366
4 gewijzigde bestanden met toevoegingen van 71 en 31 verwijderingen
  1. 17 8
      ecl/ecl-bundle/ecl-bundle.cpp
  2. 25 1
      system/jlib/jmisc.cpp
  3. 4 0
      system/jlib/jmisc.hpp
  4. 25 22
      system/jlib/jutil.cpp

+ 17 - 8
ecl/ecl-bundle/ecl-bundle.cpp

@@ -206,10 +206,16 @@ void recursiveRemoveDirectory(IFile *dir, bool isDryRun)
         IFile *thisFile = &files->query();
         if (thisFile->isDirectory()==foundYes)
             recursiveRemoveDirectory(thisFile, isDryRun);
-        if (isDryRun || optVerbose)
-            printf("rm %s\n", thisFile->queryFilename());
-        if (!isDryRun)
-            thisFile->remove();
+        else
+        {
+            if (isDryRun || optVerbose)
+                printf("rm %s\n", thisFile->queryFilename());
+            if (!isDryRun)
+            {
+                thisFile->setReadOnly(false);
+                thisFile->remove();
+            }
+        }
     }
     if (isDryRun || optVerbose)
         printf("rmdir %s\n", dir->queryFilename());
@@ -300,10 +306,13 @@ StringBuffer & fetchURL(const char *bundleName, StringBuffer &fetchedLocation)
     // If the bundle name looks like a url, fetch it somewhere temporary first...
     if (isUrl(bundleName))
     {
-        //Put it into a temp directory - we need the filename to be right
-        //I don't think there is any way to disable the following warning....
-        const char *tmp = tmpnam(NULL);
-        recursiveCreateDirectory(tmp);
+        StringBuffer tmp;
+        getTempFilePath(tmp, "ecl-bundle", nullptr);
+        tmp.append(PATHSEPCHAR).append("tmp.XXXXXX");
+        if (!mkdtemp((char *) tmp.str()))
+        {
+            throw makeStringExceptionV(0, "Failed to create temporary directory %s (error %d)", tmp.str(), errno);
+        }
         deleteOnCloseDown.append(tmp);
         if (optVerbose)
             printf("mkdir %s\n", tmp);

+ 25 - 1
system/jlib/jmisc.cpp

@@ -28,7 +28,9 @@
 #include "jstring.hpp"
 #include "jdebug.hpp"
 
-#ifndef _WIN32
+#ifdef _WIN32
+#include <direct.h>
+#else
 #include <sys/wait.h>
 #include <pwd.h>
 #endif
@@ -994,3 +996,25 @@ jlib_decl bool getHomeDir(StringBuffer & homepath)
     homepath.append(home);
     return true;
 }
+
+#ifdef _WIN32
+char *mkdtemp(char *_template)
+{
+    if (!_template || strlen(_template) < 6 || !streq(_template+strlen(_template)-6, "XXXXXX"))
+    {
+        errno = EINVAL;
+        return nullptr;
+    }
+    char * tail = _template + strlen(_template) - 6;
+    for (int i = 0; i < 100; i++)
+    {
+        snprintf(tail, 7, "%06d", rand());
+        if (!_mkdir(_template))
+            return _template;
+        if (errno != EEXIST)
+            return nullptr;
+    }
+    errno = EINVAL;
+    return nullptr;
+}
+#endif

+ 4 - 0
system/jlib/jmisc.hpp

@@ -338,4 +338,8 @@ public:
     }
 };
 
+#ifdef _WIN32
+extern jlib_decl char *mkdtemp(char *_template);
+#endif
+
 #endif

+ 25 - 22
system/jlib/jutil.cpp

@@ -1751,28 +1751,31 @@ unsigned runExternalCommand(StringBuffer &output, const char *cmd, const char *i
     try
     {
         Owned<IPipeProcess> pipe = createPipeProcess();
-        pipe->run(cmd, cmd, ".", input != NULL, true, true, 1024*1024);
-        if (input)
+        int ret = START_FAILURE;
+        if (pipe->run(cmd, cmd, ".", input != NULL, true, true, 1024*1024))
         {
-            pipe->write(strlen(input), input);
-            pipe->closeInput();
-        }
-        char buf[1024];
-        while (true)
-        {
-            size32_t read = pipe->read(sizeof(buf), buf);
-            if (!read)
-                break;
-            output.append(read, buf);
-        }
-        int ret = pipe->wait();
-        StringBuffer error;
-        while (true)
-        {
-            size32_t read = pipe->readError(sizeof(buf), buf);
-            if (!read)
-                break;
-            error.append(read, buf);
+            if (input)
+            {
+                pipe->write(strlen(input), input);
+                pipe->closeInput();
+            }
+            char buf[1024];
+            while (true)
+            {
+                size32_t read = pipe->read(sizeof(buf), buf);
+                if (!read)
+                    break;
+                output.append(read, buf);
+            }
+            ret = pipe->wait();
+            StringBuffer error;
+            while (true)
+            {
+                size32_t read = pipe->readError(sizeof(buf), buf);
+                if (!read)
+                    break;
+                error.append(read, buf);
+            }
         }
         return ret;
     }
@@ -1780,7 +1783,7 @@ unsigned runExternalCommand(StringBuffer &output, const char *cmd, const char *i
     {
         E->Release();
         output.clear();
-        return 255;
+        return START_FAILURE;
     }
 }