Skip to content

Commit 1be1bba

Browse files
committed
add is_symlink, create_symlink, read_symlink. Improve test coverage
1 parent a02a55c commit 1be1bba

30 files changed

+384
-112
lines changed

+stdlib/+fileio/canonical.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
p (1,1) string
55
end
66

7+
import java.io.File
8+
79
% have to expand ~ first (like C++ filesystem::path::absolute)
810
c = stdlib.fileio.expanduser(p);
911

@@ -28,6 +30,6 @@
2830
% REQUIRES path to exist, while java method does not.
2931
% c = builtin('_canonicalizepath', c);
3032

31-
c = stdlib.fileio.posix(string(java.io.File(c).getCanonicalPath()));
33+
c = stdlib.fileio.posix(File(c).getCanonicalPath());
3234

3335
end % function

+stdlib/+fileio/create_symlink.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function ok = create_symlink(target, link)
2+
%% create_symlink create symbolic link
3+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#createSymbolicLink(java.nio.file.Path,java.nio.file.Path,java.nio.file.attribute.FileAttribute...)
4+
5+
arguments
6+
target (1,1) string
7+
link (1,1) string
8+
end
9+
10+
% import java.io.File
11+
% import java.nio.file.Files
12+
% ok = Files.createSymbolicLink(File(link).toPath(), File(target).toPath());
13+
% the above should work, but Matlab Java doesn't recognize the optional argument omitted.
14+
15+
if stdlib.fileio.exists(link)
16+
warning("link %s already exists", link)
17+
ok = false;
18+
return
19+
end
20+
21+
if ispc
22+
cmd = "pwsh -c " + '"' + "New-Item -ItemType SymbolicLink -Path " + link + " -Target " + target + '"';
23+
else
24+
cmd = "ln -s " + target + " " + link;
25+
end
26+
27+
% suppress output text on powershell
28+
[stat, ~] = system(cmd);
29+
30+
ok = stat == 0;
31+
32+
end

+stdlib/+fileio/is_absolute_path.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
apath (1,1) string
55
end
66

7+
import java.io.File
8+
79
% expanduser() here to work like C++ filesystem::path::is_absolute()
8-
isabs = java.io.File(stdlib.fileio.expanduser(apath)).isAbsolute();
10+
isabs = File(stdlib.fileio.expanduser(apath)).isAbsolute();
911

1012
end

+stdlib/+fileio/is_readable.m

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

9+
import java.io.File
10+
import java.nio.file.Files
911

10-
ok = java.nio.file.Files.isReadable(java.io.File(stdlib.fileio.absolute_path(file)).toPath());
12+
13+
ok = Files.isReadable(File(stdlib.fileio.absolute_path(file)).toPath());
1114

1215
end

+stdlib/+fileio/is_symlink.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function ok = is_symlink(p)
2+
%% is_symlink is path symbolic link
3+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSymbolicLink(java.nio.file.Path)
4+
5+
arguments
6+
p (1,1) string
7+
end
8+
9+
import java.io.File
10+
import java.nio.file.Files
11+
12+
% must be absolute path
13+
f = File(stdlib.fileio.absolute_path(p)).toPath();
14+
ok = Files.isSymbolicLink(f);
15+
16+
end

+stdlib/+fileio/is_writable.m

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

9-
ok = java.nio.file.Files.isWritable(java.io.File(stdlib.fileio.absolute_path(file)).toPath());
9+
import java.io.File
10+
import java.nio.file.Files
11+
12+
13+
ok = Files.isWritable(File(stdlib.fileio.absolute_path(file)).toPath());
1014

1115
end

+stdlib/+fileio/normalize.m

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

8-
n = stdlib.fileio.posix(java.io.File(stdlib.fileio.expanduser(p)).toPath().normalize());
8+
import java.io.File
9+
import stdlib.fileio.expanduser
10+
11+
n = stdlib.fileio.posix(File(expanduser(p)).toPath().normalize());
912

1013
end

+stdlib/+fileio/read_symlink.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function r = read_symlink(p)
2+
%% read_symlink read symbolic link
3+
arguments
4+
p (1,1) string
5+
end
6+
7+
import java.io.File
8+
import java.nio.file.Files
9+
10+
r = stdlib.fileio.absolute_path(p);
11+
if ~stdlib.fileio.exists(r)
12+
warning("%s does not exist", r)
13+
r = string.empty;
14+
return
15+
end
16+
if ~stdlib.fileio.is_symlink(r)
17+
warning("%s is not a symlink", r)
18+
r = string.empty;
19+
return
20+
end
21+
22+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
23+
% must be absolute path
24+
r = Files.readSymbolicLink(File(r).toPath());
25+
26+
end

+stdlib/+fileio/resolve.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
p (1,1) string
66
end
77

8+
import java.io.File
9+
810
% have to expand ~ first (like C++ filesystem::path::absolute)
911
c = stdlib.fileio.expanduser(p);
1012

@@ -18,12 +20,8 @@
1820
c = stdlib.fileio.join(pwd, c);
1921
end
2022

21-
% similar benchmark time as java method
22-
% REQUIRES path to exist, while java method does not.
23-
% c = builtin('_canonicalizepath', c);
24-
2523
% % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()
2624

27-
c = stdlib.fileio.posix(string(java.io.File(c).getCanonicalPath()));
25+
c = stdlib.fileio.posix(File(c).getCanonicalPath());
2826

2927
end % function

+stdlib/+fileio/samepath.m

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
path2 (1,1) string
66
end
77

8+
import java.io.File
9+
import java.nio.file.Files
10+
import stdlib.fileio.canonical
11+
12+
issame = false;
813
% java.nio.file.Files needs CANONICAL -- not just absolute path
9-
p1 = java.io.File(stdlib.fileio.canonical(path1)).toPath();
10-
p2 = java.io.File(stdlib.fileio.canonical(path2)).toPath();
14+
p1 = canonical(path1);
15+
if ~stdlib.fileio.exists(p1), return, end
16+
p1 = File(p1).toPath();
1117

12-
try
13-
issame = java.nio.file.Files.isSameFile(p1, p2);
14-
catch e
15-
if e.identifier == "MATLAB:Java:GenericException" && contains(e.message, "NoSuchFileException")
16-
issame = false;
17-
else
18-
rethrow(e)
19-
end
20-
end
18+
p2 = canonical(path2);
19+
if ~stdlib.fileio.exists(p2), return, end
20+
p2 = File(p2).toPath();
21+
22+
issame = Files.isSameFile(p1, p2);
2123

2224
end

0 commit comments

Comments
 (0)