* 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.
This commit is contained in:
ragnarok
2005-08-01 03:32:37 +00:00
committed by zzz
parent 0637050cbc
commit 03bba51c1e
3 changed files with 25 additions and 38 deletions

View File

@ -68,17 +68,16 @@ public class AddressBook {
*/ */
public AddressBook(String url, String proxyHost, int proxyPort) { public AddressBook(String url, String proxyHost, int proxyPort) {
this.location = url; this.location = url;
EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
proxyHost, proxyPort, 0, "addressbook.tmp", url, true,
null);
get.fetch();
try { try {
EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true,
proxyHost, proxyPort, 0, "addressbook.tmp", url, true,
null);
get.fetch();
this.addresses = ConfigParser.parse("addressbook.tmp"); this.addresses = ConfigParser.parse("addressbook.tmp");
new File("addressbook.tmp").delete();
} catch (IOException exp) { } catch (IOException exp) {
this.addresses = new HashMap(); this.addresses = new HashMap();
} }
new File("addressbook.tmp").delete();
} }
/** /**
@ -91,18 +90,17 @@ public class AddressBook {
*/ */
public AddressBook(Subscription subscription, String proxyHost, int proxyPort) { public AddressBook(Subscription subscription, String proxyHost, int proxyPort) {
this.location = subscription.getLocation(); 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 { 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"); this.addresses = ConfigParser.parse("addressbook.tmp");
subscription.setEtag(get.getETag());
new File("addressbook.tmp").delete();
} catch (IOException exp) { } catch (IOException exp) {
this.addresses = new HashMap(); this.addresses = new HashMap();
} }
new File("addressbook.tmp").delete();
} }
/** /**
@ -164,7 +162,7 @@ public class AddressBook {
* @param log * @param log
* The log to write messages about new addresses or conflicts to. * 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(); Iterator otherIter = other.addresses.keySet().iterator();
while (otherIter.hasNext()) { while (otherIter.hasNext()) {
@ -172,7 +170,7 @@ public class AddressBook {
String otherValue = (String) other.addresses.get(otherKey); String otherValue = (String) other.addresses.get(otherKey);
if (otherKey.endsWith(".i2p") && otherValue.length() >= 516) { 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) if (!this.addresses.get(otherKey).equals(otherValue)
&& log != null) { && log != null) {
log.append("Conflict for " + otherKey + " from " log.append("Conflict for " + otherKey + " from "
@ -181,27 +179,19 @@ public class AddressBook {
+ otherValue); + otherValue);
} }
} else { } else {
this.addresses.put(otherKey, otherValue); if (!this.addresses.get(otherKey).equals(otherValue)) {
this.modified = true; this.addresses.put(otherKey, otherValue);
if (log != null) { this.modified = true;
log.append("New address " + otherKey if (log != null) {
log.append("New address " + otherKey
+ " added to address book."); + " 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 * Write the contents of this AddressBook out to the File file. If the file
* cannot be writen to, this method will silently fail. * cannot be writen to, this method will silently fail.

View File

@ -58,15 +58,14 @@ public class Daemon {
*/ */
public static void update(AddressBook master, AddressBook router, public static void update(AddressBook master, AddressBook router,
File published, SubscriptionList subscriptions, Log log) { File published, SubscriptionList subscriptions, Log log) {
String routerLocation = router.getLocation(); router.merge(master, true, null);
master.merge(router);
Iterator iter = subscriptions.iterator(); Iterator iter = subscriptions.iterator();
while (iter.hasNext()) { 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) if (published != null)
master.write(published); router.write(published);
subscriptions.write(); subscriptions.write();
} }

View File

@ -69,11 +69,11 @@ public class SubscriptionList {
this.lastModifiedFile = lastModifiedFile; this.lastModifiedFile = lastModifiedFile;
this.proxyHost = proxyHost; this.proxyHost = proxyHost;
this.proxyPort = proxyPort; this.proxyPort = proxyPort;
List locations;
Map etags; Map etags;
Map lastModified; Map lastModified;
String location; String location;
locations = ConfigParser.parseSubscriptions(locationsFile, defaultSubs); List locations = ConfigParser.parseSubscriptions(locationsFile,
defaultSubs);
try { try {
etags = ConfigParser.parse(etagsFile); etags = ConfigParser.parse(etagsFile);
} catch (IOException exp) { } catch (IOException exp) {
@ -90,8 +90,6 @@ public class SubscriptionList {
this.subscriptions.add(new Subscription(location, (String) etags this.subscriptions.add(new Subscription(location, (String) etags
.get(location), (String) lastModified.get(location))); .get(location), (String) lastModified.get(location)));
} }
iter = this.iterator();
} }
/** /**