From 377e3fe76bd9ec1344278015467aaa483209f494 Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Thu, 23 Jul 2009 14:30:03 +0000
Subject: [PATCH] Fix for defect 4058: Schema backend backup/restore inefficiencies.

---
 opendj-sdk/opends/src/server/org/opends/server/backends/SchemaBackend.java |  193 +++++++++++++++--------------------------------
 1 files changed, 62 insertions(+), 131 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/SchemaBackend.java b/opendj-sdk/opends/src/server/org/opends/server/backends/SchemaBackend.java
index 5ba60ca..72d8186 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/SchemaBackend.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/SchemaBackend.java
@@ -4882,18 +4882,28 @@
       zipStream.setLevel(Deflater.NO_COMPRESSION);
     }
 
+    // Create a Comment Entry in the zip file
+    // This ensure the backup is never empty, even wher
+    // there is no schema file to backup.
 
-    // Get the path to the directory in which the schema files reside and
-    // then get a list of all the files in that directory.
-    String schemaInstallDirPath =
-      SchemaConfigManager.getSchemaDirectoryPath(false);
-    String schemaInstanceDirPath =
-      SchemaConfigManager.getSchemaDirectoryPath(true);
-    File[][] schemaFiles = new File[2][];
+    String commentName = "schema.comment";
+ 
+    // We'll put the name in the hash, too.
+    if (hash)
+    {
+      if (signHash)
+      {
+        mac.update(getBytes(commentName));
+      } else
+      {
+        digest.update(getBytes(commentName));
+      }
+    }
     try
     {
-      File schemaDir = new File(schemaInstallDirPath);
-      schemaFiles[0] = schemaDir.listFiles();
+      ZipEntry zipEntry = new ZipEntry(commentName);
+      zipStream.putNextEntry(zipEntry);
+      zipStream.closeEntry();
     }
     catch (Exception e)
     {
@@ -4902,30 +4912,51 @@
         TRACER.debugCaught(DebugLogLevel.ERROR, e);
       }
 
-      message = ERR_SCHEMA_BACKUP_CANNOT_LIST_SCHEMA_FILES.get(
-          schemaInstallDirPath, getExceptionMessage(e));
-      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-                                   message, e);
+      try
+      {
+        zipStream.close();
+      } catch (Exception e2)
+      {
     }
 
+      message = ERR_SCHEMA_BACKUP_CANNOT_BACKUP_SCHEMA_FILE.get(commentName,
+          stackTraceToSingleLineString(e));
+        throw new DirectoryException(DirectoryServer
+          .getServerErrorResultCode(), message, e);
+    }
+
+    // Get the path to the directory in which the schema files reside and
+    // then get a list of all the files in that directory.
+    String schemaInstanceDirPath =
+      SchemaConfigManager.getSchemaDirectoryPath(true);
+    File schemaDir = null;
+    File[] schemaFiles = null;
+
     try
     {
-      File schemaDir = new File(schemaInstanceDirPath);
-      schemaFiles[1] = schemaDir.listFiles();
+      schemaDir = new File(schemaInstanceDirPath);
+      schemaFiles = schemaDir.listFiles();
     }
     catch (Exception e)
     {
-      schemaFiles[1] = new File[0] ;
+      // Can't locate or list Instance schema directory
+      if (debugEnabled())
+      {
+        TRACER.debugCaught(DebugLogLevel.ERROR, e);
     }
 
+      message = ERR_SCHEMA_BACKUP_CANNOT_LIST_SCHEMA_FILES.get(
+          schemaInstanceDirPath, stackTraceToSingleLineString(e));
+      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
+                                   message, e);
+    }
     // Iterate through the schema files and write them to the zip stream.  If
     // we're using a hash or MAC, then calculate that as well.
     byte[] buffer = new byte[8192];
-    String parent = ".install";
-    for (int i=0 ; i < 2 ; i++)
+    String parent = ".instance";
+
+    for (File schemaFile : schemaFiles)
     {
-      for (File schemaFile : schemaFiles[i])
-      {
         if (backupConfig.isCancelled())
         {
           break;
@@ -4945,10 +4976,10 @@
         {
           if (signHash)
           {
-            mac.update(getBytes(baseName));
+            mac.update(getBytes(baseName + parent));
           } else
           {
-            digest.update(getBytes(baseName));
+            digest.update(getBytes(baseName + parent));
           }
         }
 
@@ -5010,8 +5041,6 @@
               .getServerErrorResultCode(), message, e);
         }
       }
-      parent = ".instance";
-    }
 
 
     // We're done writing the file, so close the zip stream (which should also
@@ -5309,17 +5338,11 @@
     // try to verify the archive.  If we are not going to verify only, then
     // move the current schema directory out of the way so we can keep it around
     // to restore if a problem occurs.
-    String schemaInstallDirPath    =
-      SchemaConfigManager.getSchemaDirectoryPath(false);
     String schemaInstanceDirPath   =
       SchemaConfigManager.getSchemaDirectoryPath(true);
 
-    File   schemaInstallDir        = new File(schemaInstallDirPath);
     File   schemaInstanceDir       = new File(schemaInstanceDirPath);
 
-    String backupInstallDirPath   = null;
-    File   schemaBackupInstallDir = null;
-
     String backupInstanceDirPath   = null;
     File   schemaBackupInstanceDir = null;
     boolean verifyOnly = restoreConfig.verifyOnly();
