英文:
creating and removing symlinks
问题
嗨,我正在尝试在Rust中创建符号链接到目录,然后尝试取消链接它。我正在寻找一个跨平台的方法来实现这个目标。
我尝试使用symlink crate来完成这个任务。在Unix系统中,该crate使用fs::remove_file
来删除符号链接。但实际使用时会抛出错误。错误消息是 path is a Directory
。我还在fnm(这里)中看到了相同的函数被使用。我不知道该怎么办。
至于创建符号链接,该crate使用std::os::unix::fs::symlink
。但出于某种原因,我的符号链接目录仍然为空。总体的代码大致如下:
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(())
}
我还没有在Windows上尝试过,所以不确定Windows部分是否正常工作。只在Linux操作系统上使用GitHub Codespaces进行了尝试。
我正在尝试创建一个类似于nvm的Dart版本管理器(这里),我需要将当前使用的版本的目录创建符号链接到一个名为current
或类似的目录中。
英文:
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.
答案1
得分: 0
Rust文档中对于 std::fs::remove_file
的说明称它对应于Unix上的 unlink
系统调用,而Unix上关于 unlink
的文档则说明 如果名称指向符号链接,则会删除该链接
。因此,使用 std::fs::remove_file
删除符号链接(以及 symlink::remove_symlink_dir
)应该是有效的。
看起来,结尾带斜杠可能会导致你提到的错误。例如,在 /path/to/my/symlink
上调用 unlink 应该可以工作,但在 /path/to/my/symlink/
上不行。
英文:
Rust documentation for std::fs::remove_file
says that it corresponds to the unlink
syscall on Unix, and Unix documentation for unlink states If the name referred to a symbolic link, the link is removed
. Therefore, it should work to use std::fs::remove_file
to remove a symlink (and thereby also symlink::remove_symlink_dir
)
It appears that a trailing slash can cause the error you mentioned. For example, calling unlink on /path/to/my/symlink
should work, but not on /path/to/my/symlink/
.
答案2
得分: 0
我还没有找到答案来回答我的问题,但最终我解决了它。不确定为什么对我来说使用 fs::remove_file
不起作用。所以我采取了强制的方法,只是使用 fs::remove_dir_all
删除了符号链接。只要它能工作,我就满意。我一直在这里使用它,运行得很好,尽管我仍然认为在Unix上使用 fs::remove_file
,在Windows上使用 fs::remove_dir
应该是正确的方式。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论