Skip to content

Commit bfe3875

Browse files
committed
test, backend: more efficient, robust
1 parent aa18621 commit bfe3875

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+367
-393
lines changed

+stdlib/+dotnet/create_symlink.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
end
1010

1111
% https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink
12-
try
12+
try %#ok<TRYNC>
1313
System.IO.File.CreateSymbolicLink(link, target);
1414
ok = true;
15-
catch e
16-
warning(e.identifier, "%s", e.message)
1715
end
1816

1917
end

+stdlib/+dotnet/get_owner.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
% This is not yet possible with .NET on Unix, even with .NET 10.
66
% It would require Pinvoke or external Mono.Unix
77

8+
o = "";
9+
810
ntAccountType = System.Type.GetType('System.Security.Principal.NTAccount');
911
if isempty(ntAccountType)
10-
error('NTAccount type not found. Ensure you are running on a Windows system with .NET support.');
12+
return
1113
end
1214

1315
if isfolder(p)

+stdlib/+dotnet/is_admin.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
function y = is_admin()
44

5-
% com.sun.security.auth.module.NTSystem().getGroupIDs();
6-
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
7-
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
8-
95
% Use .NET System.Security.Principal to check for admin privileges
10-
identity = System.Security.Principal.WindowsIdentity.GetCurrent();
11-
principal = System.Security.Principal.WindowsPrincipal(identity);
12-
y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
6+
try
7+
identity = System.Security.Principal.WindowsIdentity.GetCurrent();
8+
principal = System.Security.Principal.WindowsPrincipal(identity);
9+
y = principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
10+
catch
11+
y = logical.empty;
12+
end
1313

1414
end

+stdlib/+dotnet/ram_total.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
function bytes = ram_total()
44
% .NET is 2-3x faster than Java for this
55
% https://learn.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.totalavailablememorybytes
6-
bytes = uint64(System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes);
6+
try
7+
bytes = System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
8+
catch
9+
bytes = [];
10+
end
11+
12+
bytes = uint64(bytes);
713
end

+stdlib/+dotnet/read_symlink.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
%% DOTNET.READ_SYMLINK resolve the symbolic links of a filepath
22

3-
function r = read_symlink(p)
3+
function r = read_symlink(file)
44

55
try
6-
h = System.IO.FileInfo(p);
6+
h = System.IO.FileInfo(file);
77
r = string(h.LinkTarget);
88
% on Unix, this can be empty if the file is not a symlink
9+
if isempty(r)
10+
r = "";
11+
end
912
catch
1013
r = "";
1114
end
1215

13-
if isempty(r)
14-
r = "";
15-
end
16-
1716
end

+stdlib/+java/device.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
% Java 1.8 benefits from the absolute() for stability
77
% seen on older Matlab versions on HPC
88

9-
i = java.nio.file.Files.getAttribute(jp, 'unix:dev', opt);
9+
try
10+
i = java.nio.file.Files.getAttribute(jp, 'unix:dev', opt);
11+
catch
12+
i = [];
13+
end
1014

1115
i = uint64(i);
1216
end

+stdlib/+java/hard_link_count.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
function c = hard_link_count(p)
22

33
opt = java.nio.file.LinkOption.values();
4-
c = java.nio.file.Files.getAttribute(javaPathObject(p), "unix:nlink", opt);
5-
6-
end
4+
try
5+
c = java.nio.file.Files.getAttribute(javaPathObject(p), "unix:nlink", opt);
6+
catch
7+
c = [];
8+
end
9+
end

+stdlib/+java/inode.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
% Java 1.8 benefits from the absolute() for stability--it's not an issue
77
% on every computer.
88

9-
i = java.nio.file.Files.getAttribute(jp, "unix:ino", opt);
9+
try
10+
i = java.nio.file.Files.getAttribute(jp, "unix:ino", opt);
11+
catch
12+
i = [];
13+
end
1014

1115
i = uint64(i);
1216
end

+stdlib/+java/is_admin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function y = is_admin()
1+
function ok = is_admin()
22

3-
y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
3+
% com.sun.security.auth.module.NTSystem().getGroupIDs();
4+
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
5+
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
6+
7+
ok = logical.empty;
8+
9+
try %#ok<TRYNC>
10+
ok = com.sun.security.auth.module.UnixSystem().getUid() == 0;
11+
end
412

513
end

+stdlib/+native/is_exe.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
props = "Readable";
1717
end
1818

19-
t = getPermissions(filePermissions(file(y)), props);
20-
21-
y(y) = any(t{:, :}, 2);
19+
try
20+
t = getPermissions(filePermissions(file(y)), props);
21+
y(y) = any(t{:, :}, 2);
22+
catch
23+
y = logical.empty;
24+
end
2225

2326
end

0 commit comments

Comments
 (0)