1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

fix(ext/node): add chown method to FileHandle class (#27638)

This commit is contained in:
Masato Yoshioka 2025-01-15 17:15:07 +09:00 committed by GitHub
parent afc23fb2e0
commit b22a50cb0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View file

@ -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<void> {
assertNotClosed(this, promises.chown.name);
return promises.chown(this.#path, uid, gid);
}
}
function assertNotClosed(handle: FileHandle, syscall: string) {

View file

@ -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();
},
});