Skip to content

Commit b3a8f16

Browse files
committed
owner: handle scalar or arrays
1 parent 5154d4c commit b3a8f16

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

+stdlib/+dotnet/get_owner.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
elseif isfile(p)
1616
fsec = System.IO.File.GetAccessControl(p);
1717
else
18-
o = '';
18+
o = string.empty;
1919
return
2020
end
2121

2222
owner = fsec.GetOwner(ntAccountType);
2323

24-
o = char(owner.ToString());
24+
o = string(owner.ToString());
2525

2626
end

+stdlib/+java/get_owner.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55
% 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...)
66
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html
77

8-
n = '';
9-
if ~stdlib.exists(p)
10-
return
11-
end
12-
13-
14-
try
8+
if stdlib.exists(p)
159
opt = javaMethod("values", "java.nio.file.LinkOption");
16-
17-
n = char(javaMethod("getOwner", "java.nio.file.Files", javaPathObject(p), opt));
18-
catch e
19-
warning(e.identifier, "get_owner(%s) failed: %s", p, e.message)
10+
n = string(java.nio.file.Files.getOwner(javaPathObject(p), opt));
11+
else
12+
n = "";
2013
end
2114

2215
end

+stdlib/+python/get_owner.m

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

3-
n = '';
4-
if ~stdlib.exists(p)
5-
return
6-
end
7-
8-
try
9-
n = char(py.str(py.pathlib.Path(p).owner()));
10-
catch e
11-
warning(e.identifier, "get_owner(%s) failed: %s", p, e.message)
3+
if stdlib.exists(p)
4+
n = string(py.str(py.pathlib.Path(p).owner()));
5+
else
6+
n = "";
127
end
138

149
end

+stdlib/+sys/get_owner.m

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

3-
o = '';
3+
o = "";
44
if ~stdlib.exists(p)
55
return
66
end
@@ -15,7 +15,7 @@
1515

1616
[s, m] = system(cmd);
1717
if s == 0
18-
o = strip(m);
18+
o = string(strip(m));
1919
end
2020

2121
end

+stdlib/get_modtime.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
%% GET_MODTIME get path modification time
22
%
33
%%% Inputs
4-
% * p: path to examine
4+
% * file: path to examine
55
%%% Outputs
66
% * t: modification time, or empty if path does not exist
77

8-
function t = get_modtime(p)
8+
function t = get_modtime(file)
99
arguments
10-
p {mustBeTextScalar}
10+
file
1111
end
1212

13-
finf = dir(p);
14-
if isfolder(p)
13+
finf = dir(file);
14+
if isfolder(file)
1515
% find which index of the struct array has member name == '.'
1616
i = find(strcmp({finf.name}, '.'), 1);
1717
finf = finf(i);

+stdlib/get_owner.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
%% GET_OWNER owner name of file or directory
22
%
33
%%% Inputs
4-
% * p: path to examine
4+
% * file: path to examine
5+
% * backend: backend to use
56
%%% Outputs
67
% * n: owner, or empty if path does not exist
78
% * b: backend used
89

910
function [n, b] = get_owner(file, backend)
1011
arguments
11-
file {mustBeTextScalar}
12+
file string
1213
backend (1,:) string = ["java", "dotnet", "python", "sys"]
1314
end
1415

1516
[fun, b] = hbackend(backend, "get_owner");
1617

17-
n = fun(file);
18+
if isscalar(file)
19+
n = fun(file);
20+
else
21+
n = arrayfun(fun, file);
22+
end
1823

1924
end

test/TestDisk.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ function test_inode_device(tc, java_python_sys, id_name)
141141

142142

143143
function test_owner(tc, Po, all_fun)
144-
tc.assertNotEmpty(which("stdlib." + all_fun + ".get_owner"))
145144

146145
try
147146
o = stdlib.get_owner(Po, all_fun);
@@ -150,12 +149,25 @@ function test_owner(tc, Po, all_fun)
150149
return
151150
end
152151

153-
tc.verifyClass(o, 'char')
152+
tc.verifyClass(o, "string")
154153

155154
if stdlib.exists(Po)
156155
tc.verifyGreaterThan(strlength(o), 0)
157156
else
158-
tc.verifyEmpty(o)
157+
tc.verifyEqual(o, "")
158+
end
159+
160+
end
161+
162+
163+
function test_owner_array(tc, all_fun)
164+
try
165+
o = stdlib.get_owner([".", pwd(), "not-exist", ""], all_fun);
166+
L = strlength(o);
167+
tc.verifyEqual(L(3:4), [0, 0])
168+
tc.verifyGreaterThan(strlength(o(1:2)), 0);
169+
catch e
170+
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
159171
end
160172

161173
end

0 commit comments

Comments
 (0)