mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 08:53:15 +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",
|
"/dev",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrIgnoreIrregularFile = errors.New("ignoring irregular file type")
|
||||||
|
|
||||||
var _ FileResolver = (*directoryResolver)(nil)
|
var _ FileResolver = (*directoryResolver)(nil)
|
||||||
|
|
||||||
type pathFilterFn func(string) bool
|
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 {
|
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.
|
// 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)
|
log.Warnf("unable to access path=%q: %+v", path, err)
|
||||||
r.errPaths[path] = err
|
r.errPaths[path] = err
|
||||||
@ -176,6 +178,8 @@ func (r directoryResolver) addPathToIndex(p string, info os.FileInfo) (string, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
case IrregularFile:
|
||||||
|
return "", ErrIgnoreIrregularFile
|
||||||
default:
|
default:
|
||||||
ref, err = r.fileTree.AddFile(file.Path(p))
|
ref, err = r.fileTree.AddFile(file.Path(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
package source
|
package source
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/scylladb/go-set/strset"
|
"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 (
|
const (
|
||||||
UnknownFileType FileType = "UnknownFileType"
|
UnknownFileType FileType = "UnknownFileType"
|
||||||
RegularFile FileType = "RegularFile"
|
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"
|
HardLink FileType = "HardLink"
|
||||||
SymbolicLink FileType = "SymbolicLink"
|
SymbolicLink FileType = "SymbolicLink"
|
||||||
CharacterDevice FileType = "CharacterDevice"
|
CharacterDevice FileType = "CharacterDevice"
|
||||||
@ -45,6 +49,8 @@ func newFileTypeFromMode(mode os.FileMode) FileType {
|
|||||||
return SymbolicLink
|
return SymbolicLink
|
||||||
case mode.IsDir():
|
case mode.IsDir():
|
||||||
return Directory
|
return Directory
|
||||||
|
case !mode.IsRegular():
|
||||||
|
return IrregularFile
|
||||||
default:
|
default:
|
||||||
return RegularFile
|
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