mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Add IDMatchesFilename function
This commit is contained in:
parent
3590cf1ef2
commit
04d2c96be0
1 changed files with 18 additions and 0 deletions
18
util/file.go
18
util/file.go
|
|
@ -9,6 +9,7 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -38,6 +39,23 @@ func ConformingFileName(fname string) bool {
|
||||||
return fname == CleanFileName(fname)
|
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.
|
// PathExists returns true if path exits.
|
||||||
func PathExists(path string) (bool, error) {
|
func PathExists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue