Skip to content

Commit 224a0f9

Browse files
committed
{is,read}_symlink: speedup
1 parent c958cbf commit 224a0f9

File tree

12 files changed

+99
-44
lines changed

12 files changed

+99
-44
lines changed

+stdlib/+dotnet/read_symlink.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
h = System.IO.FileInfo(file);
88
r = string(h.LinkTarget);
99
% on Unix, this can be empty if the file is not a symlink
10-
if isempty(r)
11-
r = "";
12-
end
1310
catch e
1411
dotnetException(e)
15-
r = "";
12+
r = string.empty;
1613
end
1714

1815
end

+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/+java/read_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
r = string(java.nio.file.Files.readSymbolicLink(javaAbsolutePath(file)));
1111
catch e
1212
javaException(e)
13-
r = "";
13+
r = string.empty;
1414
end
1515

1616
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/+native/read_symlink.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function r = read_symlink(file)
2+
3+
try
4+
[ok, r] = isSymbolicLink(file);
5+
if ~ok
6+
r = string.empty;
7+
end
8+
catch e
9+
if e.identifier ~= "MATLAB:UndefinedFunction"
10+
rethrow(e)
11+
end
12+
r = string.empty;
13+
end
14+
15+
end

+stdlib/+python/read_symlink.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function r = read_symlink(file)
22

3-
r = "";
3+
r = string.empty;
44

55
p = py.pathlib.Path(file);
6-
if ~p.is_symlink(), return, end
6+
if ~p.is_symlink()
7+
return
8+
end
79

810
% https://docs.python.org/3/library/pathlib.html#pathlib.Path.readlink
911
try

+stdlib/+sys/read_symlink.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
file (1,1) string
44
end
55

6-
r = "";
6+
r = string.empty;
77

88
if isunix()
99
cmd = sprintf('readlink -fn "%s"', file);
@@ -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/Backend.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@
102102
pyv = stdlib.python_version();
103103
if any(pyv(1:2) < [3, 12]), continue, end
104104
end
105+
case 'native'
106+
107+
switch functionName
108+
case 'create_symlink'
109+
% Some Windows R2025a give error 'MATLAB:io:filesystem:symlink:NeedsAdminPerms'
110+
% 25.1.0.2973910 (R2025a) Update 1 gave this error for example.
111+
if stdlib.matlabOlderThan('R2024b') || ispc()
112+
continue
113+
end
114+
case {'is_symlink', 'read_symlink'}
115+
if stdlib.matlabOlderThan('R2024b'), continue, end
116+
end
105117
end
106118

107119
if ~isempty(which(sprintf('%s.%s.%s', self.namespace, m, functionName)))

+stdlib/is_symlink.m

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,37 @@
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:is_symlink:ValueError", "Unknown backend: %s", b)
2533
end
2634

27-
backend(ismember(backend, 'native')) = [];
35+
if ~isempty(i)
36+
return
37+
end
2838
end
2939

30-
o = stdlib.Backend(mfilename(), backend);
31-
b = o.backend;
32-
ok = o.func(file);
33-
3440
end

0 commit comments

Comments
 (0)