Skip to content

Commit e2c90f8

Browse files
committed
is_{read,write}able: speed
1 parent 146b899 commit e2c90f8

File tree

9 files changed

+63
-110
lines changed

9 files changed

+63
-110
lines changed

+stdlib/+legacy/is_readable.m

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

+stdlib/+legacy/is_writable.m

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

+stdlib/+native/is_readable.m

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

+stdlib/+native/is_writable.m

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

+stdlib/is_readable.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
%%% Inputs
44
% file: single path string
55
%%% Outputs
6-
% ok: true if file is readable
6+
% y: true if file is readable
77
%
8-
% the legacy backend is like 40x faster than native
8+
% this method is like 40x faster than native
99

10-
function [ok, b] = is_readable(file)
10+
function y = is_readable(file)
1111
arguments
12-
file string
12+
file (1,1) string
1313
end
1414

15-
if isscalar(file)
16-
ok = stdlib.legacy.is_readable(file);
17-
b = 'legacy';
15+
a = stdlib.legacy.file_attributes(file);
16+
17+
if isempty(a)
18+
y = false;
1819
else
19-
ok = stdlib.native.is_readable(file);
20-
b = 'native';
20+
y = a.UserRead || a.GroupRead || a.OtherRead;
2121
end
2222

2323
end

+stdlib/is_writable.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
%%% Inputs
44
% * file: path to file or folder
55
%%% Outputs
6-
% * ok: true if file is writable
6+
% * y: true if file is writable
77
%
8-
% the legacy backend is like 40x faster than native
8+
% this method is like 40x faster than native
99

10-
function [ok, b] = is_writable(file)
10+
function y = is_writable(file)
1111
arguments
12-
file string
12+
file (1,1) string
1313
end
1414

15-
if isscalar(file)
16-
ok = stdlib.legacy.is_writable(file);
17-
b = 'legacy';
15+
a = stdlib.legacy.file_attributes(file);
16+
17+
if isempty(a)
18+
y = false;
1819
else
19-
ok = stdlib.native.is_writable(file);
20-
b = 'native';
20+
y = a.UserWrite || a.GroupWrite || a.OtherWrite;
2121
end
2222

2323
end

example/+native/is_readable.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function y = is_readable(file)
2+
arguments
3+
file (1,1) string
4+
end
5+
6+
props = "Readable";
7+
if isunix
8+
props = [props, "GroupRead", "OtherRead"];
9+
end
10+
11+
try
12+
y = getPermissions(filePermissions(file), props);
13+
catch e
14+
switch e.identifier
15+
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
16+
y = logical.empty;
17+
otherwise
18+
rethrow(e)
19+
end
20+
end
21+
22+
end

example/+native/is_writable.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function y = is_writable(file)
2+
arguments
3+
file (1,1) string
4+
end
5+
6+
props = "Writable";
7+
if isunix
8+
props = [props, "GroupWrite", "OtherWrite"];
9+
end
10+
11+
try
12+
y = getPermissions(filePermissions(file), props);
13+
catch e
14+
switch e.identifier
15+
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
16+
y = logical.empty;
17+
otherwise
18+
rethrow(e)
19+
end
20+
end
21+
22+
23+
end

test/TestExists.m

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,6 @@ function test_is_char_device(tc, B_is_char_device)
5151

5252
end
5353

54-
55-
methods (Test, TestTags={'R2025a'})
56-
57-
function test_is_readable_array(tc)
58-
tc.assumeFalse(isMATLABReleaseOlderThan('R2025a'))
59-
in = [".", tempname(), mfilename('fullpath') + ".m"];
60-
out = [true, false, true];
61-
62-
r = stdlib.is_readable(in);
63-
tc.verifyEqual(r, out)
64-
end
65-
66-
67-
function test_is_writable_array(tc)
68-
tc.assumeFalse(isMATLABReleaseOlderThan('R2025a'))
69-
in = [".", tempname(), mfilename('fullpath') + ".m"];
70-
out = [true, false, true];
71-
72-
r = stdlib.is_writable(in);
73-
tc.verifyEqual(r, out)
74-
end
75-
76-
end
77-
7854
end
7955

8056

0 commit comments

Comments
 (0)