Skip to content

Commit 6d7899c

Browse files
committed
get_owner: simplify, str2func
1 parent 9c03f88 commit 6d7899c

6 files changed

Lines changed: 25 additions & 43 deletions

File tree

+stdlib/+dotnet/get_owner.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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-
try
8+
if ispc() && stdlib.has_dotnet()
99
ntAccountType = System.Type.GetType('System.Security.Principal.NTAccount');
1010

1111
if isfolder(file)
@@ -20,8 +20,8 @@
2020
owner = fsec.GetOwner(ntAccountType);
2121

2222
o = char(owner.ToString());
23-
catch e
24-
o = dotnetException(e);
23+
else
24+
o = missing;
2525
end
2626

2727
end

+stdlib/+java/get_owner.m

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
%% JAVA.GET_OWNER get owner of file
22

3-
43
function n = get_owner(file)
54

65
% 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...)
76
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html
87

9-
n = missing;
10-
if stdlib.strempty(file)
11-
return
12-
end
13-
148
% Java 1.8 benefits from absolute.
159
% We only saw this issue with R2025a on windows-2025 GA runner image.
1610
%
1711
% if stdlib.exists() was not adequate here, as on some CI systems, despite the same setup on a laptop working.
1812
% stdlib.exists() was true, the Java function threw java.nio.file.NoSuchFileException.
1913
%
2014
% this try-catch is faster and more robust
21-
try
15+
if stdlib.has_java()
2216
p = javaAbsolutePath(file);
2317
opt = javaMethod('values', 'java.nio.file.LinkOption');
2418
n = char(javaMethod('getOwner', 'java.nio.file.Files', p, opt));
25-
catch e
26-
javaException(e);
19+
else
20+
n = missing;
2721
end
2822

2923
end

+stdlib/+python/get_owner.m

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
function n = get_owner(file)
22

3-
n = missing;
4-
5-
if stdlib.strempty(file)
6-
return
7-
end
8-
9-
try
3+
if stdlib.has_python()
104
n = char(py.pathlib.Path(file).owner());
11-
catch e
12-
pythonException(e);
5+
else
6+
n = missing;
137
end
148

159
end

+stdlib/+shell/get_owner.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
end
1212

1313
% Windows needs exists() rather than just ~strempty()
14-
if stdlib.exists(file)
15-
[s, m] = system(cmd);
16-
if s == 0
17-
o = deblank(m);
18-
end
14+
[s, m] = system(cmd);
15+
if s == 0
16+
o = deblank(m);
1917
end
2018

2119
end

+stdlib/get_owner.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,16 @@
99

1010
function [r, b] = get_owner(file, backend)
1111
arguments
12-
file {mustBeTextScalar}
12+
file {mustBeTextScalar,mustBeFileOrFolder}
1313
backend (1,:) string = ["java", "dotnet", "python", "shell"]
1414
end
1515

1616
r = missing;
1717

1818
for b = backend
19-
switch b
20-
case 'java'
21-
r = stdlib.java.get_owner(file);
22-
case 'dotnet'
23-
r = stdlib.dotnet.get_owner(file);
24-
case 'python'
25-
if stdlib.has_python()
26-
r = stdlib.python.get_owner(file);
27-
end
28-
case 'shell'
29-
r = stdlib.shell.get_owner(file);
30-
otherwise
31-
error('stdlib:get_owner:ValueError', 'Unknown backend: %s', b)
32-
end
19+
20+
f = str2func("stdlib." + b + ".get_owner");
21+
r = f(file);
3322

3423
if ~ismissing(r)
3524
return

test/TestDisk.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function test_remove_file(tc)
159159

160160
function test_device(tc, Ps, B_jps)
161161
if ~stdlib.exists(Ps)
162-
tc.verifyError(@() stdlib.device(Ps), 'MATLAB:validators:mustBeFileOrFolder')
162+
tc.verifyError(@() stdlib.device(Ps, B_jps), 'MATLAB:validators:mustBeFileOrFolder')
163163
else
164164
[i, b] = stdlib.device(Ps, B_jps);
165165
if ismember(B_jps, stdlib.Backend().select('device'))
@@ -177,7 +177,7 @@ function test_device(tc, Ps, B_jps)
177177
function test_inode(tc, Ps, B_jps)
178178

179179
if ~stdlib.exists(Ps)
180-
tc.verifyError(@() stdlib.inode(Ps), 'MATLAB:validators:mustBeFileOrFolder')
180+
tc.verifyError(@() stdlib.inode(Ps, B_jps), 'MATLAB:validators:mustBeFileOrFolder')
181181
else
182182
[i, b] = stdlib.inode(Ps, B_jps);
183183
if ismember(B_jps, stdlib.Backend().select('inode'))
@@ -194,6 +194,11 @@ function test_inode(tc, Ps, B_jps)
194194

195195

196196
function test_owner(tc, Ps, B_jdps)
197+
198+
if ~stdlib.exists(Ps)
199+
tc.verifyError(@() stdlib.get_owner(Ps, B_jdps), 'MATLAB:validators:mustBeFileOrFolder')
200+
else
201+
197202
[o, b] = stdlib.get_owner(Ps, B_jdps);
198203
tc.assertMatches(b, B_jdps)
199204

@@ -208,6 +213,8 @@ function test_owner(tc, Ps, B_jdps)
208213
tc.verifyEqual(o, missing)
209214
end
210215

216+
end % stdlib.exists
217+
211218
end
212219

213220

0 commit comments

Comments
 (0)