[tor-commits] [orbot/master] improved handling of binary upgrade process

n8fr8 at torproject.org n8fr8 at torproject.org
Fri Jan 13 16:02:28 UTC 2012


commit 2962f584475d84e75edee12f12f59904e1bfa483
Author: Nathan Freitas <nathan at freitas.net>
Date:   Fri Jan 13 10:26:56 2012 -0500

    improved handling of binary upgrade process
---
 .../android/service/TorBinaryInstaller.java        |   38 ++++++++-----
 src/org/torproject/android/service/TorService.java |   57 ++++++++++++++++----
 .../android/service/TorServiceConstants.java       |    8 +++
 3 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/src/org/torproject/android/service/TorBinaryInstaller.java b/src/org/torproject/android/service/TorBinaryInstaller.java
index 07b5049..93325db 100644
--- a/src/org/torproject/android/service/TorBinaryInstaller.java
+++ b/src/org/torproject/android/service/TorBinaryInstaller.java
@@ -7,6 +7,7 @@ import java.io.BufferedReader;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
@@ -45,28 +46,36 @@ public class TorBinaryInstaller implements TorServiceConstants {
 	/*
 	 * Extract the Tor binary from the APK file using ZIP
 	 */
-	public boolean installFromRaw () throws IOException
+	public boolean installFromRaw () throws IOException, FileNotFoundException
 	{
 		
 		InputStream is;
-		
+        File outFile;
+        
+        
 		is = context.getResources().openRawResource(R.raw.tor);			
-		streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, false, true);
+		outFile = new File(installFolder, TOR_BINARY_ASSET_KEY);
+		streamToFile(is, outFile, false, true);
 			
-		is = context.getResources().openRawResource(R.raw.torrc);			
-		streamToFile(is,installFolder, TORRC_ASSET_KEY, false, false);
+		is = context.getResources().openRawResource(R.raw.torrc);
+		outFile = new File(installFolder, TORRC_ASSET_KEY);
+		streamToFile(is,outFile, false, false);
 
-		is = context.getResources().openRawResource(R.raw.torrctether);			
-		streamToFile(is,installFolder, TORRC_TETHER_KEY, false, false);
+		is = context.getResources().openRawResource(R.raw.torrctether);		
+		outFile = new File(installFolder, TORRC_TETHER_KEY);
+		streamToFile(is, outFile, false, false);
 
-		is = context.getResources().openRawResource(R.raw.privoxy);			
-		streamToFile(is,installFolder, PRIVOXY_ASSET_KEY, false, false);
+		is = context.getResources().openRawResource(R.raw.privoxy);
+		outFile = new File(installFolder, PRIVOXY_ASSET_KEY);
+		streamToFile(is,outFile, false, false);
 
-		is = context.getResources().openRawResource(R.raw.privoxy_config);			
-		streamToFile(is,installFolder, PRIVOXYCONFIG_ASSET_KEY, false, false);
+		is = context.getResources().openRawResource(R.raw.privoxy_config);
+		outFile = new File(installFolder, PRIVOXYCONFIG_ASSET_KEY);
+		streamToFile(is,outFile, false, false);
 		
-		is = context.getResources().openRawResource(R.raw.geoip);			
-		streamToFile(is,installFolder, GEOIP_ASSET_KEY, false, true);
+		is = context.getResources().openRawResource(R.raw.geoip);
+		outFile = new File(installFolder, GEOIP_ASSET_KEY);
+		streamToFile(is, outFile, false, true);
 	
 		return true;
 	}
@@ -88,14 +97,13 @@ public class TorBinaryInstaller implements TorServiceConstants {
 	/*
 	 * Write the inputstream contents to the file
 	 */
-    private static boolean streamToFile(InputStream stm, File folder, String targetFilename, boolean append, boolean zip) throws IOException
+    private static boolean streamToFile(InputStream stm, File outFile, boolean append, boolean zip) throws IOException
 
     {
         byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE];
 
         int bytecount;
 
-        File outFile = new File(folder, targetFilename);
 
     	OutputStream stmOut = new FileOutputStream(outFile, append);
     	
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 94929f0..50ddb01 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -207,11 +207,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     	Log.i(TAG, "service started: " + intent.getAction());
 
 		try {
-			checkTorBinaries ();
+			checkTorBinaries (false);
 		} catch (Exception e) {
 
 			logNotice("unable to find tor binaries: " + e.getMessage());
-	    	showToolbarNotification(e.getMessage(), NOTIFY_ID, R.drawable.tornotificationerr);
+	    	showToolbarNotification(getString(R.string.error_installing_binares), NOTIFY_ID, R.drawable.tornotificationerr);
 
 			Log.e(TAG, "error checking tor binaries", e);
 		}
@@ -412,24 +412,46 @@ public class TorService extends Service implements TorServiceConstants, TorConst
     }
     
 
