Skip to content

Commit 13ccb2b

Browse files
committed
is_exe: exists check for speed
1 parent 4d63444 commit 13ccb2b

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

+stdlib/exists.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
end
1818

1919
if use_java
20+
% Windows: does NOT detect App Execution Aliases
2021
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#exists(java.nio.file.Path,java.nio.file.LinkOption...)
2122
% this takes 2x longer than java.io.File.exists()
2223
% opt = javaLinkOption();

+stdlib/is_exe.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use_java (1,1) logical = false
1010
end
1111

12+
ok = false;
13+
if ~stdlib.exists(p), return, end
1214

1315
if use_java
1416
% about the same time as fileattrib
@@ -20,11 +22,6 @@
2022

2123
else
2224

23-
if ~stdlib.len(p)
24-
ok = false;
25-
return
26-
end
27-
2825
[status, v] = fileattrib(p);
2926

3027
ok = status ~= 0 && (v.UserExecute || (~isnan(v.GroupExecute) && v.GroupExecute) || (~isnan(v.OtherExecute) && v.OtherExecute));

+stdlib/is_readable.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
%
33
% non-existant file is false
44

5-
function ok = is_readable(file, use_java)
5+
function ok = is_readable(p, use_java)
66
arguments
7-
file (1,1) string
7+
p (1,1) string
88
use_java (1,1) logical = false
99
end
1010

1111
ok = false;
12-
if ~stdlib.exists(file, use_java), return, end
12+
if ~stdlib.exists(p), return, end
1313
% exists() check speeds up by factor of 50x on macOS for Java or non-Java
1414

1515
if use_java
@@ -19,11 +19,11 @@
1919
% file = stdlib.absolute(file, "", false, true);
2020
% ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
2121

22-
ok = javaFileObject(file).canRead();
22+
ok = javaFileObject(p).canRead();
2323

2424
else % use_java
2525

26-
[status, v] = fileattrib(file);
26+
[status, v] = fileattrib(p);
2727

2828
ok = status ~= 0 && (v.UserRead || (~isnan(v.GroupRead) && v.GroupRead) || (~isnan(v.OtherRead) && v.OtherRead));
2929

+stdlib/is_writable.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
%% IS_WRITABLE is path writable
22
%
3-
% non-existant file is false
3+
% non-existant path is false
44

5-
function ok = is_writable(file, use_java)
5+
function ok = is_writable(p, use_java)
66
arguments
7-
file (1,1) string
7+
p (1,1) string
88
use_java (1,1) logical = false
99
end
1010

1111
ok = false;
12-
if ~stdlib.exists(file, use_java), return, end
12+
if ~stdlib.exists(p), return, end
1313
% exists() check speeds up by factor of 50x on macOS for Java or non-Java
1414

1515

@@ -20,11 +20,11 @@
2020
% file = stdlib.absolute(file, "", false, true);
2121
% ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
2222

23-
ok = javaFileObject(file).canWrite();
23+
ok = javaFileObject(p).canWrite();
2424

2525
else
2626

27-
[status, v] = fileattrib(file);
27+
[status, v] = fileattrib(p);
2828

2929
ok = status ~= 0 && (v.UserWrite || (~isnan(v.GroupWrite) && v.GroupWrite) || (~isnan(v.OtherWrite) && v.OtherWrite));
3030

+stdlib/samepath.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
% simpler our way than
2323
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
2424

25-
issame = stdlib.exists(path1, false) && stdlib.exists(path2, false) && ...
25+
issame = stdlib.exists(path1) && stdlib.exists(path2) && ...
2626
stdlib.canonical(path1, false, false) == stdlib.canonical(path2, false, false);
2727

2828
end

0 commit comments

Comments
 (0)