From d4077316f09e75fff8d74e053c59b5d0a91a66eb Mon Sep 17 00:00:00 2001 From: majidsajadi Date: Thu, 8 Apr 2021 21:51:16 +0430 Subject: [PATCH 1/2] feat(node/fs): add fstat and fstatSync --- node/_fs/_fs_fstat.ts | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 node/_fs/_fs_fstat.ts diff --git a/node/_fs/_fs_fstat.ts b/node/_fs/_fs_fstat.ts new file mode 100644 index 000000000000..86ac64984ccc --- /dev/null +++ b/node/_fs/_fs_fstat.ts @@ -0,0 +1,59 @@ +import { + BigIntStats, + CFISBIS, + statCallback, + statCallbackBigInt, + statOptions, + Stats, +} from "./_fs_stat.ts"; + +export function fstat(fd: number, callback: statCallback): void; +export function fstat( + fd: number, + options: { bigint: false }, + callback: statCallback, +): void; +export function fstat( + fd: number, + options: { bigint: true }, + callback: statCallbackBigInt, +): void; +export function fstat( + fd: number, + optionsOrCallback: statCallback | statCallbackBigInt | statOptions, + maybeCallback?: statCallback | statCallbackBigInt, +) { + const callback = + (typeof optionsOrCallback === "function" + ? optionsOrCallback + : maybeCallback) as ( + ...args: [Error] | [null, BigIntStats | Stats] + ) => void; + const options = typeof optionsOrCallback === "object" + ? optionsOrCallback + : { bigint: false }; + + if (!callback) throw new Error("No callback function supplied"); + + Deno.fstat(fd).then( + (stat) => callback(null, CFISBIS(stat, options.bigint)), + (err) => callback(err), + ); +} + +export function fstatSync(fd: number): Stats; +export function fstatSync( + fd: number, + options: { bigint: false }, +): Stats; +export function fstatSync( + fd: number, + options: { bigint: true }, +): BigIntStats; +export function fstatSync( + fd: number, + options?: statOptions, +): Stats | BigIntStats { + const origin = Deno.fstatSync(fd); + return CFISBIS(origin, options?.bigint || false); +} From d2c4c4d325c3b81e49719db68ea1a5fcea7f2cd4 Mon Sep 17 00:00:00 2001 From: majidsajadi Date: Thu, 8 Apr 2021 21:51:34 +0430 Subject: [PATCH 2/2] test(node/fs): add tests for fstat and fstatSync --- node/_fs/_fs_fstat_test.ts | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 node/_fs/_fs_fstat_test.ts diff --git a/node/_fs/_fs_fstat_test.ts b/node/_fs/_fs_fstat_test.ts new file mode 100644 index 000000000000..b6b8b1ab4a78 --- /dev/null +++ b/node/_fs/_fs_fstat_test.ts @@ -0,0 +1,82 @@ +import { fstat, fstatSync } from "./_fs_fstat.ts"; +import { fail } from "../../testing/asserts.ts"; +import { assertStats, assertStatsBigInt } from "./_fs_stat_test.ts"; +import type { BigIntStats, Stats } from "./_fs_stat.ts"; + +Deno.test({ + name: "ASYNC: get a file Stats", + async fn() { + const file = await Deno.makeTempFile(); + const { rid } = await Deno.open(file); + + await new Promise((resolve, reject) => { + fstat(rid, (err: Error | null, stat: Stats) => { + if (err) reject(err); + resolve(stat); + }); + }) + .then( + (stat) => { + assertStats(stat, Deno.fstatSync(rid)); + }, + () => fail(), + ) + .finally(() => { + Deno.removeSync(file); + Deno.close(rid); + }); + }, +}); + +Deno.test({ + name: "ASYNC: get a file BigInt Stats", + async fn() { + const file = await Deno.makeTempFile(); + const { rid } = await Deno.open(file); + + await new Promise((resolve, reject) => { + fstat(rid, { bigint: true }, (err: Error | null, stat: BigIntStats) => { + if (err) reject(err); + resolve(stat); + }); + }) + .then( + (stat) => assertStatsBigInt(stat, Deno.fstatSync(rid)), + () => fail(), + ) + .finally(() => { + Deno.removeSync(file); + Deno.close(rid); + }); + }, +}); + +Deno.test({ + name: "SYNC: get a file Stats", + fn() { + const file = Deno.makeTempFileSync(); + const { rid } = Deno.openSync(file); + + try { + assertStats(fstatSync(rid), Deno.fstatSync(rid)); + } finally { + Deno.removeSync(file); + Deno.close(rid); + } + }, +}); + +Deno.test({ + name: "SYNC: get a file BigInt Stats", + fn() { + const file = Deno.makeTempFileSync(); + const { rid } = Deno.openSync(file); + + try { + assertStatsBigInt(fstatSync(rid, { bigint: true }), Deno.fstatSync(rid)); + } finally { + Deno.removeSync(file); + Deno.close(rid); + } + }, +});