From 543e50b8f86316189f02e5e0ea6d63c7aa04ebdf Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Tue, 18 Jun 2013 08:06:53 +0000
Subject: [PATCH] CR-1848 OPENDJ-983 On Windows, unexpected error message while running start-ds -V/-F/-s on an instance not configured

---
 opendj-sdk/opends/src/build-tools/windows/common.c |   44 +++++++++++++++++++--
 opendj-sdk/opends/lib/winlauncher.exe              |    0 
 opendj-sdk/opends/resource/bin/stop-ds.bat         |   12 +++++-
 opendj-sdk/opends/resource/bin/start-ds.bat        |   10 ++++
 4 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/opendj-sdk/opends/lib/winlauncher.exe b/opendj-sdk/opends/lib/winlauncher.exe
index 730a960..3a96c39 100755
--- a/opendj-sdk/opends/lib/winlauncher.exe
+++ b/opendj-sdk/opends/lib/winlauncher.exe
Binary files differ
diff --git a/opendj-sdk/opends/resource/bin/start-ds.bat b/opendj-sdk/opends/resource/bin/start-ds.bat
index 70e2828..615443e 100644
--- a/opendj-sdk/opends/resource/bin/start-ds.bat
+++ b/opendj-sdk/opends/resource/bin/start-ds.bat
@@ -39,8 +39,16 @@
 cd /d %INSTANCE_DIR%
 set INSTANCE_ROOT=%CD%
 cd /d %CUR_DIR%
+set TEMP_LOG=%TEMP%\logs\
 
-set LOG="%INSTANCE_ROOT%\logs\native-windows.out"
+if NOT EXIST %INSTANCE_ROOT%\logs\ (
+  if NOT EXIST %TEMP_LOG% (
+    md %TEMP_LOG% 
+  )
+  set LOG="%TEMP_LOG%native-windows.out"
+) ELSE (
+  set LOG="%INSTANCE_ROOT%\logs\native-windows.out"
+)
 set SCRIPT=start-ds.bat
 
 echo %SCRIPT%: invoked >> %LOG%
diff --git a/opendj-sdk/opends/resource/bin/stop-ds.bat b/opendj-sdk/opends/resource/bin/stop-ds.bat
index b434b9d..05efad8 100644
--- a/opendj-sdk/opends/resource/bin/stop-ds.bat
+++ b/opendj-sdk/opends/resource/bin/stop-ds.bat
@@ -24,7 +24,7 @@
 rem
 rem
 rem      Copyright 2006-2010 Sun Microsystems, Inc.
-rem      Portions Copyright 2011-2012 ForgeRock AS
+rem      Portions Copyright 2011-2013 ForgeRock AS
 
 setlocal
 
@@ -50,8 +50,16 @@
 cd /d%INSTANCE_DIR%
 set INSTANCE_ROOT=%CD%
 cd /d%CUR_DIR%
+set TEMP_LOG=%TEMP%\logs\
 
-set LOG="%INSTANCE_ROOT%\logs\native-windows.out"
+if NOT EXIST %INSTANCE_ROOT%\logs\ (
+  if NOT EXIST %TEMP_LOG% (
+    md %TEMP_LOG% 
+  )
+  set LOG="%TEMP_LOG%native-windows.out"
+) ELSE (
+  set LOG="%INSTANCE_ROOT%\logs\native-windows.out"
+)
 set SCRIPT=stop-ds.bat
 
 rem This is the template to use for logging.  Make sure to use >>
diff --git a/opendj-sdk/opends/src/build-tools/windows/common.c b/opendj-sdk/opends/src/build-tools/windows/common.c
index db1d407..acf4064 100644
--- a/opendj-sdk/opends/src/build-tools/windows/common.c
+++ b/opendj-sdk/opends/src/build-tools/windows/common.c
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2008-2010 Sun Microsystems, Inc.
+*      Portions Copyright 2013 ForgeRock AS
 */
 
 #include "common.h"
@@ -40,6 +41,7 @@
 char * getDebugLogFileName();
 void debugInner(BOOL isError, const char *msg, va_list ap);
 void deleteIfLargerThan(char * fileName, DWORD maxSize);
+BOOL isExistingDirectory(char * fileName);
 
 // ----------------------------------------------------
 // Function used to create a process with the given command.
@@ -296,8 +298,8 @@
     }
           
     vfprintf(fp, msg, ap);
-
-	  fprintf(fp, "\n");
+    
+    fprintf(fp, "\n");
     fclose(fp);
   }
   else
@@ -318,12 +320,16 @@
   char path [MAX_PATH];
   char execName [MAX_PATH];
   char * lastSlash;
+  char logpath[MAX_PATH];
+  char * temp;  
+  FILE *file; 
 
   if (logFile != NULL) 
   {
     return logFile;
   }
-
+  temp = getenv("TEMP");
+  
   // Get the name of the executable.  
   GetModuleFileName (
     NULL,
@@ -339,9 +345,26 @@
   lastSlash = strrchr(execName, '\\');
   lastSlash[0] = '\0';
 
-  sprintf(path, "%s\\logs\\%s", execName, DEBUG_LOG_NAME);
-  logFile = _strdup(path);
+  // Instance root is in execName (eg. C:\opendj 
+  // and adds the log's folder name to it  
+  strcpy(logpath, execName); 
+  strcat(logpath, "\\logs\\"); 
+  
+  // If the log folder doesn's exist in the instance path
+  // we create the log file in the temp directory.
+  if (isExistingDirectory(logpath)) 
+  {
+    sprintf(path, "%s\\logs\\%s", execName, DEBUG_LOG_NAME);
+  } else {
+    strcat(temp, "\\logs\\");
+    mkdir(temp);
+    strcat(temp, DEBUG_LOG_NAME);
+    file = fopen(temp,"a+");
+    fclose(file);
+    sprintf(path, "%s", temp);
+  }
 
+  logFile = _strdup(path);
   return logFile;
 }
 
@@ -395,3 +418,14 @@
     DeleteFile(fileName);
   }
 }
+
+// ---------------------------------------------------------------
+// Checks if the specifed directory exist.
+// ---------------------------------------------------------------
+BOOL isExistingDirectory(char * fileName)
+{
+  DWORD str = GetFileAttributes(fileName);
+
+  return (str != INVALID_FILE_ATTRIBUTES && 
+         (str & FILE_ATTRIBUTE_DIRECTORY));
+}

--
Gitblit v1.10.0