Skip to content

Commit 025a133

Browse files
committed
is_symlink: .net also
1 parent 8b4253f commit 025a133

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

+stdlib/is_symlink.m

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
% optional: mex
33

44
function ok = is_symlink(p)
5+
arguments
6+
p {mustBeTextScalar}
7+
end
58

69
try
710
ok = isSymbolicLink(p);
811
catch e
912
switch e.identifier
10-
case "MATLAB:UndefinedFunction", ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
13+
case "MATLAB:UndefinedFunction"
14+
if stdlib.has_dotnet()
15+
ok = is_symlink_dotnet(p);
16+
elseif stdlib.has_java()
17+
ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
18+
else
19+
rethrow(e)
20+
end
1121
case "Octave:undefined-function"
1222
% use lstat() to work with a broken symlink, like Matlab isSymbolicLink
1323
[s, err] = lstat(p);
@@ -18,6 +28,24 @@
1828

1929
end
2030

31+
32+
function ok = is_symlink_dotnet(p)
33+
34+
try
35+
ok = ~isempty(System.IO.FileInfo(p).LinkTarget);
36+
catch e
37+
if strcmp(e.identifier, "MATLAB:noSuchMethodOrField")
38+
attr = string(System.IO.File.GetAttributes(p).ToString());
39+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributesN
40+
% ReparsePoint is for Linux, macOS, and Windows
41+
ok = contains(attr, 'ReparsePoint');
42+
else
43+
rethrow(e)
44+
end
45+
end
46+
47+
end
48+
2149
%!test
2250
%! if !ispc
2351
%! p = tempname();

0 commit comments

Comments
 (0)