From b22a50cb0cc124e3d41041abc9d08f2c289e9963 Mon Sep 17 00:00:00 2001 From: Masato Yoshioka Date: Wed, 15 Jan 2025 17:15:07 +0900 Subject: [PATCH] fix(ext/node): add chown method to FileHandle class (#27638) --- ext/node/polyfills/internal/fs/handle.ts | 5 ++++ tests/unit_node/_fs/_fs_handle_test.ts | 29 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts index 774b36dfc2..e22c3e0ae6 100644 --- a/ext/node/polyfills/internal/fs/handle.ts +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -165,6 +165,11 @@ export class FileHandle extends EventEmitter { assertNotClosed(this, promises.utimes.name); return promises.utimes(this.#path, atime, mtime); } + + chown(uid: number, gid: number): Promise { + assertNotClosed(this, promises.chown.name); + return promises.chown(this.#path, uid, gid); + } } function assertNotClosed(handle: FileHandle, syscall: string) { diff --git a/tests/unit_node/_fs/_fs_handle_test.ts b/tests/unit_node/_fs/_fs_handle_test.ts index 9134c2cb0a..d7ae32b5ea 100644 --- a/tests/unit_node/_fs/_fs_handle_test.ts +++ b/tests/unit_node/_fs/_fs_handle_test.ts @@ -2,7 +2,7 @@ import * as path from "@std/path"; import { Buffer } from "node:buffer"; import * as fs from "node:fs/promises"; -import { assert, assertEquals } from "@std/assert"; +import { assert, assertEquals, assertRejects } from "@std/assert"; const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); @@ -273,3 +273,30 @@ Deno.test({ await fileHandle.close(); }, }); + +Deno.test({ + name: "[node/fs filehandle.chown] Change owner of the file", + ignore: Deno.build.os === "windows", + async fn() { + const fileHandle = await fs.open(testData); + + const nobodyUid = 65534; + const nobodyGid = 65534; + + await assertRejects( + async () => await fileHandle.chown(nobodyUid, nobodyGid), + Deno.errors.PermissionDenied, + "Operation not permitted", + ); + + const realUid = Deno.uid() || 1000; + const realGid = Deno.gid() || 1000; + + await fileHandle.chown(realUid, realGid); + + assertEquals(Deno.statSync(testData).uid, realUid); + assertEquals(Deno.statSync(testData).gid, realGid); + + await fileHandle.close(); + }, +});