package main
import (
"context"
"fmt"
"github.com/anchore/stereoscope"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/filetree"
"github.com/anchore/stereoscope/pkg/filetree/filenode"
"os"
)
func readImage(imagePath string) int {
// context for network requests
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var err error
fmt.Printf("opening image %s\n", imagePath)
img, err := stereoscope.GetImage(ctx, imagePath)
if err != nil {
return 1
}
// note: we are writing out temp files which should be cleaned up after you're done with the image object
defer img.Cleanup()
seenTypes := make(map[file.Type]int)
err = img.SquashedTree().Walk(
func(path file.Path, f filenode.FileNode) error {
if _, ok := seenTypes[f.FileType]; !ok {
seenTypes[f.FileType] = 0
}
seenTypes[f.FileType]++
if f.IsLink() {
fmt.Printf("path: %s\n", path)
}
return nil
},
&filetree.WalkConditions{
ShouldVisit: func(path file.Path, node filenode.FileNode) bool {
return !node.IsLink()
},
ShouldContinueBranch: func(path file.Path, node filenode.FileNode) bool {
if node.IsLink() {
fmt.Printf("skipping link %s\n", path)
return false
}
return true
},
},
)
fmt.Printf("Saw the following types:\n")
if count, ok := seenTypes[file.TypeRegular]; ok {
fmt.Printf(" TypeRegular (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeHardLink]; ok {
fmt.Printf(" TypeHardLink (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeSymLink]; ok {
fmt.Printf(" TypeSymLink (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeCharacterDevice]; ok {
fmt.Printf(" TypeCharacterDevice (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeBlockDevice]; ok {
fmt.Printf(" TypeBlockDevice (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeDirectory]; ok {
fmt.Printf(" TypeDirectory (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeFIFO]; ok {
fmt.Printf(" TypeFIFO (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeSocket]; ok {
fmt.Printf(" TypeSocket (%d times)\n", count)
}
if count, ok := seenTypes[file.TypeIrregular]; ok {
fmt.Printf(" TypeIrregular (%d times)\n", count)
}
if err != nil {
fmt.Printf("error: %v", err)
return 1
}
return 0
}
func main() {
os.Exit(readImage(os.Args[1]))
}