Skip to content

Commit 4190194

Browse files
committed
Update to @zenfs/core 2.0
1 parent 8ba2938 commit 4190194

File tree

4 files changed

+53
-200
lines changed

4 files changed

+53
-200
lines changed

package-lock.json

+31-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"typescript-eslint": "^8.8.1"
5959
},
6060
"peerDependencies": {
61-
"@zenfs/core": "^1.8.0",
62-
"utilium": "^1.0.0"
61+
"@zenfs/core": "^2.0.0",
62+
"utilium": "^1.9.0"
6363
}
6464
}

src/backend.ts

+18-147
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Errno, File, FileSystem, ErrnoError, errorMessages, Sync, type Backend, type CreationOptions } from '@zenfs/core';
2-
import { basename, dirname } from '@zenfs/core/vfs/path.js';
3-
import { Stats, type StatsLike } from '@zenfs/core/stats.js';
4-
import type { Buffer } from 'buffer';
1+
import type { Backend, CreationOptions, InodeLike } from '@zenfs/core';
2+
import { Errno, ErrnoError, errorMessages, FileSystem, Inode, Sync } from '@zenfs/core';
3+
import { basename, dirname } from '@zenfs/core/path.js';
4+
import { S_IFDIR, S_IFREG } from '@zenfs/core/vfs/constants.js';
55

66
/**
77
* @hidden
@@ -21,131 +21,6 @@ function convertError(e: unknown, path: string = ''): ErrnoError {
2121
return new ErrnoError(errno, errorMessages[errno], paths.length > 0 ? '/' + paths.join('/') : path);
2222
}
2323

24-
export class EmscriptenFile extends File<EmscriptenFS> {
25-
public constructor(
26-
public fs: EmscriptenFS,
27-
protected em: typeof FS,
28-
public readonly path: string,
29-
protected stream: FS.FSStream
30-
) {
31-
super(fs, path);
32-
}
33-
34-
public get position(): number {
35-
return this.stream.position;
36-
}
37-
38-
public async close(): Promise<void> {
39-
return this.closeSync();
40-
}
41-
42-
public closeSync(): void {
43-
try {
44-
this.em.close(this.stream);
45-
} catch (e) {
46-
throw convertError(e, this.path);
47-
}
48-
}
49-
50-
public async stat(): Promise<Stats> {
51-
return this.statSync();
52-
}
53-
54-
public statSync(): Stats {
55-
try {
56-
return this.fs.statSync(this.path);
57-
} catch (e) {
58-
throw convertError(e, this.path);
59-
}
60-
}
61-
62-
public async truncate(len: number): Promise<void> {
63-
return this.truncateSync(len);
64-
}
65-
66-
public truncateSync(len: number): void {
67-
try {
68-
this.em.ftruncate(this.stream.fd!, len);
69-
} catch (e) {
70-
throw convertError(e, this.path);
71-
}
72-
}
73-
74-
public async write(buffer: Buffer, offset: number, length: number, position: number): Promise<number> {
75-
return this.writeSync(buffer, offset, length, position);
76-
}
77-
78-
public writeSync(buffer: Buffer, offset: number, length: number, position?: number): number {
79-
try {
80-
// Emscripten is particular about what position is set to.
81-
return this.em.write(this.stream, buffer, offset, length, position);
82-
} catch (e) {
83-
throw convertError(e, this.path);
84-
}
85-
}
86-
87-
public async read<TBuffer extends ArrayBufferView>(buffer: TBuffer, offset: number, length: number, position: number): Promise<{ bytesRead: number; buffer: TBuffer }> {
88-
return { bytesRead: this.readSync(buffer, offset, length, position), buffer };
89-
}
90-
91-
public readSync(buffer: ArrayBufferView, offset: number, length: number, position?: number): number {
92-
try {
93-
// Emscripten is particular about what position is set to.
94-
return this.em.read(this.stream, buffer, offset, length, position);
95-
} catch (e) {
96-
throw convertError(e, this.path);
97-
}
98-
}
99-
100-
public async sync(): Promise<void> {
101-
this.syncSync();
102-
}
103-
104-
public syncSync(): void {
105-
// NOP.
106-
}
107-
108-
public async chown(uid: number, gid: number): Promise<void> {
109-
return this.chownSync(uid, gid);
110-
}
111-
112-
public chownSync(uid: number, gid: number): void {
113-
try {
114-
this.em.fchown(this.stream.fd!, uid, gid);
115-
} catch (e) {
116-
throw convertError(e, this.path);
117-
}
118-
}
119-
120-
public async chmod(mode: number): Promise<void> {
121-
return this.chmodSync(mode);
122-
}
123-
124-
public chmodSync(mode: number): void {
125-
try {
126-
this.em.fchmod(this.stream.fd!, mode);
127-
} catch (e) {
128-
throw convertError(e, this.path);
129-
}
130-
}
131-
132-
public async utimes(atime: number, mtime: number): Promise<void> {
133-
return this.utimesSync(atime, mtime);
134-
}
135-
136-
public utimesSync(atime: number, mtime: number): void {
137-
this.fs.utimesSync(this.path, atime, mtime);
138-
}
139-
140-
public async _setType(): Promise<void> {
141-
throw ErrnoError.With('ENOSYS', this.path, '_setType');
142-
}
143-
144-
public _setTypeSync(): void {
145-
throw ErrnoError.With('ENOSYS', this.path, '_setType');
146-
}
147-
}
148-
14924
/**
15025
* Mounts an Emscripten file system into the ZenFS file system.
15126
*/
@@ -160,15 +35,19 @@ export class EmscriptenFS extends Sync(FileSystem) {
16035
this.label = 'DB_NAME' in this.em && typeof this.em.DB_NAME == 'function' ? this.em.DB_NAME() : this.name;
16136
}
16237

