mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
ignore irregular files while indexing directory (#616)
* stop hidding command from help doc Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com> * do not index irregular files Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com> * fix fixture dir and err name Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com> * improve the description of irregular files Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com> * explicitly check indexed file name Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>
This commit is contained in:
parent
23a67dd729
commit
6d0ee326d8
@ -25,6 +25,8 @@ var unixSystemRuntimePrefixes = []string{
|
||||
"/dev",
|
||||
}
|
||||
|
||||
var ErrIgnoreIrregularFile = errors.New("ignoring irregular file type")
|
||||
|
||||
var _ FileResolver = (*directoryResolver)(nil)
|
||||
|
||||
type pathFilterFn func(string) bool
|
||||
@ -137,7 +139,7 @@ func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error)
|
||||
}
|
||||
|
||||
func (r *directoryResolver) handleFileAccessErr(path string, err error) error {
|
||||
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) {
|
||||
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) || errors.Is(err, ErrIgnoreIrregularFile) {
|
||||
// don't allow for permission errors to stop indexing, keep track of the paths and continue.
|
||||
log.Warnf("unable to access path=%q: %+v", path, err)
|
||||
r.errPaths[path] = err
|
||||
@ -176,6 +178,8 @@ func (r directoryResolver) addPathToIndex(p string, info os.FileInfo) (string, e
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
case IrregularFile:
|
||||
return "", ErrIgnoreIrregularFile
|
||||
default:
|
||||
ref, err = r.fileTree.AddFile(file.Path(p))
|
||||
if err != nil {
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/scylladb/go-set/strset"
|
||||
@ -459,3 +462,29 @@ func Test_directoryResolver_FilesByMIMEType(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ignoreIrregularFiles(t *testing.T) {
|
||||
// NOTE: craeting a pipe/fifo file on demand since git doesn't let me
|
||||
// commit one. The directory resolver should ignore it (and any other
|
||||
// file that is neither regular, a symlink or directory) when indexing a directory
|
||||
dir := "./test-fixtures/irregular-files"
|
||||
f := "f.fifo"
|
||||
|
||||
err := syscall.Mknod(filepath.Join(dir, f), syscall.S_IFIFO|0666, 0)
|
||||
assert.NoError(t, err)
|
||||
defer func() {
|
||||
err := os.Remove(filepath.Join(dir, f))
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
|
||||
fileRefs, err := ioutil.ReadDir(dir)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, fileRefs, 2) // two files (f.fifo and readme)
|
||||
|
||||
resolver, err := newDirectoryResolver(dir)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, resolver.fileTree.AllFiles(), 1)
|
||||
rp := resolver.fileTree.AllFiles()[0].RealPath
|
||||
assert.True(t, strings.Contains(string(rp), filepath.Join(dir, "readme")))
|
||||
}
|
||||
|
||||
@ -8,6 +8,10 @@ import (
|
||||
const (
|
||||
UnknownFileType FileType = "UnknownFileType"
|
||||
RegularFile FileType = "RegularFile"
|
||||
// IrregularFile is how syft defines files that are neither regular, symbolic or directory.
|
||||
// For ref: the seven standard Unix file types are regular, directory, symbolic link,
|
||||
// FIFO special, block special, character special, and socket as defined by POSIX.
|
||||
IrregularFile FileType = "IrregularFile"
|
||||
HardLink FileType = "HardLink"
|
||||
SymbolicLink FileType = "SymbolicLink"
|
||||
CharacterDevice FileType = "CharacterDevice"
|
||||
@ -45,6 +49,8 @@ func newFileTypeFromMode(mode os.FileMode) FileType {
|
||||
return SymbolicLink
|
||||
case mode.IsDir():
|
||||
return Directory
|
||||
case !mode.IsRegular():
|
||||
return IrregularFile
|
||||
default:
|
||||
return RegularFile
|
||||
}
|
||||
|
||||
2
syft/source/test-fixtures/irregular-files/readme
Normal file
2
syft/source/test-fixtures/irregular-files/readme
Normal file
@ -0,0 +1,2 @@
|
||||
this directory exists for unit tests on irregular files. You can't see other files here because they are removed after each test.
|
||||
This readme is a better version of Russell's teapot.
|
||||
Loading…
x
Reference in New Issue
Block a user