@@ -175,60 +175,46 @@ FS.staticInit();
175175      path =  PATH_FS . resolve ( path ) ; 
176176
177177      if  ( ! path )  return  {  path : '' ,  node : null  } ; 
178+       opts . follow_mount  ??=  true 
178179
179-       var  defaults  =  { 
180-         follow_mount : true , 
181-         recurse_count : 0 
182-       } ; 
183-       opts  =  Object . assign ( defaults ,  opts ) 
184- 
185-       if  ( opts . recurse_count  >  8 )  {   // max recursive lookup of 8 
186-         throw  new  FS . ErrnoError ( { { {  cDefs. ELOOP  } } } ) ; 
187-       } 
180+       // limit max consecutive symlinks to 40 (SYMLOOP_MAX). 
181+       linkloop : for  ( var  nlinks  =  0 ;  nlinks  <  40 ;  nlinks ++ )  { 
182+         // split the absolute path 
183+         var  parts =  path . split ( '/' ) . filter ( ( p )  =>  ! ! p ) ; 
188184
189-       // split the absolute path 
190-       var  parts  =  path . split ( '/' ) . filter ( ( p )  =>  ! ! p ) ; 
185+         // start at the root 
186+         var  current  =  FS . root ; 
187+         var  current_path  =  '/' ; 
191188
192-       // start at the root 
193-       var  current  =  FS . root ; 
194-       var  current_path  =  '/' ; 
195- 
196-       for  ( var  i  =  0 ;  i  <  parts . length ;  i ++ )  { 
197-         var  islast  =  ( i  ===  parts . length - 1 ) ; 
198-         if  ( islast  &&  opts . parent )  { 
199-           // stop resolving 
200-           break; 
201-         } 
189+         for  ( var  i  =  0 ;  i  <  parts . length ;  i ++ )  { 
190+           var  islast =  ( i  ===  parts . length - 1 ) ; 
191+           if  ( islast  &&  opts . parent )  { 
192+             // stop resolving 
193+             break; 
194+           } 
202195
203-         current  =  FS . lookupNode ( current ,  parts [ i ] ) ; 
204-         current_path  =  PATH . join2 ( current_path ,  parts [ i ] ) ; 
196+            current  =  FS . lookupNode ( current ,  parts [ i ] ) ; 
197+            current_path  =  PATH . join2 ( current_path ,  parts [ i ] ) ; 
205198
206-         // jump to the mount's root node if this is a mountpoint 
207-         if  ( FS . isMountpoint ( current ) )  { 
208-           if  ( ! islast  ||  ( islast  &&  opts . follow_mount ) )  { 
199+           // jump to the mount's root node if this is a mountpoint 
200+           if  ( FS . isMountpoint ( current )  &&  ( ! islast  ||  opts . follow_mount ) )  { 
209201            current =  current . mounted . root ; 
210202          } 
211-         } 
212- 
213-         // by default, lookupPath will not follow a symlink if it is the final path component. 
214-         // setting opts.follow = true will override this behavior. 
215-         if  ( ! islast  ||  opts . follow )  { 
216-           var  count  =  0 ; 
217-           while  ( FS . isLink ( current . mode ) )  { 
218-             var  link  =  FS . readlink ( current_path ) ; 
219-             current_path  =  PATH_FS . resolve ( PATH . dirname ( current_path ) ,  link ) ; 
220203
221-              var   lookup   =   FS . lookupPath ( current_path ,   {   recurse_count :  opts . recurse_count   +   1   } ) ; 
222-              current  =  lookup . node ; 
223- 
224-             if  ( count ++   >   40 )  {    // limit max consecutive symlinks to 40 (SYMLOOP_MAX). 
225-               throw  new  FS . ErrnoError ( { { {  cDefs. ELOOP  } } } ) ; 
204+           // by default, lookupPath will not follow a symlink if it is the final path component. 
205+           // setting opts.follow  = true will override this behavior. 
206+            if   ( FS . isLink ( current . mode )   &&   ( ! islast   ||   opts . follow ) )   { 
207+             if  ( ! current . node_ops . readlink )  { 
208+               throw  new  FS . ErrnoError ( { { {  cDefs . ENOSYS  } } } ) ; 
226209            } 
210+             var  link  =  current . node_ops . readlink ( current ) ; 
211+             path  =  PATH_FS . resolve ( PATH . dirname ( current_path ) ,  link ,  ...parts . slice ( i  +  1 ) ) ; 
212+             continue  linkloop ; 
227213          } 
228214        } 
215+         return  {  path : current_path ,  node : current  } ; 
229216      } 
230- 
231-       return  {  path : current_path ,  node : current  } ; 
217+       throw  new  FS . ErrnoError ( { { {  cDefs. ELOOP  } } } ) ; 
232218    } , 
233219    getPath ( node )  { 
234220      var  path ; 
@@ -373,6 +359,9 @@ FS.staticInit();
373359      return  0 ; 
374360    } , 
375361    mayCreate ( dir ,  name )  { 
362+       if  ( ! FS . isDir ( dir . mode ) )  { 
363+         return  { { {  cDefs . ENOTDIR  } } } ; 
364+       } 
376365      try  { 
377366        var  node  =  FS . lookupNode ( dir ,  name ) ; 
378367        return  { { {  cDefs . EEXIST  } } } ; 
0 commit comments