Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
std::process::Command无法在macOS上运行hdiutil(装载失败-没有这样的文件或目录),但在终端上运行时,该命令工作正常_Macos_Process_Rust - Fatal编程技术网

std::process::Command无法在macOS上运行hdiutil(装载失败-没有这样的文件或目录),但在终端上运行时,该命令工作正常

std::process::Command无法在macOS上运行hdiutil(装载失败-没有这样的文件或目录),但在终端上运行时,该命令工作正常,macos,process,rust,Macos,Process,Rust,hdiutils,当提供有效文件的正确路径时,返回错误2,没有这样的文件或目录。当我将命令数组的索引与“连接起来,打印它们,复制它们,并在终端中运行精确的字符串时,它工作得很好 这是编辑为仅包含相关位的函数。为了重现我的错误,您需要位于~/Downloads/StarUML.dmg的磁盘映像 use std::env; use std::fs; use std::process::Command; fn setup_downloads(download_name: &str) {

hdiutils
,当提供有效文件的正确路径时,返回
错误2,没有这样的文件或目录
。当我将命令数组的索引与
连接起来,打印它们,复制它们,并在终端中运行精确的字符串时,它工作得很好

这是编辑为仅包含相关位的函数。为了重现我的错误,您需要位于
~/Downloads/StarUML.dmg
的磁盘映像

use std::env;
use std::fs;
use std::process::Command;

fn setup_downloads(download_name: &str) {
    let downloads_path: String = {
        if cfg!(unix) {
            //these both yield options to unwrap
            let path = env::home_dir().unwrap();
            let mut downloads_path = path.to_str().unwrap().to_owned();
            downloads_path += "/Downloads/";
            downloads_path
        } else {
            "we currently only support Mac OS".to_string()
        }
    };

    let files_in_downloads =
        fs::read_dir(&downloads_path).expect("the read_dir that sets files_in_downloads broke");
    let mut file_path: String = "None".to_string();
    for file_name in files_in_downloads {
        let file_name: String = file_name
            .expect("the pre string result which sets file_name has broken")
            .file_name()
            .into_string()
            .expect("the post string result which sets file_name has broken")
            .to_owned();

        if file_name.contains(&download_name) {
            file_path = format!("'{}{}'", &downloads_path, &file_name);
        }
    }

    let len = file_path.len();

    if file_path[len - 4..len - 1] == "dmg".to_string() {
        let mount_command = ["hdiutil", "mount"];
        let output = Command::new(&mount_command[0])
            .arg(&mount_command[1])
            .arg(&file_path)
            .output()
            .expect("failed to execute mount cmd");

        if output.status.success() {
            println!(
                "command successful, returns: {}",
                String::from_utf8_lossy(&output.stderr).into_owned()
            );
        } else {
            println!(
                "command failed, returns: {}",
                String::from_utf8_lossy(&output.stderr).into_owned()
            );
        }
    }
}

fn main() {
    setup_downloads(&"StarUML".to_string());
}


命令拆分为一个变量,并在指定参数后使用调试格式化程序打印该变量:

let mut c = Command::new(&mount_command[0]);

c
    .arg(&mount_command[1])
    .arg(&file_path);

    println!("{:?}", c);
这个输出

“hdiutil”“mount”“\”/Users/shep/Downloads/StarUML.dmg\”
请注意,
命令
会自动为每个参数提供引号,但您已经添加了自己的单引号集:

format!("'{}{}'", &downloads_path, &file_name);
//       ^    ^

删除这些单引号。

我想说的是,我没有受过高中教育,更不用说是一名CS学生,因此我很可能遗漏了很多重要的部分,这些部分在我完成一个功能项目之前,我无法通过使用工具获得。我正试图将对自动化的热情传播给其他类似情况下的人,因此我希望能从缺少很多CS上下文的人的角度来改进代码质量。我当然希望在Stack Overflow上不会有人因为正规教育的不同而对你有不同的看法。我们对所有背景的程序员都同样严格;-)。如果mac上的~/Downloads目录中有StarUML安装dmg,则条件文件路径[len-4..len-1]==“dmg”。to_string()仅会求值为true。你可以在找到dmg,这应该有助于通过条件,让你看到错误,到目前为止,你们已经提供了额外的帮助,我感谢你们毫无偏见的回应(我从来没有感觉到你们在任何时候到目前为止)。我真的很想为你们构建这段代码,所以请确保你们得到了我在上面的评论中提到的dmg。请不要将错误消息作为图像包含在内。这些是不可访问的,搜索引擎无法为其编制索引。感谢dude,这在本例中是有效的,我添加它们是因为我在windows平台上遇到问题,Command没有在事后添加它们,因此windows命令解释器仅使用将字符串添加到未缩放的空格字符。我将完成mac位的组合,然后在windows中重试,如果行为仍然相同,我将使用一个条件来设置带引号的仅限windows的文件路径。