Skip to content

Commit 0faed11

Browse files
committed
{create,is,read}_symlink: speedup
1 parent c958cbf commit 0faed11

19 files changed

+155
-75
lines changed

+stdlib/+dotnet/create_symlink.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
link (1,1) string
77
end
88

9-
ok = false;
10-
119
if ~stdlib.exists(target) || stdlib.strempty(link) || stdlib.exists(link)
10+
ok = false;
1211
return
1312
end
1413

@@ -18,6 +17,7 @@
1817
ok = true;
1918
catch e
2019
dotnetException(e)
20+
ok = logical.empty;
2121
end
2222

2323
end

+stdlib/+dotnet/is_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
end
1717
catch e
1818
dotnetException(e)
19-
y = false;
19+
y = logical.empty;
2020
end
2121

2222
end

+stdlib/+dotnet/private/dotnetException.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ function dotnetException(e)
44
case 'MException'
55
switch e.identifier
66
case {'MATLAB:NET:NetConversion:StringConversion'}
7-
otherwise, rethrow(e)
7+
% pass
8+
case {'MATLAB:undefinedVarOrClass', 'MATLAB:NET:NETInterfaceUnsupported'}
9+
% .NET not enabled
10+
case 'MATLAB:subscripting:classHasNoPropertyOrMethod'
11+
% .NET too old or not supported on this platform
12+
otherwise
13+
rethrow(e)
814
end
915
case 'NET.NetException'
1016
switch class(e.ExceptionObject)
1117
case {'System.IO.DriveNotFoundException', 'System.IO.FileNotFoundException', 'System.ArgumentException'}
12-
otherwise, rethrow(e)
18+
% pass
19+
otherwise
20+
rethrow(e)
1321
end
1422
end
1523

+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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
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+
ok = logical.empty;
8+
end
49

510
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/create_symlink.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function i = create_symlink(target, link)
2+
3+
try
4+
createSymbolicLink(link, target);
5+
i = true;
6+
catch e
7+
switch e.identifier
8+
case {'MATLAB:UndefinedFunction', 'MATLAB:io:filesystem:symlink:NeedsAdminPerms'}
9+
i = logical.empty;
10+
case {'MATLAB:io:filesystem:symlink:FileExists', 'MATLAB:io:filesystem:symlink:TargetNotFound'}
11+
i = false;
12+
otherwise
13+
rethrow(e)
14+
end
15+
end
16+
17+
end

+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

0 commit comments

Comments
 (0)