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
This commit is contained in:
jrandom
2004-09-29 19:34:02 +00:00
committed by zzz
parent 774231f347
commit 010b285e67
7 changed files with 196 additions and 100 deletions

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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;
}
}
}

View File

@ -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");
}
}

View File

@ -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

View File

@ -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