From 010b285e673e37f6975a5fa8bdf873bb5622e7c2 Mon Sep 17 00:00:00 2001 From: jrandom Date: Wed, 29 Sep 2004 19:34:02 +0000 Subject: [PATCH] 2004-09-29 jrandom * Always wipe the Jetty work directory on startup, so that web updates are reflected immediately (Jetty does not honor the cache across multiple executions) in addition, refactor various file ops out of the DataHelper into FileUtil --- .../src/net/i2p/router/web/ContentHelper.java | 6 +- .../src/net/i2p/router/web/LogsHelper.java | 4 +- .../i2p/router/web/RouterConsoleRunner.java | 10 + core/java/src/net/i2p/data/DataHelper.java | 95 +--------- core/java/src/net/i2p/util/FileUtil.java | 171 ++++++++++++++++++ history.txt | 7 +- router/java/src/net/i2p/router/Router.java | 3 +- 7 files changed, 196 insertions(+), 100 deletions(-) create mode 100644 core/java/src/net/i2p/util/FileUtil.java diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java index d22cce7960..a59ad50e4f 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java @@ -4,8 +4,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; -import net.i2p.data.DataHelper; import net.i2p.router.RouterContext; +import net.i2p.util.FileUtil; public class ContentHelper { private String _page; @@ -40,14 +40,14 @@ public class ContentHelper { } } public String getContent() { - String str = DataHelper.readTextFile(_page, _maxLines); + String str = FileUtil.readTextFile(_page, _maxLines); if (str == null) return ""; else return str; } public String getTextContent() { - String str = DataHelper.readTextFile(_page, _maxLines); + String str = FileUtil.readTextFile(_page, _maxLines); if (str == null) return ""; else diff --git a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java index 5b5902debc..93893c2836 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java @@ -4,8 +4,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; -import net.i2p.data.DataHelper; import net.i2p.router.RouterContext; +import net.i2p.util.FileUtil; public class LogsHelper { private RouterContext _context; @@ -42,7 +42,7 @@ public class LogsHelper { } public String getServiceLogs() { - String str = DataHelper.readTextFile("wrapper.log", 500); + String str = FileUtil.readTextFile("wrapper.log", 500); if (str == null) return ""; else diff --git a/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java b/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java index 6ff61582fd..1c89db03e0 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java @@ -1,10 +1,12 @@ package net.i2p.router.web; +import java.io.File; import java.io.IOException; import java.util.List; import net.i2p.router.RouterContext; import net.i2p.apps.systray.SysTray; +import net.i2p.util.FileUtil; import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.WebApplicationContext; @@ -35,6 +37,14 @@ public class RouterConsoleRunner { } public void startConsole() { + File workDir = new File("work"); + boolean workDirRemoved = FileUtil.rmdir(workDir, false); + if (!workDirRemoved) + System.err.println("ERROR: Unable to remove Jetty temporary work directory"); + boolean workDirCreated = workDir.mkdirs(); + if (!workDirCreated) + System.err.println("ERROR: Unable to create Jetty temporary work directory"); + _server = new Server(); WebApplicationContext contexts[] = null; try { diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index b2e5b4ad0e..5627a04f91 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -12,27 +12,23 @@ package net.i2p.data; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.OutputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; -import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.TreeMap; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; import net.i2p.util.OrderedProperties; @@ -640,91 +636,4 @@ public class DataHelper { return rv; } - /** - * Read in the last few lines of a (newline delimited) textfile, or null if - * the file doesn't exist. - * - */ - public static String readTextFile(String filename, int maxNumLines) { - File f = new File(filename); - if (!f.exists()) return null; - FileInputStream fis = null; - try { - fis = new FileInputStream(f); - BufferedReader in = new BufferedReader(new InputStreamReader(fis)); - List lines = new ArrayList(maxNumLines); - String line = null; - while ( (line = in.readLine()) != null) { - lines.add(line); - while (lines.size() > maxNumLines) - lines.remove(0); - } - StringBuffer buf = new StringBuffer(lines.size() * 80); - for (int i = 0; i < lines.size(); i++) - buf.append((String)lines.get(i)).append('\n'); - return buf.toString(); - } catch (IOException ioe) { - return null; - } finally { - if (fis != null) try { fis.close(); } catch (IOException ioe) {} - } - } - - public static boolean extractZip(File zipfile, File targetDir) { - try { - byte buf[] = new byte[16*1024]; - ZipFile zip = new ZipFile(zipfile); - Enumeration entries = zip.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = (ZipEntry)entries.nextElement(); - if (entry.getName().indexOf("..") != -1) { - System.err.println("ERROR: Refusing to extract a zip entry with '..' in it [" + entry.getName() + "]"); - return false; - } - File target = new File(targetDir, entry.getName()); - File parent = target.getParentFile(); - if ( (parent != null) && (!parent.exists()) ) { - boolean parentsOk = parent.mkdirs(); - if (!parentsOk) { - System.err.println("ERROR: Unable to create the parent dir for " + entry.getName() + ": [" + parent.getAbsolutePath() + "]"); - return false; - } - } - if (entry.isDirectory()) { - if (!target.exists()) { - boolean created = target.mkdirs(); - if (!created) { - System.err.println("ERROR: Unable to create the directory [" + entry.getName() + "]"); - return false; - } else { - System.err.println("INFO: Creating directory [" + entry.getName() + "]"); - } - } - } else { - try { - InputStream in = zip.getInputStream(entry); - FileOutputStream fos = new FileOutputStream(target); - int read = 0; - while ( (read = in.read(buf)) != -1) { - fos.write(buf, 0, read); - } - fos.close(); - in.close(); - - System.err.println("INFO: File [" + entry.getName() + "] extracted"); - } catch (IOException ioe) { - System.err.println("ERROR: Error extracting the zip entry (" + entry.getName() + "]"); - ioe.printStackTrace(); - return false; - } - } - } - zip.close(); - return true; - } catch (IOException ioe) { - System.err.println("ERROR: Unable to extract the zip file"); - ioe.printStackTrace(); - return false; - } - } } diff --git a/core/java/src/net/i2p/util/FileUtil.java b/core/java/src/net/i2p/util/FileUtil.java new file mode 100644 index 0000000000..7651d2ba8b --- /dev/null +++ b/core/java/src/net/i2p/util/FileUtil.java @@ -0,0 +1,171 @@ +package net.i2p.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * General helper methods for messing with files + * + */ +public class FileUtil { + /** + * Delete the path as well as any files or directories underneath it. + * + * @param path path to the directory being deleted + * @param failIfNotEmpty if true, do not delete anything if the directory + * is not empty (and return false) + * @return true if the path no longer exists (aka was removed), + * false if it remains + */ + public static final boolean rmdir(String path, boolean failIfNotEmpty) { + return rmdir(new File(path), failIfNotEmpty); + } + /** + * Delete the path as well as any files or directories underneath it. + * + * @param path path to the directory being deleted + * @param failIfNotEmpty if true, do not delete anything if the directory + * is not empty (and return false) + * @return true if the path no longer exists (aka was removed), + * false if it remains + */ + public static final boolean rmdir(File target, boolean failIfNotEmpty) { + if (!target.exists()) { + //System.out.println("info: target does not exist [" + target.getPath() + "]"); + return true; + } + if (!target.isDirectory()) { + //System.out.println("info: target is not a directory [" + target.getPath() + "]"); + return target.delete(); + } else { + File children[] = target.listFiles(); + if (children == null) { + //System.out.println("info: target null children [" + target.getPath() + "]"); + return false; + } + if ( (failIfNotEmpty) && (children.length > 0) ) { + //System.out.println("info: target is not emtpy[" + target.getPath() + "]"); + return false; + } + for (int i = 0; i < children.length; i++) { + if (!rmdir(children[i], failIfNotEmpty)) + return false; + + //System.out.println("info: target removed recursively [" + children[i].getPath() + "]"); + } + return target.delete(); + } + } + + public static boolean extractZip(File zipfile, File targetDir) { + try { + byte buf[] = new byte[16*1024]; + ZipFile zip = new ZipFile(zipfile); + Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = (ZipEntry)entries.nextElement(); + if (entry.getName().indexOf("..") != -1) { + System.err.println("ERROR: Refusing to extract a zip entry with '..' in it [" + entry.getName() + "]"); + return false; + } + File target = new File(targetDir, entry.getName()); + File parent = target.getParentFile(); + if ( (parent != null) && (!parent.exists()) ) { + boolean parentsOk = parent.mkdirs(); + if (!parentsOk) { + System.err.println("ERROR: Unable to create the parent dir for " + entry.getName() + ": [" + parent.getAbsolutePath() + "]"); + return false; + } + } + if (entry.isDirectory()) { + if (!target.exists()) { + boolean created = target.mkdirs(); + if (!created) { + System.err.println("ERROR: Unable to create the directory [" + entry.getName() + "]"); + return false; + } else { + System.err.println("INFO: Creating directory [" + entry.getName() + "]"); + } + } + } else { + try { + InputStream in = zip.getInputStream(entry); + FileOutputStream fos = new FileOutputStream(target); + int read = 0; + while ( (read = in.read(buf)) != -1) { + fos.write(buf, 0, read); + } + fos.close(); + in.close(); + + System.err.println("INFO: File [" + entry.getName() + "] extracted"); + } catch (IOException ioe) { + System.err.println("ERROR: Error extracting the zip entry (" + entry.getName() + "]"); + ioe.printStackTrace(); + return false; + } + } + } + zip.close(); + return true; + } catch (IOException ioe) { + System.err.println("ERROR: Unable to extract the zip file"); + ioe.printStackTrace(); + return false; + } + } + + /** + * Read in the last few lines of a (newline delimited) textfile, or null if + * the file doesn't exist. + * + */ + public static String readTextFile(String filename, int maxNumLines) { + File f = new File(filename); + if (!f.exists()) return null; + FileInputStream fis = null; + try { + fis = new FileInputStream(f); + BufferedReader in = new BufferedReader(new InputStreamReader(fis)); + List lines = new ArrayList(maxNumLines); + String line = null; + while ( (line = in.readLine()) != null) { + lines.add(line); + while (lines.size() > maxNumLines) + lines.remove(0); + } + StringBuffer buf = new StringBuffer(lines.size() * 80); + for (int i = 0; i < lines.size(); i++) + buf.append((String)lines.get(i)).append('\n'); + return buf.toString(); + } catch (IOException ioe) { + return null; + } finally { + if (fis != null) try { fis.close(); } catch (IOException ioe) {} + } + } + + public static void main(String args[]) { + testRmdir(); + } + private static void testRmdir() { + File t = new File("rmdirTest/test/subdir/blah"); + boolean created = t.mkdirs(); + if (!t.exists()) throw new RuntimeException("Unable to create test"); + boolean deleted = FileUtil.rmdir("rmdirTest", false); + if (!deleted) + System.err.println("FAIL: unable to delete rmdirTest"); + else + System.out.println("PASS: rmdirTest deleted"); + } +} diff --git a/history.txt b/history.txt index a7a5e7ace7..57a1cca709 100644 --- a/history.txt +++ b/history.txt @@ -1,4 +1,9 @@ -$Id: history.txt,v 1.20 2004/09/26 10:16:44 jrandom Exp $ +$Id: history.txt,v 1.21 2004/09/27 02:57:43 jrandom Exp $ + +2004-09-29 jrandom + * Always wipe the Jetty work directory on startup, so that web updates + are reflected immediately (Jetty does not honor the cache across + multiple executions) 2004-09-27 jrandom * Limit the number of connection tags saved to 10,000. This is a huge diff --git a/router/java/src/net/i2p/router/Router.java b/router/java/src/net/i2p/router/Router.java index 0ad54e3e42..d835aa1766 100644 --- a/router/java/src/net/i2p/router/Router.java +++ b/router/java/src/net/i2p/router/Router.java @@ -38,6 +38,7 @@ import net.i2p.router.message.TunnelMessageHandler; import net.i2p.router.startup.StartupJob; import net.i2p.stat.Rate; import net.i2p.stat.RateStat; +import net.i2p.util.FileUtil; import net.i2p.util.I2PThread; import net.i2p.util.Log; @@ -760,7 +761,7 @@ public class Router { File updateFile = new File(UPDATE_FILE); if (updateFile.exists()) { System.out.println("INFO: Update file exists [" + UPDATE_FILE + "] - installing"); - boolean ok = DataHelper.extractZip(updateFile, new File(".")); + boolean ok = FileUtil.extractZip(updateFile, new File(".")); if (ok) System.out.println("INFO: Update installed"); else