【问题标题】:busybox mv with whitespaces fails silently带有空格的busybox mv静默失败
【发布时间】:2013-02-17 14:54:43
【问题描述】:

我正在使用busybox 运行设备。 没有空格的文件夹或文件正确移动,但似乎有空格的文件夹没有正确移动

public static boolean mv(File source, File target) {
    if (!source.exists() || !target.exists()) {
        return false;
    }
    try {
        StringBuilder command = new StringBuilder("mv -v ");
        command.append('\"');
        command.append(source.getCanonicalPath());
        command.append('\"');
        command.append(' ');
        command.append('\"');
        command.append(target.getCanonicalPath());
        command.append('\"');
        System.out.println(command.toString());
        Process process = Runtime.getRuntime().exec(command.toString());
        StringBuilder output = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            output.append(line);
        }
        System.out.println(output.toString());
        return process.waitFor() == 0;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

输出是

mv -v "/storage/sdcard0/media/music/Progressive Death Metal" "/storage/sdcard0/Music"

没有 mv 输出,方法只返回“false”(非零退出代码)。

我必须使用规范路径,还是可以使用绝对路径并将其留给shell?

编辑

我也想出了如果文件名有引号,参数就会出错,所以我做了一个添加转义字符的方法

private static String getCommandLineString(String input) {
    return input.replace("\\", "\\\\")
            .replace("\"", "\\\"")
            .replace("\'", "\\\'")
            .replace("`", "\\`")
            .replace(" ", "\\ ");
}

现在 mv 看起来像这样

public static boolean mv(File source, File target) {
    if (!source.exists() || !target.exists()) {
        return false;
    }
    try {
        StringBuilder command = new StringBuilder("mv -v ");
        command.append(getCommandLineString(source.getAbsolutePath()));
        command.append(' ');
        command.append(getCommandLineString(target.getAbsolutePath()));
        System.out.println(command.toString());
        Process process = Runtime.getRuntime().exec(command.toString());
        StringBuilder output = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            output.append(line);
        }
        System.out.println(output.toString());
        return process.waitFor() == 0;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

我得到的是

mv -v /sdcard/media/music/Progressive\ Death\ Metal /sdcard/Music

但我仍然得到无声的非零退出代码。

【问题讨论】:

    标签: java android busybox


    【解决方案1】:

    终于搞定了。 Exec 应该请求 shell,而 OutputStream 应该编写命令。

    private static boolean execute(boolean superuser, String command) {
        DataOutputStream os = null;
        try {
            Process process = Runtime.getRuntime().exec(superuser ? "su" : "sh");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.flush();
    
            os.writeBytes("exit\n"); 
            os.flush();
    
            return process.waitFor() == 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) { try { os.close(); } catch (Exception e) {} }
        }
        return false;
    }
    
    public static boolean mv(File source, File target) {
        if (!source.exists() || !target.exists()) {
            return false;
        }
        try {
            StringBuilder command = new StringBuilder("mv ");
            command.append(getCommandLineString(source.getAbsolutePath()));
            command.append(' ');
            command.append(getCommandLineString(target.getAbsolutePath()));
            return execute(false, command.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多