2021-05-12 21:11:00 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-11-16 16:28:09 +00:00
|
|
|
"errors"
|
2021-09-08 13:18:53 +00:00
|
|
|
"fmt"
|
2021-05-12 21:11:00 +00:00
|
|
|
"io"
|
2021-09-08 13:18:53 +00:00
|
|
|
"os"
|
2021-05-12 21:11:00 +00:00
|
|
|
|
|
|
|
"github.com/anchore/syft/internal/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CloseAndLogError closes the given io.Closer and reports any errors found as a warning in the log
|
|
|
|
func CloseAndLogError(closer io.Closer, location string) {
|
|
|
|
if err := closer.Close(); err != nil {
|
|
|
|
log.Warnf("unable to close file for location=%q: %+v", location, err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 13:18:53 +00:00
|
|
|
|
|
|
|
type ErrPath struct {
|
2022-02-24 15:01:59 +00:00
|
|
|
Context string
|
|
|
|
Path string
|
|
|
|
Err error
|
2021-09-08 13:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrPath) Error() string {
|
2022-02-24 15:01:59 +00:00
|
|
|
return fmt.Sprintf("%s unable to observe contents of %+v: %v", e.Context, e.Path, e.Err)
|
2021-09-08 13:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsErrPath(err error) bool {
|
2022-11-16 16:28:09 +00:00
|
|
|
var pathErr ErrPath
|
|
|
|
return errors.As(err, &pathErr)
|
2021-09-08 13:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsErrPathPermission(err error) bool {
|
2022-11-16 16:28:09 +00:00
|
|
|
var pathErr ErrPath
|
|
|
|
if errors.As(err, &pathErr) {
|
2021-09-08 13:18:53 +00:00
|
|
|
return os.IsPermission(pathErr.Err)
|
|
|
|
}
|
2022-11-16 16:28:09 +00:00
|
|
|
return false
|
2021-09-08 13:18:53 +00:00
|
|
|
}
|