From 8636196599847e5fa861942f382353b90eae517e Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Tue, 26 Jun 2007 21:03:20 +0000
Subject: [PATCH] Addresses a problem encountered in the upgrader on Windows in which configuration changes made to the server using an internal connection where not persisted in the config.ldif file. The problem appears to have been an unclosed stream that was preventing the renaming of config.ldif.tmp to config.ldif.
---
opends/src/server/org/opends/server/util/StaticUtils.java | 41 ++++++++++++++++++++
opends/src/server/org/opends/server/messages/UtilityMessages.java | 25 ++++++++++++
opends/src/server/org/opends/server/extensions/ConfigFileHandler.java | 23 +++++++++--
3 files changed, 84 insertions(+), 5 deletions(-)
diff --git a/opends/src/server/org/opends/server/extensions/ConfigFileHandler.java b/opends/src/server/org/opends/server/extensions/ConfigFileHandler.java
index 0c984e1..e23adc2 100644
--- a/opends/src/server/org/opends/server/extensions/ConfigFileHandler.java
+++ b/opends/src/server/org/opends/server/extensions/ConfigFileHandler.java
@@ -768,11 +768,12 @@
private byte[] calculateConfigDigest()
throws DirectoryException
{
+ InputStream inputStream = null;
try
{
MessageDigest sha1Digest =
MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM_SHA_1);
- FileInputStream inputStream = new FileInputStream(configFile);
+ inputStream = new FileInputStream(configFile);
byte[] buffer = new byte[8192];
while (true)
{
@@ -784,7 +785,6 @@
sha1Digest.update(buffer, 0, bytesRead);
}
-
return sha1Digest.digest();
}
catch (Exception e)
@@ -795,6 +795,19 @@
throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
message, msgID, e);
}
+ finally
+ {
+ if (inputStream != null)
+ {
+ try
+ {
+ inputStream.close();
+ }
+ catch (IOException e) {
+ // ignore;
+ }
+ }
+ }
}
@@ -2030,9 +2043,9 @@
// Delete the previous version of the configuration and rename the new one.
try
{
- File f = new File(configFile);
- f.delete();
- new File(tempConfig).renameTo(f);
+ File actualConfig = new File(configFile);
+ File tmpConfig = new File(tempConfig);
+ renameFile(tmpConfig, actualConfig);
}
catch (Exception e)
{
diff --git a/opends/src/server/org/opends/server/messages/UtilityMessages.java b/opends/src/server/org/opends/server/messages/UtilityMessages.java
index df2540a..da43bc9 100644
--- a/opends/src/server/org/opends/server/messages/UtilityMessages.java
+++ b/opends/src/server/org/opends/server/messages/UtilityMessages.java
@@ -1652,6 +1652,23 @@
/**
+ * The message ID for the message that will be used an attempt to rename a
+ * file fails because the existing target file cannot be deleted. This takes
+ * a single argument, which is the provided path of the file to move.
+ */
+ public static final int MSGID_RENAMEFILE_CANNOT_RENAME =
+ CATEGORY_MASK_UTIL | SEVERITY_MASK_SEVERE_ERROR | 157;
+
+ /**
+ * The message ID for the message that will be used an attempt to rename a
+ * file fails because the rename operation fails. This takes two
+ * arguments, the source and target file names.
+ */
+ public static final int MSGID_RENAMEFILE_CANNOT_DELETE_TARGET =
+ CATEGORY_MASK_UTIL | SEVERITY_MASK_SEVERE_ERROR | 158;
+
+
+ /**
* Associates a set of generic messages with the message IDs defined in this
* class.
*/
@@ -2197,6 +2214,14 @@
"The accepted value for global options are:");
registerMessage(MSGID_GLOBAL_HELP_REFERENCE,
"See \"%s --help\" to get more usage help");
+
+ registerMessage(MSGID_RENAMEFILE_CANNOT_DELETE_TARGET,
+ "Failed to delete target file %s. Make sure the file is " +
+ "not currently in use by this or another application");
+
+ registerMessage(MSGID_RENAMEFILE_CANNOT_RENAME,
+ "Failed to rename file %s to %s");
+
}
}
diff --git a/opends/src/server/org/opends/server/util/StaticUtils.java b/opends/src/server/org/opends/server/util/StaticUtils.java
index 0a9a3f8..58ffeb1 100644
--- a/opends/src/server/org/opends/server/util/StaticUtils.java
+++ b/opends/src/server/org/opends/server/util/StaticUtils.java
@@ -59,6 +59,7 @@
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.types.DebugLogLevel;
import static org.opends.server.messages.MessageHandler.*;
+import static org.opends.server.messages.MessageHandler.getMessage;
import static org.opends.server.messages.UtilityMessages.*;
import static org.opends.server.util.ServerConstants.*;
@@ -3378,6 +3379,46 @@
fileToMove.delete();
}
+ /**
+ * Renames the source file to the target file. If the target file exists
+ * it is first deleted. The rename and delete operation return values
+ * are checked for success and if unsuccessful, this method throws an
+ * exception.
+ *
+ * @param fileToRename The file to rename.
+ * @param target The file to which <code>fileToRename</code> will be
+ * moved.
+ * @throws IOException If a problem occurs while attempting to rename the
+ * file. On the Windows platform, this typically
+ * indicates that the file is in use by this or another
+ * application.
+ */
+ static public void renameFile(File fileToRename, File target)
+ throws IOException {
+ if (fileToRename != null && target != null)
+ {
+ synchronized(target)
+ {
+ if (target.exists())
+ {
+ if (!target.delete())
+ {
+ int msgID = MSGID_RENAMEFILE_CANNOT_DELETE_TARGET;
+ String message = getMessage(msgID, target.getPath());
+ throw new IOException(message);
+ }
+ }
+ }
+ if (!fileToRename.renameTo(target))
+ {
+ int msgID = MSGID_RENAMEFILE_CANNOT_RENAME;
+ String message = getMessage(msgID, fileToRename.getPath(),
+ target.getPath());
+ throw new IOException(message);
+
+ }
+ }
+ }
/**
--
Gitblit v1.10.0