Skip to content

Commit 1db4ce2

Browse files
committed
is_symlink: speedup
1 parent c958cbf commit 1db4ce2

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
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/+legacy/canonical.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
c = "";
88

9-
if stdlib.strempty(file), return, end
9+
if stdlib.strempty(file)
10+
return
11+
end
1012

1113
[s, r] = fileattrib(file);
1214

+stdlib/+native/is_symlink.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
i = logical.empty;
10+
end
11+
12+
end

+stdlib/+sys/read_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
% worried if searching for "Tag value: Symbolic Link" might be locale dependent
1515
end
1616

17-
if stdlib.is_symlink(file)
17+
if stdlib.sys.is_symlink(file)
1818
[s, m] = system(cmd);
1919
if s == 0
2020
m = strip(string(m));

+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)