Skip to content

Commit

Permalink
[JVM] Be more picky when trying to rmdir or unlink
Browse files Browse the repository at this point in the history
Throw an exception when trying to rmdir something that isn't
a directory or when trying to unlink a directory.

This mimics the behaviour of MoarVM. Also there is a spectest
in S32-io/mkdir_rmdir.t that expects a complaint from an attempt
to rmdir a non-existing directory.
  • Loading branch information
usev6 committed Apr 10, 2019
1 parent 74d95cb commit 3921b1e
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -985,27 +985,33 @@ public static long chmod(String path, long mode, ThreadContext tc) {
}

public static long unlink(String path, ThreadContext tc) {
try {
if(!Files.deleteIfExists(Paths.get(path))) {
return -2;
}
Path path_o = Paths.get(path);
if (Files.isDirectory(path_o)) {
die_s("Failed to delete file: is a directory", tc);
}
catch (Exception e) {
die_s(IOExceptionMessages.message(e), tc);
else {
try {
Files.deleteIfExists(path_o);
}
catch (Exception e) {
die_s(IOExceptionMessages.message(e), tc);
}
}
return 0;
}

public static long rmdir(String path, ThreadContext tc) {
Path path_o = Paths.get(path);
try {
if (!Files.isDirectory(path_o)) {
return -2;
}
Files.delete(path_o);
if (!Files.isDirectory(path_o)) {
die_s("Failed to rmdir: not a directory", tc);
}
catch (Exception e) {
die_s(IOExceptionMessages.message(e), tc);
else {
try {
Files.delete(path_o);
}
catch (Exception e) {
die_s(IOExceptionMessages.message(e), tc);
}
}
return 0;
}
Expand Down

0 comments on commit 3921b1e

Please sign in to comment.