Hi im trying to symlink a directory in rust and later tried unlinking it. Im looking for a cross-platform method to do this.
Ive tried using the symlink crate to do this. In unix systems, the crate uses fs::remove_file
for removing symlinks. But this throws an error when actually using it. The error returns path is a Directory
. I also saw the same function being used in fnm (here). Idk what im supposed to do.
As for creating symlinks, the crate uses std::os::unix::fs::symlink
for creating the symlink, but for some reason my symlinked dir remains empty. The overall code used is more or less the following:
use std::path::Path;
#[cfg(unix)]
pub fn symlink_dir<P: AsRef<Path>, U: AsRef<Path>>(from: P, to: U) -> std::io::Result<()> {
std::os::unix::fs::symlink(from, to)?;
Ok(())
}
#[cfg(windows)]
pub fn symlink_dir<P: AsRef<Path>, U: AsRef<Path>>(from: P, to: U) -> std::io::Result<()> {
junction::create(from, to)?;
Ok(())
}
#[cfg(windows)]
pub fn remove_symlink_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
std::fs::remove_dir(path)?;
Ok(())
}
#[cfg(unix)]
pub fn remove_symlink_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
std::fs::remove_file(path)?;
Ok(())
}
Still haven't tried it on windows so not sure wether the windows counter part works. Only tried it on a linux os using github codespaces.
Im trying to make a nvm like version manager for Dart (here) and i need to symlink the currently used version's directory to a current
dir or something similar.
I havent found an answer to my question but i did end up solving it. Not sure why using fs::remove_file
wasnt working for me. So i brute forced it and just removed the symlink using fs::remove_dir_all
. As long as it works, im good with it. I've been using it here, working fine, tho i still do think using fs::remove_file
on unix and fs::remove_dir
on windows should be how it is.