Skip to content

Commit 6094406

Browse files
committed
is_exe, is_readable, is_writable: generalize
For R2025a+, can be string arrays
1 parent e12e5f4 commit 6094406

File tree

5 files changed

+34
-56
lines changed

5 files changed

+34
-56
lines changed

+stdlib/is_exe.m

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44

55
function ok = is_exe(p)
66
arguments
7-
p {mustBeTextScalar}
7+
p string
88
end
9+
% need to have string array type for p(:)
910

10-
if ~isfile(p)
11-
ok = false;
12-
return
11+
if ~stdlib.isoctave() && ~isMATLABReleaseOlderThan('R2025a')
12+
if isunix
13+
props = ["UserExecute", "GroupExecute", "OtherExecute"];
14+
else
15+
props = "Readable";
16+
end
17+
t = getPermissions(filePermissions(p), props);
18+
ok = isfile(p(:)) & any(t{:,:}, 2);
19+
else
20+
a = file_attributes_legacy(p);
21+
ok = isfile(p) && (a.UserExecute || a.GroupExecute || a.OtherExecute);
1322
end
1423

15-
try
16-
a = filePermissions(p);
17-
ok = a.UserExecute || (isa(a, "matlab.io.UnixPermissions") && (a.GroupExecute || a.OtherExecute));
18-
catch e
19-
switch e.identifier
20-
case {'Octave:undefined-function', 'MATLAB:UndefinedFunction'}
21-
a = file_attributes_legacy(p);
22-
ok = ~isempty(a) && (a.UserExecute || a.GroupExecute || a.OtherExecute);
23-
otherwise, rethrow(e)
24-
end
2524
end
2625

2726
%!assert (!is_exe("."))

+stdlib/is_readable.m

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
%% IS_READABLE is file readable
22

33
function ok = is_readable(p)
4-
arguments
5-
p {mustBeTextScalar}
6-
end
74

8-
try
9-
a = filePermissions(p);
10-
ok = a.Readable || (isa(a, "matlab.io.UnixPermissions") && (a.GroupRead || a.OtherRead));
11-
catch e
12-
switch e.identifier
13-
case {'Octave:undefined-function', 'MATLAB:UndefinedFunction'}
14-
a = file_attributes_legacy(p);
15-
ok = ~isempty(a) && (a.UserRead || a.GroupRead || a.OtherRead);
16-
otherwise, rethrow(e)
5+
if ~stdlib.isoctave() && ~isMATLABReleaseOlderThan('R2025a')
6+
props = "Readable";
7+
if isunix
8+
props = [props, "GroupRead", "OtherRead"];
179
end
10+
t = getPermissions(filePermissions(p), props);
11+
ok = any(t{:,:}, 2);
12+
else
13+
a = file_attributes_legacy(p);
14+
ok = a.UserRead || a.GroupRead || a.OtherRead;
1815
end
1916

2017
%!assert (is_readable('is_readable.m'))

+stdlib/is_writable.m

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
%% IS_WRITABLE is path writable
22

33
function ok = is_writable(p)
4-
arguments
5-
p {mustBeTextScalar}
6-
end
74

8-
try
9-
a = filePermissions(p);
10-
ok = a.Writable || (isa(a, "matlab.io.UnixPermissions") && (a.GroupWrite || a.OtherWrite));
11-
catch e
12-
switch e.identifier
13-
case {'Octave:undefined-function', 'MATLAB:UndefinedFunction'}
14-
a = file_attributes_legacy(p);
15-
ok = ~isempty(a) && (a.UserWrite || a.GroupWrite || a.OtherWrite);
16-
otherwise, rethrow(e)
5+
if ~stdlib.isoctave() && ~isMATLABReleaseOlderThan('R2025a')
6+
props = "Writable";
7+
if isunix
8+
props = [props, "GroupWrite", "OtherWrite"];
179
end
10+
t = getPermissions(filePermissions(p), props);
11+
ok = any(t{:,:}, 2);
12+
else
13+
a = file_attributes_legacy(p);
14+
ok = a.UserWrite || a.GroupWrite || a.OtherWrite;
1815
end
1916

2017
end

+stdlib/private/file_attributes_legacy.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function a = file_attributes_legacy(p)
2+
arguments
3+
p {mustBeTextScalar}
4+
end
25

36
assert(~strempty(p), 'Path must not be empty.')
47

+stdlib/private/strlength.m

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)