gpt4 book ai didi

windows - 为什么在 Windows 上创建符号链接(symbolic link)需要管理员权限?

转载 作者:行者123 更新时间:2023-12-03 11:38:41 24 4
gpt4 key购买 nike

use std::os::windows::fs;
// https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html

use std::io::*;
use std::io::{self, Read, Stdin};

fn input_from() -> io::Result<()> {
let mut buffer = String::new();
let mut stdin = io::stdin(); // We get `Stdin` here.
stdin.read_to_string(&mut buffer)?;
Ok(())
}

fn main() -> std::io::Result<()> {
let mut shortcut_dir = String::from("D:\\winr\\");

println!("Your script path:");
let mut buffer1 = String::new();
let mut stdin = io::stdin();
stdin
.read_line(&mut buffer1)
.expect("error: unable to read user input");

buffer1.pop();

println!("Your shortcut name:");
let mut buffer2 = String::new();
let mut stdin: Stdin = io::stdin();

stdin
.read_line(&mut buffer2)
.expect("error: unable to read user input");

println!("buffer1 is:{:?}", buffer1);

buffer2.pop();

let mut buffer2 = &buffer2[..];

println!("buffer2 is:{:?}", buffer2);

shortcut_dir.push_str(&buffer2);

let mut buffer3 = shortcut_dir;

println!("buffer3 is:{:?}", buffer3);

fs::symlink_file(buffer1, buffer3)?;

Ok(())
}

它提示:

Error: Os { code: 1314, kind: Other, message: "A required privilege is not held by the client" }
error: process didn't exit successfully: `target\debug\ssdkqn.exe` (exit code: 1)

我使用Windows。当我在管理员模式下运行此脚本时,问题就消失了。为什么需要管理员权限才能创建符号链接(symbolic link)?

最佳答案

这是 Windows 的要求,除非您在 Windows 10 上以开发人员模式运行。

Unix 从 4.2 BSD 开始就有符号链接(symbolic link)。它们的行为是众所周知的、易于理解的,并且被广泛使用。任何 Unix 程序员都知道符号链接(symbolic link)。

但是,当它们可用时,开发人员必须注意某些安全问题。例如,存在检查时间/使用时间竞争条件,其中符号链接(symbolic link)的目标可以更改,因此不能安全地假设仅仅因为您检查了路径就意味着目标不会改变。经验丰富的 Unix 开发人员了解这些并仔细考虑它们。

Windows 添加符号链接(symbolic link)的时间相对较晚(Windows Vista 用于用户空间),因此开发人员不习惯使用它们。有人担心启用它们会导致旧程序或不熟悉符号链接(symbolic link)的开发人员编写的程序出现安全问题,其中一些问题无法修复。因此,我们决定将符号链接(symbolic link)的使用限制为管理员,除非用户具有“创建符号链接(symbolic link)”权限。

如果您使用的是 Windows 10,则可以启用开发人员模式并重新启动,然后您将能够创建符号链接(symbolic link)。您还可以在没有权限的情况下在 WSL 下创建符号链接(symbolic link)。

关于windows - 为什么在 Windows 上创建符号链接(symbolic link)需要管理员权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64991523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com