Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Run the app entirely out of /data/data, only the data touches sdcard

  • Loading branch information...
commit ec8be8c886a8739b53dd73190567dfd4cba9e2b2 1 parent de6b5ee
Dale Harvey authored
View
BIN  assets/release-0.1.tgz.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
154 src/com/couchbase/libcouch/CouchInitializer.java
@@ -1,154 +0,0 @@
-package com.couchbase.libcouch;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import android.os.Handler;
-import android.os.Message;
-import android.util.Log;
-
-public class CouchInitializer
-{
- private final static String t = "CouchInitializer";
-
- private static final String COUCHDIR = "couchdb";
- private static final String ERLANGDIR = "erlang";
-
- private static final String DIRECTORY = "d";
- private static final String LINK = "l";
- private static final String FILE = "f";
-
- // The private data directory to initialize into
- private static String destinationPath() {
- return CouchInstaller.dataPath();
- }
-
- public static boolean isEnvironmentInitialized()
- {
- return new File(destinationPath(), COUCHDIR).isDirectory();
- }
-
- public static void initializeEnvironment(Handler handler)
- {
- List<String> index = new ArrayList<String>();
-
- // This method must have the index of installed files as prepared by CouchInstaller
- if (new File(CouchInstaller.indexFile()).exists() == false) {
- Log.e(t, "unable to initialize data directory: index of installed files is missing");
- return;
- }
-
- // Remove pre-existing files (from upgrades, broken installs, etc)
- if (new File(destinationPath(), COUCHDIR).isDirectory()) {
- Log.v(t, "purging " + COUCHDIR + " data directory...");
- CouchInstaller.deleteDirectory(new File(destinationPath(), COUCHDIR));
- }
-
- // Remove pre-existing files (from upgrades, broken installs, etc)
- if (new File(destinationPath(), ERLANGDIR).isDirectory()) {
- Log.v(t, "purging " + ERLANGDIR + " data directory...");
- CouchInstaller.deleteDirectory(new File(destinationPath(), ERLANGDIR));
- }
-
- Log.i(t, "initializing data directory for CouchDB and Erlang/OTP");
- Log.d(t, "destination path is " + destinationPath());
-
- /*
- * Go through each of the files listed in the index of installed files and take appropriate
- * action depending on the type of file listed. The purpose of this loop is to copy or link
- * files into the /data/data hierarchy where executable files can be used (and where the
- * compiled version of Couch/Erlang expects to find them).
- */
- try {
- index = org.apache.commons.io.FileUtils.readLines(new File(CouchInstaller.indexFile()));
- Iterator<String> entries = index.iterator();
-
- float entriesProcessed = 0;
-
- while (entries.hasNext()) {
- String entry = entries.next();
-
- initializeEntry(entry);
-
- if (handler != null) {
- Message progress = new Message();
- progress.arg1 = (int) ++entriesProcessed;
- progress.arg2 = index.size();
- progress.what = CouchService.PROGRESS;
- handler.sendMessage(progress);
- }
- }
-
- // Close progress bar
- Message progress = new Message();
- progress.arg1 = 1; // Our way of telling the handler not to restart the activity
- progress.arg2 = 0;
- progress.what = CouchService.COMPLETE;
- handler.sendMessage(progress);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.e(t, "unable to read index file: " + e.toString());
- e.printStackTrace();
- return;
- }
- }
-
- /*
- * Figure out what to do with a file listed in the index of installed files
- *
- * The format of an entry in the index is
- * FILE_TYPE FILE_MODE FILE_PATH LINK_TO
- * 0 1 2 3
- */
- private static void initializeEntry(String entry)
- {
- Log.v(t, "initializing " + entry);
-
- String [] info = entry.split("\\s");
- String neutralPath = info[2].replace(CouchInstaller.externalPath(), "");
-
- if (info[0].equals(DIRECTORY)) {
- new File(destinationPath(), neutralPath).mkdirs();
-
- try {
- Runtime.getRuntime().exec("chmod 755 " + destinationPath() + neutralPath);
- } catch (IOException e) {
- Log.e(t, "failed to chmod 755 " + e.toString());
- e.printStackTrace();
- }
- }
-
- if (info[0].equals(LINK)) {
- try {
- Runtime.getRuntime().exec("/system/bin/ln -s " + info[3] + " " + destinationPath() + neutralPath);
- } catch (IOException e) {
- Log.e(t, "failed to link " + e.toString());
- e.printStackTrace();
- }
- }
-
- if (info[0].equals(FILE)) {
- if (info[1].equals("420")) {
- try {
- Runtime.getRuntime().exec("/system/bin/ln -s " + CouchInstaller.externalPath() + neutralPath + " " + destinationPath() + neutralPath);
- } catch (IOException e) {
- Log.e(t, "failed to link " + e.toString());
- e.printStackTrace();
- }
- } else if (info[1].equals("493")) {
- try {
- org.apache.commons.io.FileUtils.copyFile(new File(CouchInstaller.externalPath(), neutralPath), new File(destinationPath(), neutralPath));
- Runtime.getRuntime().exec("/system/bin/chmod 755 " + destinationPath() + neutralPath);
- } catch (IOException e) {
- Log.e(t, "unable to duplicate " + e.toString());
- e.printStackTrace();
- }
- } else {
- Log.e(t, "unhandled file mode " + info[1]);
- }
- }
- }
-}
View
53 src/com/couchbase/libcouch/CouchInstaller.java
@@ -39,14 +39,8 @@ public static String dataPath() {
public static String externalPath() {
return Environment.getExternalStorageDirectory() + "/Android/data/" + appNamespace;
}
- public static String erlangPath() {
- return externalPath() + "/erlang";
- }
- public static String couchPath() {
- return externalPath() + "/couchdb";
- }
public static String indexFile() {
- return externalPath() + "/installedfiles.index";
+ return dataPath() + "/installedfiles.index";
}
public static void doInstall(String url, String pkg, Handler handler, CouchService service)
@@ -57,12 +51,12 @@ public static void doInstall(String url, String pkg, Handler handler, CouchServi
// android app behaviour. However there doesnt look to be a way to protect
// ourselves from wiping the entire sdcard with a typo, so just be
// careful
- File couchDir = new File(couchPath());
+ File couchDir = new File(dataPath() + "/couchdb");
if (couchDir.exists()) {
deleteDirectory(couchDir);
}
- if(!(new File(externalPath() + "/" + pkg + ".installedfiles")).exists()) {
+ if(!(new File(dataPath() + "/" + pkg + ".installedfiles")).exists()) {
installPackage(url, pkg, handler, service);
}
@@ -114,8 +108,8 @@ private static void installPackage(String baseUrl, String pkg, Handler handler,
}
}
- // Ensure /sdcard/Android/data/com.my.app exists
- File externalPath = new File(externalPath() + "/");
+ // Ensure /sdcard/Android/data/com.my.app/db exists
+ File externalPath = new File(externalPath() + "/db/");
if (!externalPath.exists()) {
externalPath.mkdirs();
}
@@ -130,7 +124,7 @@ private static void installPackage(String baseUrl, String pkg, Handler handler,
while ((e = tarstream.getNextTarEntry()) != null) {
- String fullName = externalPath() + "/" + e.getName();
+ String fullName = dataPath() + "/" + e.getName();
// Obtain count of files in this archive so that we can indicate install progress
if (filesInArchive == 0 && e.getName().startsWith("filecount")) {
String[] count = e.getName().split("\\.");
@@ -182,23 +176,18 @@ private static void installPackage(String baseUrl, String pkg, Handler handler,
Message progress = new Message();
progress.arg1 = (int) ++filesUnpacked;
progress.arg2 = (int) filesInArchive;
- progress.what = CouchService.DOWNLOAD;
+ progress.what = CouchService.PROGRESS;
handler.sendMessage(progress);
}
tarstream.close();
instream.close();
- FileWriter iLOWriter = new FileWriter(externalPath() + "/" + pkg + ".installedfiles");
+ FileWriter iLOWriter = new FileWriter(dataPath() + "/" + pkg + ".installedfiles");
for (String file : installedfiles) {
iLOWriter.write(file+"\n");
}
iLOWriter.close();
- for (String file : installedfiles) {
- if(file.endsWith(".postinst.sh")) {
- Runtime.getRuntime().exec("sh " + file);
- }
- }
// Write out full list of all installed files + file modes
iLOWriter = new FileWriter(indexFile());
@@ -218,18 +207,16 @@ private static void installPackage(String baseUrl, String pkg, Handler handler,
{"%sdk_int%", Integer.toString(android.os.Build.VERSION.SDK_INT)}
};
- replace(CouchInstaller.erlangPath() + "/erts-5.7.5/bin/start", replacements);
- replace(CouchInstaller.erlangPath() + "/erts-5.7.5/bin/erl", replacements);
- replace(CouchInstaller.erlangPath() + "/bin/start", replacements);
- replace(CouchInstaller.erlangPath() + "/bin/erl", replacements);
- replace(CouchInstaller.couchPath() + "/etc/init.d/couchdb", replacements);
- replace(CouchInstaller.couchPath() + "/etc/logrotate.d/couchdb", replacements);
- replace(CouchInstaller.couchPath() + "/lib/couchdb/erlang/lib/couch-1.0.2/ebin/couch.app", replacements);
- replace(CouchInstaller.couchPath() + "/lib/couchdb/erlang/lib/couch-1.0.2/priv/lib/couch_icu_driver.la", replacements);
- replace(CouchInstaller.couchPath() + "/bin/couchdb", replacements);
- replace(CouchInstaller.couchPath() + "/bin/couchjs", replacements);
- replace(CouchInstaller.couchPath() + "/bin/couchjs_wrapper", replacements);
- replace(CouchInstaller.couchPath() + "/etc/couchdb/local.ini", replacements);
+ replace(CouchInstaller.dataPath() + "/erlang/erts-5.7.5/bin/start", replacements);
+ replace(CouchInstaller.dataPath() + "/erlang/erts-5.7.5/bin/erl", replacements);
+ replace(CouchInstaller.dataPath() + "/erlang/bin/start", replacements);
+ replace(CouchInstaller.dataPath() + "/erlang/bin/erl", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/lib/couchdb/erlang/lib/couch-1.0.2/ebin/couch.app", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/lib/couchdb/erlang/lib/couch-1.0.2/priv/lib/couch_icu_driver.la", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/bin/couchdb", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/bin/couchjs", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/bin/couchjs_wrapper", replacements);
+ replace(CouchInstaller.dataPath() + "/couchdb/etc/couchdb/local.ini", replacements);
}
/*
@@ -238,12 +225,12 @@ private static void installPackage(String baseUrl, String pkg, Handler handler,
*/
public static boolean checkInstalled(String pkg) {
- File file = new File(externalPath() + "/" + pkg + ".installedfiles");
+ File file = new File(dataPath() + "/" + pkg + ".installedfiles");
if (!file.exists()) {
return false;
}
- return new File(couchPath()).exists();
+ return true;//new File(couchPath()).exists();
}
/*
View
29 src/com/couchbase/libcouch/CouchService.java
@@ -24,7 +24,6 @@
public final static int INSTALLING = 3;
public final static int INITIALIZING = 4;
- private int status;
private ICouchClient client;
private final Handler mHandler = new Handler() {
@@ -41,18 +40,11 @@ public void handleMessage(Message msg) {
client.exit(stacktrace);
}
break;
- case DOWNLOAD:
- client.downloading(msg.arg1, msg.arg2);
- break;
case PROGRESS:
client.installing(msg.arg1, msg.arg2);
break;
case COMPLETE:
- if (status == INSTALLING) {
- initCouch();
- } else {
- startCouch();
- }
+ startCouch();
break;
case COUCH_STARTED:
URL url = (URL) msg.obj;
@@ -102,8 +94,6 @@ public void initCouchDB(ICouchClient cb, final String url, final String pkg) thr
client = cb;
if (!CouchInstaller.checkInstalled(pkg)) {
installCouch(url, pkg);
- } else if (!CouchInitializer.isEnvironmentInitialized()) {
- initCouch();
} else {
if (couch.started == true) {
couchStarted();
@@ -128,11 +118,10 @@ void couchStarted() throws RemoteException {
}
void startCouch() {
- couch.start("/system/bin/sh", CouchInstaller.couchPath() + "/bin/couchdb", "", mHandler);
+ couch.start("/system/bin/sh", CouchInstaller.dataPath() + "/couchdb/bin/couchdb", "", mHandler);
}
void installCouch(final String url, final String pkg) {
- status = INSTALLING;
final CouchService service = this;
new Thread() {
@Override
@@ -145,18 +134,4 @@ public void run() {
}
}.start();
}
-
- void initCouch() {
- status = INITIALIZING;
- new Thread() {
- @Override
- public void run() {
- try {
- CouchInitializer.initializeEnvironment(mHandler);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
}
View
1  src/com/couchbase/libcouch/ICouchClient.aidl
@@ -6,7 +6,6 @@ interface ICouchClient
void couchStarted(String host, int port);
/* Callback for notifications on how the CouchDB install is progressing */
- void downloading(int completed, int total);
void installing(int completed, int total);
void exit(String error);
Please sign in to comment.
Something went wrong with that request. Please try again.