Skip to content

Commit 44d3e7e

Browse files
committed
document createSymbolicLink
1 parent c972820 commit 44d3e7e

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

+stdlib/create_symlink.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
% https://www.mathworks.com/help/releases/R2024b/matlab/ref/createsymboliclink.html
3030
% ok = java.nio.file.Files.createSymbolicLink(java.io.File(link).toPath(), java.io.File(target).toPath());
3131
% Matlab Java doesn't recognize the optional argument omitted.
32+
% see example/Filesystem.java for this working in plain Java.
33+
% see example/javaCreateSymbolicLink.m for a non-working attempt in Matlab.
3234

3335
if ispc
3436
cmd = "pwsh -c " + '"' + "New-Item -ItemType SymbolicLink -Path " + link + ...

example/Filesystem.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,54 @@
99

1010
import java.util.Scanner;
1111
import java.io.File;
12+
import java.nio.file.Path;
1213

1314

1415
public class Filesystem {
15-
public static void main(String[] args) {
16+
public static void main(String[] argv) {
1617
Scanner scanner = new Scanner(System.in);
1718

18-
String argument = "";
19+
String a1 = "";
20+
String a2 = "";
1921
final String prompt = "Jfs> ";
2022

2123
System.out.print(prompt);
2224

2325
while (scanner.hasNextLine()) {
2426
String input = scanner.nextLine();
2527

26-
String[] inputArray = input.split(" ");
27-
String command = inputArray[0];
28+
String[] args = input.split(" ");
29+
String command = args[0];
2830

29-
if (inputArray.length > 1) {
30-
argument = inputArray[1];
31-
} else {
32-
argument = "";
31+
if (args.length > 1) {
32+
a1 = args[1];
33+
}
34+
if (args.length > 2) {
35+
a2 = args[2];
3336
}
3437

3538
if (command.equals("exit") || command.equals("q") || command.equals("\u0004")) {
3639
break;
3740
} else if (command.equals("java_version")) {
3841
System.out.println(System.getProperty("java.version"));
3942
} else if (command.equals("absolute")) {
40-
System.out.println(absolute(argument));
43+
System.out.println(absolute(a1));
44+
} else if (command.equals("create_symlink")) {
45+
System.out.println(create_symlink(a1, a2));
4146
} else if (command.equals("expanduser")) {
42-
System.out.println(expanduser(argument));
47+
System.out.println(expanduser(a1));
4348
} else if (command.equals("canonical")) {
44-
System.out.println(canonical(argument));
49+
System.out.println(canonical(a1));
4550
} else if (command.equals("parent")) {
46-
System.out.println(parent(argument));
51+
System.out.println(parent(a1));
4752
} else if (command.equals("root")) {
48-
System.out.println(root(argument));
53+
System.out.println(root(a1));
4954
} else if (command.equals("is_absolute")) {
50-
System.out.println(is_absolute(argument));
55+
System.out.println(is_absolute(a1));
5156
} else if (command.equals("is_exe")) {
52-
System.out.println(is_exe(argument));
57+
System.out.println(is_exe(a1));
5358
} else if (command.equals("is_readable")) {
54-
System.out.println(is_readable(argument));
59+
System.out.println(is_readable(a1));
5560
} else if (command.equals("ram_free")) {
5661
System.out.println(ram_free());
5762
} else if (command.equals("cpu_count")) {
@@ -71,6 +76,18 @@ public static String absolute(String path) {
7176
return f.getAbsolutePath();
7277
}
7378

79+
public static Boolean create_symlink(String target, String link) {
80+
Path t = new File(target).toPath();
81+
Path l = new File(link).toPath();
82+
try {
83+
java.nio.file.Files.createSymbolicLink(l, t);
84+
return true;
85+
} catch (java.io.IOException e) {
86+
System.err.println(e);
87+
return false;
88+
}
89+
}
90+
7491
public static String expanduser(String path) {
7592
if (path.startsWith("~"))
7693
return System.getProperty("user.home") + path.substring(1);

example/javaCreateSymbolicLink.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
f = stdlib.absolute("Readme.md");
2+
link = "alsoread.md";
3+
4+
perms = stdlib.get_permissions(f);
5+
6+
pp = java.nio.file.attribute.PosixFilePermissions.fromString(perms);
7+
8+
fp = java.nio.file.attribute.PosixFilePermissions.asFileAttribute(pp);
9+
10+
tp = java.io.File(f).toPath();
11+
lp = java.io.File(link).toPath();
12+
13+
java.nio.file.Files.createSymbolicLink(lp, tp, fp);
14+
% this fails with No method 'java.nio.file.Files.createSymbolicLink' with matching signature found.

0 commit comments

Comments
 (0)