Skip to content

Commit 63f4c7e

Browse files
committed
shell fallback is_symlink
1 parent 0dd2573 commit 63f4c7e

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

+stdlib/+sys/is_symlink.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function ok = is_symlink(p)
2+
3+
if ispc()
4+
[s, m] = system(sprintf('pwsh -command "(Get-Item -Path %s).Attributes"', p));
5+
ok = s == 0 && contains(m, 'ReparsePoint');
6+
else
7+
[s, ~] = system(sprintf('test -L %s', p));
8+
ok = s == 0;
9+
end
10+
11+
end

+stdlib/is_symlink.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
p {mustBeTextScalar}
66
end
77

8+
if strempty(p)
9+
ok = false;
10+
return
11+
end
12+
813
try
914
ok = isSymbolicLink(p);
15+
return
1016
catch e
1117
switch e.identifier
1218
case "MATLAB:UndefinedFunction"
@@ -16,11 +22,6 @@
1622
ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
1723
elseif stdlib.has_python()
1824
ok = stdlib.python.is_symlink(p);
19-
elseif isunix()
20-
ok = system(sprintf('test -L %s', p)) == 0;
21-
elseif ispc()
22-
[s, m] = system(sprintf('pwsh -command "(Get-Item -Path %s).Attributes"', p));
23-
ok = s == 0 && contains(m, 'ReparsePoint');
2425
end
2526
case "Octave:undefined-function"
2627
% use lstat() to work with a broken symlink, like Matlab isSymbolicLink
@@ -30,6 +31,10 @@
3031
end
3132
end
3233

34+
if ~ok
35+
ok = stdlib.sys.is_symlink(p);
36+
end
37+
3338
end
3439

3540

test/TestSymlink.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
end
88

99
properties (TestParameter)
10-
p = init_symlink()
10+
p = {{"not-exist", false}, ...
11+
{mfilename("fullpath") + ".m", false}, ...
12+
{"", false}};
1113
end
1214

1315

@@ -86,9 +88,3 @@ function test_create_symlink(tc)
8688
end
8789
end
8890

89-
90-
function p = init_symlink()
91-
p = {{"not-exist", false}, ...
92-
{mfilename("fullpath") + ".m", false}, ...
93-
{"", false}};
94-
end

0 commit comments

Comments
 (0)