From f63b8c6059974c45f34cdffef8fb72fb69fe23d3 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Mon, 02 Mar 2015 14:00:24 +0000
Subject: [PATCH] CR-6056 fix OPENDJ-1764: admin-backend.ldif can end up empty

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java |   46 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
index d683c58..6613bc9 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
@@ -251,21 +251,49 @@
       }
     }
 
-    StaticUtils.close(writer);
-
-
-    // Rename the existing "live" file out of the way and move the new file
-    // into place.
+    // On Linux the final write() on a file can actually fail but not throw an Exception.
+    // The close() will throw an Exception in this case so we MUST check for Exceptions
+    // here.
     try
     {
-      if (oldFile.exists())
-      {
-        oldFile.delete();
-      }
+        writer.close();
     }
     catch (Exception e)
     {
       logger.traceException(e);
+      LocalizableMessage m = ERR_LDIF_BACKEND_ERROR_CLOSING_FILE.get(
+                       tempFile.getAbsolutePath(),
+                       currentConfig.dn(),
+                       stackTraceToSingleLineString(e));
+      DirectoryServer.sendAlertNotification(this,
+                           ALERT_TYPE_LDIF_BACKEND_CANNOT_WRITE_UPDATE, m);
+      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
+                                   m, e);
+    }
+
+    // Extra sanity check
+    if (!entryMap.isEmpty() && tempFile.exists() && tempFile.length() == 0)
+    {
+      LocalizableMessage m = ERR_LDIF_BACKEND_ERROR_EMPTY_FILE.get(
+                       tempFile.getAbsolutePath(),
+                       currentConfig.dn());
+      DirectoryServer.sendAlertNotification(this,
+                           ALERT_TYPE_LDIF_BACKEND_CANNOT_WRITE_UPDATE, m);
+      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), m);
+    }
+
+    if (tempFile.exists())
+    {
+      // Rename the existing "live" file out of the way and move the new file
+      // into place.
+      try
+      {
+        oldFile.delete();
+      }
+      catch (Exception e)
+      {
+        logger.traceException(e);
+      }
     }
 
     try

--
Gitblit v1.10.0