2023-11-24 14:57:32 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-01-11 09:28:58 +00:00
|
|
|
"slices"
|
2023-11-24 14:57:32 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Masterminds/semver/v3"
|
2023-12-19 16:20:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-11-24 14:57:32 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func isYAMLFileName(path string) bool {
|
|
|
|
return strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")
|
|
|
|
}
|
|
|
|
|
|
|
|
// linkTarget returns the target of a symlink, or empty string if it's dangling.
|
2023-12-19 16:20:09 +00:00
|
|
|
func linkTarget(path string, logger *logrus.Logger) (string, error) {
|
2023-11-24 14:57:32 +00:00
|
|
|
hubpath, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to read symlink: %s", path)
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("symlink %s -> %s", path, hubpath)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
_, err = os.Lstat(hubpath)
|
|
|
|
if os.IsNotExist(err) {
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Warningf("link target does not exist: %s -> %s", path, hubpath)
|
2023-11-24 14:57:32 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return hubpath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSHA256(filepath string) (string, error) {
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to open '%s': %w", filepath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return "", fmt.Errorf("unable to calculate sha256 of '%s': %w", filepath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// information used to create a new Item, from a file path.
|
|
|
|
type itemFileInfo struct {
|
|
|
|
inhub bool
|
|
|
|
fname string
|
|
|
|
stage string
|
|
|
|
ftype string
|
|
|
|
fauthor string
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
func (h *Hub) getItemFileInfo(path string, logger *logrus.Logger) (*itemFileInfo, error) {
|
2023-11-24 14:57:32 +00:00
|
|
|
var ret *itemFileInfo
|
|
|
|
|
|
|
|
hubDir := h.local.HubDir
|
|
|
|
installDir := h.local.InstallDir
|
|
|
|
|
|
|
|
subs := strings.Split(path, string(os.PathSeparator))
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("path:%s, hubdir:%s, installdir:%s", path, hubDir, installDir)
|
|
|
|
logger.Tracef("subs:%v", subs)
|
2023-11-24 14:57:32 +00:00
|
|
|
// we're in hub (~/.hub/hub/)
|
|
|
|
if strings.HasPrefix(path, hubDir) {
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("in hub dir")
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
//.../hub/parsers/s00-raw/crowdsec/skip-pretag.yaml
|
|
|
|
//.../hub/scenarios/crowdsec/ssh_bf.yaml
|
|
|
|
//.../hub/profiles/crowdsec/linux.yaml
|
|
|
|
if len(subs) < 4 {
|
|
|
|
return nil, fmt.Errorf("path is too short: %s (%d)", path, len(subs))
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = &itemFileInfo{
|
|
|
|
inhub: true,
|
|
|
|
fname: subs[len(subs)-1],
|
|
|
|
fauthor: subs[len(subs)-2],
|
|
|
|
stage: subs[len(subs)-3],
|
|
|
|
ftype: subs[len(subs)-4],
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(path, installDir) { // we're in install /etc/crowdsec/<type>/...
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("in install dir")
|
2023-11-24 14:57:32 +00:00
|
|
|
if len(subs) < 3 {
|
|
|
|
return nil, fmt.Errorf("path is too short: %s (%d)", path, len(subs))
|
|
|
|
}
|
|
|
|
///.../config/parser/stage/file.yaml
|
|
|
|
///.../config/postoverflow/stage/file.yaml
|
|
|
|
///.../config/scenarios/scenar.yaml
|
|
|
|
///.../config/collections/linux.yaml //file is empty
|
|
|
|
ret = &itemFileInfo{
|
|
|
|
inhub: false,
|
|
|
|
fname: subs[len(subs)-1],
|
|
|
|
stage: subs[len(subs)-2],
|
|
|
|
ftype: subs[len(subs)-3],
|
|
|
|
fauthor: "",
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("file '%s' is not from hub '%s' nor from the configuration directory '%s'", path, hubDir, installDir)
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("stage:%s ftype:%s", ret.stage, ret.ftype)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if ret.ftype != PARSERS && ret.ftype != POSTOVERFLOWS {
|
|
|
|
if !slices.Contains(ItemTypes, ret.stage) {
|
|
|
|
return nil, fmt.Errorf("unknown configuration type for file '%s'", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.ftype = ret.stage
|
2023-11-24 14:57:32 +00:00
|
|
|
ret.stage = ""
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Tracef("CORRECTED [%s] by [%s] in stage [%s] of type [%s]", ret.fname, ret.fauthor, ret.stage, ret.ftype)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortedVersions returns the input data, sorted in reverse order (new, old) by semver.
|
|
|
|
func sortedVersions(raw []string) ([]string, error) {
|
|
|
|
vs := make([]*semver.Version, len(raw))
|
|
|
|
|
|
|
|
for idx, r := range raw {
|
|
|
|
v, err := semver.NewVersion(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vs[idx] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(semver.Collection(vs)))
|
|
|
|
|
|
|
|
ret := make([]string, len(vs))
|
|
|
|
for idx, v := range vs {
|
|
|
|
ret[idx] = v.Original()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLocalItem(h *Hub, path string, info *itemFileInfo) (*Item, error) {
|
|
|
|
type localItemName struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
_, fileName := filepath.Split(path)
|
|
|
|
|
|
|
|
item := &Item{
|
|
|
|
hub: h,
|
|
|
|
Name: info.fname,
|
|
|
|
Stage: info.stage,
|
|
|
|
Type: info.ftype,
|
|
|
|
FileName: fileName,
|
|
|
|
State: ItemState{
|
|
|
|
LocalPath: path,
|
|
|
|
Installed: true,
|
|
|
|
UpToDate: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to read the name from the file
|
|
|
|
itemName := localItemName{}
|
|
|
|
|
|
|
|
itemContent, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(itemContent, &itemName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if itemName.Name != "" {
|
|
|
|
item.Name = itemName.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hub) itemVisit(path string, f os.DirEntry, err error) error {
|
|
|
|
hubpath := ""
|
|
|
|
|
|
|
|
if err != nil {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Debugf("while syncing hub dir: %s", err)
|
2023-11-24 14:57:32 +00:00
|
|
|
// there is a path error, we ignore the file
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// only happens if the current working directory was removed (!)
|
|
|
|
path, err = filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only care about YAML files
|
|
|
|
if f == nil || f.IsDir() || !isYAMLFileName(f.Name()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
info, err := h.getItemFileInfo(path, h.logger)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// non symlinks are local user files or hub files
|
|
|
|
if f.Type()&os.ModeSymlink == 0 {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("%s is not a symlink", path)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if !info.inhub {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("%s is a local file, skip", path)
|
2023-12-15 14:27:22 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
item, err := newLocalItem(h, path, info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-15 14:27:22 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
h.addItem(item)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-19 16:20:09 +00:00
|
|
|
hubpath, err = linkTarget(path, h.logger)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hubpath == "" {
|
|
|
|
// target does not exist, the user might have removed the file
|
|
|
|
// or switched to a hub branch without it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to find which configuration item it is
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("check [%s] of %s", info.fname, info.ftype)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
for _, item := range h.GetItemMap(info.ftype) {
|
2023-11-24 14:57:32 +00:00
|
|
|
if info.fname != item.FileName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Stage != info.stage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we are walking hub dir, just mark present files as downloaded
|
|
|
|
if info.inhub {
|
|
|
|
// wrong author
|
|
|
|
if info.fauthor != item.Author {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// not the item we're looking for
|
|
|
|
if !item.validPath(info.fauthor, info.fname) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := item.downloadPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == src {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("marking %s as downloaded", item.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
item.State.Downloaded = true
|
|
|
|
}
|
|
|
|
} else if !hasPathSuffix(hubpath, item.RemotePath) {
|
|
|
|
// wrong file
|
|
|
|
// <type>/<stage>/<author>/<name>.yaml
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := item.setVersionState(path, info.inhub)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Infof("Ignoring file %s of type %s", path, info.ftype)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkSubItemVersions checks for the presence, taint and version state of sub-items.
|
2023-12-15 14:27:22 +00:00
|
|
|
func (i *Item) checkSubItemVersions() []string {
|
|
|
|
warn := make([]string, 0)
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if !i.HasSubItems() {
|
2023-12-15 14:27:22 +00:00
|
|
|
return warn
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if i.versionStatus() != versionUpToDate {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("%s dependencies not checked: not up-to-date", i.Name)
|
2023-12-15 14:27:22 +00:00
|
|
|
return warn
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure all the sub-items are installed, or tag the parent as tainted
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("checking submembers of %s installed:%t", i.Name, i.State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
for _, sub := range i.SubItems() {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("check %s installed:%t", sub.Name, sub.State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if !i.State.Installed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
if w := sub.checkSubItemVersions(); len(w) > 0 {
|
2023-11-24 14:57:32 +00:00
|
|
|
if sub.State.Tainted {
|
2023-12-15 14:27:22 +00:00
|
|
|
i.addTaint(sub)
|
|
|
|
warn = append(warn, fmt.Sprintf("%s is tainted by %s", i.Name, sub.FQName()))
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
warn = append(warn, w...)
|
|
|
|
|
|
|
|
continue
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sub.State.Tainted {
|
2023-12-15 14:27:22 +00:00
|
|
|
i.addTaint(sub)
|
|
|
|
warn = append(warn, fmt.Sprintf("%s is tainted by %s", i.Name, sub.FQName()))
|
|
|
|
|
|
|
|
continue
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sub.State.Installed && i.State.Installed {
|
2023-12-15 14:27:22 +00:00
|
|
|
i.addTaint(sub)
|
|
|
|
warn = append(warn, fmt.Sprintf("%s is tainted by missing %s", i.Name, sub.FQName()))
|
|
|
|
|
|
|
|
continue
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sub.State.UpToDate {
|
|
|
|
i.State.UpToDate = false
|
2023-12-15 14:27:22 +00:00
|
|
|
warn = append(warn, fmt.Sprintf("%s is tainted by outdated %s", i.Name, sub.FQName()))
|
|
|
|
|
|
|
|
continue
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("checking for %s - tainted:%t uptodate:%t", sub.Name, i.State.Tainted, i.State.UpToDate)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
return warn
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// syncDir scans a directory for items, and updates the Hub state accordingly.
|
|
|
|
func (h *Hub) syncDir(dir string) error {
|
2023-12-07 15:20:13 +00:00
|
|
|
// For each, scan PARSERS, POSTOVERFLOWS... and COLLECTIONS last
|
2023-11-24 14:57:32 +00:00
|
|
|
for _, scan := range ItemTypes {
|
|
|
|
// cpath: top-level item directory, either downloaded or installed items.
|
|
|
|
// i.e. /etc/crowdsec/parsers, /etc/crowdsec/hub/parsers, ...
|
|
|
|
cpath, err := filepath.Abs(fmt.Sprintf("%s/%s", dir, scan))
|
|
|
|
if err != nil {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Errorf("failed %s: %s", cpath, err)
|
2023-11-24 14:57:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// explicit check for non existing directory, avoid spamming log.Debug
|
|
|
|
if _, err = os.Stat(cpath); os.IsNotExist(err) {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("directory %s doesn't exist, skipping", cpath)
|
2023-11-24 14:57:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = filepath.WalkDir(cpath, h.itemVisit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a string in a sorted slice, case insensitive, and return the new slice.
|
|
|
|
func insertInOrderNoCase(sl []string, value string) []string {
|
|
|
|
i := sort.Search(len(sl), func(i int) bool {
|
|
|
|
return strings.ToLower(sl[i]) >= strings.ToLower(value)
|
|
|
|
})
|
|
|
|
|
|
|
|
return append(sl[:i], append([]string{value}, sl[i:]...)...)
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
func removeDuplicates(sl []string) []string {
|
|
|
|
seen := make(map[string]struct{}, len(sl))
|
|
|
|
j := 0
|
|
|
|
|
|
|
|
for _, v := range sl {
|
|
|
|
if _, ok := seen[v]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
seen[v] = struct{}{}
|
|
|
|
sl[j] = v
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
return sl[:j]
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// localSync updates the hub state with downloaded, installed and local items.
|
|
|
|
func (h *Hub) localSync() error {
|
|
|
|
err := h.syncDir(h.local.InstallDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to scan %s: %w", h.local.InstallDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.syncDir(h.local.HubDir); err != nil {
|
|
|
|
return fmt.Errorf("failed to scan %s: %w", h.local.HubDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
for _, item := range h.GetItemMap(COLLECTIONS) {
|
2023-11-24 14:57:32 +00:00
|
|
|
// check for cyclic dependencies
|
|
|
|
subs, err := item.descendants()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate the sub- and sub-sub-items with the collections they belong to
|
|
|
|
for _, sub := range subs {
|
|
|
|
sub.State.BelongsToCollections = insertInOrderNoCase(sub.State.BelongsToCollections, item.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !item.State.Installed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
vs := item.versionStatus()
|
|
|
|
switch vs {
|
|
|
|
case versionUpToDate: // latest
|
2023-12-15 14:27:22 +00:00
|
|
|
if w := item.checkSubItemVersions(); len(w) > 0 {
|
|
|
|
warnings = append(warnings, w...)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
case versionUpdateAvailable: // not up-to-date
|
|
|
|
warnings = append(warnings, fmt.Sprintf("update for collection %s available (currently:%s, latest:%s)", item.Name, item.State.LocalVersion, item.Version))
|
|
|
|
case versionFuture:
|
|
|
|
warnings = append(warnings, fmt.Sprintf("collection %s is in the future (currently:%s, latest:%s)", item.Name, item.State.LocalVersion, item.Version))
|
|
|
|
case versionUnknown:
|
2023-12-05 12:38:52 +00:00
|
|
|
if !item.State.IsLocal() {
|
2023-12-15 14:27:22 +00:00
|
|
|
warnings = append(warnings, fmt.Sprintf("collection %s is tainted by local changes (latest:%s)", item.Name, item.Version))
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Debugf("installed (%s) - status: %d | installed: %s | latest: %s | full: %+v", item.Name, vs, item.State.LocalVersion, item.Version, item.Versions)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
h.Warnings = removeDuplicates(warnings)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Item) setVersionState(path string, inhub bool) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
i.State.LocalHash, err = getSHA256(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get sha256 of %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's reverse sort the versions to deal with hash collisions (#154)
|
|
|
|
versions := make([]string, 0, len(i.Versions))
|
|
|
|
for k := range i.Versions {
|
|
|
|
versions = append(versions, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
versions, err = sortedVersions(versions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("while syncing %s %s: %w", i.Type, i.FileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.State.LocalVersion = "?"
|
|
|
|
|
|
|
|
for _, version := range versions {
|
|
|
|
if i.Versions[version].Digest == i.State.LocalHash {
|
|
|
|
i.State.LocalVersion = version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.State.LocalVersion == "?" {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("got tainted match for %s: %s", i.Name, path)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if !inhub {
|
|
|
|
i.State.LocalPath = path
|
|
|
|
i.State.Installed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
i.State.UpToDate = false
|
2023-12-15 14:27:22 +00:00
|
|
|
i.addTaint(i)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// we got an exact match, update struct
|
|
|
|
|
|
|
|
i.State.Downloaded = true
|
|
|
|
|
|
|
|
if !inhub {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("found exact match for %s, version is %s, latest is %s", i.Name, i.State.LocalVersion, i.Version)
|
2023-11-24 14:57:32 +00:00
|
|
|
i.State.LocalPath = path
|
|
|
|
i.State.Tainted = false
|
|
|
|
// if we're walking the hub, present file doesn't means installed file
|
|
|
|
i.State.Installed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.State.LocalVersion == i.Version {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("%s is up-to-date", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
i.State.UpToDate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|