From 18574fd9d0b48883271cc7ca2e93b28e2e573668 Mon Sep 17 00:00:00 2001 From: Daniel Green Date: Tue, 25 Oct 2022 10:14:15 -0400 Subject: [PATCH 1/2] Bump MoarVM for chown op --- tools/templates/MOAR_REVISION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/templates/MOAR_REVISION b/tools/templates/MOAR_REVISION index d369a6238..938cdfa48 100644 --- a/tools/templates/MOAR_REVISION +++ b/tools/templates/MOAR_REVISION @@ -1 +1 @@ -2022.07-16-g3ae8a31c1 +2022.07-18-gd22223381 From 66675457e9193627667aaaff6be207e2ff25c4ca Mon Sep 17 00:00:00 2001 From: Daniel Green Date: Tue, 25 Oct 2022 10:19:59 -0400 Subject: [PATCH 2/2] Add nqp::chown() op Works for the MoarVM and JVM backends, untested for the JS backend. Currently is Linux(Posix?) only, though that isn't checked at this level and will have to be checked in Rakudo. --- docs/ops.markdown | 7 +++++++ src/vm/js/Operations.nqp | 1 + src/vm/js/nqp-runtime/io.js | 4 ++++ src/vm/jvm/QAST/Compiler.nqp | 1 + src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java | 13 +++++++++++++ src/vm/moar/QAST/QASTOperationsMAST.nqp | 1 + 6 files changed, 27 insertions(+) diff --git a/docs/ops.markdown b/docs/ops.markdown index 304499684..dbe780b90 100644 --- a/docs/ops.markdown +++ b/docs/ops.markdown @@ -234,6 +234,7 @@ The opcodes are grouped into the following categories: [chdir](#chdir) | [chmod](#chmod) | +[chown](#chown) | [closedir](#closedir) | [copy](#copy) | [cwd](#cwd) | @@ -1519,6 +1520,12 @@ Change the working directory to the given path. Change the permissions of `$path` to the posix style permissions of `$mode`. Returns 0 on success, throws an exception on failure. +## chown +* `chown(str $path, int $uid, int $gid --> int)` + +Change the owner or group of the path. +Throws an exception on failure. + ## closedir * `closedir(Handle $)` diff --git a/src/vm/js/Operations.nqp b/src/vm/js/Operations.nqp index c7bf822ab..668c56403 100644 --- a/src/vm/js/Operations.nqp +++ b/src/vm/js/Operations.nqp @@ -576,6 +576,7 @@ class QAST::OperationsJS { add_simple_op('mkdir', $T_VOID, [$T_STR, $T_INT], :side_effects); add_simple_op('chmod', $T_VOID, [$T_STR, $T_INT], :side_effects); + add_simple_op('chown', $T_VOID, [$T_STR, $T_INT, $T_INT], :side_effects); add_simple_op('getenvhash', $T_OBJ, [], :side_effects); add_simple_op('getsignals', $T_OBJ, [], :side_effects, :takes_hll); diff --git a/src/vm/js/nqp-runtime/io.js b/src/vm/js/nqp-runtime/io.js index c1d7770bb..9a4ee2d6d 100644 --- a/src/vm/js/nqp-runtime/io.js +++ b/src/vm/js/nqp-runtime/io.js @@ -479,6 +479,10 @@ op.chmod = function(path, mode) { return fs.chmodSync(path, mode); }; +op.chown = function(path, uid, gid) { + return fs.chownSync(path, uid, gid); +}; + op.sleep = function(seconds) { sleep.usleep(Math.floor(seconds * 1000000)); return seconds; diff --git a/src/vm/jvm/QAST/Compiler.nqp b/src/vm/jvm/QAST/Compiler.nqp index 4c03a37fe..cff6a6fdc 100644 --- a/src/vm/jvm/QAST/Compiler.nqp +++ b/src/vm/jvm/QAST/Compiler.nqp @@ -2270,6 +2270,7 @@ QAST::OperationsJAST.map_classlib_core_op('filenofh', $TYPE_OPS, 'filenofh', [$R QAST::OperationsJAST.map_classlib_core_op('setbuffersizefh', $TYPE_OPS, 'setbuffersizefh', [$RT_OBJ, $RT_INT], $RT_OBJ, :tc); QAST::OperationsJAST.map_classlib_core_op('chmod', $TYPE_OPS, 'chmod', [$RT_STR, $RT_INT], $RT_INT, :tc); +QAST::OperationsJAST.map_classlib_core_op('chown', $TYPE_OPS, 'chown', [$RT_STR, $RT_INT, $RT_INT], $RT_INT, :tc); QAST::OperationsJAST.map_classlib_core_op('unlink', $TYPE_OPS, 'unlink', [$RT_STR], $RT_INT, :tc); QAST::OperationsJAST.map_classlib_core_op('rmdir', $TYPE_OPS, 'rmdir', [$RT_STR], $RT_INT, :tc); QAST::OperationsJAST.map_classlib_core_op('cwd', $TYPE_OPS, 'cwd', [], $RT_STR); diff --git a/src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java b/src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java index 8a2b6d168..6fd79a6c8 100644 --- a/src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java +++ b/src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java @@ -868,6 +868,19 @@ public static long chmod(String path, long mode, ThreadContext tc) { return 0; } + public static long chown(String path, long uid, long gid, ThreadContext tc) { + Path path_o; + try { + path_o = Paths.get(path); + Files.setAttribute(path_o, "unix:uid", uid); + Files.setAttribute(path_o, "unix:gid", gid); + } + catch (Exception e) { + die_s(IOExceptionMessages.message(e), tc); + } + return 0; + } + public static long unlink(String path, ThreadContext tc) { Path path_o = Paths.get(path); if (Files.isDirectory(path_o)) { diff --git a/src/vm/moar/QAST/QASTOperationsMAST.nqp b/src/vm/moar/QAST/QASTOperationsMAST.nqp index ac24a6c0c..46fa4e15f 100644 --- a/src/vm/moar/QAST/QASTOperationsMAST.nqp +++ b/src/vm/moar/QAST/QASTOperationsMAST.nqp @@ -3197,6 +3197,7 @@ my constant SIMPLE_OP_MAPPINGS_RESULT_ZERO := nqp::list_s( 'bindsock', 'bind_sk', 'setbuffersizefh', 'setbuffersize_fh', 'chmod', 'chmod_f', + 'chown', 'chown_f', 'unlink', 'delete_f', 'rmdir', 'rmdir', 'chdir', 'chdir',