check xml domain name vs xml filename

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-23 19:59:08 -05:00
parent d09f4e25c2
commit 37a053dae9
1 changed files with 12 additions and 0 deletions

12
xml.go
View File

@ -6,7 +6,9 @@ import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"go.wit.com/log"
"libvirt.org/go/libvirtxml"
@ -76,6 +78,9 @@ func addDefaults(d *libvirtxml.Domain, filename string) {
func readXml(filename string) (*libvirtxml.Domain, error) {
log.Verbose("parse xml file:", filename)
hostname := filepath.Base(filename)
hostname = strings.TrimSuffix(hostname, ".xml")
pfile, err := os.ReadFile(filename)
if err != nil {
log.Println("ERROR:", err)
@ -89,6 +94,13 @@ func readXml(filename string) (*libvirtxml.Domain, error) {
log.Info("Marshal failed on file", filename, err)
return nil, ErrorParseXML
}
if domcfg.Name != hostname {
log.Info("ERROR: domcfg.Name != name", domcfg.Name, hostname)
log.Info("ERROR: xml filenames must match the xml name")
os.Exit(-1)
}
return domcfg, nil
}