[tor-commits] [orbot/master] fixed and cleaned up file path issues with binary installs and execution

n8fr8 at torproject.org n8fr8 at torproject.org
Fri Oct 28 15:28:28 UTC 2011


commit 7a1c0f322408f9415acd1a4dce0513247c89b40f
Author: Nathan Freitas <nathan at freitas.net>
Date:   Mon Jul 25 16:12:41 2011 -0400

    fixed and cleaned up file path issues with binary installs and execution
---
 .../android/service/TorBinaryInstaller.java        |  151 +++++---------------
 src/org/torproject/android/service/TorService.java |   54 ++++----
 2 files changed, 61 insertions(+), 144 deletions(-)

diff --git a/src/org/torproject/android/service/TorBinaryInstaller.java b/src/org/torproject/android/service/TorBinaryInstaller.java
index 3c4907d..ba6ab3f 100644
--- a/src/org/torproject/android/service/TorBinaryInstaller.java
+++ b/src/org/torproject/android/service/TorBinaryInstaller.java
@@ -20,169 +20,88 @@ import android.util.Log;
 public class TorBinaryInstaller implements TorServiceConstants {
 
 	
-	String installPath;
-	String apkPath;
+	File installFolder;
 	Context context;
 	
-	public TorBinaryInstaller (Context context, String installPath, String apkPath)
+	public TorBinaryInstaller (Context context, File installFolder)
 	{
-		this.installPath = installPath;
-		this.apkPath = apkPath;
-		this.context = context;
-	}
-	
-	/*
-	 * Start the binary installation if the file doesn't exist or is forced
-	 */
-	public void start (boolean force)
-	{
-		
-		boolean torBinaryExists = new File(installPath + TOR_BINARY_ASSET_KEY).exists();
-		Log.d(TAG,"Tor binary exists=" + torBinaryExists);
-		
-		boolean privoxyBinaryExists = new File(installPath + PRIVOXY_ASSET_KEY).exists();
-		Log.d(TAG,"Privoxy binary exists=" + privoxyBinaryExists);
-	
-		if (!(torBinaryExists && privoxyBinaryExists) || force)
-			installFromRaw ();
-		
-	
+		this.installFolder = installFolder;
 		
+		this.context = context;
 	}
 	
 	//		
 	/*
 	 * Extract the Tor binary from the APK file using ZIP
 	 */
-	private void installFromRaw () 
+	public boolean installFromRaw () 
 	{
+		boolean result = false;
 		
 			
-			InputStream is = context.getResources().openRawResource(R.raw.toraa);			
-			streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, false);
+		try
+		{
+			InputStream is;
+			
+			is = context.getResources().openRawResource(R.raw.toraa);			
+			streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, false);
 		
 			is = context.getResources().openRawResource(R.raw.torab);			
-			streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
+			streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
 		
 			is = context.getResources().openRawResource(R.raw.torac);			
-			streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
+			streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
 		
 			is = context.getResources().openRawResource(R.raw.torad);			
-			streamToFile(is,installPath + TOR_BINARY_ASSET_KEY, true);
+			streamToFile(is,installFolder, TOR_BINARY_ASSET_KEY, true);
 		
 			is = context.getResources().openRawResource(R.raw.torrc);			
-			streamToFile(is,installPath + TORRC_ASSET_KEY, false);
-
+			streamToFile(is,installFolder, TORRC_ASSET_KEY, false);
+	
 			is = context.getResources().openRawResource(R.raw.privoxy);			
-			streamToFile(is,installPath + PRIVOXY_ASSET_KEY, false);
-
+			streamToFile(is,installFolder, PRIVOXY_ASSET_KEY, false);
+	
 			is = context.getResources().openRawResource(R.raw.privoxy_config);			
-			streamToFile(is,installPath + PRIVOXYCONFIG_ASSET_KEY, false);
+			streamToFile(is,installFolder, PRIVOXYCONFIG_ASSET_KEY, false);
 
-			
-			
-			Log.d(TAG,"SUCCESS: installed tor, privoxy binaries from raw");
-	
-		
-	}
-	
-	/*
-	private void installFromZip ()
-	{
-		
-		try
-		{
-			
-			ZipFile zip = new ZipFile(apkPath);
-	
-			ZipEntry zipen = zip.getEntry(ASSETS_BASE + TOR_BINARY_ASSET_KEY);
-			streamToFile(zip.getInputStream(zipen),installPath + TOR_BINARY_ASSET_KEY, false);
-			
-			zipen = zip.getEntry(ASSETS_BASE + TORRC_ASSET_KEY);
-			streamToFile(zip.getInputStream(zipen),installPath + TORRC_ASSET_KEY);
-			
-			zipen = zip.getEntry(ASSETS_BASE + PRIVOXY_ASSET_KEY);
-			streamToFile(zip.getInputStream(zipen),installPath + PRIVOXY_ASSET_KEY);
-			
-			zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY);
-			streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY);
-			
-			zipen = zip.getEntry(ASSETS_BASE + PRIVOXYCONFIG_ASSET_KEY);
-			streamToFile(zip.getInputStream(zipen),installPath + PRIVOXYCONFIG_ASSET_KEY);
-			
-			
-			zip.close();
-			
-			Log.d(TAG,"SUCCESS: unzipped tor, privoxy, iptables binaries from apk");
-	
 		}
 		catch (IOException ioe)
 		{
-			Log.d(TAG,"FAIL: unable to unzip binaries from apk",ioe);
-		
+			Log.e(TAG, "unable to install tor binaries from raw", ioe);
+			return false;
 		}
+			
+	
+		return true;
 	}
-	*/
 	
