1

In my application I'm doing:

try {
    String[] cmd = {"su", "-c",  "\"ls /data/\""}; //to debug, will be cp /src /dest

    ProcessBuilder builder = new ProcessBuilder(cmd);
    builder.redirectErrorStream(true);
    Process process = builder.start();
    InputStream is = process.getInputStream();

    Log.e("copy", is.toString());
    Log.e("copy", convertStreamToString(is));

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

The app is installed in /system/app/ and running with root permissions.

I see SuperSu's overlay that it's granted permissions for the operation.

With the cp /src /dest in place of ls command above, it doesn't copy, so debugging with ls, I get:

tmp-mksh: ls /data: not found

Why is this, and how can I fix it?

NB: This is the same issue as this question, except that was resolved by adding external write permissions - I should note that both paths in my command are in /data/...

Community
  • 1
  • 1
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • 1
    it is not silent, it is just ignored. you need to read the `getInputStream` (and redirect the outputstream too). Also, you should read the documentation for `Process`, as this is plainly stated and detailed there. – njzk2 Feb 27 '14 at 20:31
  • @njzk2 Okay, so I have `ProcessBuilder builder = new ProcessBuilder(cmd); builder.redirectErrorStream(true); Process process = builder.start(); InputStream in = process.getInputStream(); OutputStream out = ?` - I'm looking at the documentation but I'm not clear what the returned I/O streams from `process` are, why hasn't it "already done" the copy? – OJFord Feb 27 '14 at 20:45
  • What I mean is, `process` has it's in and out files as arguments, so I don't understand what the streams are in this context? – OJFord Feb 27 '14 at 20:47
  • @njzk2 I know this is so simple for you, but I'm new to Java, and I can't figure out how to apply what's written in the documentation for `Process` to what I have here. As it's so basic to you, I'd really appreciate you helping me out. – OJFord Feb 27 '14 at 21:16
  • in a shell, if you type `cat`, after the process is started, it can still read inputs. In your case, a `cp` will not read inputs, so `getOutputStream` is useless. `getInputStream` is sufficient. Also, it is buffered, so you don't have to worry about the copy being done already. Also `redirectErrorStream(true)` on the ProcessBuilder allows you to receive also stderr on the same outputstream. Just `getOutputStream` and read the content. – njzk2 Feb 27 '14 at 21:24
  • @njzk2 But what do you mean by 'read the content' - `getOutputStream` is just going to contain any `stderr`s? I think I misunderstood you before, I thought you were saying the os would contain the file... So how do I read it? – OJFord Feb 27 '14 at 21:30
  • @njzk2 Wait, "getOutputStream is useless. ... Just getOutputStream and read the content." - I don't understand, do I use it or not? – OJFord Feb 27 '14 at 21:38
  • I meant getInputStream, like I mentioned at first. (you can't read from an outputstream anyway.). This stream will contain everything the shell command would normally write on your console. (typically, errors) – njzk2 Feb 27 '14 at 21:45
  • @njzk2 Ah, thank you. So `is.read()` returns `int` representation of bytes, how do I view these as errors in `logcat` or wherever? – OJFord Feb 27 '14 at 21:56
  • @njzk2 Okay I figured out how to `Log.e()` the inputstream. It says file not found. This command works fine over adb (the file *is* there). – OJFord Feb 27 '14 at 22:19
  • @ChrisStratton File to be copied is in `/data/data/com.pkg/..`. Don't really care where I copy it to, but I've been doing it just to the top level of `/data/data/com.my_pkg/` – OJFord Feb 27 '14 at 22:30
  • 1
    now that you know how to get the output of a command, you can use `ls` to make sure the file is there – njzk2 Feb 27 '14 at 22:35
  • @njzk2 Why am I seeing `tmp-mksh: ls /data: not found`? Is it the command or directory it hasn't found? Obviously `/data` exists.. – OJFord Feb 27 '14 at 22:51
  • That sort of hints at it not parsing the distinction between `ls` as an executable and `/data` as an argument to it. – Chris Stratton Feb 27 '14 at 22:56
  • @ChrisStratton :/ any idea how to fix? I don't get why such basic commands (`ls`, `cp`) are just not working.. – OJFord Feb 27 '14 at 22:58
  • Updated OP to reflect the actual issue. – OJFord Feb 27 '14 at 23:24
  • @njzk2 Can you offer any insight? I'm completely stumped here - `cat` `cp` `ls` all give this `tmp-mksh : not found` error. – OJFord Feb 28 '14 at 00:18
  • @ChrisStratton Weird update - I just tried to copy it to `/sdcard/` instead. Works fine. So I can read from `/data/data/com.not_my_pkg` but I can't write to `/data/data/com.my_pkg`? – OJFord Feb 28 '14 at 00:33
  • Are you *sure* you are doing this as root? – Chris Stratton Feb 28 '14 at 06:26
  • Yes. Triggers pop up and I grant root permissions. If I didn't have root, there's no way in hell it would be copying the file out of another apps directory, wherever the destination was. – OJFord Feb 28 '14 at 08:06

0 Answers0