Skip to content

Commit 315558d

Browse files
committed
is_symlink: speedup
1 parent c958cbf commit 315558d

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

+stdlib/+java/is_symlink.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function ok = is_symlink(file)
22

3-
ok = java.nio.file.Files.isSymbolicLink(javaAbsolutePath(file));
3+
try
4+
ok = java.nio.file.Files.isSymbolicLink(javaAbsolutePath(file));
5+
catch e
6+
javaException(e)
7+
end
48

59
end

+stdlib/+native/is_symlink.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function i = is_symlink(file)
2+
3+
try
4+
i = isSymbolicLink(file);
5+
catch e
6+
if e.identifier ~= "MATLAB:UndefinedFunction"
7+
rethrow(e)
8+
end
9+
end
10+
11+
end

+stdlib/is_symlink.m

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@
44
% * file: path to check
55
% * backend: backend to use
66
%%% Outputs
7-
% * ok: true if path is a symbolic link
7+
% * i: true if path is a symbolic link
88
% * b: backend used
99

10-
function [ok, b] = is_symlink(file, backend)
10+
function [i, b] = is_symlink(file, backend)
1111
arguments
12-
file string
12+
file (1,1) string
1313
backend (1,:) string = ["native", "java", "python", "dotnet", "sys"]
1414
end
1515

16-
if ismember('native', backend) || stdlib.strempty(backend)
17-
try
18-
ok = isSymbolicLink(file);
19-
b = "native";
20-
return
21-
catch e
22-
if e.identifier ~= "MATLAB:UndefinedFunction"
23-
rethrow(e)
24-
end
16+
i = logical.empty;
17+
18+
for b = backend
19+
switch b
20+
case "java"
21+
i = stdlib.java.is_symlink(file);
22+
case "native"
23+
i = stdlib.native.is_symlink(file);
24+
case "dotnet"
25+
i = stdlib.dotnet.is_symlink(file);
26+
case "python"
27+
if stdlib.matlabOlderThan('R2022a'), continue, end
28+
i = stdlib.python.is_symlink(file);
29+
case "sys"
30+
i = stdlib.sys.is_symlink(file);
31+
otherwise
32+
error("stdlib:hard_link_count:ValueError", "Unknown backend: %s", b)
2533
end
2634

27-
backend(ismember(backend, 'native')) = [];
35+
if ~isempty(i)
36+
return
37+
end
2838
end
29-
30-
o = stdlib.Backend(mfilename(), backend);
31-
b = o.backend;
32-
ok = o.func(file);
33-
3439
end

0 commit comments

Comments
 (0)