Skip to content

Commit 047daac

Browse files
committed
get_permissions, simplify, reuse code, more robust
1 parent b95404e commit 047daac

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

+stdlib/get_permissions.m

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,31 @@
99

1010
p = '';
1111

12-
try
12+
if ~stdlib.exists(f), return, end
13+
14+
if ~isMATLABReleaseOlderThan('R2025a')
1315
v = filePermissions(f);
14-
catch e
15-
switch e.identifier
16-
case "MATLAB:UndefinedFunction", v = file_attributes_legacy(f);
17-
case "Octave:undefined-function"
18-
[s, err] = stat(f);
19-
if err == 0
20-
p = s.modestr;
21-
end
22-
return
23-
otherwise, rethrow(e)
16+
elseif stdlib.isoctave()
17+
[s, err] = stat(f);
18+
if err == 0
19+
p = s.modestr;
2420
end
21+
return
22+
else
23+
v = file_attributes_legacy(f);
2524
end
2625

27-
p = perm2char(v);
26+
p = perm2char(v, f);
2827

2928
end
3029

3130

32-
function p = perm2char(v)
31+
function p = perm2char(v, f)
32+
arguments
33+
v (1,1)
34+
f {mustBeTextScalar}
35+
end
36+
3337

3438
p = '---------';
3539

@@ -41,30 +45,31 @@
4145
if v.UserWrite, p(2) = 'w'; end
4246
else
4347
% cloud / remote locations we don't handle
44-
p = [];
48+
p = '';
4549
return
4650
end
4751

52+
if isa(v, "matlab.io.WindowsPermissions") || ispc()
4853

49-
if isfield(v, 'UserExecute') || isa(v, "matlab.io.UnixPermissions")
50-
if v.UserExecute, p(3) = 'x'; end
51-
elseif ispc() && (isstruct(v) || isa(v, "matlab.io.WindowsPermissions"))
52-
% on Windows, any readable file has executable permission
53-
if p(1) == 'r', p(3) = 'x'; end
54-
end
54+
if p(1) == 'r' && has_windows_executable_suffix(f)
55+
p(3) = 'x';
56+
end
5557

58+
return
5659

57-
if isstruct(v) || isa(v, "matlab.io.UnixPermissions")
60+
else
5861

59-
if v.GroupRead, p(4) = 'r'; end
60-
if v.GroupWrite, p(5) = 'w'; end
61-
if v.GroupExecute, p(6) = 'x'; end
62-
if v.OtherRead, p(7) = 'r'; end
63-
if v.OtherWrite, p(8) = 'w'; end
64-
if v.OtherExecute, p(9) = 'x'; end
62+
if v.UserExecute, p(3) = 'x'; end
6563

6664
end
6765

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+
6873
end
6974

7075
%!assert(length(get_permissions('get_permissions.m')) >= 9)

test/TestPermissions.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef TestPermissions < matlab.unittest.TestCase
22

33
properties (TestParameter)
4-
Ps = {".", pwd()}
4+
Ps = {".", pwd(), "", tempname()}
55
end
66

77

@@ -12,9 +12,14 @@ function test_get_permissions(tc, Ps)
1212

1313
p = stdlib.get_permissions(Ps);
1414

15-
tc.verifyThat(p, StartsWithSubstring("r"))
1615
tc.verifyClass(p, "char")
1716

17+
if stdlib.exists(Ps)
18+
tc.verifyThat(p, StartsWithSubstring("r"))
19+
else
20+
tc.verifyEmpty(p)
21+
end
22+
1823
end
1924

2025

0 commit comments

Comments
 (0)