@@ -49,6 +49,17 @@ addToLibrary({
4949#endif
5050 return ERRNO_CODES [ code ] ;
5151 } ,
52+ tryFSOperation ( f ) {
53+ try {
54+ return f ( ) ;
55+ } catch ( e ) {
56+ if ( ! e . code ) throw e ;
57+ // node under windows can return code 'UNKNOWN' here:
58+ // https://github.com/emscripten-core/emscripten/issues/15468
59+ if ( e . code === 'UNKNOWN' ) throw new FS . ErrnoError ( { { { cDefs . EINVAL } } } ) ;
60+ throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
61+ }
62+ } ,
5263 mount ( mount ) {
5364#if ASSERTIONS
5465 assert ( ENVIRONMENT_IS_NODE ) ;
@@ -66,18 +77,15 @@ addToLibrary({
6677 } ,
6778 getMode ( path ) {
6879 var stat ;
69- try {
80+ return NODEFS . tryFSOperation ( ( ) => {
7081 stat = fs . lstatSync ( path ) ;
7182 if ( NODEFS . isWindows ) {
7283 // Node.js on Windows never represents permission bit 'x', so
7384 // propagate read bits to execute bits
7485 stat . mode |= ( stat . mode & { { { cDefs . S_IRUSR | cDefs . S_IRGRP | cDefs . S_IROTH } } } ) >> 2 ;
7586 }
76- } catch ( e ) {
77- if ( ! e . code ) throw e ;
78- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
79- }
80- return stat . mode ;
87+ return stat . mode ;
88+ } ) ;
8189 } ,
8290 realPath ( node ) {
8391 var parts = [ ] ;
@@ -109,17 +117,14 @@ addToLibrary({
109117 }
110118 return newFlags ;
111119 } ,
120+
112121 node_ops : {
113122 getattr ( node ) {
114123 var path = NODEFS . realPath ( node ) ;
115124 var stat ;
116- try {
117- stat = fs . lstatSync ( path ) ;
118- } catch ( e ) {
119- if ( ! e . code ) throw e ;
120- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
121- }
122- // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake them with default blksize of 4096.
125+ NODEFS . tryFSOperation ( ( ) => stat = fs . lstatSync ( path ) ) ;
126+ // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake
127+ // them with default blksize of 4096.
123128 // See http://support.microsoft.com/kb/140365
124129 if ( NODEFS . isWindows && ! stat . blksize ) {
125130 stat . blksize = 4096 ;
@@ -145,7 +150,7 @@ addToLibrary({
145150 } ,
146151 setattr ( node , attr ) {
147152 var path = NODEFS . realPath ( node ) ;
148- try {
153+ NODEFS . tryFSOperation ( ( ) => {
149154 if ( attr . mode !== undefined ) {
150155 fs . chmodSync ( path , attr . mode ) ;
151156 // update the common node structure mode as well
@@ -158,10 +163,7 @@ addToLibrary({
158163 if ( attr . size !== undefined ) {
159164 fs . truncateSync ( path , attr . size ) ;
160165 }
161- } catch ( e ) {
162- if ( ! e . code ) throw e ;
163- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
164- }
166+ } ) ;
165167 } ,
166168 lookup ( parent , name ) {
167169 var path = PATH . join2 ( NODEFS . realPath ( parent ) , name ) ;
@@ -172,132 +174,84 @@ addToLibrary({
172174 var node = NODEFS . createNode ( parent , name , mode , dev ) ;
173175 // create the backing node for this in the fs root as well
174176 var path = NODEFS . realPath ( node ) ;
175- try {
177+ NODEFS . tryFSOperation ( ( ) => {
176178 if ( FS . isDir ( node . mode ) ) {
177179 fs . mkdirSync ( path , node . mode ) ;
178180 } else {
179181 fs . writeFileSync ( path , '' , { mode : node . mode } ) ;
180182 }
181- } catch ( e ) {
182- if ( ! e . code ) throw e ;
183- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
184- }
183+ } ) ;
185184 return node ;
186185 } ,
187186 rename ( oldNode , newDir , newName ) {
188187 var oldPath = NODEFS . realPath ( oldNode ) ;
189188 var newPath = PATH . join2 ( NODEFS . realPath ( newDir ) , newName ) ;
190- try {
191- fs . renameSync ( oldPath , newPath ) ;
192- } catch ( e ) {
193- if ( ! e . code ) throw e ;
194- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
195- }
189+ NODEFS . tryFSOperation ( ( ) => fs . renameSync ( oldPath , newPath ) ) ;
196190 oldNode . name = newName ;
197191 } ,
198192 unlink ( parent , name ) {
199193 var path = PATH . join2 ( NODEFS . realPath ( parent ) , name ) ;
200- try {
201- fs . unlinkSync ( path ) ;
202- } catch ( e ) {
203- if ( ! e . code ) throw e ;
204- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
205- }
194+ NODEFS . tryFSOperation ( ( ) => fs . unlinkSync ( path ) ) ;
206195 } ,
207196 rmdir ( parent , name ) {
208197 var path = PATH . join2 ( NODEFS . realPath ( parent ) , name ) ;
209- try {
210- fs . rmdirSync ( path ) ;
211- } catch ( e ) {
212- if ( ! e . code ) throw e ;
213- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
214- }
198+ NODEFS . tryFSOperation ( ( ) => fs . rmdirSync ( path ) ) ;
215199 } ,
216200 readdir ( node ) {
217201 var path = NODEFS . realPath ( node ) ;
218- try {
219- return fs . readdirSync ( path ) ;
220- } catch ( e ) {
221- if ( ! e . code ) throw e ;
222- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
223- }
202+ return NODEFS . tryFSOperation ( ( ) => fs . readdirSync ( path ) ) ;
224203 } ,
225204 symlink ( parent , newName , oldPath ) {
226205 var newPath = PATH . join2 ( NODEFS . realPath ( parent ) , newName ) ;
227- try {
228- fs . symlinkSync ( oldPath , newPath ) ;
229- } catch ( e ) {
230- if ( ! e . code ) throw e ;
231- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
232- }
206+ NODEFS . tryFSOperation ( ( ) => fs . symlinkSync ( oldPath , newPath ) ) ;
233207 } ,
234208 readlink ( node ) {
235209 var path = NODEFS . realPath ( node ) ;
236- try {
237- return fs . readlinkSync ( path ) ;
238- } catch ( e ) {
239- if ( ! e . code ) throw e ;
240- // node under windows can return code 'UNKNOWN' here:
241- // https://github.com/emscripten-core/emscripten/issues/15468
242- if ( e . code === 'UNKNOWN' ) throw new FS . ErrnoError ( { { { cDefs . EINVAL } } } ) ;
243- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
244- }
210+ return NODEFS . tryFSOperation ( ( ) => fs . readlinkSync ( path ) ) ;
245211 } ,
246212 } ,
247213 stream_ops : {
248214 open ( stream ) {
249215 var path = NODEFS . realPath ( stream . node ) ;
250- try {
216+ NODEFS . tryFSOperation ( ( ) => {
251217 if ( FS . isFile ( stream . node . mode ) ) {
252218 stream . shared . refcount = 1 ;
253219 stream . nfd = fs . openSync ( path , NODEFS . flagsForNode ( stream . flags ) ) ;
254220 }
255- } catch ( e ) {
256- if ( ! e . code ) throw e ;
257- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
258- }
221+ } ) ;
259222 } ,
260223 close ( stream ) {
261- try {
224+ NODEFS . tryFSOperation ( ( ) => {
262225 if ( FS . isFile ( stream . node . mode ) && stream . nfd && -- stream . shared . refcount === 0 ) {
263226 fs . closeSync ( stream . nfd ) ;
264227 }
265- } catch ( e ) {
266- if ( ! e . code ) throw e ;
267- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
268- }
228+ } ) ;
269229 } ,
270230 dup ( stream ) {
271231 stream . shared . refcount ++ ;
272232 } ,
273233 read ( stream , buffer , offset , length , position ) {
274234 // Node.js < 6 compatibility: node errors on 0 length reads
275235 if ( length === 0 ) return 0 ;
276- try {
277- return fs . readSync ( stream . nfd , new Int8Array ( buffer . buffer , offset , length ) , 0 , length , position ) ;
278- } catch ( e ) {
279- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
280- }
236+ return NODEFS . tryFSOperation ( ( ) =>
237+ fs . readSync ( stream . nfd , new Int8Array ( buffer . buffer , offset , length ) , 0 , length , position )
238+ ) ;
281239 } ,
282240 write ( stream , buffer , offset , length , position ) {
283- try {
284- return fs . writeSync ( stream . nfd , new Int8Array ( buffer . buffer , offset , length ) , 0 , length , position ) ;
285- } catch ( e ) {
286- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
287- }
241+ return NODEFS . tryFSOperation ( ( ) =>
242+ fs . writeSync ( stream . nfd , new Int8Array ( buffer . buffer , offset , length ) , 0 , length , position )
243+ ) ;
288244 } ,
289245 llseek ( stream , offset , whence ) {
290246 var position = offset ;
291247 if ( whence === { { { cDefs . SEEK_CUR } } } ) {
292248 position + = stream . position ;
293249 } else if ( whence = = = { { { cDefs . SEEK_END } } } ) {
294250 if ( FS . isFile ( stream . node . mode ) ) {
295- try {
251+ NODEFS . tryFSOperation ( ( ) => {
296252 var stat = fs . fstatSync ( stream . nfd ) ;
297253 position += stat . size ;
298- } catch ( e ) {
299- throw new FS . ErrnoError ( NODEFS . convertNodeCode ( e ) ) ;
300- }
254+ } ) ;
301255 }
302256 }
303257
0 commit comments