+
 	/*
 	 * Write the inputstream contents to the file
 	 */
-    private static void streamToFile(InputStream stm, String targetFilename, boolean append)
+    private static boolean streamToFile(InputStream stm, File folder, String targetFilename, boolean append) throws IOException
 
     {
-
-        FileOutputStream stmOut = null;
-
         byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE];
 
         int bytecount;
 
-       
-        File outFile = new File(targetFilename);
-        
-        try {
-           if (!append)
-        	   outFile.createNewFile();
-
-        	stmOut = new FileOutputStream(outFile);
-        }
+        File outFile = new File(folder, targetFilename);
 
-        catch (java.io.IOException e)
+    	FileOutputStream stmOut = new FileOutputStream(outFile, append);
+    	
+        while ((bytecount = stm.read(buffer)) > 0)
 
         {
 
-        	Log.d(TAG,"Error opening output file " + targetFilename,e);
+            stmOut.write(buffer, 0, bytecount);
 
-        	return;
         }
 
-       
-
-        try
+        stmOut.close();
 
-        {
-
-            while ((bytecount = stm.read(buffer)) > 0)
-
-            {
-
-                stmOut.write(buffer, 0, bytecount);
-
-            }
-
-            stmOut.close();
-
-        }
-
-        catch (java.io.IOException e)
-
-        {
-
-            Log.d(TAG,"Error writing output file '" + targetFilename + "': " + e.toString());
-
-            return;
-
-        }
+        
+        return true;
 
     }
 	
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 57c43b0..0f9cee3 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -60,16 +60,13 @@ public class TorService extends Service implements TorServiceConstants, Runnable
     private ArrayList<String> resetBuffer = null;
      
    
-    private String appHome;
-    private String appBinHome;
-    private String appDataHome;
+   //   private String appHome;
+    private File appBinHome;
+    private File appDataHome;
     
     private String torBinaryPath;
     private String privoxyPath;
     
-	
-    private boolean hasRoot = false;
-    
     /** Called when the activity is first created. */
     public void onCreate() {
     	super.onCreate();
@@ -419,36 +416,34 @@ public class TorService extends Service implements TorServiceConstants, Runnable
     	//check and install iptables
     	IptablesManager.assertBinaries(this, true);
     	
-    	File fileInstall = getDir("bin/",0);
     	
-		appHome = fileInstall.getAbsolutePath();
-		appDataHome = getCacheDir().getAbsolutePath() + '/';
-		logNotice( "appHome=" + appHome);
-		
-		torBinaryPath = appBinHome + TOR_BINARY_ASSET_KEY;
-    	privoxyPath = appBinHome + PRIVOXY_ASSET_KEY;
+    	appBinHome = getDir("bin",0);
+    	appDataHome = getCacheDir();
     	
+	//	logNotice( "appHome=" + appHome);
+		
+		File fileTor = new File(appBinHome, TOR_BINARY_ASSET_KEY);
+		File filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY);
+
+		
 		logNotice( "checking Tor binaries");
