Skip to content

Commit 5cc8c64

Browse files
committed
get_permissions: streamline code
1 parent 9fdc343 commit 5cc8c64

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

+stdlib/get_permissions.m

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,56 @@
1010
p = '';
1111

1212
try
13-
perms = filePermissions(f);
14-
p = perm2char(perms);
13+
v = filePermissions(f);
1514
catch e
1615
if ~strcmp(e.identifier, "MATLAB:UndefinedFunction") && ...
1716
~strcmp(e.identifier, "Octave:undefined-function")
1817
rethrow(e)
1918
end
2019

21-
[status, v] = fileattrib(f);
22-
if status == 0
23-
return
24-
end
25-
p = perm2char(v);
20+
v = file_attributes(f);
21+
if isempty(v), return, end
2622
end
2723

24+
p = perm2char(v);
25+
2826
end
2927

3028

3129
function p = perm2char(v)
3230

3331
p = '---------';
3432

35-
try
36-
% filePermissions object
33+
if isa(v, "matlab.io.WindowsPermissions") || isa(v, "matlab.io.UnixPermissions")
3734
if v.Readable, p(1) = 'r'; end
3835
if v.Writable, p(2) = 'w'; end
39-
catch e
40-
if ~strcmp(e.identifier, "MATLAB:nonExistentField") && ...
41-
~strcmp(e.identifier, "Octave:invalid-indexing")
42-
rethrow(e)
43-
end
44-
36+
elseif isstruct(v)
4537
if v.UserRead, p(1) = 'r'; end
4638
if v.UserWrite, p(2) = 'w'; end
39+
else
40+
% cloud / remote locations we don't handle
41+
p = [];
42+
return
4743
end
4844

49-
% on Windows, any readable file has executable permission
50-
if ispc
51-
if p(1) == 'r'
52-
p(3) = 'x';
53-
end
54-
else
45+
46+
if isfield(v, 'UserExecute') || isa(v, "matlab.io.UnixPermissions")
5547
if v.UserExecute, p(3) = 'x'; end
48+
elseif ispc && (isstruct(v) || isa(v, "matlab.io.WindowsPermissions"))
49+
% on Windows, any readable file has executable permission
50+
if p(1) == 'r', p(3) = 'x'; end
5651
end
5752

58-
% Windows doesn't have these permissions
5953

60-
try
54+
if isstruct(v) || isa(v, "matlab.io.UnixPermissions")
55+
6156
if v.GroupRead, p(4) = 'r'; end
6257
if v.GroupWrite, p(5) = 'w'; end
6358
if v.GroupExecute, p(6) = 'x'; end
6459
if v.OtherRead, p(7) = 'r'; end
6560
if v.OtherWrite, p(8) = 'w'; end
6661
if v.OtherExecute, p(9) = 'x'; end
67-
catch e
68-
if ~strcmp(e.identifier, "MATLAB:nologicalnan") && ...
69-
~strcmp(e.identifier, "MATLAB:nonExistentField") && ...
70-
~strcmp(e.identifier, "MATLAB:noSuchMethodOrField") && ...
71-
~strcmp(e.message, "invalid conversion from NaN to logical")
72-
rethrow(e)
73-
end
62+
7463
end
7564

7665
end

+stdlib/private/file_attributes.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
[status, a] = fileattrib(p);
1313
if status ~= 1
14+
% matlab puts the error message in the struct
1415
a = [];
1516
return
1617
end

0 commit comments

Comments
 (0)