Skip to content

Commit

Permalink
fs: add FileHandle.chown
Browse files Browse the repository at this point in the history
Ref: #247
  • Loading branch information
saghul committed Jul 19, 2024
1 parent 923b5bf commit 0532230
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/mod_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,32 @@ static JSValue tjs_file_chmod(JSContext *ctx, JSValue this_val, int argc, JSValu
return tjs_fsreq_init(ctx, fr, this_val);
}

static JSValue tjs_file_chown(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
TJSFile *f = tjs_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;

int uid;
if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &uid, argv[0]))
return JS_ThrowTypeError(ctx, "expected a number for uid parameter");

int gid;
if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &gid, argv[1]))
return JS_ThrowTypeError(ctx, "expected a number for gid parameter");

TJSFsReq *fr = js_malloc(ctx, sizeof(*fr));
if (!fr)
return JS_EXCEPTION;

int r = uv_fs_fchown(tjs_get_loop(ctx), &fr->req, f->fd, uid, gid, uv__fs_req_cb);
if (r != 0) {
js_free(ctx, fr);
return tjs_throw_errno(ctx, r);
}

return tjs_fsreq_init(ctx, fr, this_val);
}

static JSValue tjs_file_fileno(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
TJSFile *f = tjs_file_get(ctx, this_val);
if (!f)
Expand Down Expand Up @@ -1240,6 +1266,7 @@ static const JSCFunctionListEntry tjs_file_proto_funcs[] = {
TJS_CFUNC_DEF("sync", 0, tjs_file_sync),
TJS_CFUNC_DEF("datasync", 0, tjs_file_datasync),
TJS_CFUNC_DEF("chmod", 1, tjs_file_chmod),
TJS_CFUNC_DEF("chown", 2, tjs_file_chown),
TJS_CGETSET_DEF("path", tjs_file_path_get, NULL),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "FileHandle", JS_PROP_C_W_E),
};
Expand Down

0 comments on commit 0532230

Please sign in to comment.