Skip to content

Commit a523146

Browse files
committed
add too_old(). get_permissions() outputs char
1 parent ea6d532 commit a523146

File tree

6 files changed

+69
-62
lines changed

6 files changed

+69
-62
lines changed

+stdlib/get_permissions.m

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%% GET_PERMISSIONS permissions of file or directory
22
%
3-
% output is string like "rwxrwxr--"
3+
% output is char like 'rwxrwxr--'
44

55
function p = get_permissions(f)
66
arguments
@@ -9,14 +9,27 @@
99

1010
p = "";
1111

12-
% Get the permissions of a file or directory
13-
[status, v] = fileattrib(f);
14-
if status == 0
15-
return
12+
if ~stdlib.isoctave() && ~stdlib.too_old("R2025a")
13+
perms = filePermissions(f);
14+
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
20+
[status, v] = fileattrib(f);
21+
if status == 0
22+
return
23+
end
24+
p = perm2char(v);
25+
end
26+
1627
end
1728

18-
% Extract the permission string
19-
p = "---------"; % Default permissions
29+
30+
function p = perm2char(v)
31+
32+
p = '---------';
2033

2134
groupRead = ~isnan(v.GroupRead) && logical(v.GroupRead);
2235
groupWrite = ~isnan(v.GroupWrite) && logical(v.GroupWrite);
@@ -25,34 +38,22 @@
2538
otherWrite = ~isnan(v.OtherWrite) && logical(v.OtherWrite);
2639
otherExecute = ~isnan(v.OtherExecute) && logical(v.OtherExecute);
2740

28-
try
29-
30-
if v.UserRead, p = replaceBetween(p, 1, 1, "r"); end
31-
if v.UserWrite, p = replaceBetween(p, 2, 2, "w"); end
32-
if v.UserExecute, p = replaceBetween(p, 3, 3, "x"); end
33-
if groupRead, p = replaceBetween(p, 4, 4, "r"); end
34-
if groupWrite, p = replaceBetween(p, 5, 5, "w"); end
35-
if groupExecute, p = replaceBetween(p, 6, 6, "x"); end
36-
if otherRead, p = replaceBetween(p, 7, 7, "r"); end
37-
if otherWrite, p = replaceBetween(p, 8, 8, "w"); end
38-
if otherExecute, p = replaceBetween(p, 9, 9, "x"); end
39-
40-
catch e
41-
if ~strcmp(e.identifier, "Octave:undefined-function")
42-
rethrow(e)
43-
end
44-
45-
if v.UserRead, p(1) = "r"; end
46-
if v.UserWrite, p(2) = "w"; end
47-
if v.UserExecute, p(3) = "x"; end
48-
if groupRead, p(4) = "r"; end
49-
if groupWrite, p(5) = "w"; end
50-
if groupExecute, p(6) = "x"; end
51-
if otherRead, p(7) = "r"; end
52-
if otherWrite, p(8) = "w"; end
53-
if otherExecute, p(9) = "x"; end
41+
if isstruct(v) % from fileattrib
42+
if v.UserRead, p(1) = "r"; end
43+
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
5447
end
5548

49+
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
56+
5657
end
5758

5859
%!assert(length(get_permissions('get_permissions.m')) == 9)

+stdlib/too_old.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
%% TOO_OLD check if Matlab older than R2020b or as specificed by release "r"
2+
% returns true for any version for any Matlab older than R2020b
3+
% purpose is to avoid verLessThan lint warnings.
4+
%
5+
% example:
6+
% too_old("R2020b")
7+
%
8+
9+
10+
function old = too_old(r)
11+
12+
try
13+
old = isMATLABReleaseOlderThan(r);
14+
catch e
15+
if strcmp(e.identifier, 'MATLAB:UndefinedFunction')
16+
old = true;
17+
else
18+
rethrow(e)
19+
end
20+
end
21+
22+
end

test/TestFileImpure.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ function test_get_pid(tc)
7777

7878
function test_get_permissions(tc)
7979
import matlab.unittest.constraints.StartsWithSubstring
80+
import matlab.unittest.constraints.IsOfClass
81+
8082
p = stdlib.get_permissions(".");
8183
tc.verifyThat(p, StartsWithSubstring("r"))
84+
tc.verifyThat(p, IsOfClass("char"))
8285
end
8386

8487
function test_handle2filename(tc, ph)

test/TestHDF5.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function setup_file(tc)
4444
stdlib.h5save(bf, '/A3', A3, "size", size(A3))
4545
stdlib.h5save(bf, '/A4', A4)
4646

