Skip to content

Commit

Permalink
Add nqp::chown() op
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MasterDuke17 committed Oct 25, 2022
1 parent 18574fd commit 6667545
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/ops.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ The opcodes are grouped into the following categories:

[chdir](#chdir) |
[chmod](#chmod) |
[chown](#chown) |
[closedir](#closedir) |
[copy](#copy) |
[cwd](#cwd) |
Expand Down Expand Up @@ -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 $)`

Expand Down
1 change: 1 addition & 0 deletions src/vm/js/Operations.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/vm/js/nqp-runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/vm/moar/QAST/QASTOperationsMAST.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 6667545

Please sign in to comment.