Skip to content

Commit 1124e50

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

File tree

10 files changed

+51
-48
lines changed

10 files changed

+51
-48
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
catch e
2525
switch e.identifier
2626
case "MATLAB:UndefinedFunction", error("buildtool mex")
27-
case "MATLAB:io:filesystem:filePermissions:CannotFindLocation", return
2827
otherwise, rethrow(e)
2928
end
3029
end

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()

test/TestPermissions.m

Lines changed: 3 additions & 7 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 = {".", tempname(), "", "not-exist"}
4+
Ps = {".", pwd()}
55
end
66

77

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

1313
p = stdlib.get_permissions(Ps);
1414

15-
if stdlib.exists(Ps)
16-
tc.verifyThat(p, StartsWithSubstring("r"))
17-
tc.verifyClass(p, "char")
18-
else
19-
tc.verifyEmpty(p)
20-
end
15+
tc.verifyThat(p, StartsWithSubstring("r"))
16+
tc.verifyClass(p, "char")
2117

2218
end
2319

0 commit comments

Comments
 (0)