From 03bba51c1ec338946bbbc32ee6311a6d75e4f26c Mon Sep 17 00:00:00 2001
From: ragnarok <ragnarok>
Date: Mon, 1 Aug 2005 03:32:37 +0000
Subject: [PATCH] * Fixed some issues with the merge logic that caused
 addressbooks to be written to disk even when unmodified. * Fixed a bug that
 could result in a downloaded remote addressbook not being deleted, halting
 the update process.

---
 .../java/src/addressbook/AddressBook.java     | 48 ++++++++-----------
 .../java/src/addressbook/Daemon.java          |  9 ++--
 .../src/addressbook/SubscriptionList.java     |  6 +--
 3 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/apps/addressbook/java/src/addressbook/AddressBook.java b/apps/addressbook/java/src/addressbook/AddressBook.java
index c345c07d8b..c496141e3e 100644
--- a/apps/addressbook/java/src/addressbook/AddressBook.java
+++ b/apps/addressbook/java/src/addressbook/AddressBook.java
@@ -68,17 +68,16 @@ public class AddressBook {
      */
     public AddressBook(String url, String proxyHost, int proxyPort) {
         this.location = url;
-
+        EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
+                proxyHost, proxyPort, 0, "addressbook.tmp", url, true, 
+                null);
+        get.fetch();
         try {
-            EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
-                    proxyHost, proxyPort, 0, "addressbook.tmp", url, true, 
-                    null);
-            get.fetch();
             this.addresses = ConfigParser.parse("addressbook.tmp");
-            new File("addressbook.tmp").delete();
         } catch (IOException exp) {
             this.addresses = new HashMap();
         }
+        new File("addressbook.tmp").delete();
     }
 
     /**
@@ -91,18 +90,17 @@ public class AddressBook {
      */
     public AddressBook(Subscription subscription, String proxyHost, int proxyPort) {
         this.location = subscription.getLocation();
-
+        EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
+                proxyHost, proxyPort, 0, "addressbook.tmp", 
+                subscription.getLocation(), true, subscription.getEtag());
+        get.fetch();
+        subscription.setEtag(get.getETag());
         try {            
-            EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
-                    proxyHost, proxyPort, 0, "addressbook.tmp", 
-                    subscription.getLocation(), true, subscription.getEtag());
-            get.fetch();
             this.addresses = ConfigParser.parse("addressbook.tmp");
-            subscription.setEtag(get.getETag());
-            new File("addressbook.tmp").delete();
         } catch (IOException exp) {
             this.addresses = new HashMap();
         }
+        new File("addressbook.tmp").delete();
     }
 
     /**
@@ -164,7 +162,7 @@ public class AddressBook {
      * @param log
      *            The log to write messages about new addresses or conflicts to.
      */
-    public void merge(AddressBook other, Log log) {
+    public void merge(AddressBook other, boolean overwrite, Log log) {
         Iterator otherIter = other.addresses.keySet().iterator();
 
         while (otherIter.hasNext()) {
@@ -172,7 +170,7 @@ public class AddressBook {
             String otherValue = (String) other.addresses.get(otherKey);
 
             if (otherKey.endsWith(".i2p") && otherValue.length() >= 516) {
-                if (this.addresses.containsKey(otherKey)) {
+                if (this.addresses.containsKey(otherKey) && !overwrite) {
                     if (!this.addresses.get(otherKey).equals(otherValue)
                             && log != null) {
                         log.append("Conflict for " + otherKey + " from "
@@ -181,27 +179,19 @@ public class AddressBook {
                                 + otherValue);
                     }
                 } else {
-                    this.addresses.put(otherKey, otherValue);
-                    this.modified = true;
-                    if (log != null) {
-                        log.append("New address " + otherKey
+                    if (!this.addresses.get(otherKey).equals(otherValue)) {
+                        this.addresses.put(otherKey, otherValue);
+                        this.modified = true;
+                        if (log != null) {
+                            log.append("New address " + otherKey
                                 + " added to address book.");
+                        }
                     }
                 }
             }
         }
     }
 
-    /**
-     * Merge this AddressBook with other, without logging.
-     * 
-     * @param other
-     *            An AddressBook to merge with.
-     */
-    public void merge(AddressBook other) {
-        this.merge(other, null);
-    }
-
     /**
      * Write the contents of this AddressBook out to the File file. If the file
      * cannot be writen to, this method will silently fail.
diff --git a/apps/addressbook/java/src/addressbook/Daemon.java b/apps/addressbook/java/src/addressbook/Daemon.java
index 72757a569f..4b724602b6 100644
--- a/apps/addressbook/java/src/addressbook/Daemon.java
+++ b/apps/addressbook/java/src/addressbook/Daemon.java
@@ -58,15 +58,14 @@ public class Daemon {
      */
     public static void update(AddressBook master, AddressBook router,
             File published, SubscriptionList subscriptions, Log log) {
-        String routerLocation = router.getLocation();
-        master.merge(router);
+        router.merge(master, true, null);
         Iterator iter = subscriptions.iterator();
         while (iter.hasNext()) {
-            master.merge((AddressBook) iter.next(), log);
+            router.merge((AddressBook) iter.next(), false, log);
         }
-        master.write(new File(routerLocation));
+        router.write();
         if (published != null)
-            master.write(published);
+            router.write(published);
         subscriptions.write();
     }
 
diff --git a/apps/addressbook/java/src/addressbook/SubscriptionList.java b/apps/addressbook/java/src/addressbook/SubscriptionList.java
index 12036c54a4..9b20add2c3 100644
--- a/apps/addressbook/java/src/addressbook/SubscriptionList.java
+++ b/apps/addressbook/java/src/addressbook/SubscriptionList.java
@@ -69,11 +69,11 @@ public class SubscriptionList {
         this.lastModifiedFile = lastModifiedFile;
         this.proxyHost = proxyHost;
         this.proxyPort = proxyPort;
-        List locations;
         Map etags;
         Map lastModified;
         String location;
-        locations = ConfigParser.parseSubscriptions(locationsFile, defaultSubs);
+        List locations = ConfigParser.parseSubscriptions(locationsFile, 
+                defaultSubs);
         try {
             etags = ConfigParser.parse(etagsFile);
         } catch (IOException exp) {
@@ -90,8 +90,6 @@ public class SubscriptionList {
             this.subscriptions.add(new Subscription(location, (String) etags
                     .get(location), (String) lastModified.get(location)));
         }
-
-        iter = this.iterator();
     }
     
     /**
-- 
GitLab