fs::create_dir_all() doesn't throw an error, but also does not work

huangapple go评论95阅读模式
英文:

fs::create_dir_all() doesn't throw an error, but also does not work

问题

fs::create_dir_all("$DOCUMENT/QueryIOProjects"); 的结果是 Ok(),并且打印输出 Projects Directory created at /Users/username/Documents/QueryIOProjects,但我无法在Finder或终端中看到任何文件夹。为了调试,我使用了read_dir方法来打印所有的DirEntry,它只输出了一个结果 dirEntry = QueryIOProjects,这让我更加困惑。

  1. tauri::Builder::default()
  2. .invoke_handler(tauri::generate_handler![greet])
  3. .menu(menu)
  4. .on_menu_event(menu_item_handler)
  5. .setup(|app| {
  6. let path_str = match path::parse(
  7. &app.config(),
  8. app.package_info(),
  9. &app.env(),
  10. "$DOCUMENT/QueryIOProjects",
  11. ) {
  12. Ok(path) => path.to_string_lossy().to_string(),
  13. Err(_) => "Path creation failed".to_string(),
  14. };
  15. {
  16. let result = fs::create_dir_all("$DOCUMENT/QueryIOProjects");
  17. match result {
  18. Ok(_) => println!("Projects Directory created at {}", path_str),
  19. Err(_) => println!("Failed to create project directory at {}", path_str),
  20. }
  21. }
  22. {
  23. let result = fs::read_dir("$DOCUMENT/");
  24. match result {
  25. Ok(readDir) => {
  26. readDir.for_each(|dirEntryResult| match dirEntryResult {
  27. Ok(dirEntry) => println!(
  28. "dirEntry = {}",
  29. dirEntry
  30. .file_name()
  31. .into_string()
  32. .expect("dir entry string conversion failed")
  33. ),
  34. Err(err) => println!("error in dirEntry"),
  35. });
  36. }
  37. Err(_) => println!("Failed to read directory at {}", "$DOCUMENT/"),
  38. }
  39. }
  40. Ok(())
  41. })
  42. .run(tauri::generate_context!())
  43. .expect("error while running tauri application");
  1. {
  2. "tauri": {
  3. "allowlist": {
  4. "all": false,
  5. "shell": {
  6. "all": false,
  7. "open": true
  8. },
  9. "fs": {
  10. "scope": [
  11. "$DOCUMENT",
  12. "$DOCUMENT/*"
  13. ],
  14. "all": true,
  15. "readFile": true,
  16. "writeFile": true,
  17. "readDir": true,
  18. "copyFile": true,
  19. "createDir": true,
  20. "removeFile": true,
  21. "renameFile": true,
  22. "exists": true
  23. },
  24. "path": {
  25. "all": true
  26. },
  27. "dialog": {
  28. "open": true,
  29. "save": true
  30. }
  31. }
  32. }
  33. }
英文:

I'm new to Rust, so maybe I'm not understanding this correctly. I am creating a multiplatform application using Tauri + Yew. In my backend code when I'm setting up the app, I wanted to create a directory in the $DOCUMENT directory, for projects made inside my application.

The result for fs::create_dir_all("$DOCUMENT/QueryIOProjects"); is Ok() and it prints Projects Directory created at /Users/username/Documents/QueryIOProjects, but I cant see any folder in Finder or via terminal. To debug, I used the read_dir method to print all the DirEntrys,which prints out only one output dirEntry = QueryIOProjects, leaving me even more confused.

src-tauri/src/main.rs:

  1. tauri::Builder::default()
  2. .invoke_handler(tauri::generate_handler![greet])
  3. .menu(menu)
  4. .on_menu_event(menu_item_handler)
  5. .setup(|app| {
  6. let path_str = match path::parse(
  7. &app.config(),
  8. app.package_info(),
  9. &app.env(),
  10. "$DOCUMENT/QueryIOProjects",
  11. ) {
  12. Ok(path) => path.to_string_lossy().to_string(),
  13. Err(_) => "Path creation failed".to_string(),
  14. };
  15. {
  16. let result = fs::create_dir_all("$DOCUMENT/QueryIOProjects");
  17. match result {
  18. Ok(_) => println!("Projects Directory created at {}", path_str),
  19. Err(_) => println!("Failed to create project directory at {}", path_str),
  20. }
  21. }
  22. {
  23. let result = fs::read_dir("$DOCUMENT/");
  24. match result {
  25. Ok(readDir) => {
  26. readDir.for_each(|dirEntryResult| match dirEntryResult {
  27. Ok(dirEntry) => println!(
  28. "dirEntry = {}",
  29. dirEntry
  30. .file_name()
  31. .into_string()
  32. .expect("dir entry string conversion failed")
  33. ),
  34. Err(err) => println!("error in dirEntry"),
  35. });
  36. }
  37. Err(_) => println!("Failed to read directory at {}", "$DOCUMENT/"),
  38. }
  39. }
  40. Ok(())
  41. })
  42. .run(tauri::generate_context!())
  43. .expect("error while running tauri application");

tauri.conf.json:

  1. {
  2. "tauri": {
  3. "allowlist": {
  4. "all": false,
  5. "shell": {
  6. "all": false,
  7. "open": true
  8. },
  9. "fs": {
  10. "scope": [
  11. "$DOCUMENT",
  12. "$DOCUMENT/*"
  13. ],
  14. "all": true,
  15. "readFile": true,
  16. "writeFile": true,
  17. "readDir": true,
  18. "copyFile": true,
  19. "createDir": true,
  20. "removeFile": true,
  21. "renameFile": true,
  22. "exists": true
  23. },
  24. "path": {
  25. "all": true
  26. },
  27. "dialog": {
  28. "open": true,
  29. "save": true
  30. }
  31. }
  32. }
  33. }

答案1

得分: 2

你正在创建文件夹,位于字面路径"$DOCUMENT/QueryIOProjects",而不是path_str。在你的计算机上有一个名为$DOCUMENT的文件夹,字面上就是这个名字。

英文:

You are creating the folder at the literal path "$DOCUMENT/QueryIOProjects", not path_str. Somewhere on your computer is a folder called, literally, $DOCUMENT.

huangapple
  • 本文由 发表于 2023年5月22日 02:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定