Android Runtime.getRuntime().exec and rsync

3

我想在我的设备上运行rsync。二进制文件位于/system/xbin/rsync,我正在尝试使用Runtime.getRuntime().exec启动它。我是Android编程的新手,所以我还不确定是否需要使用超级用户权限。

我的代码如下:

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);
}

它没有起作用,只打印“RSYNC STARTED”“RSYNC FINISHED”。

但是如果我运行:

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

它能够很好地工作,我可以从LogCat窗口看到输出。

因此我猜我需要使用超级用户权限,所以我将我的代码修改如下:

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 {
}

当我运行代码时,应用程序会冻结,但没有错误提示。


“freezes with no errors” 你确定没有LogCat吗? - Budius
什么也没有,它只会打印“RSYNC STARTED”而已。 - phcaze
是的,使用Cyanogenmod可以。我正在尝试在两个设备上使用它,而且我得到了相同的情况。 - phcaze
你有尝试使用调试断点逐步执行代码吗?这样你就可以看到它运行到哪一步了... - IgorGanapolsky
1个回答

0

好的,我正在使用稍微不同版本的代码:

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();
}   

我了解问题出在“su”命令上。如果我使用“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();
}   

它运行良好,但当然我从rsync得到了一个错误,因为我没有权限,同步无法工作。我该如何解决我的问题?


我解决了!最后的代码运行良好,权限问题是由于错误的rsync配置引起的!谢谢。 - phcaze

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接