github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/internal/fileresolver/unindexed_directory.go (about)

     1  package fileresolver
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/fs"
     7  	"os"
     8  	"path"
     9  	"sort"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/bmatcuk/doublestar/v4"
    14  	"github.com/mitchellh/go-homedir"
    15  	"github.com/spf13/afero"
    16  	"golang.org/x/exp/slices"
    17  
    18  	"github.com/anchore/syft/internal/log"
    19  	"github.com/anchore/syft/syft/file"
    20  )
    21  
    22  var _ file.Resolver = (*UnindexedDirectory)(nil)
    23  var _ file.WritableResolver = (*UnindexedDirectory)(nil)
    24  
    25  type UnindexedDirectory struct {
    26  	ls   afero.Lstater
    27  	lr   afero.LinkReader
    28  	base string
    29  	dir  string
    30  	fs   afero.Fs
    31  }
    32  
    33  func NewFromUnindexedDirectory(dir string) file.WritableResolver {
    34  	return NewFromUnindexedDirectoryFS(afero.NewOsFs(), dir, "")
    35  }
    36  
    37  func NewFromRootedUnindexedDirectory(dir string, base string) file.WritableResolver {
    38  	return NewFromUnindexedDirectoryFS(afero.NewOsFs(), dir, base)
    39  }
    40  
    41  func NewFromUnindexedDirectoryFS(fs afero.Fs, dir string, base string) file.WritableResolver {
    42  	ls, ok := fs.(afero.Lstater)
    43  	if !ok {
    44  		panic(fmt.Sprintf("unable to get afero.Lstater interface from: %+v", fs))
    45  	}
    46  	lr, ok := fs.(afero.LinkReader)
    47  	if !ok {
    48  		panic(fmt.Sprintf("unable to get afero.Lstater interface from: %+v", fs))
    49  	}
    50  	expanded, err := homedir.Expand(dir)
    51  	if err == nil {
    52  		dir = expanded
    53  	}
    54  	if base != "" {
    55  		expanded, err = homedir.Expand(base)
    56  		if err == nil {
    57  			base = expanded
    58  		}
    59  	}
    60  	wd, err := os.Getwd()
    61  	if err == nil {
    62  		if !path.IsAbs(dir) {
    63  			dir = path.Clean(path.Join(wd, dir))
    64  		}
    65  		if base != "" && !path.IsAbs(base) {
    66  			base = path.Clean(path.Join(wd, base))
    67  		}
    68  	}
    69  	return UnindexedDirectory{
    70  		base: base,
    71  		dir:  dir,
    72  		fs:   fs,
    73  		ls:   ls,
    74  		lr:   lr,
    75  	}
    76  }
    77  
    78  func (u UnindexedDirectory) FileContentsByLocation(location file.Location) (io.ReadCloser, error) {
    79  	p := u.absPath(u.scrubInputPath(location.RealPath))
    80  	f, err := u.fs.Open(p)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	fi, err := f.Stat()
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	if fi.IsDir() {
    89  		return nil, fmt.Errorf("unable to get contents of directory: %s", location.RealPath)
    90  	}
    91  	return f, nil
    92  }
    93  
    94  // - full symlink resolution should be performed on all requests
    95  // - returns locations for any file or directory
    96  func (u UnindexedDirectory) HasPath(p string) bool {
    97  	locs, err := u.filesByPath(true, true, p)
    98  	return err == nil && len(locs) > 0
    99  }
   100  
   101  func (u UnindexedDirectory) canLstat(p string) bool {
   102  	_, _, err := u.ls.LstatIfPossible(u.absPath(p))
   103  	return err == nil
   104  }
   105  
   106  func (u UnindexedDirectory) isRegularFile(p string) bool {
   107  	fi, _, err := u.ls.LstatIfPossible(u.absPath(p))
   108  	return err == nil && !fi.IsDir()
   109  }
   110  
   111  func (u UnindexedDirectory) scrubInputPath(p string) string {
   112  	if path.IsAbs(p) {
   113  		p = p[1:]
   114  	}
   115  	return path.Clean(p)
   116  }
   117  
   118  func (u UnindexedDirectory) scrubResolutionPath(p string) string {
   119  	if u.base != "" {
   120  		if path.IsAbs(p) {
   121  			p = p[1:]
   122  		}
   123  		for strings.HasPrefix(p, "../") {
   124  			p = p[3:]
   125  		}
   126  	}
   127  	return path.Clean(p)
   128  }
   129  
   130  func (u UnindexedDirectory) absPath(p string) string {
   131  	if u.base != "" {
   132  		if path.IsAbs(p) {
   133  			p = p[1:]
   134  		}
   135  		for strings.HasPrefix(p, "../") {
   136  			p = p[3:]
   137  		}
   138  		p = path.Join(u.base, p)
   139  		return path.Clean(p)
   140  	}
   141  	if path.IsAbs(p) {
   142  		return p
   143  	}
   144  	return path.Clean(path.Join(u.dir, p))
   145  }
   146  
   147  // - full symlink resolution should be performed on all requests
   148  // - only returns locations to files (NOT directories)
   149  func (u UnindexedDirectory) FilesByPath(paths ...string) (out []file.Location, _ error) {
   150  	return u.filesByPath(true, false, paths...)
   151  }
   152  
   153  func (u UnindexedDirectory) filesByPath(resolveLinks bool, includeDirs bool, paths ...string) (out []file.Location, _ error) {
   154  	// sort here for stable output
   155  	sort.Strings(paths)
   156  nextPath:
   157  	for _, p := range paths {
   158  		p = u.scrubInputPath(p)
   159  		if u.canLstat(p) && (includeDirs || u.isRegularFile(p)) {
   160  			l := u.newLocation(p, resolveLinks)
   161  			if l == nil {
   162  				continue
   163  			}
   164  			// only include the first entry we find
   165  			for i := range out {
   166  				existing := &out[i]
   167  				if existing.RealPath == l.RealPath {
   168  					if l.VirtualPath == "" {
   169  						existing.VirtualPath = ""
   170  					}
   171  					continue nextPath
   172  				}
   173  			}
   174  			out = append(out, *l)
   175  		}
   176  	}
   177  	return
   178  }
   179  
   180  // - full symlink resolution should be performed on all requests
   181  // - if multiple paths to the same file are found, the best single match should be returned
   182  // - only returns locations to files (NOT directories)
   183  func (u UnindexedDirectory) FilesByGlob(patterns ...string) (out []file.Location, _ error) {
   184  	return u.filesByGlob(true, false, patterns...)
   185  }
   186  
   187  func (u UnindexedDirectory) filesByGlob(resolveLinks bool, includeDirs bool, patterns ...string) (out []file.Location, _ error) {
   188  	f := unindexedDirectoryResolverFS{
   189  		u: u,
   190  	}
   191  	var paths []string
   192  	for _, p := range patterns {
   193  		opts := []doublestar.GlobOption{doublestar.WithNoFollow()}
   194  		if !includeDirs {
   195  			opts = append(opts, doublestar.WithFilesOnly())
   196  		}
   197  		found, err := doublestar.Glob(f, p, opts...)
   198  		if err != nil {
   199  			return nil, err
   200  		}
   201  		paths = append(paths, found...)
   202  	}
   203  	return u.filesByPath(resolveLinks, includeDirs, paths...)
   204  }
   205  
   206  func (u UnindexedDirectory) FilesByMIMEType(_ ...string) ([]file.Location, error) {
   207  	panic("FilesByMIMEType unsupported")
   208  }
   209  
   210  // RelativeFileByPath fetches a single file at the given path relative to the layer squash of the given reference.
   211  // This is helpful when attempting to find a file that is in the same layer or lower as another file.
   212  func (u UnindexedDirectory) RelativeFileByPath(l file.Location, p string) *file.Location {
   213  	p = path.Clean(path.Join(l.RealPath, p))
   214  	locs, err := u.filesByPath(true, false, p)
   215  	if err != nil || len(locs) == 0 {
   216  		return nil
   217  	}
   218  	l = locs[0]
   219  	p = l.RealPath
   220  	if u.isRegularFile(p) {
   221  		return u.newLocation(p, true)
   222  	}
   223  	return nil
   224  }
   225  
   226  // - NO symlink resolution should be performed on results
   227  // - returns locations for any file or directory
   228  func (u UnindexedDirectory) AllLocations() <-chan file.Location {
   229  	out := make(chan file.Location)
   230  	go func() {
   231  		defer close(out)
   232  		err := afero.Walk(u.fs, u.absPath("."), func(p string, info fs.FileInfo, err error) error {
   233  			p = strings.TrimPrefix(p, u.dir)
   234  			if p == "" {
   235  				return nil
   236  			}
   237  			p = strings.TrimPrefix(p, "/")
   238  			out <- file.NewLocation(p)
   239  			return nil
   240  		})
   241  		if err != nil {
   242  			log.Debug(err)
   243  		}
   244  	}()
   245  	return out
   246  }
   247  
   248  func (u UnindexedDirectory) FileMetadataByLocation(_ file.Location) (file.Metadata, error) {
   249  	panic("FileMetadataByLocation unsupported")
   250  }
   251  
   252  func (u UnindexedDirectory) Write(location file.Location, reader io.Reader) error {
   253  	filePath := location.RealPath
   254  	if path.IsAbs(filePath) {
   255  		filePath = filePath[1:]
   256  	}
   257  	absPath := u.absPath(filePath)
   258  	return afero.WriteReader(u.fs, absPath, reader)
   259  }
   260  
   261  func (u UnindexedDirectory) newLocation(filePath string, resolveLinks bool) *file.Location {
   262  	filePath = path.Clean(filePath)
   263  
   264  	virtualPath := ""
   265  	realPath := filePath
   266  
   267  	if resolveLinks {
   268  		paths := u.resolveLinks(filePath)
   269  		if len(paths) > 1 {
   270  			realPath = paths[len(paths)-1]
   271  			if realPath != path.Clean(filePath) {
   272  				virtualPath = paths[0]
   273  			}
   274  		}
   275  		if len(paths) == 0 {
   276  			// this file does not exist, don't return a location
   277  			return nil
   278  		}
   279  	}
   280  
   281  	l := file.NewVirtualLocation(realPath, virtualPath)
   282  	return &l
   283  }
   284  
   285  //nolint:gocognit
   286  func (u UnindexedDirectory) resolveLinks(filePath string) []string {
   287  	var visited []string
   288  
   289  	out := []string{}
   290  
   291  	resolvedPath := ""
   292  
   293  	parts := strings.Split(filePath, "/")
   294  	for i := 0; i < len(parts); i++ {
   295  		part := parts[i]
   296  		if resolvedPath == "" {
   297  			resolvedPath = part
   298  		} else {
   299  			resolvedPath = path.Clean(path.Join(resolvedPath, part))
   300  		}
   301  		resolvedPath = u.scrubResolutionPath(resolvedPath)
   302  		if resolvedPath == ".." {
   303  			resolvedPath = ""
   304  			continue
   305  		}
   306  
   307  		absPath := u.absPath(resolvedPath)
   308  		if slices.Contains(visited, absPath) {
   309  			return nil // circular links can't resolve
   310  		}
   311  		visited = append(visited, absPath)
   312  
   313  		fi, wasLstat, err := u.ls.LstatIfPossible(absPath)
   314  		if fi == nil || err != nil {
   315  			// this file does not exist
   316  			return nil
   317  		}
   318  
   319  		for wasLstat && u.isSymlink(fi) {
   320  			next, err := u.lr.ReadlinkIfPossible(absPath)
   321  			if err == nil {
   322  				if !path.IsAbs(next) {
   323  					next = path.Clean(path.Join(path.Dir(resolvedPath), next))
   324  				}
   325  				next = u.scrubResolutionPath(next)
   326  				absPath = u.absPath(next)
   327  				if slices.Contains(visited, absPath) {
   328  					return nil // circular links can't resolve
   329  				}
   330  				visited = append(visited, absPath)
   331  
   332  				fi, wasLstat, err = u.ls.LstatIfPossible(absPath)
   333  				if fi == nil || err != nil {
   334  					// this file does not exist
   335  					return nil
   336  				}
   337  				if i < len(parts) {
   338  					out = append(out, path.Join(resolvedPath, path.Join(parts[i+1:]...)))
   339  				}
   340  				if u.base != "" && path.IsAbs(next) {
   341  					next = next[1:]
   342  				}
   343  				resolvedPath = next
   344  			}
   345  		}
   346  	}
   347  
   348  	out = append(out, resolvedPath)
   349  
   350  	return out
   351  }
   352  
   353  func (u UnindexedDirectory) isSymlink(fi os.FileInfo) bool {
   354  	return fi.Mode().Type()&fs.ModeSymlink == fs.ModeSymlink
   355  }
   356  
   357  // ------------------------- fs.FS ------------------------------
   358  
   359  // unindexedDirectoryResolverFS wraps the UnindexedDirectory as a fs.FS, fs.ReadDirFS, and fs.StatFS
   360  type unindexedDirectoryResolverFS struct {
   361  	u UnindexedDirectory
   362  }
   363  
   364  // resolve takes a virtual path and returns the resolved absolute or relative path and file info
   365  func (f unindexedDirectoryResolverFS) resolve(filePath string) (resolved string, fi fs.FileInfo, err error) {
   366  	parts := strings.Split(filePath, "/")
   367  	var visited []string
   368  	for i, part := range parts {
   369  		if i > 0 {
   370  			resolved = path.Clean(path.Join(resolved, part))
   371  		} else {
   372  			resolved = part
   373  		}
   374  		abs := f.u.absPath(resolved)
   375  		fi, _, err = f.u.ls.LstatIfPossible(abs)
   376  		if err != nil {
   377  			return resolved, fi, err
   378  		}
   379  		for f.u.isSymlink(fi) {
   380  			if slices.Contains(visited, resolved) {
   381  				return resolved, fi, fmt.Errorf("link cycle detected at: %s", f.u.absPath(resolved))
   382  			}
   383  			visited = append(visited, resolved)
   384  			link, err := f.u.lr.ReadlinkIfPossible(abs)
   385  			if err != nil {
   386  				return resolved, fi, err
   387  			}
   388  			if !path.IsAbs(link) {
   389  				link = path.Clean(path.Join(path.Dir(abs), link))
   390  				link = strings.TrimPrefix(link, abs)
   391  			} else if f.u.base != "" {
   392  				link = path.Clean(path.Join(f.u.base, link[1:]))
   393  			}
   394  			resolved = link
   395  			abs = f.u.absPath(resolved)
   396  			fi, _, err = f.u.ls.LstatIfPossible(abs)
   397  			if err != nil {
   398  				return resolved, fi, err
   399  			}
   400  		}
   401  	}
   402  	return resolved, fi, err
   403  }
   404  
   405  func (f unindexedDirectoryResolverFS) ReadDir(name string) (out []fs.DirEntry, _ error) {
   406  	p, _, err := f.resolve(name)
   407  	if err != nil {
   408  		return nil, err
   409  	}
   410  	entries, err := afero.ReadDir(f.u.fs, f.u.absPath(p))
   411  	if err != nil {
   412  		return nil, err
   413  	}
   414  	for _, e := range entries {
   415  		isDir := e.IsDir()
   416  		_, fi, _ := f.resolve(path.Join(name, e.Name()))
   417  		if fi != nil && fi.IsDir() {
   418  			isDir = true
   419  		}
   420  		out = append(out, unindexedDirectoryResolverDirEntry{
   421  			unindexedDirectoryResolverFileInfo: newFsFileInfo(f.u, e.Name(), isDir, e),
   422  		})
   423  	}
   424  	return out, nil
   425  }
   426  
   427  func (f unindexedDirectoryResolverFS) Stat(name string) (fs.FileInfo, error) {
   428  	fi, err := f.u.fs.Stat(f.u.absPath(name))
   429  	if err != nil {
   430  		return nil, err
   431  	}
   432  	return newFsFileInfo(f.u, name, fi.IsDir(), fi), nil
   433  }
   434  
   435  func (f unindexedDirectoryResolverFS) Open(name string) (fs.File, error) {
   436  	_, err := f.u.fs.Open(f.u.absPath(name))
   437  	if err != nil {
   438  		return nil, err
   439  	}
   440  
   441  	return unindexedDirectoryResolverFile{
   442  		u:    f.u,
   443  		path: name,
   444  	}, nil
   445  }
   446  
   447  var _ fs.FS = (*unindexedDirectoryResolverFS)(nil)
   448  var _ fs.StatFS = (*unindexedDirectoryResolverFS)(nil)
   449  var _ fs.ReadDirFS = (*unindexedDirectoryResolverFS)(nil)
   450  
   451  type unindexedDirectoryResolverDirEntry struct {
   452  	unindexedDirectoryResolverFileInfo
   453  }
   454  
   455  func (f unindexedDirectoryResolverDirEntry) Name() string {
   456  	return f.name
   457  }
   458  
   459  func (f unindexedDirectoryResolverDirEntry) IsDir() bool {
   460  	return f.isDir
   461  }
   462  
   463  func (f unindexedDirectoryResolverDirEntry) Type() fs.FileMode {
   464  	return f.mode
   465  }
   466  
   467  func (f unindexedDirectoryResolverDirEntry) Info() (fs.FileInfo, error) {
   468  	return f, nil
   469  }
   470  
   471  var _ fs.DirEntry = (*unindexedDirectoryResolverDirEntry)(nil)
   472  
   473  type unindexedDirectoryResolverFile struct {
   474  	u    UnindexedDirectory
   475  	path string
   476  }
   477  
   478  func (f unindexedDirectoryResolverFile) Stat() (fs.FileInfo, error) {
   479  	fi, err := f.u.fs.Stat(f.u.absPath(f.path))
   480  	if err != nil {
   481  		return nil, err
   482  	}
   483  	return newFsFileInfo(f.u, fi.Name(), fi.IsDir(), fi), nil
   484  }
   485  
   486  func (f unindexedDirectoryResolverFile) Read(_ []byte) (int, error) {
   487  	panic("Read not implemented")
   488  }
   489  
   490  func (f unindexedDirectoryResolverFile) Close() error {
   491  	panic("Close not implemented")
   492  }
   493  
   494  var _ fs.File = (*unindexedDirectoryResolverFile)(nil)
   495  
   496  type unindexedDirectoryResolverFileInfo struct {
   497  	u       UnindexedDirectory
   498  	name    string
   499  	size    int64
   500  	mode    fs.FileMode
   501  	modTime time.Time
   502  	isDir   bool
   503  	sys     any
   504  }
   505  
   506  func newFsFileInfo(u UnindexedDirectory, name string, isDir bool, fi os.FileInfo) unindexedDirectoryResolverFileInfo {
   507  	return unindexedDirectoryResolverFileInfo{
   508  		u:       u,
   509  		name:    name,
   510  		size:    fi.Size(),
   511  		mode:    fi.Mode() & ^fs.ModeSymlink, // pretend nothing is a symlink
   512  		modTime: fi.ModTime(),
   513  		isDir:   isDir,
   514  		// sys:     fi.Sys(), // what values does this hold?
   515  	}
   516  }
   517  
   518  func (f unindexedDirectoryResolverFileInfo) Name() string {
   519  	return f.name
   520  }
   521  
   522  func (f unindexedDirectoryResolverFileInfo) Size() int64 {
   523  	return f.size
   524  }
   525  
   526  func (f unindexedDirectoryResolverFileInfo) Mode() fs.FileMode {
   527  	return f.mode
   528  }
   529  
   530  func (f unindexedDirectoryResolverFileInfo) ModTime() time.Time {
   531  	return f.modTime
   532  }
   533  
   534  func (f unindexedDirectoryResolverFileInfo) IsDir() bool {
   535  	return f.isDir
   536  }
   537  
   538  func (f unindexedDirectoryResolverFileInfo) Sys() any {
   539  	return f.sys
   540  }
   541  
   542  var _ fs.FileInfo = (*unindexedDirectoryResolverFileInfo)(nil)