diff --git a/apps/syndie/java/src/net/i2p/syndie/Archive.java b/apps/syndie/java/src/net/i2p/syndie/Archive.java index 22232e3de..5c757fb23 100644 --- a/apps/syndie/java/src/net/i2p/syndie/Archive.java +++ b/apps/syndie/java/src/net/i2p/syndie/Archive.java @@ -67,8 +67,10 @@ public class Archive { File meta = new File(f[i], METADATA_FILE); if (meta.exists()) { BlogInfo bi = new BlogInfo(); + FileInputStream fi = null; try { - bi.load(new FileInputStream(meta)); + fi = new FileInputStream(meta); + bi.load(fi); if (bi.verify(_context)) { info.add(bi); } else { @@ -77,6 +79,8 @@ public class Archive { } } catch (IOException ioe) { _log.error("Error loading the blog", ioe); + } finally { + if (fi != null) try { fi.close(); } catch (IOException ioe) {} } } } @@ -276,7 +280,13 @@ public class Archive { } else { // we have an explicit key - no caching entry = new EntryContainer(); - entry.load(new FileInputStream(entries[i])); + FileInputStream fi = null; + try { + fi = new FileInputStream(entries[i]); + entry.load(fi); + } finally { + if (fi != null) try { fi.close(); } catch (IOException ioe) {} + } boolean ok = entry.verifySignature(_context, info); if (!ok) { _log.error("Keyed entry " + entries[i].getPath() + " is not valid"); diff --git a/apps/syndie/java/src/net/i2p/syndie/ArchiveIndexer.java b/apps/syndie/java/src/net/i2p/syndie/ArchiveIndexer.java index 8bc3b4fe0..45ca0ad4a 100644 --- a/apps/syndie/java/src/net/i2p/syndie/ArchiveIndexer.java +++ b/apps/syndie/java/src/net/i2p/syndie/ArchiveIndexer.java @@ -26,8 +26,9 @@ class ArchiveIndexer { File headerFile = new File(rootDir, Archive.HEADER_FILE); if (headerFile.exists()) { + BufferedReader in = null; try { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(headerFile), "UTF-8")); + in = new BufferedReader(new InputStreamReader(new FileInputStream(headerFile), "UTF-8")); String line = null; while ( (line = in.readLine()) != null) { StringTokenizer tok = new StringTokenizer(line, ":"); @@ -36,6 +37,8 @@ class ArchiveIndexer { } } catch (IOException ioe) { log.error("Error reading header file", ioe); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} } } diff --git a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java index a32ca428e..69b6d91f2 100644 --- a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java +++ b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java @@ -193,9 +193,11 @@ public class BlogManager { List rv = new ArrayList(); for (int i = 0; i < files.length; i++) { if (files[i].isFile() && !files[i].isHidden()) { + FileInputStream in = null; try { SigningPublicKey pub = new SigningPublicKey(); - pub.readBytes(new FileInputStream(files[i])); + in = new FileInputStream(files[i]); + pub.readBytes(in); BlogInfo info = _archive.getBlogInfo(pub.calculateHash()); if (info != null) rv.add(info); @@ -203,6 +205,8 @@ public class BlogManager { _log.error("Error listing the blog", ioe); } catch (DataFormatException dfe) { _log.error("Error listing the blog", dfe); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} } } } @@ -212,8 +216,9 @@ public class BlogManager { public SigningPrivateKey getMyPrivateKey(BlogInfo blog) { if (blog == null) return null; File keyFile = new File(_privKeyDir, Base64.encode(blog.getKey().calculateHash().getData()) + ".priv"); + FileInputStream in = null; try { - FileInputStream in = new FileInputStream(keyFile); + in = new FileInputStream(keyFile); SigningPublicKey pub = new SigningPublicKey(); pub.readBytes(in); SigningPrivateKey priv = new SigningPrivateKey(); @@ -225,6 +230,8 @@ public class BlogManager { } catch (DataFormatException dfe) { _log.error("Error reading the blog key", dfe); return null; + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} } } @@ -264,10 +271,11 @@ public class BlogManager { } private Properties loadUserProps(File userFile) { + BufferedReader in = null; try { Properties props = new Properties(); FileInputStream fin = new FileInputStream(userFile); - BufferedReader in = new BufferedReader(new InputStreamReader(fin, "UTF-8")); + in = new BufferedReader(new InputStreamReader(fin, "UTF-8")); String line = null; while ( (line = in.readLine()) != null) { int split = line.indexOf('='); @@ -281,6 +289,8 @@ public class BlogManager { return props; } catch (IOException ioe) { return null; + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} } } @@ -737,6 +747,8 @@ public class BlogManager { long entryId = getNextBlogEntry(user); + _log.debug("Next blog entry ID = " + entryId + " for user " + user.getUsername()); + StringTokenizer tok = new StringTokenizer(tags, " ,\n\t"); String tagList[] = new String[tok.countTokens()]; for (int i = 0; i < tagList.length; i++) @@ -803,11 +815,14 @@ public class BlogManager { boolean ok = getArchive().storeEntry(c); if (ok) { getArchive().regenerateIndex(); + long prevEntryId = user.getMostRecentEntry(); user.setMostRecentEntry(entryId); - if(shouldAuthenticate) + if(shouldAuthenticate) { saveUser(user); - else + } else { storeUser(user); + } + _log.debug("New entry posted, entryId=" + entryId + " prev=" + prevEntryId); return uri; } else { return null; @@ -887,7 +902,13 @@ public class BlogManager { private Properties getKnownHosts(File filename) throws IOException { Properties rv = new Properties(); if (filename.exists()) { - rv.load(new FileInputStream(filename)); + FileInputStream in = null; + try { + in = new FileInputStream(filename); + rv.load(in); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + } } return rv; } diff --git a/apps/syndie/java/src/net/i2p/syndie/EntryExtractor.java b/apps/syndie/java/src/net/i2p/syndie/EntryExtractor.java index f1451e787..ef343f11d 100644 --- a/apps/syndie/java/src/net/i2p/syndie/EntryExtractor.java +++ b/apps/syndie/java/src/net/i2p/syndie/EntryExtractor.java @@ -37,7 +37,13 @@ public class EntryExtractor { public boolean extract(File entryFile, File entryDir, SessionKey entryKey, BlogInfo info) throws IOException { EntryContainer entry = new EntryContainer(); - entry.load(new FileInputStream(entryFile)); + FileInputStream in = null; + try { + in = new FileInputStream(entryFile); + entry.load(in); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + } boolean ok = entry.verifySignature(_context, info); if (!ok) { return false; diff --git a/apps/syndie/java/src/net/i2p/syndie/Sucker.java b/apps/syndie/java/src/net/i2p/syndie/Sucker.java index 33d4d8510..d66b4f20b 100644 --- a/apps/syndie/java/src/net/i2p/syndie/Sucker.java +++ b/apps/syndie/java/src/net/i2p/syndie/Sucker.java @@ -158,12 +158,15 @@ public class Sucker { if (!lastIdFile.exists()) lastIdFile.createNewFile(); - FileInputStream fis = new FileInputStream(lastIdFile); - String number = readLine(fis); + FileInputStream fis = null; try { + fis = new FileInputStream(lastIdFile); + String number = readLine(fis); messageNumber = Integer.parseInt(number); } catch (NumberFormatException e) { messageNumber = 0; + } finally { + if (fis != null) try { fis.close(); } catch (IOException ioe) {} } // Create outputDir if missing @@ -226,28 +229,39 @@ public class Sucker { _log.debug("entries: " + entries.size()); - FileOutputStream hos = new FileOutputStream(historyFile, true); + FileOutputStream hos = null; - // Process list backwards to get syndie to display the - // entries in the right order. (most recent at top) - for (int i = entries.size()-1; i >= 0; i--) { - SyndEntry e = (SyndEntry) entries.get(i); + try { + hos = new FileOutputStream(historyFile, true); - attachmentCounter=0; - - if (_log.shouldLog(Log.DEBUG)) - _log.debug("Syndicate entry: " + e.getLink()); - - String messageId = convertToSml(e); - if (messageId!=null) { - hos.write(messageId.getBytes()); - hos.write("\n".getBytes()); + // Process list backwards to get syndie to display the + // entries in the right order. (most recent at top) + for (int i = entries.size()-1; i >= 0; i--) { + SyndEntry e = (SyndEntry) entries.get(i); + + attachmentCounter=0; + + if (_log.shouldLog(Log.DEBUG)) + _log.debug("Syndicate entry: " + e.getLink()); + + String messageId = convertToSml(e); + if (messageId!=null) { + hos.write(messageId.getBytes()); + hos.write("\n".getBytes()); + } } + } finally { + if (hos != null) try { hos.close(); } catch (IOException ioe) {} } if(!pushToSyndie) { - FileOutputStream fos = new FileOutputStream(lastIdFile); - fos.write(("" + messageNumber).getBytes()); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(lastIdFile); + fos.write(("" + messageNumber).getBytes()); + } finally { + if (fos != null) try { fos.close(); } catch (IOException ioe) {} + } } _log.debug("done fetching"); @@ -734,8 +748,9 @@ public class Sucker { String lineToCompare = messageId.substring(0, idx-1); idx = lineToCompare.lastIndexOf(":"); lineToCompare = lineToCompare.substring(0, idx-1); + FileInputStream his = null; try { - FileInputStream his = new FileInputStream(historyFile); + his = new FileInputStream(historyFile); String line; while ((line = readLine(his)) != null) { idx = line.lastIndexOf(":"); @@ -751,6 +766,8 @@ public class Sucker { } } catch (FileNotFoundException e) { e.printStackTrace(); + } finally { + if (his != null) try { his.close(); } catch (IOException ioe) {} } return false; } diff --git a/apps/syndie/java/src/net/i2p/syndie/web/ArchiveServlet.java b/apps/syndie/java/src/net/i2p/syndie/web/ArchiveServlet.java index 7de9b3397..cb45ed73c 100644 --- a/apps/syndie/java/src/net/i2p/syndie/web/ArchiveServlet.java +++ b/apps/syndie/java/src/net/i2p/syndie/web/ArchiveServlet.java @@ -196,13 +196,20 @@ public class ArchiveServlet extends HttpServlet { } private void dump(File source, HttpServletResponse resp) throws ServletException, IOException { - FileInputStream in = new FileInputStream(source); - OutputStream out = resp.getOutputStream(); - byte buf[] = new byte[1024]; - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); - out.close(); - in.close(); + FileInputStream in = null; + OutputStream out = null; + try { + in = new FileInputStream(source); + out = resp.getOutputStream(); + byte buf[] = new byte[1024]; + int read = 0; + while ( (read = in.read(buf)) != -1) + out.write(buf, 0, read); + out.close(); + in.close(); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + if (out != null) try { out.close(); } catch (IOException ioe) {} + } } } diff --git a/apps/syndie/java/src/net/i2p/syndie/web/ExportServlet.java b/apps/syndie/java/src/net/i2p/syndie/web/ExportServlet.java index a586f5f7d..51179a789 100644 --- a/apps/syndie/java/src/net/i2p/syndie/web/ExportServlet.java +++ b/apps/syndie/java/src/net/i2p/syndie/web/ExportServlet.java @@ -136,10 +136,15 @@ public class ExportServlet extends HttpServlet { ze = new ZipEntry("meta" + i); ze.setTime(0); zo.putNextEntry(ze); - FileInputStream in = new FileInputStream((File)metaFiles.get(i)); - while ( (read = in.read(buf)) != -1) - zo.write(buf, 0, read); - zo.closeEntry(); + FileInputStream in = null; + try { + in = new FileInputStream((File)metaFiles.get(i)); + while ( (read = in.read(buf)) != -1) + zo.write(buf, 0, read); + zo.closeEntry(); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + } } List entryFiles = getEntryFiles(entries); @@ -147,10 +152,15 @@ public class ExportServlet extends HttpServlet { ze = new ZipEntry("entry" + i); ze.setTime(0); zo.putNextEntry(ze); - FileInputStream in = new FileInputStream((File)entryFiles.get(i)); - while ( (read = in.read(buf)) != -1) - zo.write(buf, 0, read); - zo.closeEntry(); + FileInputStream in = null; + try { + in = new FileInputStream((File)entryFiles.get(i)); + while ( (read = in.read(buf)) != -1) + zo.write(buf, 0, read); + zo.closeEntry(); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + } } if (zo != null) { diff --git a/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java b/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java index c254b176d..57baf2086 100644 --- a/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java +++ b/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java @@ -74,12 +74,17 @@ public class PostBean { } public void writeAttachmentData(int id, OutputStream out) throws IOException { - FileInputStream in = new FileInputStream((File)_localFiles.get(id)); - byte buf[] = new byte[1024]; - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); - out.close(); + FileInputStream in = null; + try { + in = new FileInputStream((File)_localFiles.get(id)); + byte buf[] = new byte[1024]; + int read = 0; + while ( (read = in.read(buf)) != -1) + out.write(buf, 0, read); + out.close(); + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + } } public void addAttachment(String filename, InputStream fileStream, String mimeType) { diff --git a/apps/syndie/java/src/net/i2p/syndie/web/ViewThreadedServlet.java b/apps/syndie/java/src/net/i2p/syndie/web/ViewThreadedServlet.java index cafcdc914..1cd4cf7c0 100644 --- a/apps/syndie/java/src/net/i2p/syndie/web/ViewThreadedServlet.java +++ b/apps/syndie/java/src/net/i2p/syndie/web/ViewThreadedServlet.java @@ -250,6 +250,7 @@ public class ViewThreadedServlet extends BaseServlet { out.write(rend.getEntryDate(node.getEntry().getEntryId())); out.write(": "); EntryContainer entry = archive.getEntry(node.getEntry()); + if (entry == null) throw new RuntimeException("Unable to fetch the entry " + node.getEntry()); HeaderReceiver rec = new HeaderReceiver(); parser.parse(entry.getEntry().getText(), rec); diff --git a/history.txt b/history.txt index 2af5c0032..bc8af4097 100644 --- a/history.txt +++ b/history.txt @@ -1,4 +1,7 @@ -$Id: history.txt,v 1.322 2005/11/15 22:20:22 jrandom Exp $ +$Id: history.txt,v 1.323 2005/11/16 06:50:56 jrandom Exp $ + +2005-11-17 jrandom + * More cautious file handling in Syndie 2005-11-16 jrandom * More aggressive I2PTunnel content encoding munging to work around some diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index 0e8b82f41..c8d1e34e9 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -15,9 +15,9 @@ import net.i2p.CoreVersion; * */ public class RouterVersion { - public final static String ID = "$Revision: 1.290 $ $Date: 2005/11/15 22:20:22 $"; + public final static String ID = "$Revision: 1.291 $ $Date: 2005/11/16 06:50:57 $"; public final static String VERSION = "0.6.1.5"; - public final static long BUILD = 1; + public final static long BUILD = 2; public static void main(String args[]) { System.out.println("I2P Router version: " + VERSION + "-" + BUILD); System.out.println("Router ID: " + RouterVersion.ID);