Skip to content

Commit 684b904

Browse files
committed
dedupe with file_attributes()
1 parent 45df6b0 commit 684b904

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

+stdlib/is_exe.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222

2323
else
2424

25-
[status, v] = fileattrib(p);
25+
a = file_attributes(p);
26+
if isempty(a), return, end
2627

27-
ok = status ~= 0 && (v.UserExecute || (~isnan(v.GroupExecute) && v.GroupExecute) || (~isnan(v.OtherExecute) && v.OtherExecute));
28+
ok = a.UserExecute || a.GroupExecute || a.OtherExecute;
2829

2930
end
3031

31-
end
32-
33-
%!assert (!is_exe(''))
34-
%!assert (!is_exe(tempname))
35-
%!assert (is_exe("."))
36-
%!assert (is_exe(program_invocation_name))
32+
%!assert (!is_exe('', 0))
33+
%!assert (!is_exe(tempname, 0))
34+
%!assert (is_exe(".", 0))
35+
%!assert (is_exe(program_invocation_name, 0))

+stdlib/is_readable.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
if use_java
1616
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)
1717
% java.nio.file.Files is about 10x slower than fileattrib
18-
% needs absolute()
1918
% file = stdlib.absolute(file, "", false, true);
2019
% ok = java.nio.file.Files.isReadable(javaPathObject(file));
2120

2221
ok = javaFileObject(p).canRead();
2322

24-
else % use_java
23+
else
2524

26-
[status, v] = fileattrib(p);
25+
a = file_attributes(p);
26+
if isempty(a), return, end
2727

28-
ok = status ~= 0 && (v.UserRead || (~isnan(v.GroupRead) && v.GroupRead) || (~isnan(v.OtherRead) && v.OtherRead));
29-
30-
end
28+
ok = a.UserRead || v.GroupRead || v.OtherRead;
3129

3230
end
3331

+stdlib/is_symlink.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
%% IS_SYMLINK is path a symbolic link
2-
% on Windows, distinguishes between symlinks and App Execution Aliases,
3-
% which are not symlinks.
42

53
function ok = is_symlink(p)
64
arguments

+stdlib/is_writable.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424

2525
else
2626

27-
[status, v] = fileattrib(p);
27+
a = file_attributes(p);
28+
if isempty(a), return, end
2829

29-
ok = status ~= 0 && (v.UserWrite || (~isnan(v.GroupWrite) && v.GroupWrite) || (~isnan(v.OtherWrite) && v.OtherWrite));
30-
31-
end
30+
ok = a.UserWrite || v.GroupWrite || v.OtherWrite;
3231

3332
end
3433

+stdlib/private/file_attributes.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function a = file_attributes(p)
2+
arguments
3+
p (1,1) string
4+
end
5+
6+
a = struct.empty;
7+
8+
if stdlib.len(p) == 0
9+
return
10+
end
11+
12+
[status, a] = fileattrib(p);
13+
if status ~= 1
14+
a = struct.empty;
15+
return
16+
end
17+
18+
for n = {"GroupRead", "GroupWrite", "GroupExecute", "OtherRead", "OtherWrite", "OtherExecute"}
19+
name = n{1};
20+
if ~isfield(a, name) || isnan(a.(name))
21+
a.(name) = false;
22+
end
23+
end
24+
25+
end

0 commit comments

Comments
 (0)