diff options
Diffstat (limited to 'libvirt/volume_def.go')
-rw-r--r-- | libvirt/volume_def.go | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/libvirt/volume_def.go b/libvirt/volume_def.go index 0e72123d..281adf49 100644 --- a/libvirt/volume_def.go +++ b/libvirt/volume_def.go @@ -2,13 +2,49 @@ package libvirt import ( "encoding/xml" + "fmt" + "math" + "strconv" + "time" + + libvirt "github.com/dmacvicar/libvirt-go" ) +type UnixTimestamp struct{ time.Time } + +func (t *UnixTimestamp) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + var content string + if err := d.DecodeElement(&content, &start); err != nil { + return err + } + + ts, err := strconv.ParseFloat(content, 64) + if err != nil { + return err + } + s, ns := math.Modf(ts) + *t = UnixTimestamp{time.Time(time.Unix(int64(s), int64(ns)))} + return nil +} + +func (t *UnixTimestamp) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + s := t.UTC().Unix() + ns := t.UTC().UnixNano() + return e.EncodeElement(fmt.Sprintf("%d.%d", s, ns), start) +} + +type defTimestamps struct { + Change *UnixTimestamp `xml:"ctime,omitempty"` + Modification *UnixTimestamp `xml:"mtime,omitempty"` + Access *UnixTimestamp `xml:"atime,omitempty"` +} + type defBackingStore struct { Path string `xml:"path"` Format struct { Type string `xml:"type,attr"` } `xml:"format"` + Timestamps *defTimestamps `xml:"timestamps,omitempty"` } type defVolume struct { @@ -21,11 +57,12 @@ type defVolume struct { Permissions struct { Mode int `xml:"mode,omitempty"` } `xml:"permissions,omitempty"` + Timestamps *defTimestamps `xml:"timestamps,omitempty"` } `xml:"target"` Allocation int `xml:"allocation"` Capacity struct { Unit string `xml:"unit,attr"` - Amount int64 `xml:"chardata"` + Amount uint64 `xml:"chardata"` } `xml:"capacity"` BackingStore *defBackingStore `xml:"backingStore,omitempty"` } @@ -38,3 +75,29 @@ func newDefVolume() defVolume { volumeDef.Capacity.Amount = 1 return volumeDef } + +// Creates a volume definition from a XML +func newDefVolumeFromXML(s string) (defVolume, error) { + var volumeDef defVolume + err := xml.Unmarshal([]byte(s), &volumeDef) + if err != nil { + return defVolume{}, err + } + return volumeDef, nil +} + +func newDefVolumeFromLibvirt(volume *libvirt.VirStorageVol) (defVolume, error) { + name, err := volume.GetName() + if err != nil { + return defVolume{}, fmt.Errorf("could not get name for volume: %s.", err) + } + volumeDefXml, err := volume.GetXMLDesc(0) + if err != nil { + return defVolume{}, fmt.Errorf("could not get XML description for volume %s: %s.", name, err) + } + volumeDef, err := newDefVolumeFromXML(volumeDefXml) + if err != nil { + return defVolume{}, fmt.Errorf("could not get a volume definition from XML for %s: %s.", volumeDef.Name, err) + } + return volumeDef, nil +} |