wa-lang.org/wazero@v1.0.2/cmd/wazero/compositefs.go (about)

     1  package main
     2  
     3  import (
     4  	"io/fs"
     5  	"strings"
     6  )
     7  
     8  type compositeFS struct {
     9  	paths map[string]fs.FS
    10  }
    11  
    12  func (c *compositeFS) Open(name string) (fs.File, error) {
    13  	if !fs.ValidPath(name) {
    14  		return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
    15  	}
    16  	for path, f := range c.paths {
    17  		if !strings.HasPrefix(name, path) {
    18  			continue
    19  		}
    20  		rest := name[len(path):]
    21  		if len(rest) == 0 {
    22  			// Special case reading directory
    23  			rest = "."
    24  		} else {
    25  			// fs.Open requires a relative path
    26  			if rest[0] == '/' {
    27  				rest = rest[1:]
    28  			}
    29  		}
    30  		file, err := f.Open(rest)
    31  		if err == nil {
    32  			return file, err
    33  		}
    34  	}
    35  
    36  	return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
    37  }