@@ -5328,43 +5351,6 @@
       // Rename the current schema directory if it exists.
       try
       {
-        if (schemaInstallDir.exists())
-        {
-          String schemaBackupInstallDirPath = schemaInstallDirPath + ".save";
-          backupInstallDirPath = schemaBackupInstallDirPath;
-          schemaBackupInstallDir = new File(backupInstallDirPath);
-          if (schemaBackupInstallDir.exists())
-          {
-            int i=2;
-            while (true)
-            {
-              backupInstallDirPath = schemaBackupInstallDirPath + i;
-              schemaBackupInstallDir = new File(backupInstallDirPath);
-              if (schemaBackupInstallDir.exists())
-              {
-                i++;
-              }
-              else
-              {
-                break;
-              }
-            }
-          }
-
-          schemaInstallDir.renameTo(schemaBackupInstallDir);
-        }
-      }
-      catch (Exception e)
-      {
-        Message message = ERR_SCHEMA_RESTORE_CANNOT_RENAME_CURRENT_DIRECTORY.
-            get(backupID, schemaInstallDirPath,
-                String.valueOf(backupInstallDirPath),
-                stackTraceToSingleLineString(e));
-        throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
-                                     message, e);
-      }
-      try
-      {
         if (schemaInstanceDir.exists())
         {
           String schemaBackupInstanceDirPath = schemaInstanceDirPath + ".save";
@@ -5405,30 +5391,12 @@
       // Create a new directory to hold the restored schema files.
       try
       {
-        schemaInstallDir.mkdirs();
         schemaInstanceDir.mkdirs();
       }
       catch (Exception e)
       {
         // Try to restore the previous schema directory if possible.  This will
         // probably fail in this case, but try anyway.
-        if (schemaBackupInstallDir != null)
-        {
-          try
-          {
-            schemaBackupInstallDir.renameTo(schemaInstallDir);
-            Message message =
-                NOTE_SCHEMA_RESTORE_RESTORED_OLD_SCHEMA.get(
-                    schemaInstallDirPath);
-            logError(message);
-          }
-          catch (Exception e2)
-          {
-            Message message = ERR_SCHEMA_RESTORE_CANNOT_RESTORE_OLD_SCHEMA.get(
-                schemaBackupInstallDir.getPath());
-            logError(message);
-          }
-        }
         if (schemaBackupInstanceDir != null)
         {
           try
@@ -5447,10 +5415,8 @@
           }
         }
 
-
         Message message = ERR_SCHEMA_RESTORE_CANNOT_CREATE_SCHEMA_DIRECTORY.get(
-            backupID, schemaInstallDirPath,schemaInstanceDirPath,
-            stackTraceToSingleLineString(e));
+            backupID, schemaInstanceDirPath, stackTraceToSingleLineString(e));
         throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
                                      message, e);
       }
@@ -5471,12 +5437,6 @@
       catch (Exception e)
       {
         // Tell the user where the previous schema was archived.
-        if (schemaBackupInstallDir != null)
-        {
-          Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
-              schemaBackupInstallDir.getPath());
-          logError(message);
-        }
         if (schemaBackupInstanceDir != null)
         {
           Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
@@ -5508,23 +5468,23 @@
         mac.update(getBytes(fileName));
       }
 
-      String baseDirPath ;
-      if (fileName.endsWith(".install"))
+      String baseDirPath = schemaInstanceDirPath;
+      Boolean restoreIt = true;
+      if (fileName.endsWith(".instance"))
       {
-        fileName = fileName.substring(0,fileName.lastIndexOf(".install"));
-        baseDirPath = schemaInstallDirPath;
+        fileName = fileName.substring(0,fileName.lastIndexOf(".instance"));
       }
       else
       {
-        fileName = fileName.substring(0,fileName.lastIndexOf(".instance"));
-        baseDirPath = schemaInstanceDirPath;
+        // Skip file.
+        // ".install" files are from old backups and should be ignored
+        restoreIt = false;
       }
 
-
       // If we're doing the restore, then create the output stream to write the
       // file.
       OutputStream outputStream = null;
-      if (! verifyOnly)
+      if ((! verifyOnly) && restoreIt)
       {
         String filePath = baseDirPath + File.separator + fileName;
         try
@@ -5534,12 +5494,6 @@
         catch (Exception e)
         {
           // Tell the user where the previous schema was archived.
-          if (schemaBackupInstallDir != null)
-          {
-            Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
-                schemaBackupInstallDir.getPath());
-            logError(message);
-          }
           if (schemaBackupInstanceDir != null)
           {
             Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
@@ -5601,12 +5555,6 @@
       catch (Exception e)
       {
         // Tell the user where the previous schema was archived.
-        if (schemaBackupInstallDir != null)
-        {
-          Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
-              schemaBackupInstallDir.getPath());
-          logError(message);
-        }
         if (schemaBackupInstanceDir != null)
         {
           Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
@@ -5650,13 +5598,6 @@
       else
       {
         // Tell the user where the previous schema was archived.
-        if (schemaBackupInstallDir != null)
-        {
-          Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
-              schemaBackupInstallDir.getPath());
-          logError(message);
-        }
-        // Tell the user where the previous schema was archived.
         if (schemaBackupInstanceDir != null)
         {
           Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
@@ -5682,12 +5623,6 @@
       else
       {
         // Tell the user where the previous schema was archived.
-        if (schemaBackupInstallDir != null)
-        {
-          Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
-              schemaBackupInstallDir.getPath());
-          logError(message);
-        }
         if (schemaBackupInstanceDir != null)
         {
           Message message = ERR_SCHEMA_RESTORE_OLD_SCHEMA_SAVED.get(
@@ -5715,10 +5650,6 @@
     // If we've gotten here, then the archive was restored successfully.  Get
     // rid of the temporary copy we made of the previous schema directory and
     // exit.
-    if (schemaBackupInstallDir != null)
-    {
-      recursiveDelete(schemaBackupInstallDir);
-    }
     if (schemaBackupInstanceDir != null)
     {
       recursiveDelete(schemaBackupInstanceDir);

--
Gitblit v1.10.0