All files / src/node StatFs.ts

100% Statements 19/19
66.66% Branches 2/3
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                65x         7x   35x       7x     7x     7x     7x 7x 7x   7x 7x 7x     7x 7x 7x   7x                       65x  
import { Superblock } from '../core';
import type { IStatFs } from './types/misc';
 
export type TStatNumber = number | bigint;
 
/**
 * Statistics about a file system, like `fs.StatFs`.
 */
export class StatFs<T = TStatNumber> implements IStatFs<T> {
  static build(superblock: Superblock, bigint: false): StatFs<number>;
  static build(superblock: Superblock, bigint: true): StatFs<bigint>;
  static build(superblock: Superblock, bigint?: boolean): StatFs<TStatNumber>;
  static build(superblock: Superblock, bigint: boolean = false): StatFs<TStatNumber> {
    const statfs = new StatFs<TStatNumber>();
 
    const getStatNumber = !bigint ? number => number : number => BigInt(number);
 
    // For in-memory filesystem, provide mock but reasonable values
    // Magic number for in-memory filesystem type (similar to ramfs)
    statfs.type = getStatNumber(0x858458f6);
 
    // Optimal transfer block size - commonly 4096 bytes
    statfs.bsize = getStatNumber(4096);
 
    // Calculate filesystem stats based on current state
    const totalInodes = Object.keys(superblock.inodes).length;
 
    // Mock large filesystem capacity (appears as a large filesystem to applications)
    const totalBlocks = 1000000;
    const usedBlocks = Math.min(totalInodes * 2, totalBlocks); // Rough estimation
    const freeBlocks = totalBlocks - usedBlocks;
 
    statfs.blocks = getStatNumber(totalBlocks); // Total data blocks
    statfs.bfree = getStatNumber(freeBlocks); // Free blocks in file system
    statfs.bavail = getStatNumber(freeBlocks); // Free blocks available to unprivileged users
 
    // File node statistics
    const maxFiles = 1000000; // Mock large number of available inodes
    statfs.files = getStatNumber(maxFiles); // Total file nodes in file system
    statfs.ffree = getStatNumber(maxFiles - totalInodes); // Free file nodes
 
    return statfs;
  }
 
  type: T;
  bsize: T;
  blocks: T;
  bfree: T;
  bavail: T;
  files: T;
  ffree: T;
}
 
export default StatFs;