From fc0edcb55feb2e35e60564de1eeee97b48880a00 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Tue, 04 Aug 2026 08:25:45 +0000
Subject: [PATCH] [#828] Fix infinite loop picking the errors-encountered LDIF file name (#839)

---
 opendj-server-legacy/src/main/java/org/opends/server/protocols/LDIFConnectionHandler.java |   62 +++++++++++++++----------------
 1 files changed, 30 insertions(+), 32 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/protocols/LDIFConnectionHandler.java b/opendj-server-legacy/src/main/java/org/opends/server/protocols/LDIFConnectionHandler.java
index 771f625..535a5b8 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/protocols/LDIFConnectionHandler.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/protocols/LDIFConnectionHandler.java
@@ -13,11 +13,13 @@
  *
  * Copyright 2008-2009 Sun Microsystems, Inc.
  * Portions Copyright 2014-2016 ForgeRock AS.
+ * Portions Copyright 2026 3A Systems, LLC.
  */
 package org.opends.server.protocols;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -311,21 +313,8 @@
     importConfig.setInvokeImportPlugins(false);
     importConfig.setValidateSchema(true);
 
-    String outputPath = inputPath + ".applied." + TimeThread.getGMTTime();
-    if (new File(outputPath).exists())
-    {
-      int i=2;
-      while (true)
-      {
-        if (! new File(outputPath + "." + i).exists())
-        {
-          outputPath = outputPath + "." + i;
-          break;
-        }
-
-        i++;
-      }
-    }
+    String outputPath = selectUnusedPath(inputPath + ".applied." +
+                                         TimeThread.getGMTTime());
 
     LDIFExportConfig exportConfig =
          new LDIFExportConfig(outputPath, ExistingFileBehavior.APPEND);
@@ -470,21 +459,8 @@
 
     if (errorEncountered || !fullyProcessed)
     {
-      String renamedPath = inputPath + ".errors-encountered." +
-                           TimeThread.getGMTTime();
-      if (new File(renamedPath).exists())
-      {
-        int i=2;
-        while (true)
-        {
-          if (! new File(renamedPath + "." + i).exists())
-          {
-            renamedPath = renamedPath + "." + i;
-          }
-
-          i++;
-        }
-      }
+      String renamedPath = selectUnusedPath(inputPath + ".errors-encountered." +
+                                            TimeThread.getGMTTime());
 
       try
       {
@@ -493,7 +469,7 @@
           logger.trace("Renaming source file to " + renamedPath);
         }
 
-        ldifFile.renameTo(new File(renamedPath));
+        renameFile(ldifFile, new File(renamedPath));
       }
       catch (Exception e)
       {
@@ -515,7 +491,7 @@
           logger.trace("Deleting source file");
         }
 
-        ldifFile.delete();
+        Files.delete(ldifFile.toPath());
       }
       catch (Exception e)
       {
@@ -532,6 +508,28 @@
 
 
 
+  /**
+   * Returns the provided path if no file exists at it.  Otherwise, appends
+   * ".2", ".3", ... to the provided path and returns the first resulting path
+   * at which no file exists.
+   *
+   * @param  basePath  The desired path for the file.
+   *
+   * @return  The first path at which no file exists.
+   */
+  static String selectUnusedPath(String basePath)
+  {
+    String path = basePath;
+    for (int i=2; new File(path).exists(); i++)
+    {
+      path = basePath + "." + i;
+    }
+
+    return path;
+  }
+
+
+
   @Override
   public void toString(StringBuilder buffer)
   {

--
Gitblit v1.10.0