47-
if ~too_old("R2020b")
47+
if ~stdlib.too_old("R2020b")
4848
stdlib.h5save(bf, "/utf", utf)
4949
stdlib.h5save(bf, "/utf2", utf2)
5050
end
@@ -75,7 +75,7 @@ function test_get_variables(tc)
7575
v = stdlib.h5variables(basic);
7676
k = ["A0", "A1", "A2", "A3", "A4"];
7777

78-
if ~too_old("R2020b")
78+
if ~stdlib.too_old("R2020b")
7979
k = [k, "utf", "utf2"];
8080
end
8181

@@ -128,7 +128,7 @@ function test_size(tc)
128128
tc.verifyTrue(isvector(s))
129129
tc.verifyEqual(s, [4,3,2,5])
130130

131-
if ~too_old("R2020b")
131+
if ~stdlib.too_old("R2020b")
132132
s = stdlib.h5size(basic, '/utf');
133133
tc.verifyEmpty(s)
134134

@@ -164,7 +164,7 @@ function test_read(tc)
164164
tc.verifyEqual(ndims(s), 4)
165165
tc.verifyEqual(s, tc.TestData.A4)
166166

167-
if ~too_old("R2020b")
167+
if ~stdlib.too_old("R2020b")
168168
s = h5read(basic, '/utf');
169169
tc.verifyTrue(ischar(s))
170170
tc.verifyEqual(s, tc.TestData.utf)
@@ -196,7 +196,7 @@ function test_coerce(tc, type)
196196
basic = tc.TestData.basic;
197197

198198
if any(type == ["string", "char"])
199-
tc.assumeFalse(too_old("R2020b"))
199+
tc.assumeFalse(stdlib.too_old("R2020b"))
200200
end
201201

202202
stdlib.h5save(basic, "/" + type, 0, "type", type)
@@ -245,7 +245,7 @@ function test_int8(tc)
245245

246246
function test_string(tc, str)
247247

248-
tc.assumeFalse(too_old("R2020b"))
248+
tc.assumeFalse(stdlib.too_old("R2020b"))
249249

250250
basic = tc.TestData.basic;
251251

test/TestNetCDF.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function setup_file(tc)
4141
stdlib.ncsave(basic, 'A3', A3, "dims", {'x3', size(A3,1), 'y3', size(A3,2), 'z3', size(A3,3)})
4242
stdlib.ncsave(basic, 'A4', A4, "dims", {'x4', size(A4,1), 'y4', size(A4,2), 'z4', size(A4,3), 'w4', size(A4,4)})
4343

44-
if ~too_old('R2021b')
44+
if ~stdlib.too_old('R2021b')
4545
stdlib.ncsave(basic, "utf0", utf0)
4646
stdlib.ncsave(basic, "utf1", utf1, "dims", {'s1', size(utf1, 1)})
4747
stdlib.ncsave(basic, "utf2", utf2, "dims", {'s1', size(utf2, 1), 't1', size(utf2, 2)})
@@ -62,7 +62,7 @@ function test_get_variables(tc)
6262

6363
k = ["A0", "A1", "A2", "A3", "A4"];
6464

65-
if ~too_old('R2021b')
65+
if ~stdlib.too_old('R2021b')
6666
k = [k, ["utf0", "utf1", "utf2"]];
6767
end
6868

@@ -122,7 +122,7 @@ function test_size(tc)
122122
function test_size_string(tc)
123123
basic = tc.TestData.basic;
124124

125-
tc.assumeFalse(too_old('R2021b'), "NetCDF4 string requires Matlab >= R2021b")
125+
tc.assumeFalse(stdlib.too_old('R2021b'), "NetCDF4 string requires Matlab >= R2021b")
126126

127127
s = stdlib.ncsize(basic, 'utf0');
128128
tc.verifyEmpty(s)
@@ -164,7 +164,7 @@ function test_read_string(tc)
164164
import matlab.unittest.constraints.IsOfClass
165165
basic = tc.TestData.basic;
166166

167-
tc.assumeFalse(too_old('R2021b'), "NetCDF4 string requires Matlab >= R2021b")
167+
tc.assumeFalse(stdlib.too_old('R2021b'), "NetCDF4 string requires Matlab >= R2021b")
168168

169169
s = ncread(basic, 'utf0');
170170
tc.verifyThat(s, IsOfClass('string'))

test/too_old.m

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

0 commit comments

Comments
 (0)