Skip to content

Commit 168313f

Browse files
authored
Merge pull request bjorn3#86 from kateinoigakukun/yt/issue-ino
Return unique ino for each node for `Filestat` and `Dirent`
2 parents 94a5651 + a267bd6 commit 168313f

File tree

6 files changed

+62
-15
lines changed

6 files changed

+62
-15
lines changed

src/fd.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ export abstract class Fd {
117117
}
118118

119119
export abstract class Inode {
120+
ino: bigint;
121+
122+
constructor() {
123+
this.ino = Inode.issue_ino();
124+
}
125+
126+
// NOTE: ino 0 is reserved for the root directory
127+
private static next_ino: bigint = 1n;
128+
static issue_ino(): bigint {
129+
return Inode.next_ino++;
130+
}
131+
static root_ino(): bigint {
132+
return 0n;
133+
}
134+
120135
abstract path_open(
121136
oflags: number,
122137
fs_rights_base: bigint,

src/fs_mem.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,17 @@ export class OpenDirectory extends Fd {
159159
if (cookie == 0n) {
160160
return {
161161
ret: wasi.ERRNO_SUCCESS,
162-
dirent: new wasi.Dirent(1n, ".", wasi.FILETYPE_DIRECTORY),
162+
dirent: new wasi.Dirent(1n, this.dir.ino, ".", wasi.FILETYPE_DIRECTORY),
163163
};
164164
} else if (cookie == 1n) {
165165
return {
166166
ret: wasi.ERRNO_SUCCESS,
167-
dirent: new wasi.Dirent(2n, "..", wasi.FILETYPE_DIRECTORY),
167+
dirent: new wasi.Dirent(
168+
2n,
169+
this.dir.parent_ino(),
170+
"..",
171+
wasi.FILETYPE_DIRECTORY,
172+
),
168173
};
169174
}
170175

@@ -178,7 +183,12 @@ export class OpenDirectory extends Fd {
178183

179184
return {
180185
ret: 0,
181-
dirent: new wasi.Dirent(cookie + 1n, name, entry.stat().filetype),
186+
dirent: new wasi.Dirent(
187+
cookie + 1n,
188+
entry.ino,
189+
name,
190+
entry.stat().filetype,
191+
),
182192
};
183193
}
184194

@@ -501,7 +511,7 @@ export class File extends Inode {
501511
}
502512

503513
stat(): wasi.Filestat {
504-
return new wasi.Filestat(wasi.FILETYPE_REGULAR_FILE, this.size);
514+
return new wasi.Filestat(this.ino, wasi.FILETYPE_REGULAR_FILE, this.size);
505515
}
506516
}
507517

@@ -547,6 +557,7 @@ class Path {
547557

548558
export class Directory extends Inode {
549559
contents: Map<string, Inode>;
560+
private parent: Directory | null = null;
550561

551562
constructor(contents: Map<string, Inode> | [string, Inode][]) {
552563
super();
@@ -555,6 +566,19 @@ export class Directory extends Inode {
555566
} else {
556567
this.contents = contents;
557568
}
569+
570+
for (const entry of this.contents.values()) {
571+
if (entry instanceof Directory) {
572+
entry.parent = this;
573+
}
574+
}
575+
}
576+
577+
parent_ino(): bigint {
578+
if (this.parent == null) {
579+
return Inode.root_ino();
580+
}
581+
return this.parent.ino;
558582
}
559583

560584
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -563,7 +587,7 @@ export class Directory extends Inode {
563587
}
564588

565589
stat(): wasi.Filestat {
566-
return new wasi.Filestat(wasi.FILETYPE_DIRECTORY, 0n);
590+
return new wasi.Filestat(this.ino, wasi.FILETYPE_DIRECTORY, 0n);
567591
}
568592

569593
get_entry_for_path(path: Path): { ret: number; entry: Inode | null } {
@@ -697,15 +721,18 @@ export class Directory extends Inode {
697721
}
698722

699723
export class ConsoleStdout extends Fd {
724+
private ino: bigint;
700725
write: (buffer: Uint8Array) => void;
701726

702727
constructor(write: (buffer: Uint8Array) => void) {
703728
super();
729+
this.ino = Inode.issue_ino();
704730
this.write = write;
705731
}
706732

707733
fd_filestat_get(): { ret: number; filestat: wasi.Filestat } {
708734
const filestat = new wasi.Filestat(
735+
this.ino,
709736
wasi.FILETYPE_CHARACTER_DEVICE,
710737
BigInt(0),
711738
);

src/fs_opfs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ export class SyncOPFSFile extends Inode {
5858
}
5959

6060
stat(): wasi.Filestat {
61-
return new wasi.Filestat(wasi.FILETYPE_REGULAR_FILE, this.size);
61+
return new wasi.Filestat(this.ino, wasi.FILETYPE_REGULAR_FILE, this.size);
6262
}
6363
}
6464

6565
export class OpenSyncOPFSFile extends Fd {
6666
file: SyncOPFSFile;
6767
position: bigint = 0n;
68+
ino: bigint;
6869

6970
constructor(file: SyncOPFSFile) {
7071
super();
7172
this.file = file;
73+
this.ino = Inode.issue_ino();
7274
}
7375

7476
fd_allocate(offset: bigint, len: bigint): number {
@@ -89,6 +91,7 @@ export class OpenSyncOPFSFile extends Fd {
8991
return {
9092
ret: 0,
9193
filestat: new wasi.Filestat(
94+
this.ino,
9295
wasi.FILETYPE_REGULAR_FILE,
9396
BigInt(this.file.handle.getSize()),
9497
),

src/wasi_defs.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,16 @@ export const FILETYPE_SYMBOLIC_LINK = 7;
184184

185185
export class Dirent {
186186
d_next: bigint;
187-
d_ino: bigint = 0n;
187+
d_ino: bigint;
188188
d_namlen: number;
189189
d_type: number;
190190
dir_name: Uint8Array;
191191

192-
constructor(next_cookie: bigint, name: string, type: number) {
192+
constructor(next_cookie: bigint, d_ino: bigint, name: string, type: number) {
193193
const encoded_name = new TextEncoder().encode(name);
194194

195195
this.d_next = next_cookie;
196+
this.d_ino = d_ino;
196197
this.d_namlen = encoded_name.byteLength;
197198
this.d_type = type;
198199
this.dir_name = encoded_name;
@@ -265,15 +266,16 @@ export const OFLAGS_TRUNC = 1 << 3;
265266

266267
export class Filestat {
267268
dev: bigint = 0n;
268-
ino: bigint = 0n;
269+
ino: bigint;
269270
filetype: number;
270271
nlink: bigint = 0n;
271272
size: bigint;
272273
atim: bigint = 0n;
273274
mtim: bigint = 0n;
274275
ctim: bigint = 0n;
275276

276-
constructor(filetype: number, size: bigint) {
277+
constructor(ino: bigint, filetype: number, size: bigint) {
278+
this.ino = ino;
277279
this.filetype = filetype;
278280
this.size = size;
279281
}

test/adapters/node/run-wasi.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#!/usr/bin/env node
22

33
import fs from 'fs/promises';
4-
import { WASI, wasi, strace, OpenFile, File, Directory, PreopenDirectory, Fd } from "../../../dist/index.js"
4+
import { WASI, wasi, strace, OpenFile, File, Directory, PreopenDirectory, Fd, Inode } from "../../../dist/index.js"
55
import { parseArgs } from "../shared/parseArgs.mjs"
66
import { walkFs } from "../shared/walkFs.mjs"
77

88
class NodeStdout extends Fd {
99
constructor(out) {
1010
super();
1111
this.out = out;
12+
this.ino = Inode.issue_ino();
1213
}
1314

1415
fd_filestat_get() {
1516
const filestat = new wasi.Filestat(
17+
this.ino,
1618
wasi.FILETYPE_CHARACTER_DEVICE,
1719
BigInt(0),
1820
);

threads/src/shared_array_buffer/ref.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,8 @@ export class WASIFarmRefUseArrayBuffer extends WASIFarmRef {
521521

522522
this.release_fd(fd);
523523

524-
const file_stat = new wasi.Filestat(fs_filetype, fs_size);
524+
const file_stat = new wasi.Filestat(fs_ino, fs_filetype, fs_size);
525525
file_stat.dev = fs_dev;
526-
file_stat.ino = fs_ino;
527526
file_stat.nlink = fs_nlink;
528527
file_stat.atim = fs_atim;
529528
file_stat.mtim = fs_mtim;
@@ -1092,9 +1091,8 @@ export class WASIFarmRefUseArrayBuffer extends WASIFarmRef {
10921091

10931092
this.release_fd(fd);
10941093

1095-
const file_stat = new wasi.Filestat(fs_filetype, fs_size);
1094+
const file_stat = new wasi.Filestat(fs_ino, fs_filetype, fs_size);
10961095
file_stat.dev = fs_dev;
1097-
file_stat.ino = fs_ino;
10981096
file_stat.nlink = fs_nlink;
10991097
file_stat.atim = fs_atim;
11001098
file_stat.mtim = fs_mtim;

0 commit comments

Comments
 (0)