- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.4k
Refactor lookupPath to avoid recursion. NFC #23017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            sbc100
  merged 9 commits into
  emscripten-core:main
from
hoodmane:lookup-path-nonrecursive
  
      
      
   
  Dec 3, 2024 
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      1611617
              
                Remove recursive call
              
              
                hoodmane 240e164
              
                Address review comments
              
              
                hoodmane 05a1842
              
                Rebaseline code size
              
              
                hoodmane eb9c2b5
              
                Update changelog
              
              
                hoodmane f0a353c
              
                Revert "Rebaseline code size"
              
              
                hoodmane 8dd57fa
              
                Merge branch 'main' into lookup-path-nonrecursive
              
              
                hoodmane 4d87d91
              
                Revert accidental changelog entry
              
              
                hoodmane b5fc476
              
                Merge branch 'main' into lookup-path-nonrecursive
              
              
                hoodmane 1d9c156
              
                retrigger ci
              
              
                hoodmane File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -177,57 +177,47 @@ FS.staticInit(); | |
|  | ||
| var defaults = { | ||
| follow_mount: true, | ||
| recurse_count: 0 | ||
| }; | ||
| opts = Object.assign(defaults, opts) | ||
|  | ||
| if (opts.recurse_count > 8) { // max recursive lookup of 8 | ||
| throw new FS.ErrnoError({{{ cDefs.ELOOP }}}); | ||
| } | ||
|  | ||
| // split the absolute path | ||
| var parts = path.split('/').filter((p) => !!p); | ||
| // limit max consecutive symlinks to 40 (SYMLOOP_MAX). | ||
| linkloop: for (var nlinks = 0; nlinks < 40; nlinks ++) { | ||
|          | ||
| // split the absolute path | ||
| var parts = path.split('/').filter((p) => !!p); | ||
|  | ||
| // start at the root | ||
| var current = FS.root; | ||
| var current_path = '/'; | ||
| // start at the root | ||
| var current = FS.root; | ||
| var current_path = '/'; | ||
|  | ||
| for (var i = 0; i < parts.length; i++) { | ||
| var islast = (i === parts.length-1); | ||
| if (islast && opts.parent) { | ||
| // stop resolving | ||
| break; | ||
| } | ||
| for (var i = 0; i < parts.length; i++) { | ||
| var islast = (i === parts.length-1); | ||
| if (islast && opts.parent) { | ||
| // stop resolving | ||
| break; | ||
| } | ||
|  | ||
| current = FS.lookupNode(current, parts[i]); | ||
| current_path = PATH.join2(current_path, parts[i]); | ||
| current = FS.lookupNode(current, parts[i]); | ||
| current_path = PATH.join2(current_path, parts[i]); | ||
|  | ||
| // jump to the mount's root node if this is a mountpoint | ||
| if (FS.isMountpoint(current)) { | ||
| if (!islast || (islast && opts.follow_mount)) { | ||
| // jump to the mount's root node if this is a mountpoint | ||
| if (FS.isMountpoint(current) && (!islast || opts.follow_mount)) { | ||
| current = current.mounted.root; | ||
| } | ||
| } | ||
|  | ||
| // by default, lookupPath will not follow a symlink if it is the final path component. | ||
| // setting opts.follow = true will override this behavior. | ||
| if (!islast || opts.follow) { | ||
| var count = 0; | ||
| while (FS.isLink(current.mode)) { | ||
| var link = FS.readlink(current_path); | ||
| current_path = PATH_FS.resolve(PATH.dirname(current_path), link); | ||
|  | ||
| var lookup = FS.lookupPath(current_path, { recurse_count: opts.recurse_count + 1 }); | ||
| current = lookup.node; | ||
|  | ||
| if (count++ > 40) { // limit max consecutive symlinks to 40 (SYMLOOP_MAX). | ||
| throw new FS.ErrnoError({{{ cDefs.ELOOP }}}); | ||
| // by default, lookupPath will not follow a symlink if it is the final path component. | ||
| // setting opts.follow = true will override this behavior. | ||
| if (FS.isLink(current.mode) && (!islast || opts.follow)) { | ||
| if (!current.node_ops.readlink) { | ||
| throw new FS.ErrnoError({{{ cDefs.ENOSYS }}}); | ||
| } | ||
| var link = current.node_ops.readlink(current); | ||
| path = PATH_FS.resolve(PATH.dirname(current_path), link, ...parts.slice(i + 1)); | ||
| continue linkloop; | ||
| } | ||
| } | ||
| return { path: current_path, node: current }; | ||
| } | ||
|  | ||
| return { path: current_path, node: current }; | ||
| throw new FS.ErrnoError({{{ cDefs.ELOOP }}}); | ||
| }, | ||
| getPath(node) { | ||
| var path; | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.