From 8591dfe71cf9128d5a8e6405008052596f1c58ae Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 10 Feb 2009 02:34:48 +0000
Subject: [PATCH] i2psnark tmp files take 3

---
 .../java/src/org/klomp/snark/I2PSnarkUtil.java   | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
index 26ed5860f..b7e623060 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
@@ -23,6 +23,7 @@ import net.i2p.data.DataFormatException;
 import net.i2p.data.Destination;
 import net.i2p.data.Hash;
 import net.i2p.util.EepGet;
+import net.i2p.util.FileUtil;
 import net.i2p.util.Log;
 import net.i2p.util.SimpleScheduler;
 import net.i2p.util.SimpleTimer;
@@ -49,6 +50,7 @@ public class I2PSnarkUtil {
     private int _maxUploaders;
     private int _maxUpBW;
     private int _maxConnections;
+    private File _tmpDir;
     
     public static final String PROP_USE_OPENTRACKERS = "i2psnark.useOpentrackers";
     public static final boolean DEFAULT_USE_OPENTRACKERS = true;
@@ -68,6 +70,12 @@ public class I2PSnarkUtil {
         _maxUploaders = Snark.MAX_TOTAL_UPLOADERS;
         _maxUpBW = DEFAULT_MAX_UP_BW;
         _maxConnections = MAX_CONNECTIONS;
+        // This is used for both announce replies and .torrent file downloads,
+        // so it must be available even if not connected to I2CP.
+        // so much for multiple instances
+        _tmpDir = new File("tmp", "i2psnark");
+        FileUtil.rmdir(_tmpDir, false);
+        _tmpDir.mkdirs();
     }
     
     /**
@@ -95,6 +103,7 @@ public class I2PSnarkUtil {
             _i2cpHost = i2cpHost;
         if (i2cpPort > 0)
             _i2cpPort = i2cpPort;
+        // can't remove any options this way...
         if (opts != null)
             _opts.putAll(opts);
         _configured = true;
@@ -167,6 +176,10 @@ public class I2PSnarkUtil {
         _manager = null;
         _shitlist.clear();
         mgr.destroySocketManager();
+        // this will delete a .torrent file d/l in progress so don't do that...
+        FileUtil.rmdir(_tmpDir, false);
+        // in case the user will d/l a .torrent file next...
+        _tmpDir.mkdirs();
     }
     
     /** connect to the given destination */
@@ -205,7 +218,8 @@ public class I2PSnarkUtil {
         _log.debug("Fetching [" + url + "] proxy=" + _proxyHost + ":" + _proxyPort + ": " + _shouldProxy);
         File out = null;
         try {
-            out = File.createTempFile("i2psnark", "url", new File("."));
+            // we could use the system tmp dir but deleteOnExit() doesn't seem to work on all platforms...
+            out = File.createTempFile("i2psnark", null, _tmpDir);
         } catch (IOException ioe) {
             ioe.printStackTrace();
             if (out != null)
-- 
GitLab