Paul
Paul

Reputation: 970

cp command does not work on Android 4.4

I am facing a weird behavior on Android 4.4. The command below works perfectly on android Jelly Bean rooted device:

Process process = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(process.getOutputStream());

// path is a folder saved on sdcard
dos.writeBytes("cp -a " + path + " " + "/data/data" + " \n");
dos.flush();
process.waitFor() return 0 mean Ok

However on Android 4.4 rooted device, the above commands does not work. process.waitFor() return 1 mean there is error somewhere.

I notice that when running on Android Jelly Bean rooted device, the permission for files are: rwxr-x. On Android 4.4 rooted, they are: rwxrwx--

I googled this problem but did not find any result so far. Thanks. :)

UPDATE: I can copy data from /data/data folder using cp command to sdcard folder.

Upvotes: 0

Views: 747

Answers (2)

Paul
Paul

Reputation: 970

I managed to make it work by changing the command.

cp -a " + path + " " + "/data/data" + " \n"

to

cp -r " + path + " " + "/data/data" + " \n"

The difference between -a and -r is, -a will remain all the permission of the copying files that I dont know why it does not work on android 4.4. -r will also copy all the files and folders but it changes the permission of file, the simple solution (I think) is to chmod the permission manually after copying.

Upvotes: 0

ChuongPham
ChuongPham

Reputation: 4801

Before running su on Android 4.4+ to copy/write files in a rooted device, make a backup of this file:

/system/etc/permissions/platform.xml

Then see if the following permission and groups have been defined in platform.xml:

<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
    <group gid="sdcard_r" />
    <group gid="sdcard_rw" />
    <group gid="media_rw" />
</permission>

If not, edit platform.xml and add them, save, then reboot your device and try your su command again.

If all else fail, try to install the BusyBox binary on the rooted device and run the su command through it.

Upvotes: 1

Related Questions