Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): implement fs.statfs() (#22862)
- Loading branch information
1 parent
cae190b
commit 49eaeed
Showing
8 changed files
with
285 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { BigInt } from "ext:deno_node/internal/primordials.mjs"; | ||
import { op_node_statfs } from "ext:core/ops"; | ||
import { promisify } from "ext:deno_node/internal/util.mjs"; | ||
|
||
class StatFs { | ||
type; | ||
bsize; | ||
blocks; | ||
bfree; | ||
bavail; | ||
files; | ||
ffree; | ||
constructor(type, bsize, blocks, bfree, bavail, files, ffree) { | ||
this.type = type; | ||
this.bsize = bsize; | ||
this.blocks = blocks; | ||
this.bfree = bfree; | ||
this.bavail = bavail; | ||
this.files = files; | ||
this.ffree = ffree; | ||
} | ||
} | ||
|
||
export function statfs(path, options, callback) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} | ||
try { | ||
const res = statfsSync(path, options); | ||
callback(null, res); | ||
} catch (err) { | ||
callback(err, null); | ||
} | ||
} | ||
|
||
export function statfsSync(path, options) { | ||
const bigint = typeof options?.bigint === "boolean" ? options.bigint : false; | ||
const statFs = op_node_statfs( | ||
path, | ||
bigint, | ||
); | ||
return new StatFs( | ||
bigint ? BigInt(statFs.type) : statFs.type, | ||
bigint ? BigInt(statFs.bsize) : statFs.bsize, | ||
bigint ? BigInt(statFs.blocks) : statFs.blocks, | ||
bigint ? BigInt(statFs.bfree) : statFs.bfree, | ||
bigint ? BigInt(statFs.bavail) : statFs.bavail, | ||
bigint ? BigInt(statFs.files) : statFs.files, | ||
bigint ? BigInt(statFs.ffree) : statFs.ffree, | ||
); | ||
} | ||
|
||
export const statfsPromise = promisify(statfs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import * as fs from "node:fs"; | ||
import { assertEquals, assertRejects } from "@std/assert/mod.ts"; | ||
import * as path from "@std/path/mod.ts"; | ||
|
||
function assertStatFs( | ||
statFs: fs.StatsFsBase<unknown>, | ||
{ bigint = false } = {}, | ||
) { | ||
assertEquals(statFs.constructor.name, "StatFs"); | ||
const expectedType = bigint ? "bigint" : "number"; | ||
assertEquals(typeof statFs.type, expectedType); | ||
assertEquals(typeof statFs.bsize, expectedType); | ||
assertEquals(typeof statFs.blocks, expectedType); | ||
assertEquals(typeof statFs.bfree, expectedType); | ||
assertEquals(typeof statFs.bavail, expectedType); | ||
assertEquals(typeof statFs.files, expectedType); | ||
assertEquals(typeof statFs.ffree, expectedType); | ||
if (Deno.build.os == "windows") { | ||
assertEquals(statFs.type, bigint ? 0n : 0); | ||
assertEquals(statFs.files, bigint ? 0n : 0); | ||
assertEquals(statFs.ffree, bigint ? 0n : 0); | ||
} | ||
} | ||
|
||
const filePath = path.fromFileUrl(import.meta.url); | ||
|
||
Deno.test({ | ||
name: "fs.statfs()", | ||
async fn() { | ||
await new Promise<fs.StatsFsBase<unknown>>((resolve, reject) => { | ||
fs.statfs(filePath, (err, statFs) => { | ||
if (err) reject(err); | ||
resolve(statFs); | ||
}); | ||
}).then((statFs) => assertStatFs(statFs)); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "fs.statfs() bigint", | ||
async fn() { | ||
await new Promise<fs.StatsFsBase<unknown>>((resolve, reject) => { | ||
fs.statfs(filePath, { bigint: true }, (err, statFs) => { | ||
if (err) reject(err); | ||
resolve(statFs); | ||
}); | ||
}).then((statFs) => assertStatFs(statFs, { bigint: true })); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "fs.statfsSync()", | ||
fn() { | ||
const statFs = fs.statfsSync(filePath); | ||
assertStatFs(statFs); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "fs.statfsSync() bigint", | ||
fn() { | ||
const statFs = fs.statfsSync(filePath, { bigint: true }); | ||
assertStatFs(statFs, { bigint: true }); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "fs.statfs() non-existent path", | ||
async fn() { | ||
const nonExistentPath = path.join(filePath, "../non-existent"); | ||
await assertRejects(async () => { | ||
await fs.promises.statfs(nonExistentPath); | ||
}, "NotFound"); | ||
}, | ||
}); |