better code: accidentally using blocking apis

Massimo Melina committed Jan 18, 2025 at 14:46 UTC 44f48cc38b4d7e13e2627673691d1f2ede0a379a
1 file changed +5 -3
src/util-os.ts
+5 -3
@@ -6,11 +6,13 @@ import Parser from '@gregoranders/csv';
6 import { pid, ppid } from 'node:process'
7 import { promisify } from 'util'
8 import { IS_WINDOWS } from './const'
9 +import { access } from 'fs/promises'
10 +import { statfs } from 'node:fs/promises'
11
12 const DF_TIMEOUT = 2000
13
14 export function getDiskSpaceSync(path: string) {
13 - while (path && !existsSync(path))
15 + while (path && !isWindowsDrive(path) && !existsSync(path))
16 path = dirname(path)
17 const res = statfsSync(path)
18 return { free: res.bavail * res.bsize, total: res.blocks * res.bsize, name: path }
@@ -25,9 +27,9 @@ export function cmdEscape(par: string) {
27 }
28
29 export async function getDiskSpace(path: string) {
28 - while (path && !isWindowsDrive(path) && !existsSync(path))
30 + while (path && !isWindowsDrive(path) && !access(path))
31 path = dirname(path)
30 - const res = statfsSync(path)
32 + const res = await statfs(path)
33 return { free: res.bavail * res.bsize, total: res.blocks * res.bsize, name: path }
34 }
35