繁体   English   中英

符号链接始终指向其父目录

[英]Symbolic-Link always points to its parent directory

放置当前目录以外的符号链接不起作用,因为它们总是指向当前目录

我在 Windows 10,我错过了什么?

fs.symlink('./testFile', './testDir/testSymLink', function(err){       // creates a symbolic- link in the 'testDir' subfolder relative to the current directory 
    if(err) console.log(err);
});

fs.readlink('./testDir/testSymlink',function(err, links){              // reads the created symbolic link 
    if(err) console.log(err);
    console.log(links);                                                // -> '.\testFile'   (points to the current directory not to the parent directory)
});

fs.readFile('./testDir/testSymlink.txt', function(err, data){          // file doesn't exist
    if(err) console.log(err);                                          // -> ENOENT no such file or directory 
    console.log(data);                                                 // -> undefined 
});

符号链接已创建(我们可以读取)但指向其当前目录.\testFile它应该指向其参考文件为..\testFile的父目录

symlink()方法的第一个参数是创建的符号链接使用的目标位置。

重要的是要知道符号链接在传递时使用第一个参数! (不将其解析为绝对路径!)

这是棘手的部分,因为上面的第一个参数是./testFile符号链接将使用此路径来引用其自己目录中的“testFile”(而不是在父目录中)

解决方案:

1:上面的代码可以这样固定( testSymLink目录下的testDir引用了父目录下的testFile

fs.symlink('../testFile', './testDir/testSymLink', function(err){       
    if(err) console.log(err);
});

2:在symlink()方法中将绝对路径作为第一个参数传递(这将为您节省很多麻烦!)

fs.symlink(/* absolute path */, './testDir/testSymLink', function(err){       
    if(err) console.log(err);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM