Siriusmart
Siriusmart

Reputation: 197

Permission denied when creating directory

So I would like to create a directory in $HOME but then it says permisson denied, how does creating directories in $HOME require permissions?

Here's the code

let mut dir = home::home_dir().expect("Cannot get your home directory"); // Go to the home directory

dbg!(&dir);

dir.push("/.siriusmart");

if !dir.exists() { // Create a directory if it doesn't exist
    fs::create_dir(&dir)?;
}

Here's the output

[src/main.rs:122] &dir = "/home/siriusmart"
Error: Os { code: 13, kind: PermissionDenied, message: "Permission denied" }

Upvotes: 0

Views: 1783

Answers (1)

Peter Hall
Peter Hall

Reputation: 58785

Don't include the leading / in the directory name. This is interpreted as replacing the entire path from root.

If you move the dbg! statement to after the push, then you'd see the directory was /.siriusmart instead of /your/home/director/.siriusmart as you may have expected.

So just do:

dir.push(".siriusmart");

Upvotes: 2

Related Questions