github.com/anchore/syft@v1.38.2/internal/err_helper.go (about)

     1  package internal
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"io/fs"
     8  	"os"
     9  
    10  	"github.com/anchore/syft/internal/log"
    11  )
    12  
    13  // CloseAndLogError closes the given io.Closer and reports any errors found as a warning in the log
    14  func CloseAndLogError(closer io.Closer, location string) {
    15  	if err := closer.Close(); err != nil {
    16  		// suppress "file already closed" log messages
    17  		if errors.Is(err, fs.ErrClosed) {
    18  			return
    19  		}
    20  		log.Debugf("unable to close file for location=%q: %+v", location, err)
    21  	}
    22  }
    23  
    24  type ErrPath struct {
    25  	Context string
    26  	Path    string
    27  	Err     error
    28  }
    29  
    30  func (e ErrPath) Error() string {
    31  	return fmt.Sprintf("%s unable to observe contents of %+v: %v", e.Context, e.Path, e.Err)
    32  }
    33  
    34  func IsErrPath(err error) bool {
    35  	var pathErr ErrPath
    36  	return errors.As(err, &pathErr)
    37  }
    38  
    39  func IsErrPathPermission(err error) bool {
    40  	var pathErr ErrPath
    41  	if errors.As(err, &pathErr) {
    42  		return os.IsPermission(pathErr.Err)
    43  	}
    44  	return false
    45  }