Skip to content

Commit f496011

Browse files
committed
is_{exe,readable,writable} simplify
1 parent 03e5651 commit f496011

7 files changed

Lines changed: 66 additions & 65 deletions

File tree

+stdlib/get_permissions.m

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@
1313
file {mustBeTextScalar}
1414
end
1515

16-
try
16+
if stdlib.matlabOlderThan('R2025a')
17+
if ~stdlib.exists(file)
18+
error('MATLAB:io:filesystem:filePermissions:CannotFindLocation', '%s', file)
19+
end
20+
perm = perm2char(file_attributes(file));
21+
b = 'legacy';
22+
else
1723
perm = perm2char(filePermissions(file));
1824
b = 'native';
19-
catch e
20-
switch e.identifier
21-
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
22-
perm = perm2char(file_attributes(file));
23-
b = 'legacy';
24-
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
25-
perm = '';
26-
b = '';
27-
otherwise
28-
rethrow(e)
29-
end
3025
end
3126

3227
end

+stdlib/is_exe.m

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
if ispc() && ~has_windows_executable_suffix(file)
1717
return
1818
end
19+
if ~stdlib.exists(file)
20+
return
21+
end
1922

20-
21-
try
23+
if stdlib.matlabOlderThan('R2025a')
24+
a = file_attributes(file);
25+
y = ~a.directory && (a.UserExecute || a.GroupExecute || a.OtherExecute);
26+
else
2227
a = filePermissions(file);
2328
if a.Type == matlab.io.FileSystemEntryType.File
2429
if ispc
@@ -27,16 +32,7 @@
2732
y = a.UserExecute || a.GroupExecute || a.OtherExecute;
2833
end
2934
end
30-
catch e
31-
switch e.identifier
32-
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
33-
y = false;
34-
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
35-
a = file_attributes(file);
36-
y = ~isempty(a) && ~a.directory && (a.UserExecute || a.GroupExecute || a.OtherExecute);
37-
otherwise
38-
rethrow(e)
39-
end
35+
4036
end
4137

4238
end

+stdlib/is_readable.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
file {mustBeTextScalar}
1111
end
1212

13-
try
13+
if stdlib.matlabOlderThan('R2025a')
14+
a = file_attributes(file);
15+
if ismissing(a)
16+
y = missing;
17+
else
18+
y = a.UserRead || a.GroupRead || a.OtherRead;
19+
end
20+
else
1421
a = filePermissions(file);
15-
y = a.Readable;
16-
catch e
17-
switch e.identifier
18-
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
19-
y = false;
20-
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
21-
a = file_attributes(file);
22-
y = ~isempty(a) && (a.UserRead || a.GroupRead || a.OtherRead);
23-
otherwise
24-
rethrow(e)
22+
if ismissing(a)
23+
y = missing;
24+
else
25+
y = a.Readable;
2526
end
2627
end
2728

+stdlib/is_writable.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@
1010
file {mustBeTextScalar}
1111
end
1212

13-
try
13+
if stdlib.matlabOlderThan('R2025a')
14+
a = file_attributes(file);
15+
y = a.UserWrite || a.GroupWrite || a.OtherWrite;
16+
else
1417
a = filePermissions(file);
1518
y = a.Writable;
16-
catch e
17-
switch e.identifier
18-
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
19-
y = false;
20-
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
21-
a = file_attributes(file);
22-
y = ~isempty(a) && (a.UserWrite || a.GroupWrite || a.OtherWrite);
23-
otherwise
24-
rethrow(e)
25-
end
2619
end
2720

2821
end

+stdlib/private/file_attributes.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[s, r] = fileattrib(file);
44

55
if s ~= 1
6-
r = struct([]);
6+
r = missing;
77
return
88
end
99

test/TestExists.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function test_exists(tc, Ps)
2424
tc.verifyEqual(r, Ps{2})
2525
end
2626

27+
function test_not_exists(tc)
28+
tc.verifyFalse(stdlib.exists(''))
29+
tc.verifyFalse(stdlib.exists(tempname()))
30+
end
2731

2832
function test_is_readable(tc, Ps)
2933
r = stdlib.is_readable(Ps{1});
@@ -33,13 +37,25 @@ function test_is_readable(tc, Ps)
3337
tc.verifyEqual(r, Ps{2});
3438
end
3539

40+
function test_is_readable_not_exist(tc)
41+
e = 'MATLAB:io:filesystem:filePermissions:CannotFindLocation';
42+
tc.verifyError(@() stdlib.is_readable(''), e)
43+
tc.verifyError(@() stdlib.is_readable('not-here'), e)
44+
end
45+
3646
function test_is_writable(tc, Ps)
3747
r = stdlib.is_writable(Ps{1});
3848
tc.verifyEqual(r, Ps{2})
3949

4050
r = stdlib.is_writable(string(Ps{1}));
4151
tc.verifyEqual(r, Ps{2});
4252
end
53+
54+
function test_is_writable_not_exist(tc)
55+
e = 'MATLAB:io:filesystem:filePermissions:CannotFindLocation';
56+
tc.verifyError(@() stdlib.is_writable(''), e)
57+
tc.verifyError(@() stdlib.is_writable('not-here'), e)
58+
end
4359
end
4460

4561

@@ -68,11 +84,7 @@ function test_is_char_device(tc, B_is_char_device)
6884
function Ps = init_val()
6985

7086

71-
Ps = {
72-
{pwd(), true}, ...
73-
{tempname(), false}, ...
74-
{'', false}
75-
};
87+
Ps = {{pwd(), true}};
7688

7789
o = mfilename('fullpath');
7890
if ~isempty(o)

test/TestPermissions.m

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
end
77

88
properties (TestParameter)
9-
Ps = {'.', pwd(), '', tempname(), 'perm.txt'}
9+
Ps = {'.', pwd(), 'perm.txt'}
10+
Pe = {'', tempname()}
1011
end
1112

1213

@@ -20,25 +21,28 @@ function w_dirs(tc)
2021

2122
methods (Test, TestTags={'R2016a'})
2223

24+
function test_not_exist(tc, Pe)
25+
26+
e = 'MATLAB:io:filesystem:filePermissions:CannotFindLocation';
27+
tc.verifyError(@() stdlib.get_permissions(Pe), e)
28+
29+
end
30+
2331
function test_get_permissions(tc, Ps)
2432
import matlab.unittest.constraints.StartsWithSubstring
2533

2634
[p, b] = stdlib.get_permissions(Ps);
2735
tc.verifyClass(p, 'char')
2836

29-
if ~stdlib.exists(Ps)
30-
tc.verifyEmpty(p)
37+
if stdlib.matlabOlderThan('R2025a')
38+
tc.assertEqual(b, 'legacy')
3139
else
32-
if stdlib.matlabOlderThan('R2025a')
33-
tc.assertEqual(b, 'legacy')
34-
else
35-
tc.assertEqual(b, 'native')
36-
end
37-
38-
tc.verifyThat(p, StartsWithSubstring('r'))
39-
if ~ispc() && strcmp(Ps, tc.file)
40-
tc.verifyEqual(p(3), '-')
41-
end
40+
tc.assertEqual(b, 'native')
41+
end
42+
43+
tc.verifyThat(p, StartsWithSubstring('r'))
44+
if ~ispc() && strcmp(Ps, tc.file)
45+
tc.verifyEqual(p(3), '-')
4246
end
4347
end
4448

0 commit comments

Comments
 (0)