paulgavrikov
paulgavrikov

Reputation: 1891

Write files to /system

I know this has been asked a lot but never answered. I definetly need to write files to root there is no other solution. I currently use this code but it doesn't show anything new in /system/. I want to copy file from my assets to the /system folder (with it's subdir's)

public void installFiles(View v) {
            try {
        Runtime.getRuntime().exec("su");
    } catch (IOException e) {
        mDebugView.append(e.toString());
    }
    copyPath("system/bin", "/system/bin/");
    copyPath("system/lib", "/system/lib/");
    copyPath("system/etc", "/system/etc/");
    copyPath("system/etc/audio", "/system/etc/audio/");
    copyPath("system/etc/soundimage", "/system/etc/soundimage/");
    copyPath("system/lib/soundfx", "/system/bin/soundfx/");
}

public void copyPath(String from, String to) {
    mDebugView.append("Copying path assets/" + from + " to " + to + "\n");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(from);
        for (String filename : files) {
            mDebugView.append(filename + "... \n");
            if (new File(filename).isFile()) {
                mDebugView.append("Copying " + filename + "\n");
                InputStream in = null;
                OutputStream out = null;
                in = assetManager.open(filename);
                out = new FileOutputStream(to);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
        }
    } catch (IOException e) {
        Log.e(this.getClass().toString(), e.toString());
        mDebugView.append(e.toString() + "\n");
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    mDebugView.append("..");
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

Upvotes: 3

Views: 18590

Answers (4)

paulgavrikov
paulgavrikov

Reputation: 1891

I kept digging and I have found a way to do it.

  1. After some attempts my ROM (more exactly /system folder) was so heavily damaged that I always had seen the errorstream input : "no permission" -> reinstalled ROM with wipes

  2. I copied files to external storage (in this case /sdcard but be aware that it might be another path - use Environment Object to get the path). I extracted all subfolders to main folders.

  3. New code as the previous ones opened a SU session and closed it again.

    Process proc = runtime.exec("su");
                new ProcessReader(proc).start();
                new ErrorReader(proc).start();
                DataOutputStream os = new DataOutputStream(
                        proc.getOutputStream());
                os.writeBytes("chmod 755 /system/" + "\n");
                os.writeBytes("find /sdcard/.beats_cache/systembin -exec mv '{}' /system/bin +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemlib -exec mv '{}' /system/lib +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetc -exec mv '{}' /system/etc +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetcaudio -exec mv '{}' /system/etc/audio +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetcsoundimage -exec mv '{}' /system/etc/soundimage +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemlibsoundfx -exec mv '{}' /system/lib/soundfx +\n");
                os.writeBytes("exit\n");
                os.flush();
    

Upvotes: 2

Andrey Ermakov
Andrey Ermakov

Reputation: 3328

You don't see any changes on /system because it's mounted as read-only by default. Please ensure that you remounted it before writing files. Although as others mentioned su should be used with a command.

Upvotes: 2

Jug6ernaut
Jug6ernaut

Reputation: 8325

Your

Runtime.getRuntime().exec("su");

does nothing. As the process is created and then released. To move files you will need to use the cat binary with su. IE

Runtime.getRuntime().exec("su cat filepath1 > filepath2");

for as many commands as you want to do it would be better to get the process instance of su and then execute all of your move commands at once.

Also note that you may have to mount the system partition as rw as it is probably not r/w by default.

Upvotes: 3

Corey Ogburn
Corey Ogburn

Reputation: 24717

Running 'su' at the beginning may not be enough to have write permissions to the /system folder. Root Explorer and other file management apps all have to remount /system as r/w and mount back as read-only. The answer to this question shows commands to remount the /system path. The answer is using adb, but running it on the device should work just as good.

On a side note, it may just be easier to execute system commands to move the files rather than move them yourself. On my LG Optimus T running Cyanogenmod 7.x, in /system/xbin there's cp and mv that may copy/move a file without having to remount /system (if so, probably only through su mv or su cp). I don't know enough about this part of android to know for sure if you (or whoever installs your app) will also have those files, but its worth looking into. They may require busybox, I haven't looked into it.

Upvotes: 1

Related Questions