Skip to content

Commit 1654455

Browse files
committed
get_owner: add dotnet and tests
1 parent fde23b0 commit 1654455

File tree

7 files changed

+65
-19
lines changed

7 files changed

+65
-19
lines changed

+stdlib/+dotnet/get_owner.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function o = get_owner(p)
2+
3+
if ~stdlib.exists(p)
4+
o = string.empty;
5+
return
6+
end
7+
8+
ntAccountType = System.Type.GetType('System.Security.Principal.NTAccount');
9+
if isempty(ntAccountType)
10+
error('NTAccount type not found. Ensure you are running on a Windows system with .NET support.');
11+
end
12+
13+
if isfolder(p)
14+
fsec = System.IO.Directory.GetAccessControl(p);
15+
else
16+
fsec = System.IO.File.GetAccessControl(p);
17+
end
18+
19+
owner = fsec.GetOwner(ntAccountType);
20+
21+
o = string(owner.Value);
22+
23+
end

+stdlib/+java/get_owner.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
function n = get_owner(p)
22

3+
if ~stdlib.exists(p)
4+
n = string.empty;
5+
return
6+
end
7+
38
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#getOwner(java.nio.file.Path,java.nio.file.LinkOption...)
49
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html
510

+stdlib/+python/get_owner.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
function n = get_owner(p)
22

3+
if ~stdlib.exists(p)
4+
n = string.empty;
5+
return
6+
end
7+
38
try
49
n = string(py.str(py.pathlib.Path(p).owner()));
510
catch e

+stdlib/+sys/get_owner.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
function o = get_owner(p)
22

3+
if ~stdlib.exists(p)
4+
o = string.empty;
5+
return
6+
end
7+
38
if ispc()
49
cmd = "pwsh -c (Get-Acl -Path '" + p + "').Owner";
510
elseif ismac()
@@ -10,7 +15,7 @@
1015

1116
[s, o] = system(cmd);
1217
if s == 0
13-
o = strip(o);
18+
o = string(strip(o));
1419
else
1520
o = string.empty;
1621
end

+stdlib/get_owner.m

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,14 @@
1010
p {mustBeTextScalar}
1111
end
1212

13-
n = string.empty;
14-
15-
if ~ispc() && stdlib.has_python()
16-
n = stdlib.python.get_owner(p);
17-
elseif stdlib.has_java()
13+
if stdlib.has_java()
1814
n = stdlib.java.get_owner(p);
19-
end
20-
21-
if strempty(n)
15+
elseif ispc() && stdlib.has_dotnet()
16+
n = stdlib.dotnet.get_owner(p);
17+
elseif ~ispc() && stdlib.has_python()
18+
n = stdlib.python.get_owner(p);
19+
else
2220
n = stdlib.sys.get_owner(p);
2321
end
2422

25-
try %#ok<*TRYNC>
26-
n = string(n);
27-
end
28-
2923
end
30-
31-
%!assert(!isempty(get_owner(pwd)))

test/TestDisk.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
classdef TestDisk < matlab.unittest.TestCase
22

33
properties
4-
disk_fun = stdlib.has_python() || stdlib.has_dotnet() || stdlib.has_java()
54
CI = getenv("CI") == "true" || getenv("GITHUB_ACTIONS") == "true"
65
end
76

87
properties (TestParameter)
98
Ps = {".", "", "/", getenv("SystemDrive"), "not-exist"}
9+
Po = {mfilename("fullpath") + ".m", pwd(), ".", "", tempname()}
1010
device_fun = {@stdlib.device, @stdlib.sys.device, @stdlib.java.device, @stdlib.python.device}
1111
disk_available_fun = {@stdlib.disk_available, @stdlib.sys.disk_available, @stdlib.dotnet.disk_available, @stdlib.java.disk_available, @stdlib.python.disk_available}
1212
disk_capacity_fun = {@stdlib.disk_capacity, @stdlib.sys.disk_capacity, @stdlib.dotnet.disk_capacity, @stdlib.java.disk_capacity, @stdlib.python.disk_capacity}
13+
owner_fun = {@stdlib.get_owner, @stdlib.sys.get_owner, @stdlib.dotnet.get_owner, @stdlib.java.get_owner, @stdlib.python.get_owner}
1314
end
1415

1516
methods (Test)
@@ -82,12 +83,19 @@ function test_inode(tc)
8283
end
8384

8485

85-
function test_owner(tc)
86+
function test_owner(tc, Po, owner_fun)
87+
is_capable(tc, owner_fun)
8688

87-
s = stdlib.get_owner(mfilename("fullpath") + ".m");
89+
s = owner_fun(Po);
8890

8991
tc.verifyClass(s, 'string')
90-
tc.verifyGreaterThan(strlength(s), 0)
92+
93+
if stdlib.exists(Po)
94+
tc.verifyGreaterThan(strlength(s), 0)
95+
else
96+
tc.verifyEmpty(s)
97+
end
98+
9199
end
92100

93101
end

test/is_capable.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function is_capable(tc, f)
1212

1313
tc.assumeGreaterThan(dapi, 0)
1414

15+
if endsWith(n, ["owner"])
16+
tc.assumeFalse(isunix(), "Windows only function")
17+
end
18+
1519
if endsWith(n, ["create_symlink", "ram_total", "read_symlink"])
1620
tc.assumeGreaterThanOrEqual(dapi, 6);
1721
end
@@ -36,6 +40,10 @@ function is_capable(tc, f)
3640
tc.assumeTrue(has_psutil, "need Python psutil package")
3741
end
3842

43+
if endsWith(n, ["owner"])
44+
tc.assumeFalse(ispc(), "unix only function")
45+
end
46+
3947
end
4048

4149
end

0 commit comments

Comments
 (0)