1

I'm trying to copy a file from /path/to the/file.ext (yes, it has spaces, I suspect this is at least part of the trouble) to /data/data/com.my_pkg.app/file.ext.

In my (root) app, I do:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";
try {
    Process process;
    process = new ProcessBuilder(cmd).start();
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

But this raises an IOException: no such file or directory.

Checking my sanity, I opened a root file explorer app, navigated to the path above, copied it, and pasted it in my app's directory, and everything was fine.

Why is cp not finding it? - the file explorer app must be doing the same (well, with modification that makes it work!) thing underneath all that GUI.

Edit: the full error:

W/System.err( 2441): java.io.IOException: Error running exec(). 
     Command: [su -c "cp /path/to\ the/file.ext /data/data/com.my_pkg.app/file.ext"] 
     Working Directory: null 
     Environment: [ANDROID_ROOT=/system, 
          EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, 
          LOOP_MOUNTPOINT=/mnt/obb, 
          EMULATED_STORAGE_TARGET=/storage/emulated, 
          ANDROID_BOOTLOGO=1, LD_LIBRARY_PATH=/vendor/lib:/system/lib,             
          EXTERNAL_STORAGE=/storage/emulated/legacy, 
          ANDROID_SOCKET_zygote=10, 
          ANDROID_DATA=/data, 
          PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, 
          ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, 
          BOOTCLASSPATH=/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/webviewchromium.jar, 
          ANDROID_PROPERTY_WORKSPACE=9,0, 
          ANDROID_STORAGE=/storage]
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • Note that your **java** IOException **does not** come from `cp` not finding a file. Rather it is likely coming from failing to find the executable - which would actually be `su` rather than `cp`. – Chris Stratton Feb 27 '14 at 18:39
  • @ChrisStratton Failing to find what executable? – OJFord Feb 27 '14 at 18:40
  • @ChrisStratton Never mind, you edited in. How do I "help it find" `su` then? – OJFord Feb 27 '14 at 18:41
  • You could give the explicit path - assuming you *have* an su for it to find. You might want to try a series of simpler tests building up to your actual goal to figure out where the problem(s) are - though I agree the embedded quotes may be *one of* them. – Chris Stratton Feb 27 '14 at 18:43
  • Ah so I just give the path to it, as though it's not in `PATH`, and just any old program being passed some arguments? i.e. replace `su` with `path/to/su`? – OJFord Feb 27 '14 at 18:49
  • @ChrisStratton Actually though, when I used the same code for different `cmd` (still `su`), it worked fine, requested root permissions, so clearly had no trouble finding `su`. – OJFord Feb 27 '14 at 18:53
  • Replace `/path/to..` with original path of file. – Yuvi Feb 27 '14 at 18:54
  • @Yuvi I'm not completely stupid, this is my code, I just simplified the path of original file to `/path/to..` to make it clearer/more readable for you guys. – OJFord Feb 27 '14 at 19:06
  • @OllieFord Are you able to execute your command from adb shell ? – Yuvi Feb 27 '14 at 19:09
  • One interesting question would be if any shell ends up processing your argument list (with its escaped space) or not - that would depend a bit on how your specific implementation of `su` works. – Chris Stratton Feb 27 '14 at 19:18
  • @ChrisStratton Does it working over adb point back to a `su` issue? I'm afraid I don't really understand your last comment. – OJFord Feb 27 '14 at 19:35
  • @ChrisStratton I get the same error by providing path to `su` - but it isn't requesting root permissions. Strange. That's probably why it doesn't work, but I mean it's strange it isn't requesting. – OJFord Feb 27 '14 at 20:05
  • You may be able to try using `su` from the adb shell (even though it is unnecessary there). Or you could try it from within a terminal app like connectbot, where you actually would need to escalate from an unprivileged app userid. – Chris Stratton Feb 27 '14 at 20:11
  • @ChrisStratton I was using `su` when Yuvi asked if my command worked from adb. – OJFord Feb 27 '14 at 20:13

1 Answers1

1

I'm posting this as an answer, because it resolved my question as I asked it, and so should solve this problem for any one happening across this by search. For me, however, it just led to another question.

By changing the string:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";

to an array (that the rest of the code as-is concatenates/builds):

String[] cmd = {"su", "-c", "\"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\""};

All the errors were resolved. I can't tell you why, but at least they are.

Community
  • 1
  • 1
OJFord
  • 10,522
  • 8
  • 64
  • 98