1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Add IDMatchesFilename function

This commit is contained in:
Bernhard Herzog 2023-04-25 17:08:14 +02:00
parent 3590cf1ef2
commit 04d2c96be0

View file

@ -9,6 +9,7 @@
package util
import (
"fmt"
"io"
"math/rand"
"os"
@ -38,6 +39,23 @@ func ConformingFileName(fname string) bool {
return fname == CleanFileName(fname)
}
// IDMatchesFilename checks that filename can be derived from the value
// of document/tracking/id extracted from doc using eval.
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#51-filename
func IDMatchesFilename(eval *PathEval, doc any, filename string) error {
var id string
if err := eval.Extract(`$.document.tracking.id`, StringMatcher(&id), false, doc); err != nil {
return fmt.Errorf("check that ID matches filename: %v", err)
}
if CleanFileName(id) != filename {
return fmt.Errorf("document/tracking/id %q does not match filename %s",
id, filename)
}
return nil
}
// PathExists returns true if path exits.
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)