From 299214aa1dc7114b2cf12273aa29850e68e13410 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Fri, 26 Feb 2010 16:44:49 +0000 Subject: [PATCH] * i2psnark: - Fix NPE after create file failure - Sanitize more characters in file names --- .../src/org/klomp/snark/SnarkManager.java | 13 ++++++++-- .../java/src/org/klomp/snark/Storage.java | 25 ++++++++++++++++--- .../src/org/klomp/snark/bencode/BEValue.java | 2 +- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java index 905c27cee6..c909a112f0 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java +++ b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java @@ -425,9 +425,18 @@ public class SnarkManager implements Snark.CompleteListener { FileInputStream fis = null; try { fis = new FileInputStream(sfile); + } catch (IOException ioe) { + // catch this here so we don't try do delete it below + addMessage(_("Cannot open \"{0}\"", sfile.getName()) + ": " + ioe.getMessage()); + return; + } + + try { MetaInfo info = new MetaInfo(fis); - fis.close(); - fis = null; + try { + fis.close(); + fis = null; + } catch (IOException e) {} if (!TrackerClient.isValidAnnounce(info.getAnnounce())) { if (_util.shouldUseOpenTrackers() && _util.getOpenTrackers() != null) { diff --git a/apps/i2psnark/java/src/org/klomp/snark/Storage.java b/apps/i2psnark/java/src/org/klomp/snark/Storage.java index cf3fcdc4b0..63787fe053 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/Storage.java +++ b/apps/i2psnark/java/src/org/klomp/snark/Storage.java @@ -420,13 +420,29 @@ public class Storage } } + private static final char[] ILLEGAL = new char[] { + '<', '>', ':', '"', '/', '\\', '|', '?', '*', + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; + /** - * Removes 'suspicious' characters from the give file name. + * Removes 'suspicious' characters from the given file name. + * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx */ private static String filterName(String name) { - // XXX - Is this enough? - return name.replace(File.separatorChar, '_'); + if (name.equals(".") || name.equals(" ")) + return "_"; + String rv = name; + if (rv.startsWith(".")) + rv = '_' + rv.substring(1); + if (rv.endsWith(".") || rv.endsWith(" ")) + rv = rv.substring(0, rv.length() - 1) + '_'; + for (int i = 0; i < ILLEGAL.length; i++) { + if (rv.indexOf(ILLEGAL[i]) >= 0) + rv = rv.replace(ILLEGAL[i], '_'); + } + return rv; } private File createFileFromNames(File base, List names) throws IOException @@ -577,6 +593,9 @@ public class Storage if (rafs == null) return; for (int i = 0; i < rafs.length; i++) { + // if we had an IOE in check(), the RAFlock may be null + if (RAFlock[i] == null) + continue; try { synchronized(RAFlock[i]) { closeRAF(i); diff --git a/apps/i2psnark/java/src/org/klomp/snark/bencode/BEValue.java b/apps/i2psnark/java/src/org/klomp/snark/bencode/BEValue.java index c32fa9e100..cba6b973fd 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/bencode/BEValue.java +++ b/apps/i2psnark/java/src/org/klomp/snark/bencode/BEValue.java @@ -179,7 +179,7 @@ public class BEValue if (value instanceof byte[]) { byte[] bs = (byte[])value; - // XXX - Stupid heuristic... + // XXX - Stupid heuristic... and not UTF-8 if (bs.length <= 12) valueString = new String(bs); else -- GitLab