New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(node): implement fs.statfs() #22862
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
880e614
feat(node): implement fs.statfs()
nayeemrmn 4d71378
don't use statfs64 on macos
nayeemrmn f9a2e56
fixup! don't use statfs64 on macos
nayeemrmn 1372429
lint
nayeemrmn a8de458
review
nayeemrmn d62ca7c
fixup! review
nayeemrmn 14034b6
fix test import
nayeemrmn e3c318b
try using module's path
nayeemrmn 57aff80
Merge remote-tracking branch 'upstream/main' into node-statfs
nayeemrmn 28abdf2
fix path
nayeemrmn 589768a
add test for non-existent path
nayeemrmn 723e6af
fix test imports again
nayeemrmn fdc1865
Merge remote-tracking branch 'upstream/main' into node-statfs
nayeemrmn 71c11e6
remove unwraps
nayeemrmn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ export const ArrayPrototypeSlice = (that, ...args) => that.slice(...args); | |
export const ArrayPrototypeSome = (that, ...args) => that.some(...args); | ||
export const ArrayPrototypeSort = (that, ...args) => that.sort(...args); | ||
export const ArrayPrototypeUnshift = (that, ...args) => that.unshift(...args); | ||
export const BigInt = globalThis.BigInt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
export const ObjectAssign = Object.assign; | ||
export const ObjectCreate = Object.create; | ||
export const ObjectHasOwn = Object.hasOwn; | ||
|
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,64 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { statfs, statfsSync, StatsFsBase } from "node:fs"; | ||
import { assertEquals } from "@std/assert/mod.ts"; | ||
import * as path from "@std/path/mod.ts"; | ||
|
||
function assertStatFs(statFs: 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<StatsFsBase<unknown>>((resolve, reject) => { | ||
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<StatsFsBase<unknown>>((resolve, reject) => { | ||
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 = statfsSync(filePath); | ||
assertStatFs(statFs); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "fs.statfsSync() bigint", | ||
fn() { | ||
const statFs = statfsSync(filePath, { bigint: true }); | ||
assertStatFs(statFs, { bigint: true }); | ||
}, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't
canonicalize()
fail ifCargo.toml
is not present?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why we need this. The user
path
argument is shadowed by this hardcoded path to Cargo.tomlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have copy-pasted this from my windows playground repo... that's frustrating there was no lint error.