163-
public syncSync(path: string, data?: Uint8Array, stats: Readonly<Partial<Stats>> = {}): void {
38+
public touchSync(path: string, inode: Partial<InodeLike>): void {
16439
try {
165-
if (data) this.em.writeFile(path, data);
166-
if (stats.mode) this.em.chmod(path, stats.mode);
40+
const existing = this.em.stat(path);
41+
if (inode.mode) this.em.chmod(path, inode.mode);
42+
if (inode.atimeMs) this.em.utime(path, inode.atimeMs, existing.mtime.getTime());
43+
if (inode.mtimeMs) this.em.utime(path, existing.atime.getTime(), inode.mtimeMs);
16744
} catch (e) {
16845
throw convertError(e, path);
16946
}
17047
}
17148

49+
public syncSync(): void {}
50+
17251
public renameSync(oldPath: string, newPath: string): void {
17352
try {
17453
this.em.rename(oldPath, newPath);
@@ -177,10 +56,10 @@ export class EmscriptenFS extends Sync(FileSystem) {
17756
}
17857
}
17958

180-
public statSync(path: string): Stats {
59+
public statSync(path: string): Inode {
18160
try {
18261
const stats = this.em.stat(path);
183-
return new Stats({
62+
return new Inode({
18463
mode: stats.mode,
18564
size: stats.size,
18665
atimeMs: stats.atime.getTime(),
@@ -192,21 +71,12 @@ export class EmscriptenFS extends Sync(FileSystem) {
19271
}
19372
}
19473

195-
public createFileSync(path: string): EmscriptenFile {
74+
public createFileSync(path: string, options: CreationOptions): Inode {
19675
try {
19776
const node = this.em.createDataFile(dirname(path), basename(path), new Uint8Array(), true, true, true);
19877
const stream = new this.em.FSStream();
19978
stream.object = node;
200-
return new EmscriptenFile(this, this.em, path, stream);
201-
} catch (e) {
202-
throw convertError(e, path);
203-
}
204-
}
205-
206-
public openFileSync(path: string, flag: string): EmscriptenFile {
207-
try {
208-
const stream = this.em.open(path, flag);
209-
return new EmscriptenFile(this, this.em, path, stream);
79+
return new Inode({ mode: options.mode | S_IFREG });
21080
} catch (e) {
21181
throw convertError(e, path);
21282
}
@@ -228,9 +98,10 @@ export class EmscriptenFS extends Sync(FileSystem) {
22898
}
22999
}
230100

231-
public mkdirSync(path: string, mode: number): void {
101+
public mkdirSync(path: string, options: CreationOptions): Inode {
232102
try {
233-
this.em.mkdir(path, mode);
103+
this.em.mkdir(path, options.mode);
104+
return new Inode({ mode: options.mode | S_IFDIR });
234105
} catch (e) {
235106
throw convertError(e, path);
236107
}

src/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import zfs, { Errno, parseFlag, type Stats } from '@zenfs/core';
1+
import zfs, { Errno, type Stats } from '@zenfs/core';
2+
import { parse as parseFlag } from '@zenfs/core/vfs/flags.js';
23
import type { EmFS } from './emscripten.js';
34

45
interface Mount extends EmFS.Mount {

0 commit comments

Comments
 (0)