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 ' ;
5
5
6
6
/**
7
7
* @hidden
@@ -21,131 +21,6 @@ function convertError(e: unknown, path: string = ''): ErrnoError {
21
21
return new ErrnoError ( errno , errorMessages [ errno ] , paths . length > 0 ? '/' + paths . join ( '/' ) : path ) ;
22
22
}
23
23
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
-
149
24
/**
150
25
* Mounts an Emscripten file system into the ZenFS file system.
151
26
*/
@@ -160,15 +35,19 @@ export class EmscriptenFS extends Sync(FileSystem) {
160
35
this . label = 'DB_NAME' in this . em && typeof this . em . DB_NAME == 'function' ? this . em . DB_NAME ( ) : this . name ;
161
36
}
162
37
163
- public syncSync ( path : string , data ?: Uint8Array , stats : Readonly < Partial < Stats > > = { } ) : void {
38
+ public touchSync ( path : string , inode : Partial < InodeLike > ) : void {
164
39
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 ) ;
167
44
} catch ( e ) {
168
45
throw convertError ( e , path ) ;
169
46
}
170
47
}
171
48
49
+ public syncSync ( ) : void { }
50
+
172
51
public renameSync ( oldPath : string , newPath : string ) : void {
173
52
try {
174
53
this . em . rename ( oldPath , newPath ) ;
@@ -177,10 +56,10 @@ export class EmscriptenFS extends Sync(FileSystem) {
177
56
}
178
57
}
179
58
180
- public statSync ( path : string ) : Stats {
59
+ public statSync ( path : string ) : Inode {
181
60
try {
182
61
const stats = this . em . stat ( path ) ;
183
- return new Stats ( {
62
+ return new Inode ( {
184
63
mode : stats . mode ,
185
64
size : stats . size ,
186
65
atimeMs : stats . atime . getTime ( ) ,
@@ -192,21 +71,12 @@ export class EmscriptenFS extends Sync(FileSystem) {
192
71
}
193
72
}
194
73
195
- public createFileSync ( path : string ) : EmscriptenFile {
74
+ public createFileSync ( path : string , options : CreationOptions ) : Inode {
196
75
try {
197
76
const node = this . em . createDataFile ( dirname ( path ) , basename ( path ) , new Uint8Array ( ) , true , true , true ) ;
198
77
const stream = new this . em . FSStream ( ) ;
199
78
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 } ) ;
210
80
} catch ( e ) {
211
81
throw convertError ( e , path ) ;
212
82
}
@@ -228,9 +98,10 @@ export class EmscriptenFS extends Sync(FileSystem) {
228
98
}
229
99
}
230
100
231
- public mkdirSync ( path : string , mode : number ) : void {
101
+ public mkdirSync ( path : string , options : CreationOptions ) : Inode {
232
102
try {
233
- this . em . mkdir ( path , mode ) ;
103
+ this . em . mkdir ( path , options . mode ) ;
104
+ return new Inode ( { mode : options . mode | S_IFDIR } ) ;
234
105
} catch ( e ) {
235
106
throw convertError ( e , path ) ;
236
107
}
0 commit comments