Perl - 在 Windows 10 下检测符号链接 [英] Perl - Detecting symbolic links under Windows 10

查看:32
本文介绍了Perl - 在 Windows 10 下检测符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在支持符号链接的 Windows 10 下检测符号链接时遇到问题.首先我试过这个:

I have a problem with detecting symbolic links under Windows 10, which supports them. First I tried this:

if(! -l $import_filename) {
    print "$0: $import_filename is not a symlink";
}

那行不通.当 $import_filename 是一个符号链接时它会被执行.然后我尝试了这个:

That doesn't work. It gets executed when $import_filename is a symlink. Then I tried this:

use File::stat;
use Fcntl;

my $statbuf = lstat($import_filename);
if(!($statbuf->mode & S_ISLNK)) {
    print "$0: $import_filename is not a symlink";
}

而且说同样的话似乎是不同的方式.正如预期的那样,在具有符号链接/连接支持的 Windows 版本下,是否有任何幸运的方法可以做到这一点?如果没有,命令行工具也是可以接受的答案.

And it seems to be a different way to say the same thing. As expected, is there any blessed way to do this under Windows versions with symlink/junction support? If there isn't, a command line tool is also an acceptable answer.

推荐答案

Given

>mklink file_symlink file
symbolic link created for file_symlink <<===>> file

>mklink /d dir_symlink dir
symbolic link created for dir_symlink <<===>> dir

>mklink /h file_hardlink file
Hardlink created for file_hardlink <<===>> file

>mklink /j dir_hardlink dir
Junction created for dir_hardlink <<===>> dir

>dir
...
2018-05-09  12:59 AM    <JUNCTION>     dir_hardlink [C:\...\dir]
2018-05-09  12:58 AM    <SYMLINKD>     dir_symlink [dir]
2018-05-09  12:56 AM                 6 file_hardlink
2018-05-09  12:58 AM    <SYMLINK>      file_symlink [file]
...

您可以使用以下内容将 file_symlinkdir_symlinkdir_hardlink(但不是 file_hardlink)检测为链接:

You can use the following to detect file_symlink, dir_symlink and dir_hardlink (but not file_hardlink) as a link:

use Win32API::File qw( GetFileAttributes FILE_ATTRIBUTE_REPARSE_POINT );

my $is_link = GetFileAttributes($qfn) & FILE_ATTRIBUTE_REPARSE_POINT;

我不知道如何区分硬链接和符号链接(尽管可以使用 & FILE_ATTRIBUTE_DIRECTORY 来区分文件和目录).

I don't know how to distinguish between hard links and symlinks (though differentiating between files and dirs can be done using & FILE_ATTRIBUTE_DIRECTORY).

这篇关于Perl - 在 Windows 10 下检测符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