Skip to content

Commit 017f4b1

Browse files
committed
sysfs: Fixed root directory operations
1 parent ac5b8a5 commit 017f4b1

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/fs/sysfs.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { FileSystem, Sync } from '@zenfs/core';
1+
import { FileSystem, Inode, Sync } from '@zenfs/core';
22
import type { InodeLike } from '@zenfs/core';
33
import { withErrno } from 'kerium';
4-
import { kobj_find, KObject } from '../kobject.js';
4+
import { kobj_find, KObject, sysfs_root } from '../kobject.js';
5+
import { S_IFDIR } from '@zenfs/core/constants';
56

67
/**
78
* @todo
89
*/
910
export class SysFS extends Sync(FileSystem) {
1011
protected readonly initTime = Date.now();
1112

13+
protected readonly _rootInode = new Inode({ mode: S_IFDIR | 0o555 });
14+
1215
public constructor() {
1316
super(0x62656572, 'sysfs');
1417
}
@@ -21,6 +24,7 @@ export class SysFS extends Sync(FileSystem) {
2124
* @todo
2225
*/
2326
statSync(path: string): InodeLike {
27+
if (path === '/') return this._rootInode;
2428
const node = kobj_find(path);
2529
if (!node) throw withErrno('ENOENT');
2630
throw withErrno('ENOSYS');
@@ -50,6 +54,7 @@ export class SysFS extends Sync(FileSystem) {
5054
}
5155

5256
readdirSync(path: string): string[] {
57+
if (path === '/') return Array.from(sysfs_root.keys());
5358
const obj = kobj_find(path);
5459
if (!(obj instanceof KObject)) throw withErrno('ENOTDIR');
5560
return [...obj.children.keys(), ...obj.attributes.keys()];

src/kobject.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { dirname } from '@zenfs/core/path.js';
22
import { withErrno } from 'kerium';
33

4-
const root = new Map<string, KObject>();
4+
/**
5+
* @internal
6+
*/
7+
export const sysfs_root = new Map<string, KObject>();
58

69
/**
710
* Unlike Linux, we don't use ksets, ktypes, or attribute groups.
@@ -19,7 +22,7 @@ export class KObject {
1922
public parent: KObject | null
2023
) {
2124
if (!parent) {
22-
root.set(name, this);
25+
sysfs_root.set(name, this);
2326
} else {
2427
parent.children.set(name, this);
2528
}
@@ -41,7 +44,7 @@ export interface KObjectAttribute extends Attribute {
4144

4245
export function kobj_find(path: string): KObject | Attribute | null {
4346
const [group, ...parts] = path.split('/').filter(p => p);
44-
let current: KObject | Attribute | undefined = root.get(group);
47+
let current: KObject | Attribute | undefined = sysfs_root.get(group);
4548
for (const part of parts) {
4649
if (!(current instanceof KObject)) throw withErrno('ENOTDIR');
4750
if (!current.children.has(part)) throw withErrno('ENOENT');

0 commit comments

Comments
 (0)