Skip to content

Commit 8e711ee

Browse files
committed
get_permissions: simplify, robust
1 parent 9c25abb commit 8e711ee

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

+stdlib/get_permissions.m

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
p = '';
1111

12-
if ~stdlib.isoctave() && ~stdlib.too_old('R2025a')
12+
try
1313
perms = filePermissions(f);
1414
p = perm2char(perms);
15-
% elseif ~ispc && ~isMATLABReleaseOlderThan('R2024b')
16-
% % undocumented internals in Matlab R2024b, does not work on Windows
17-
% perms = matlab.io.UnixPermissions(f);
18-
% p = perm2str(perms);
19-
else
15+
catch e
16+
if ~strcmp(e.identifier, "MATLAB:UndefinedFunction") && ...
17+
~strcmp(e.identifier, "Octave:undefined-function")
18+
rethrow(e)
19+
end
20+
2021
[status, v] = fileattrib(f);
2122
if status == 0
2223
return
@@ -31,28 +32,36 @@
3132

3233
p = '---------';
3334

34-
groupRead = ~isnan(v.GroupRead) && logical(v.GroupRead);
35-
groupWrite = ~isnan(v.GroupWrite) && logical(v.GroupWrite);
36-
groupExecute = ~isnan(v.GroupExecute) && logical(v.GroupExecute);
37-
otherRead = ~isnan(v.OtherRead) && logical(v.OtherRead);
38-
otherWrite = ~isnan(v.OtherWrite) && logical(v.OtherWrite);
39-
otherExecute = ~isnan(v.OtherExecute) && logical(v.OtherExecute);
35+
try
36+
% filePermissions object
37+
if v.Readable, p(1) = 'r'; end
38+
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
4044

41-
if isstruct(v) % from fileattrib
4245
if v.UserRead, p(1) = 'r'; end
4346
if v.UserWrite, p(2) = 'w'; end
44-
else % filePermissions object
45-
if v.Readable, p(1) = 'r'; end
46-
if v.Writable, p(2) = 'w'; end
4747
end
4848

4949
if v.UserExecute, p(3) = 'x'; end
50-
if groupRead, p(4) = 'r'; end
51-
if groupWrite, p(5) = 'w'; end
52-
if groupExecute, p(6) = 'x'; end
53-
if otherRead, p(7) = 'r'; end
54-
if otherWrite, p(8) = 'w'; end
55-
if otherExecute, p(9) = 'x'; end
50+
51+
try
52+
if v.GroupRead, p(4) = 'r'; end
53+
if v.GroupWrite, p(5) = 'w'; end
54+
if v.GroupExecute, p(6) = 'x'; end
55+
if v.OtherRead, p(7) = 'r'; end
56+
if v.OtherWrite, p(8) = 'w'; end
57+
if v.OtherExecute, p(9) = 'x'; end
58+
catch e
59+
if ~strcmp(e.identifier, "MATLAB:nologicalnan") && ...
60+
~strcmp(e.identifier, "MATLAB:nonExistentField") && ...
61+
~strcmp(e.message, "invalid conversion from NaN to logical")
62+
rethrow(e)
63+
end
64+
end
5665

5766
end
5867

example/Filesystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static Boolean is_absolute(String path) {
112112

113113
public static Boolean is_exe(String path) {
114114
File f = new File(path);
115-
return f.isFile() && f.canExecute();
115+
return f.canExecute();
116116
}
117117

118118
public static Boolean is_readable(String path) {

0 commit comments

Comments
 (0)