Skip to content

Commit 70a22d5

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

File tree

16 files changed

+148
-71
lines changed

16 files changed

+148
-71
lines changed

+stdlib/+dotnet/private/dotnetException.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ 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'
9+
% .NET not enabled
10+
otherwise
11+
rethrow(e)
812
end
913
case 'NET.NetException'
1014
switch class(e.ExceptionObject)
1115
case {'System.IO.DriveNotFoundException', 'System.IO.FileNotFoundException', 'System.ArgumentException'}
12-
otherwise, rethrow(e)
16+
% pass
17+
otherwise
18+
rethrow(e)
1319
end
1420
end
1521

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

+stdlib/+python/create_symlink.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
ok = true;
1515
catch e
1616
warning(e.identifier, "create_symlink(%s, %s) failed: %s", target, link, e.message);
17-
ok = false;
1817
end
1918

2019
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

0 commit comments

Comments
 (0)