checking for duplicate filenames works

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-25 20:05:04 -05:00
parent 030af1bcfb
commit b35c04414a
4 changed files with 88 additions and 28 deletions

View File

@ -10,7 +10,7 @@ all:
./virtigo --help
xml-add:
./virtigo --libvirt ~/libvirt/*.xml --xml-ignore-disk
./virtigo --libvirt ~/libvirt/*.xml
start-all-droplets:
curl --silent http://localhost:8080/start?start=git.wit.org

View File

@ -166,7 +166,7 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error)
nete, err := updateNetwork(d, domcfg)
if err != nil {
log.Info("updateNetwork() failed", err)
return alle, errors.New("updateNetwork() failed")
return alle, err
}
for _, e := range nete {
@ -176,8 +176,14 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error)
log.Info("TODO: fix updateDisk() change events")
log.Info("TODO: fix updateDisk() change events")
log.Info("TODO: fix updateDisk() change events")
if !updateDisk(d, domcfg) {
return alle, errors.New("updateDisk() failed")
nete, err = updateDisk(d, domcfg)
if err != nil {
log.Info("updateDisk() failed", err)
return alle, err
}
for _, e := range nete {
alle = append(alle, e)
}
if alle == nil {
@ -185,8 +191,6 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error)
return alle, nil
}
log.Info("libvirt xml import worked. droplet changed", domcfg.Name)
// log.Info("all change events", alle)
me.changed = true
// append each change event
for _, e := range alle {
@ -349,9 +353,11 @@ func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error)
*/
// returns false if something went wrong
func updateDisk(d *DropletT, domcfg *libvirtxml.Domain) bool {
func updateDisk(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error) {
var alle []*pb.Event
if (d == nil) || (domcfg == nil) {
return false
return nil, errors.New("domcfg == nil")
}
for _, disk := range domcfg.Devices.Disks {
var t *libvirtxml.DomainDiskSourceFile
@ -366,21 +372,32 @@ func updateDisk(d *DropletT, domcfg *libvirtxml.Domain) bool {
continue
}
var found bool = false
for _, disk := range d.pb.Disks {
if disk.Filename == filename {
log.Verbose("OKAY. FOUND filename", filename)
found = true
e, err := insertFilename(d.pb, filename)
if err != nil {
return alle, err
}
if e == nil {
continue
}
alle = append(alle, e)
/*
var found bool = false
for _, disk := range d.pb.Disks {
if disk.Filename == filename {
log.Verbose("OKAY. FOUND filename", filename)
found = true
}
}
}
if !found {
var disk *pb.Disk
disk = new(pb.Disk)
disk.Filename = filename
d.pb.Disks = append(d.pb.Disks, disk)
log.Info("New filename", filename)
me.changed = true
}
if !found {
var disk *pb.Disk
disk = new(pb.Disk)
disk.Filename = filename
d.pb.Disks = append(d.pb.Disks, disk)
log.Info("New filename", filename)
me.changed = true
}
*/
}
return true
return alle, nil
}

View File

@ -63,7 +63,6 @@ func main() {
}
checkUniqueFilenames()
for _, filename := range argv.Xml {
domcfg, err := readXml(filename)
if err != nil {

View File

@ -14,6 +14,7 @@ package main
*/
import (
"errors"
"os"
"path/filepath"
@ -50,6 +51,49 @@ func addClusterFilepath(dir string) {
}
}
// returns the droplet using a filename
func lookupFilename(filename string) *pb.Droplet {
filebase := filepath.Base(filename)
for _, d := range me.cluster.Droplets {
for _, disk := range d.Disks {
if filebase == disk.Filename {
return d
}
}
}
return nil
}
func insertFilename(d *pb.Droplet, filename string) (*pb.Event, error) {
dupd := lookupFilename(filename)
if dupd != nil {
log.Info("file", filename, "already on droplet", dupd.Hostname)
log.Info("file", filename, "on new droplet", d.Hostname)
log.Info("duplicate disk names (--xml-ignore-disk to ignore)")
return nil, errors.New("duplicate disk names")
}
filebase := filepath.Base(filename)
dir := filepath.Dir(filename)
for _, disk := range d.Disks {
if disk.Filename == filebase {
log.Info("already have disk", filename)
return nil, nil
}
}
// make a new Add Event
e := NewChangeEvent(d, "Add Disk", "", filename)
// add the disk protobuf entry
var disk *pb.Disk
disk = new(pb.Disk)
disk.Filename = filebase
disk.Filepath = dir
d.Disks = append(d.Disks, disk)
log.Info("New filename", filebase, dir)
return e, nil
}
func checkUniqueFilenames() bool {
var ok bool = true
var disks map[string]string
@ -60,10 +104,10 @@ func checkUniqueFilenames() bool {
filename := disk.Filename
if _, ok := disks[filename]; ok {
/*
if argv.IgnDisk {
log.Info("ignore dup disk", filename, disks[filename], d.Hostname)
} else {
}
if argv.IgnDisk {
log.Info("ignore dup disk", filename, disks[filename], d.Hostname)
} else {
}
*/
log.Info("file", filename, "on droplet", disks[filename])
log.Info("file", filename, "on droplet", d.Hostname)