-    
-    private boolean checkTorBinaries () throws Exception
+    private boolean checkTorBinaries (boolean forceInstall) throws Exception
     {
+    	
     	//check and install iptables
     	TorBinaryInstaller.assertIpTablesBinaries(this, true);
-    	
+
     	appBinHome = getDir("bin",0);
     	appDataHome = getCacheDir();
     	
-	//	logNotice( "appHome=" + appHome);
-		
-		File fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY);
+    	File fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY);
 		File filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY);
 
+		File fileTorOld = null;
+		File filePrivoxyOld = null;
 		
+    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+    	String currTorBinary = prefs.getString(TorServiceConstants.PREF_BINARY_TOR_VERSION_INSTALLED, null);
+    	String currPrivoxyBinary = prefs.getString(TorServiceConstants.PREF_BINARY_PRIVOXY_VERSION_INSTALLED, null);
+    	
+    	if (currTorBinary == null || (!currTorBinary.equals(TorServiceConstants.BINARY_TOR_VERSION)))
+    		if (fileTor.exists())
+    		{
+    			killTorProcess ();
+    			fileTorOld = new File(fileTor.getAbsolutePath() + ".old");
+    			fileTor.renameTo(fileTorOld);
+    		}
+    
+    	if (currPrivoxyBinary == null || (!currPrivoxyBinary.equals(TorServiceConstants.BINARY_PRIVOXY_VERSION)))
+    		if (filePrivoxy.exists())
+    		{
+    			killTorProcess ();
+    			filePrivoxyOld = new File(filePrivoxy.getAbsolutePath() + ".old");
+    			filePrivoxy.renameTo(filePrivoxyOld);
+    		}
+    	
+    	
+
 		logNotice( "checking Tor binaries");
 		
-		if (!(fileTor.exists() && filePrivoxy.exists()))
+		if ((!(fileTor.exists() && filePrivoxy.exists())) || forceInstall)
 		{
 			killTorProcess ();
 			
@@ -438,6 +460,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			
     		if (success)
     		{
+    			
+    			Editor edit = prefs.edit();
+    			edit.putString(TorServiceConstants.PREF_BINARY_TOR_VERSION_INSTALLED, TorServiceConstants.BINARY_TOR_VERSION);
+    			edit.putString(TorServiceConstants.PREF_BINARY_PRIVOXY_VERSION_INSTALLED, TorServiceConstants.BINARY_PRIVOXY_VERSION);
+    			edit.commit();
+    			
+    			if (fileTorOld != null)
+    				fileTorOld.delete();
+    			
+    			if (filePrivoxyOld != null)
+    				filePrivoxyOld.delete();
+    			
     			logNotice(getString(R.string.status_install_success));
     	
     			showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification);
@@ -1028,10 +1062,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
         
     	_torInstance = this;
     	
+    	/*
 		try
 		{
 	
-			checkTorBinaries();
+			checkTorBinaries(false);
     	
 		}
 		catch (Exception e)
@@ -1041,7 +1076,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 
 			Log.d(TAG,"Unable to check for Tor binaries",e);
 			return null;
-		}
+		}*/
     	
 		findExistingProc ();
 		
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index b06565e..bd60c1e 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -85,4 +85,12 @@ public interface TorServiceConstants {
     public static final int ENABLE_TOR_MSG = 2;
     public static final int DISABLE_TOR_MSG = 3;
     public static final int LOG_MSG = 4;
+    
+    public static final String BINARY_TOR_VERSION = "0.2.3.10-alpha";
+    public static final String BINARY_PRIVOXY_VERSION = "1.4.13";
+    public static final String PREF_BINARY_TOR_VERSION_INSTALLED = "BINARY_TOR_VERSION_INTALLED";
+    public static final String PREF_BINARY_PRIVOXY_VERSION_INSTALLED = "BINARY_PRIVOXY_VERSION_INTALLED";
+    
+    
+    
 }





More information about the tor-commits mailing list