Skip to content

Commit e12e5f4

Browse files
committed
use more modern functions if available
1 parent a1f15b8 commit e12e5f4

File tree

11 files changed

+58
-56
lines changed

11 files changed

+58
-56
lines changed

+stdlib/get_permissions.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
v = filePermissions(f);
1414
catch e
1515
switch e.identifier
16-
case "MATLAB:io:filesystem:filePermissions:CannotFindLocation", return
16+
case "MATLAB:UndefinedFunction", v = file_attributes_legacy(f);
1717
case "Octave:undefined-function"
1818
[s, err] = stat(f);
1919
if err == 0
2020
p = s.modestr;
2121
end
2222
return
23-
case "MATLAB:UndefinedFunction"
24-
v = file_attributes(f);
25-
if isempty(v), return, end
2623
otherwise, rethrow(e)
2724
end
2825
end

+stdlib/is_exe.m

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
%% IS_EXE is file executable
22
%
3-
% false if file does not exist
3+
% false if not a file
44

55
function ok = is_exe(p)
66
arguments
77
p {mustBeTextScalar}
88
end
99

10-
a = file_attributes(p);
11-
12-
ok = ~isempty(a) && (a.UserExecute || a.GroupExecute || a.OtherExecute);
10+
if ~isfile(p)
11+
ok = false;
12+
return
13+
end
1314

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
1425
end
1526

16-
%!assert (!is_exe(''))
17-
%!assert (!is_exe(tempname))
18-
%!assert (is_exe("."))
27+
%!assert (!is_exe("."))
1928
%!assert (is_exe(program_invocation_name))

+stdlib/is_readable.m

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
%% IS_READABLE is file readable
2-
%
3-
% non-existant file is false
42

53
function ok = is_readable(p)
64
arguments
75
p {mustBeTextScalar}
86
end
97

10-
a = file_attributes(p);
11-
12-
ok = ~isempty(a) && (a.UserRead || a.GroupRead || a.OtherRead);
13-
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)
17+
end
1418
end
1519

1620
%!assert (is_readable('is_readable.m'))
17-
%!assert (!is_readable(''))

+stdlib/is_writable.m

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
%% IS_WRITABLE is path writable
2-
%
3-
% non-existant path is false
42

53
function ok = is_writable(p)
64
arguments
75
p {mustBeTextScalar}
86
end
97

10-
a = file_attributes(p);
11-
12-
ok = ~isempty(a) && (a.UserWrite || a.GroupWrite || a.OtherWrite);
13-
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)
17+
end
1418
end
1519

20+
end
1621
%!assert (is_writable('is_writable.m'))
17-
%!assert (!is_writable(''))
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
function a = file_attributes(p)
1+
function a = file_attributes_legacy(p)
22

3-
a = [];
3+
assert(~strempty(p), 'Path must not be empty.')
44

5-
if strempty(p), return, end
5+
[status, s] = fileattrib(p);
66

7-
[status, a] = fileattrib(p);
8-
if status ~= 1
9-
% matlab puts the error message in the struct
10-
a = [];
11-
return
12-
end
7+
assert(status == 1, "'%s' is not a file or directory.", p);
138

9+
a = s;
1410
for n = {"GroupRead", "GroupWrite", "GroupExecute", "OtherRead", "OtherWrite", "OtherExecute"}
1511
name = n{1};
12+
1613
if ~isfield(a, name) || isnan(a.(name))
1714
a.(name) = false;
1815
end
1916
end
20-
2117
end

+stdlib/set_permissions.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,23 @@
99
%%% Outputs
1010
% * ok (1,1) logical
1111

12-
function ok = set_permissions(path, readable, writable, executable)
12+
function set_permissions(path, readable, writable, executable)
1313
arguments
1414
path {mustBeTextScalar,mustBeFile}
1515
readable (1,1) int8
1616
writable (1,1) int8
1717
executable (1,1) int8
1818
end
1919

20-
ok = false;
21-
2220
try
2321
p = filePermissions(path);
2422
catch e
2523
switch e.identifier
2624
case "MATLAB:UndefinedFunction", error("buildtool mex")
27-
case "MATLAB:io:filesystem:filePermissions:CannotFindLocation", return
2825
otherwise, rethrow(e)
2926
end
3027
end
3128

32-
ok = true;
33-
3429
if readable ~= 0
3530
setPermissions(p, "Readable", readable > 0);
3631
end

src/set_permissions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class MexFunction : public matlab::mex::Function {
8686
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html
8787
// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html
8888

89-
outputs[0] = factory.createScalar<bool>(y);
89+
if (!y) {
90+
matlabEng->feval(u"error", 0,
91+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: Failed to set permissions") }));
92+
}
9093
}
9194
};

test/TestExists.m

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

33
properties(TestParameter)
44
Ps = {{pwd(), true}, {mfilename("fullpath") + ".m", true}, ...
5-
{"TestFileImpure.m", true} {tempname(), false}, ...
6-
{"", false}}
5+
{"TestFileImpure.m", true}}
76
% on CI matlabroot can be writable!
87
end
98

test/TestFileImpure.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef TestFileImpure < matlab.unittest.TestCase
22

33
properties(TestParameter)
4-
p_is_writable = {{pwd(), true}, {"not-exists", false}, {"", false}};
4+
p_is_writable = {{pwd(), true}};
55

66
p_same = {...
77
{"..", "./.."}, ...

test/TestIsExe.m

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

33
properties (TestParameter)
4-
p = {{"", false}, {tempname(), false}, {".", true}}
4+
p = {{".", false}}
55
end
66

77
methods(Test, TestTags="impure")
88
function test_is_exe(tc, p)
99
tc.verifyEqual(stdlib.is_exe(p{1}), p{2})
1010
end
1111

12-
function test_matlabroot(tc)
12+
function test_matlab_exe(tc)
1313

1414
f = fullfile(matlabroot, "bin", "matlab");
1515
if ispc()

0 commit comments

Comments
 (0)