简体   繁体   English

Android Runtime.getRuntime()。exec和rsync

[英]Android Runtime.getRuntime().exec and rsync

I'm tryng to run rsync on my device. 我试着在我的设备上运行rsync。 I have the binary in /system/xbin/rsync and I'm trying to launch it with Runtime.getRuntime().exec . 我在/system/xbin/rsync有二进制文件,我正在尝试使用Runtime.getRuntime().exec启动它。 I'm new to Android programming, so I don't get yet if I have to user Superuser Permissions. 我是Android编程的新手,所以如果我必须使用超级用户权限,我还没有得到。

My code is: 我的代码是:

try {
    Process process = Runtime.getRuntime().exec(
            "/system/xbin/rsync -avzru /sdcard/Tinybox/ " +
            "mattiazeni@192.168.1.6::88124bb378ac994088e704d553de6f19");
    System.out.print("RSYNC LAUNCHED\n");

    // Reads stdout.
    // NOTE: You can write to stdin of the command using
    // process.getOutputStream().
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    int read;
    char[] buffer = new char[4096];
    StringBuffer output = new StringBuffer();
    while ((read = reader.read(buffer)) > 0) {
        output.append(buffer, 0, read);
    }
    reader.close();

    // Waits for the command to finish.
    process.waitFor();
    System.out.print(output);
    System.out.print("RSYNC FINISHED\n");

    // return output.toString();
} catch (IOException e) {
    throw new RuntimeException(e);
} catch (InterruptedException e) {
    throw new RuntimeException(e);
}

And it doesn't work, it just prints "RSYNC STARTED" "RSYNC FINISHED". 它不起作用,只打印“RSYNC STARTED”“RSYNC FINISHED”。

But if I run: 但如果我跑:

Process process = Runtime.getRuntime().exec("/system/xbin/rsync --help");

it works fine, I can see the output from the LogCat window. 它工作正常,我可以看到LogCat窗口的输出。

So I guess I have to use Superuser Permissions and so I modified my code as follows: 所以我想我必须使用超级用户权限,所以我修改了我的代码如下:

try {
    System.out.print("RSYNC STARTED\n");
    Process process = Runtime.getRuntime().exec("/system/xbin/su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());
    DataInputStream is = new DataInputStream(process.getInputStream());
    os.writeBytes("/system/xbin/rsync --help");
    String output = new String();
    String temp = new String();
    output = is.readLine();

    System.out.print(output);
    os.flush();
    System.out.print("RSYNC FINISHED\n");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {
}

But when I run the code the App freezes with no errors. 但是当我运行代码时,应用程序冻结没有错误。

Ok, I'm using a slightly different version of the code: 好的,我使用的是稍微不同的代码版本:

try {
    System.out.print("RSYNC STARTED\n");
    process = Runtime.getRuntime().exec("/system/xbin/su -c sh");
    OutputStream os = process.getOutputStream();
    Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");
    writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");

    os.flush();
    System.out.print("RSYNC FINISHED\n");
    }
catch ( IOException e ) {
   e.printStackTrace();
}   

I understood that the problem is on the su command. 我明白问题出在su命令上。 If I launch rsync using su it blocks with no error message as I previously said, if I remove the su command and launch just: 如果我使用su启动rsync阻止没有错误消息,如前所述,如果我删除su命令并启动只是:

try {
    System.out.print("RSYNC STARTED\n");
    process = Runtime.getRuntime().exec("sh");
    OutputStream os = process.getOutputStream();
    Log.d("RSYNC","/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");
    writeLine( os, "/system/xbin/rsync" + " -avzru /sdcard/download/ mattiazeni@192.168.1.6::TinyBox &");

    os.flush();
    System.out.print("RSYNC FINISHED\n");
    }
catch ( IOException e ) {
   e.printStackTrace();
}   

it works fine, but of course I got an error from rsync because I have no permissions and the synchronization won't work. 它工作正常,但当然我从rsync得到一个错误,因为我没有权限,同步将无法正常工作。 How can I solve my problem?? 我怎样才能解决我的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM