Skip to content

Commit 5e74566

Browse files
committed
get_permissions: choose_method
1 parent 6519391 commit 5e74566

File tree

9 files changed

+84
-76
lines changed

9 files changed

+84
-76
lines changed

+stdlib/+legacy/get_permissions.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function p = get_permissions(file)
2+
3+
p = '';
4+
5+
if ~stdlib.exists(file), return, end
6+
7+
p = stdlib.native.perm2char(filePermissions(file), file);
8+
9+
end

+stdlib/+legacy/is_exe.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
if ~y, return, end
1010

11-
a = stdlib.native.file_attributes(file);
11+
a = file_attributes(file);
1212
y = a.UserExecute || a.GroupExecute || a.OtherExecute;
1313

1414
end

+stdlib/+legacy/is_readable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function y = is_readable(file)
22

33
if stdlib.exists(file)
4-
a = stdlib.native.file_attributes(file);
4+
a = file_attributes(file);
55
y = a.UserRead || a.GroupRead || a.OtherRead;
66
else
77
y = false;

+stdlib/+legacy/is_writable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function y = is_writable(file)
22

33
if stdlib.exists(file)
4-
a = stdlib.native.file_attributes(file);
4+
a = file_attributes(file);
55
y = a.UserWrite || a.GroupWrite || a.OtherWrite;
66
else
77
y = false;
File renamed without changes.

+stdlib/+native/get_permissions.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function p = get_permissions(file)
2+
3+
p = '';
4+
5+
if ~stdlib.exists(file), return, end
6+
7+
v = filePermissions(file);
8+
9+
p = stdlib.native.perm2char(v, file);
10+
11+
end

+stdlib/+native/perm2char.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%% NATIVE.PERM2CHAR convert file permissions to permission string
2+
3+
function p = perm2char(v, file)
4+
arguments
5+
v (1,1)
6+
file {mustBeTextScalar}
7+
end
8+
9+
10+
p = '---------';
11+
12+
if isa(v, "matlab.io.WindowsPermissions") || isa(v, "matlab.io.UnixPermissions")
13+
if v.Readable, p(1) = 'r'; end
14+
if v.Writable, p(2) = 'w'; end
15+
elseif isstruct(v)
16+
if v.UserRead, p(1) = 'r'; end
17+
if v.UserWrite, p(2) = 'w'; end
18+
else
19+
% cloud / remote locations we don't handle
20+
p = '';
21+
return
22+
end
23+
24+
if isa(v, "matlab.io.WindowsPermissions") || ispc()
25+
26+
if p(1) == 'r' && stdlib.native.has_windows_executable_suffix(file)
27+
p(3) = 'x';
28+
end
29+
30+
return
31+
32+
else
33+
34+
if v.UserExecute, p(3) = 'x'; end
35+
36+
end
37+
38+
if v.GroupRead, p(4) = 'r'; end
39+
if v.GroupWrite, p(5) = 'w'; end
40+
if v.GroupExecute, p(6) = 'x'; end
41+
if v.OtherRead, p(7) = 'r'; end
42+
if v.OtherWrite, p(8) = 'w'; end
43+
if v.OtherExecute, p(9) = 'x'; end
44+
45+
end

+stdlib/get_permissions.m

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,14 @@
22
%
33
% output is char like 'rwxrwxr--'
44

5-
function p = get_permissions(f)
5+
function p = get_permissions(file, method)
66
arguments
7-
f {mustBeTextScalar}
7+
file {mustBeTextScalar}
8+
method (1,:) string = ["native", "legacy"]
89
end
910

10-
p = '';
11+
fun = choose_method(method, "get_permissions", 'R2025a');
1112

12-
if ~stdlib.exists(f), return, end
13+
p = fun(file);
1314

14-
if ~isMATLABReleaseOlderThan('R2025a')
15-
v = filePermissions(f);
16-
elseif stdlib.isoctave()
17-
[s, err] = stat(f);
18-
if err == 0
19-
p = s.modestr;
20-
end
21-
return
22-
else
23-
v = stdlib.native.file_attributes(f);
2415
end
25-
26-
p = perm2char(v, f);
27-
28-
end
29-
30-
31-
function p = perm2char(v, f)
32-
arguments
33-
v (1,1)
34-
f {mustBeTextScalar}
35-
end
36-
37-
38-
p = '---------';
39-
40-
if isa(v, "matlab.io.WindowsPermissions") || isa(v, "matlab.io.UnixPermissions")
41-
if v.Readable, p(1) = 'r'; end
42-
if v.Writable, p(2) = 'w'; end
43-
elseif isstruct(v)
44-
if v.UserRead, p(1) = 'r'; end
45-
if v.UserWrite, p(2) = 'w'; end
46-
else
47-
% cloud / remote locations we don't handle
48-
p = '';
49-
return
50-
end
51-
52-
if isa(v, "matlab.io.WindowsPermissions") || ispc()
53-
54-
if p(1) == 'r' && stdlib.native.has_windows_executable_suffix(f)
55-
p(3) = 'x';
56-
end
57-
58-
return
59-
60-
else
61-
62-
if v.UserExecute, p(3) = 'x'; end
63-
64-
end
65-
66-
if v.GroupRead, p(4) = 'r'; end
67-
if v.GroupWrite, p(5) = 'w'; end
68-
if v.GroupExecute, p(6) = 'x'; end
69-
if v.OtherRead, p(7) = 'r'; end
70-
if v.OtherWrite, p(8) = 'w'; end
71-
if v.OtherExecute, p(9) = 'x'; end
72-
73-
end
74-
75-
%!assert(length(get_permissions('get_permissions.m')) >= 9)

test/TestPermissions.m

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

33
properties (TestParameter)
44
Ps = {".", pwd(), "", tempname(), mfilename('fullpath') + ".m"}
5-
sp_fun = {'native', 'legacy'}
5+
fname = {'native', 'legacy'}
66
end
77

88
methods(TestClassSetup)
@@ -14,11 +14,15 @@ function pkg_path(tc)
1414

1515
methods (Test, TestTags="impure")
1616

17-
function test_get_permissions(tc, Ps)
17+
function test_get_permissions(tc, Ps, fname)
1818
import matlab.unittest.constraints.StartsWithSubstring
1919

20-
p = stdlib.get_permissions(Ps);
21-
20+
try
21+
p = stdlib.get_permissions(Ps, fname);
22+
catch e
23+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
24+
return
25+
end
2226
tc.verifyClass(p, "char")
2327

2428
if stdlib.exists(Ps)
@@ -54,7 +58,7 @@ function test_set_permissions_noread(tc)
5458
end
5559

5660

57-
function test_set_permissions_nowrite(tc, sp_fun)
61+
function test_set_permissions_nowrite(tc, fname)
5862
import matlab.unittest.constraints.StartsWithSubstring
5963

6064
tc.assumeFalse(isMATLABReleaseOlderThan('R2022a'))
@@ -64,15 +68,14 @@ function test_set_permissions_nowrite(tc, sp_fun)
6468

6569
tc.verifyTrue(stdlib.touch(nw))
6670
try
67-
tc.verifyTrue(stdlib.set_permissions(nw, 0, -1, 0, sp_fun))
71+
tc.verifyTrue(stdlib.set_permissions(nw, 0, -1, 0, fname))
6872
catch e
6973
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
7074
return
7175
end
7276

73-
7477
p = stdlib.get_permissions(nw);
75-
if ~ispc() || sp_fun ~= "legacy"
78+
if ~ispc() || fname ~= "legacy"
7679
tc.verifyThat(p, StartsWithSubstring("r-"), "no-write permission failed to set")
7780
end
7881

0 commit comments

Comments
 (0)