- NPE fix on signed udpates

- More work on snark updater
- Clean up imports
This commit is contained in:
zzz
2012-10-21 13:34:23 +00:00
parent 6331cb2374
commit 6e19854e4c
10 changed files with 71 additions and 55 deletions

View File

@ -46,7 +46,6 @@ public class UpdateHandler implements Updater {
method != UpdateMethod.TORRENT || updateSources.isEmpty())
return null;
UpdateRunner update = new UpdateRunner(_context, _umgr, _smgr, updateSources, newVersion);
// set status before thread to ensure UI feedback
_umgr.notifyProgress(update, "<b>" + _smgr.util().getString("Updating") + "</b>");
update.start();
return update;

View File

@ -31,10 +31,9 @@ class UpdateRunner implements UpdateTask, CompleteListener {
private ByteArrayOutputStream _baos;
private URI _currentURI;
private Snark _snark;
private boolean _hasMetaInfo;
private static final long CONNECT_TIMEOUT = 55*1000;
private static final long INACTIVITY_TIMEOUT = 5*60*1000;
private static final long NOPROXY_INACTIVITY_TIMEOUT = 60*1000;
private static final long MAX_LENGTH = 30*1024*1024;
public UpdateRunner(I2PAppContext ctx, UpdateManager umgr, SnarkManager smgr,
List<URI> uris, String newVersion) {
@ -101,17 +100,43 @@ class UpdateRunner implements UpdateTask, CompleteListener {
}
} catch (IllegalArgumentException iae) {}
}
if (_snark == null) {
_umgr.notifyTaskFailed(this, "", null);
if (_snark == null)
fatal("No valid URLs");
}
private void fatal(String error) {
if (_snark != null) {
if (_hasMetaInfo) {
_smgr.stopTorrent(_snark, true);
String file = _snark.getName();
_smgr.removeTorrent(file);
// delete torrent file
File f = new File(_smgr.getDataDir(), file);
f.delete();
// delete data
file = _snark.getBaseName();
f = new File(_smgr.getDataDir(), file);
f.delete();
} else {
_smgr.deleteMagnet(_snark);
}
}
_umgr.notifyTaskFailed(this, error, null);
_log.error(error);
_isRunning = false;
}
}
//////// begin CompleteListener methods
//////// all pass through to SnarkManager
public void torrentComplete(Snark snark) {
String dataFile = snark.getBaseName();
File f = new File(_smgr.getDataDir(), dataFile);
String sudVersion = TrustedUpdate.getVersionString(f);
if (!_newVersion.equals(sudVersion)) {
fatal("version mismatch");
}
_umgr.notifyComplete(this, _newVersion, f);
_smgr.torrentComplete(snark);
}
@ -121,11 +146,26 @@ class UpdateRunner implements UpdateTask, CompleteListener {
}
public String gotMetaInfo(Snark snark) {
Storage storage = snark.getStorage();
MetaInfo info = snark.getMetaInfo();
if (info.getFiles() != null) {
fatal("more than 1 file");
return null;
}
if (info.isPrivate()) {
fatal("private torrent");
return null;
}
if (info.getTotalLength() > MAX_LENGTH) {
fatal("too big");
return null;
}
_hasMetaInfo = true;
return _smgr.gotMetaInfo(snark);
}
public void fatal(Snark snark, String error) {
fatal(error);
_smgr.fatal(snark, error);
}

View File

@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletResponse;
import net.i2p.I2PAppContext;
import net.i2p.data.Base64;
import net.i2p.data.DataHelper;
import net.i2p.update.*;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
@ -42,6 +43,7 @@ import org.klomp.snark.SnarkManager;
import org.klomp.snark.Storage;
import org.klomp.snark.Tracker;
import org.klomp.snark.TrackerClient;
import org.klomp.snark.UpdateHandler;
import org.klomp.snark.dht.DHT;
import org.mortbay.jetty.servlet.DefaultServlet;
@ -57,6 +59,8 @@ public class I2PSnarkServlet extends DefaultServlet {
private I2PAppContext _context;
private Log _log;
private SnarkManager _manager;
private UpdateManager _umgr;
private UpdateHandler _uhandler;
private static long _nonce;
private Resource _resourceBase;
private String _themePath;
@ -76,6 +80,11 @@ public class I2PSnarkServlet extends DefaultServlet {
configFile = "i2psnark.config";
_manager.loadConfig(configFile);
_manager.start();
_umgr = _context.updateManager();
if (_umgr != null) {
_uhandler = new UpdateHandler(_context, _umgr, _manager);
_umgr.register(_uhandler, UpdateType.ROUTER_SIGNED, UpdateMethod.TORRENT, 10);
}
try {
_resourceBase = Resource.newResource(_manager.getDataDir().getAbsolutePath());
} catch (IOException ioe) {}
@ -86,6 +95,10 @@ public class I2PSnarkServlet extends DefaultServlet {
public void destroy() {
if (_manager != null)
_manager.stop();
if (_umgr != null && _uhandler != null) {
//_uhandler.shutdown();
_umgr.unregister(_uhandler, UpdateType.ROUTER_SIGNED, UpdateMethod.TORRENT);
}
super.destroy();
}