Skip to content

Commit 5a2ae56

Browse files
committed
distinguish absolute() and resolve()
1 parent 5173fff commit 5a2ae56

File tree

12 files changed

+87
-44
lines changed

12 files changed

+87
-44
lines changed

+stdlib/absolute.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function c = absolute(p, base, expand_tilde)
2+
%% absolute(p)
3+
% path need not exist
4+
% absolute path will be relative to pwd if not exist
5+
%
6+
%%% Inputs
7+
% * p: path to make absolute
8+
% * base: if present, base on this instead of cwd
9+
% * expand_tilde: expand ~ to username if present
10+
%%% Outputs
11+
% * c: resolved path
12+
% does not normalize
13+
% non-existant path is made absolute relative to pwd
14+
arguments
15+
p (1,1) string
16+
base string {mustBeScalarOrEmpty}=string.empty
17+
expand_tilde (1,1) logical=true
18+
end
19+
20+
if expand_tilde
21+
c = stdlib.expanduser(p);
22+
else
23+
c = p;
24+
end
25+
26+
if ~stdlib.is_absolute(c)
27+
% .getAbsolutePath(), .toAbsolutePath()
28+
% default is Documents/Matlab, which is probably not wanted.
29+
if isempty(base) || strlength(base) == 0
30+
c = stdlib.join(pwd, c);
31+
else
32+
c = stdlib.join(stdlib.absolute(base), c);
33+
end
34+
end
35+
36+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
37+
38+
c = stdlib.posix(java.io.File(c).getAbsolutePath());
39+
40+
end % function

+stdlib/canonical.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
%
1212
%%% Inputs
1313
% * p: path to make canonical
14+
% * expand_tilde: expand ~ to username if present
1415
%%% Outputs
1516
% * c: canonical path, if determined
1617
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()

+stdlib/extract_zstd.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function extract_zstd(archive, out_dir)
99
out_dir (1,1) string {mustBeFolder}
1010
end
1111

12-
archive = stdlib.canonical(archive);
13-
out_dir = stdlib.canonical(out_dir);
12+
archive = stdlib.absolute(archive);
13+
out_dir = stdlib.absolute(out_dir);
1414

1515
exe = stdlib.which("cmake");
1616
if isempty(exe)

+stdlib/is_exe.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
file (1,1) string
77
end
88

9+
% doesn't need absolute path like other Java functions
10+
911
ok = java.io.File(file).canExecute();
1012

1113
% more complicated

+stdlib/is_readable.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
file (1,1) string
77
end
88

9-
ok = java.nio.file.Files.isReadable(java.io.File(stdlib.canonical(file)).toPath());
9+
% needs absolute()
10+
file = stdlib.absolute(file);
11+
12+
ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
1013

1114
end

+stdlib/is_regular_file.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
opt = java.nio.file.LinkOption.values;
77

8-
% not correct without canonical(). Normalize() doesn't help.
9-
p = stdlib.canonical(p);
8+
% needs absolute()
9+
p = stdlib.absolute(p);
1010

1111
isreg = java.nio.file.Files.isRegularFile(java.io.File(p).toPath(), opt);
1212

+stdlib/is_symlink.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
if isMATLABReleaseOlderThan("R2024b")
1010
% must be absolute path
11-
% must be .resolve, NOT .canonical or symlink is gobbled!
12-
p = stdlib.resolve(p);
11+
% NOT .canonical or symlink is gobbled!
12+
p = stdlib.absolute(p);
1313
ok = java.nio.file.Files.isSymbolicLink(java.io.File(p).toPath());
1414
else
1515
ok = isSymbolicLink(p);

+stdlib/is_writable.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
file (1,1) string
88
end
99

10-
ok = java.nio.file.Files.isWritable(java.io.File(stdlib.canonical(file)).toPath());
10+
% needs absolute()
11+
file = stdlib.absolute(file);
12+
13+
ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
1114

1215
end

+stdlib/read_symlink.m

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

1515
% must be absolute path
1616
% must not be .canonical or symlink is gobbled!
17-
r = stdlib.resolve(p);
17+
r = stdlib.absolute(p);
1818

1919
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
2020
r = stdlib.posix(...

+stdlib/resolve.m

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
function c = resolve(p, expand_tilde)
2-
%% resolve(p)
3-
% path need not exist--absolute path will be relative to pwd if not exist
4-
% if path exists, same result as canonical()
5-
%
6-
% NOTE: some network file systems are not resolvable by Matlab Java
7-
% subsystem, but are sometimes still valid--so return
8-
% unmodified path if this occurs.
9-
%
10-
% This also resolves Windows short paths to full long paths.
11-
%
1+
function r = resolve(p, expand_tilde)
2+
% resolve path, to cwd if relative
3+
% effectively canonical(absolute(p))
124
%%% Inputs
13-
% * p: path to resolve
5+
% * p: path to make absolute
6+
% * expand_tilde: expand ~ to username if present
147
%%% Outputs
158
% * c: resolved path
169
% distinct from canonical(), resolve() always returns absolute path
@@ -20,24 +13,6 @@
2013
expand_tilde (1,1) logical=true
2114
end
2215

23-
if expand_tilde
24-
c = stdlib.expanduser(p);
25-
else
26-
c = p;
27-
end
28-
29-
if ispc && startsWith(c, "\\")
30-
% UNC path is not canonicalized
31-
return
32-
end
33-
34-
if ~stdlib.is_absolute(c)
35-
% .toAbsolutePath() default is Documents/Matlab, which is probably not wanted.
36-
c = stdlib.join(pwd, c);
37-
end
38-
39-
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
40-
41-
c = stdlib.posix(java.io.File(java.io.File(c).getAbsolutePath()).toPath().normalize());
16+
r = stdlib.canonical(stdlib.absolute(p, expand_tilde), false);
4217

43-
end % function
18+
end

0 commit comments

Comments
 (0)