-	    
-		boolean torBinaryExists = new File(torBinaryPath).exists();
-		boolean privoxyBinaryExists = new File(privoxyPath).exists();
 		
-		if (!(torBinaryExists && privoxyBinaryExists))
+		if (!(fileTor.exists() && filePrivoxy.exists()))
 		{
 			killTorProcess ();
 			
-			TorBinaryInstaller installer = new TorBinaryInstaller(this, appBinHome, appBinHome); 
-			installer.start(true);
-			
-			torBinaryExists = new File(torBinaryPath).exists();
-			privoxyBinaryExists = new File(privoxyPath).exists();
+			TorBinaryInstaller installer = new TorBinaryInstaller(this, appBinHome); 
+			boolean success = installer.installFromRaw();
 			
-    		if (torBinaryExists && privoxyBinaryExists)
+    		if (success)
     		{
     			logNotice(getString(R.string.status_install_success));
     	
     			showToolbarNotification(getString(R.string.status_install_success), NOTIFY_ID, R.drawable.tornotification);
     		
+
+    			torBinaryPath = fileTor.getAbsolutePath();
+    			privoxyPath = filePrivoxy.getAbsolutePath();
     		}
     		else
     		{
@@ -466,6 +461,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
 			logNotice("Found Tor binary: " + torBinaryPath);
 			logNotice("Found Privoxy binary: " + privoxyPath);
 
+
+			torBinaryPath = fileTor.getAbsolutePath();
+			privoxyPath = filePrivoxy.getAbsolutePath();
 		}
 	
 		StringBuilder log = new StringBuilder ();
@@ -627,9 +625,9 @@ public class TorService extends Service implements TorServiceConstants, Runnable
     	
     	StringBuilder log = new StringBuilder();
 		
-		String torrcPath = appBinHome + TORRC_ASSET_KEY;
+		String torrcPath = new File(appBinHome, TORRC_ASSET_KEY).getAbsolutePath();
 		
-		String[] torCmd = {torBinaryPath + " DataDirectory " + appDataHome + " -f " + torrcPath  + " || exit\n"};
+		String[] torCmd = {torBinaryPath + " DataDirectory " + appDataHome.getAbsolutePath() + " -f " + torrcPath  + " || exit\n"};
 		
 		boolean runAsRootFalse = false;
 		boolean waitForProcess = false;
@@ -697,7 +695,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
     		{
     			log = new StringBuilder();
     			
-    			String privoxyConfigPath = appBinHome + PRIVOXYCONFIG_ASSET_KEY;
+    			String privoxyConfigPath = new File(appBinHome, PRIVOXYCONFIG_ASSET_KEY).getAbsolutePath();
     			
     			String[] cmds = 
     			{ privoxyPath + " " + privoxyConfigPath + " &" };
@@ -762,7 +760,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
 
 					logNotice( "SUCCESS connected to control port");
 			        
-			        String torAuthCookie = appDataHome + TOR_CONTROL_COOKIE;
+			        String torAuthCookie = new File(appDataHome, TOR_CONTROL_COOKIE).getAbsolutePath();
 			        
 			        File fileCookie = new File(torAuthCookie);
 			        
@@ -1406,7 +1404,7 @@ public class TorService extends Service implements TorServiceConstants, Runnable
 
         if (enableHiddenServices)
         {
-        	mBinder.updateConfiguration("HiddenServiceDir",appDataHome, false);
+        	mBinder.updateConfiguration("HiddenServiceDir",appDataHome.getAbsolutePath(), false);
         	
         	String hsPorts = prefs.getString("pref_hs_ports","");
         	





More information about the tor-commits mailing list