Remove busybox dependency

This commit is contained in:
topjohnwu
2016-11-29 13:24:48 +08:00
parent bb878a1ccf
commit 24ef80351c
18 changed files with 110 additions and 108 deletions

View File

@ -23,6 +23,7 @@ import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File;
import java.util.List;
@ -77,6 +78,19 @@ public class MagiskFragment extends Fragment {
@Override
public void task(Uri uri) {
new Async.FlashZIP(mContext, uri, mFilename) {
@Override
protected boolean unzipAndCheck() {
publishProgress(mContext.getString(R.string.zip_install_unzip_zip_msg));
// We might not have busybox yet, unzip with Java
// We will have complete busybox after Magisk installation
ZipUtils.unzip(mCachedFile, new File(mCachedFile.getParent(), "magisk"));
Shell.su(
"mkdir -p " + Async.TMP_FOLDER_PATH + "/magisk",
"cp -af " + mCachedFile.getParent() + "/magisk/. " + Async.TMP_FOLDER_PATH + "/magisk"
);
return super.unzipAndCheck();
}
@Override
protected void done() {
Shell.su("setprop magisk.version " + String.valueOf(remoteMagiskVersion));
@ -110,14 +124,16 @@ public class MagiskFragment extends Fragment {
appLink,
"MagiskManager-v" + remoteAppVersion + ".apk");
static {
updateMagiskVersion();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.magisk_fragment, container, false);
ButterKnife.bind(this, v);
updateMagiskVersion();
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
@ -167,7 +183,7 @@ public class MagiskFragment extends Fragment {
prefs.unregisterOnSharedPreferenceChangeListener(listener);
}
private void updateMagiskVersion() {
private static void updateMagiskVersion() {
List<String> ret = Shell.sh("getprop magisk.version");
if (ret.get(0).length() == 0) {
magiskVersion = -1;
@ -178,6 +194,16 @@ public class MagiskFragment extends Fragment {
// Custom version don't need to receive updates
magiskVersion = Double.POSITIVE_INFINITY;
}
}
private void updateUI() {
String theme = prefs.getString("theme", "");
if (theme.equals("Dark")) {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialog_dh);
} else {
builder = new AlertDialog.Builder(getActivity());
}
if (magiskVersion == -1) {
magiskStatusContainer.setBackgroundColor(colorNeutral);
@ -192,16 +218,6 @@ public class MagiskFragment extends Fragment {
magiskVersionText.setText(getString(R.string.magisk_version, magiskVersionString));
magiskVersionText.setTextColor(colorOK);
}
}
private void updateUI() {
String theme = prefs.getString("theme", "");
if (theme.equals("Dark")) {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialog_dh);
} else {
builder = new AlertDialog.Builder(getActivity());
}
if (remoteMagiskVersion == -1) {
appCheckUpdatesContainer.setBackgroundColor(colorWarn);

View File

@ -91,8 +91,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
navigationView.setNavigationItemSelectedListener(this);
checkHideSection();
}
@Override
@ -104,13 +102,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_ITEM_ID, mSelectedId);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
@ -123,18 +119,21 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
mSelectedId = menuItem.getItemId();
mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId()), 250);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void checkHideSection() {
Menu menu = navigationView.getMenu();
if (PreferenceManager.getDefaultSharedPreferences(getApplication()).getBoolean("magiskhide", false)) {
menu.findItem(R.id.magiskhide).setVisible(true);
} else {
if (MagiskFragment.magiskVersion == -1) {
menu.findItem(R.id.magiskhide).setVisible(false);
menu.findItem(R.id.modules).setVisible(false);
menu.findItem(R.id.downloads).setVisible(false);
} else {
menu.findItem(R.id.modules).setVisible(true);
menu.findItem(R.id.downloads).setVisible(true);
menu.findItem(R.id.magiskhide).setVisible(
PreferenceManager.getDefaultSharedPreferences(this).getBoolean("magiskhide", false));
}
}

View File

@ -37,7 +37,7 @@ public class SplashActivity extends AppCompatActivity {
.apply();
new Async.CheckUpdates(prefs).exec();
new Async.ConstructEnv(getApplicationInfo()).exec();
// new Async.ConstructEnv(getApplicationInfo()).exec();
new Async.LoadModules(prefs) {
@Override

View File

@ -21,15 +21,10 @@ import android.widget.TextView;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.receivers.RepoDlReceiver;
import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.Utils.ByteArrayInOutStream;
import com.topjohnwu.magisk.utils.WebWindow;
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.OutputStream;
import java.util.List;
import butterknife.BindView;

View File

@ -4,7 +4,6 @@ import android.net.Uri;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.Utils.ByteArrayInOutStream;
import com.topjohnwu.magisk.utils.ZipUtils;

View File

@ -3,7 +3,6 @@ package com.topjohnwu.magisk.utils;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
@ -45,46 +44,7 @@ public class Async {
public static final String UPDATE_JSON = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
public static final String MAGISK_HIDE_PATH = "/magisk/.core/magiskhide/";
public static class ConstructEnv extends NormalTask<Void, Void, Void> {
ApplicationInfo mInfo;
public ConstructEnv(ApplicationInfo info) {
mInfo = info;
}
@Override
protected Void doInBackground(Void... voids) {
String toolPath = mInfo.dataDir + "/tools";
String busybox = mInfo.dataDir + "/lib/libbusybox.so";
if (!Utils.itemExist(false, toolPath)) {
Shell.sh(
"mkdir " + toolPath,
"chmod 755 " + toolPath,
"cd " + toolPath,
"ln -s " + busybox + " busybox",
"for tool in $(./busybox --list); do",
"ln -s " + busybox + " $tool",
"done",
"rm -f su sh"
);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
new RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Shell.su("PATH=" + mInfo.dataDir + "/tools:$PATH");
return null;
}
}.exec();
}
}
public static final String TMP_FOLDER_PATH = "/dev/tmp";
public static class CheckUpdates extends NormalTask<Void, Void, Void> {
@ -226,6 +186,12 @@ public class Async {
}
}
protected boolean unzipAndCheck() {
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android");
return Utils.readFile(mCachedFile.getParent() + "/META-INF/com/google/android/updater-script")
.get(0).contains("#MAGISK");
}
@Override
protected void onPreExecute() {
progress = new ProgressDialog(mContext);
@ -249,27 +215,22 @@ public class Async {
e.printStackTrace();
return -1;
}
if (!unzipAndCheck()) return 0;
publishProgress(mContext.getString(R.string.zip_install_progress_msg, mFilename));
if (Shell.rootAccess()) {
ret = Shell.su(
"unzip -o " + mCachedFile.getPath() + " META-INF/com/google/android/* -d " + mCachedFile.getParent(),
"if [ \"$(cat " + mCachedFile.getParent() + "/META-INF/com/google/android/updater-script)\" = \"#MAGISK\" ]; then echo true; else echo false; fi"
);
if (! Boolean.parseBoolean(ret.get(ret.size() - 1))) {
return 0;
}
ret = Shell.su(
"BOOTMODE=true sh " + mCachedFile.getParent() + "/META-INF/com/google/android/update-binary dummy 1 "+ mCachedFile.getPath(),
"BOOTMODE=true sh " + mCachedFile.getParent() +
"/META-INF/com/google/android/update-binary dummy 1 " + mCachedFile.getPath(),
"if [ $? -eq 0 ]; then echo true; else echo false; fi"
);
Shell.su("rm -rf " + mCachedFile.getParent() + "/META-INF");
Logger.dev("FlashZip: Console log:");
for (String line : ret) {
Logger.dev(line);
}
}
if (mCachedFile != null && mCachedFile.exists() && !mCachedFile.delete()) {
Utils.removeItem(mCachedFile.getPath());
Shell.su(
"rm -rf " + mCachedFile.getParent() + "/*",
"rm -rf " + TMP_FOLDER_PATH
);
}
if (ret != null && Boolean.parseBoolean(ret.get(ret.size() - 1))) {
return 1;
@ -284,7 +245,9 @@ public class Async {
progress.dismiss();
switch (result) {
case -1:
Toast.makeText(mContext, mContext.getString(R.string.manual_install, mUri.getPath()), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, mContext.getString(R.string.install_error), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, mContext.getString(R.string.manual_install_1, mUri.getPath()), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, mContext.getString(R.string.manual_install_2), Toast.LENGTH_LONG).show();
break;
case 0:
Toast.makeText(mContext, mContext.getString(R.string.invalid_zip), Toast.LENGTH_LONG).show();

View File

@ -39,8 +39,9 @@ public class Shell {
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList, true);
rootSTDOUT.start();
// Setup umask
// Setup umask and PATH
su("umask 022");
su("PATH=/data/busybox:$PATH");
List<String> ret = su("echo -BOC-", "id");

View File

@ -25,6 +25,7 @@ import org.spongycastle.util.encoders.Base64;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -48,6 +49,7 @@ import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
@ -98,7 +100,7 @@ public class ZipUtils {
byte buffer[] = new byte[4096];
while ((entry = source.getNextJarEntry()) != null) {
// Remove the top directory from the path
path = entry.toString().substring(entry.toString().indexOf("/") + 1);
path = entry.getName().substring(entry.getName().indexOf("/") + 1);
// If it's the top folder, ignore it
if (path.isEmpty())
continue;
@ -118,6 +120,44 @@ public class ZipUtils {
}
}
public static void unzip(File file, File folder) {
unzip(file, folder, "");
}
public static void unzip(File file, File folder, String path) {
try {
int count;
FileOutputStream out;
File dest;
InputStream is;
JarEntry entry;
byte data[] = new byte[4096];
JarFile zipfile = new JarFile(file);
Enumeration e = zipfile.entries();
while(e.hasMoreElements()) {
entry = (JarEntry) e.nextElement();
if (!entry.getName().contains(path)) {
continue;
}
Logger.dev("Extracting: " + entry);
is = zipfile.getInputStream(entry);
dest = new File(folder, entry.getName());
if (dest.getParentFile().mkdirs()) {
dest.createNewFile();
}
out = new FileOutputStream(dest);
while ((count = is.read(data, 0, 4096)) != -1) {
out.write(data, 0, count);
}
out.flush();
out.close();
is.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void signZip(Context context, InputStream inputStream,
OutputStream outputStream, boolean signWholeFile) {
JarMap inputJar;