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 +++++++++++++++++++++++++++++++++++++---------
 opendj-server-legacy/src/messages/org/opends/messages/backend.properties       |    4 ++++
 2 files changed, 41 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
diff --git a/opendj-server-legacy/src/messages/org/opends/messages/backend.properties b/opendj-server-legacy/src/messages/org/opends/messages/backend.properties
index 90f7ad6..2aecb51 100644
--- a/opendj-server-legacy/src/messages/org/opends/messages/backend.properties
+++ b/opendj-server-legacy/src/messages/org/opends/messages/backend.properties
@@ -836,6 +836,10 @@
 ERR_LDIF_BACKEND_ERROR_WRITING_FILE_346=An error occurred while \
  trying to write updated data to file %s for the LDIF backend defined in \
  configuration entry %s:  %s
+ERR_LDIF_BACKEND_ERROR_CLOSING_FILE_426=An error occurred while trying to \
+ close file %s for the LDIF backend defined in configuration entry %s:  %s
+ERR_LDIF_BACKEND_ERROR_EMPTY_FILE_427=The file %s written for the LDIF backend \
+ defined in configuration entry %s is 0 bytes long and unusable.
 ERR_LDIF_BACKEND_ERROR_RENAMING_FILE_347=An error occurred while \
  attempting to rename file %s to %s while writing updated data for the LDIF \
  backend defined in configuration entry %s:  %s

--
Gitblit v1.10.0