Skip to content

Commit 2133449

Browse files
committed
is_readable: java optional, much faster
1 parent 73c63d8 commit 2133449

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed

+stdlib/+java/is_readable.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function y = is_readable(p)
2+
3+
y = javaObject("java.io.File", p).canRead();
4+
5+
end

+stdlib/private/file_attributes_legacy.m renamed to +stdlib/+native/file_attributes.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function a = file_attributes_legacy(p)
1+
function a = file_attributes(p)
22
arguments
33
p {mustBeTextScalar}
44
end

+stdlib/+native/is_readable.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function y = is_readable(p)
2+
3+
y = false;
4+
5+
if ~stdlib.exists(p), return, end
6+
7+
if ~isMATLABReleaseOlderThan('R2025a')
8+
9+
props = "Readable";
10+
if isunix
11+
props = [props, "GroupRead", "OtherRead"];
12+
end
13+
14+
t = getPermissions(filePermissions(p), props);
15+
y = t.Readable;
16+
17+
else
18+
19+
a = stdlib.native.file_attributes(p);
20+
y = a.UserRead || a.GroupRead || a.OtherRead;
21+
22+
end
23+
24+
end

+stdlib/get_permissions.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121
return
2222
else
23-
v = file_attributes_legacy(f);
23+
v = stdlib.native.file_attributes(f);
2424
end
2525

2626
p = perm2char(v, f);

+stdlib/is_exe.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
else
3333

34-
a = file_attributes_legacy(p);
34+
a = stdlib.native.file_attributes(p);
3535
ok = a.UserExecute || a.GroupExecute || a.OtherExecute;
3636

3737
end

+stdlib/is_readable.m

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,16 @@
55
%% Outputs
66
% ok: logical array of the same size as p, true if file is readable
77

8-
function ok = is_readable(p)
8+
function y = is_readable(p)
99
arguments
10-
p string
10+
p {mustBeTextScalar}
1111
end
1212

13-
ok(size(p)) = false;
14-
15-
i = stdlib.exists(p);
16-
17-
if ~any(i), return, end
18-
19-
if ~isMATLABReleaseOlderThan('R2025a')
20-
21-
props = "Readable";
22-
if isunix
23-
props = [props, "GroupRead", "OtherRead"];
24-
end
25-
26-
t = getPermissions(filePermissions(p(i)), props);
27-
ok(i) = any(t{:,:}, 2);
28-
13+
if stdlib.has_java()
14+
y = stdlib.java.is_readable(p);
2915
else
30-
31-
a = file_attributes_legacy(p);
32-
ok = a.UserRead || a.GroupRead || a.OtherRead;
33-
16+
y = stdlib.native.is_readable(p);
3417
end
3518

19+
3620
end

+stdlib/is_writable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
ok(i) = any(t{:,:}, 2);
2828

2929
else
30-
a = file_attributes_legacy(p);
30+
a = stdlib.native.file_attributes(p);
3131
ok = a.UserWrite || a.GroupWrite || a.OtherWrite;
3232
end
3333

test/TestExists.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{tempname(), false}
99
}
1010
% on CI matlabroot can be writable!
11+
isr_fun = {@stdlib.is_readable, @stdlib.java.is_readable, @stdlib.native.is_readable}
1112
end
1213

1314
methods(TestClassSetup)
@@ -25,8 +26,10 @@ function test_exists(tc, Ps)
2526
end
2627

2728

28-
function test_is_readable(tc, Ps)
29-
ok = stdlib.is_readable(Ps{1});
29+
function test_is_readable(tc, Ps, isr_fun)
30+
is_capable(tc, isr_fun)
31+
32+
ok = isr_fun(Ps{1});
3033
tc.verifyEqual(ok, Ps{2}, Ps{1})
3134
end
3235

0 commit comments

Comments
 (0)