From 72c4b2518a77ee9952a0fa25ae671f06d8d85570 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 26 Jan 2020 22:30:00 -0500 Subject: it compiles --- rrdformat/format.go | 60 ++++++++++------ rrdformat/rrdbinary/errors.go | 2 +- rrdformat/rrdbinary/errors_test.go | 4 +- rrdformat/rrdbinary/types.go | 2 +- rrdformat/rrdbinary/unmarshal.go | 6 +- rrdformat/sniff.go | 141 +++++++++++++++++++------------------ 6 files changed, 118 insertions(+), 97 deletions(-) diff --git a/rrdformat/format.go b/rrdformat/format.go index 71bd966..8898ffd 100644 --- a/rrdformat/format.go +++ b/rrdformat/format.go @@ -1,11 +1,8 @@ package rrdformat import ( - "bytes" - "encoding" - "encoding/binary" + //"encoding" "encoding/xml" - "math" "git.lukeshu.com/go/librrd/rrdformat/rrdbinary" ) @@ -48,12 +45,12 @@ const XMLNS = "https://oss.oetiker.ch/rrdtool/rrdtool-dump.xml" type RRDValue = rrdbinary.Float type Header struct { - Cookie rrdbinary.String `rrdbinary:"size=3" xml:"-"` - Version rrdbinary.String `rrdbinary:"size=4" xml:"version"` + Cookie rrdbinary.String `rrdbinary:"size=4" xml:"-"` + Version rrdbinary.String `rrdbinary:"size=5" xml:"version"` FloatCookie rrdbinary.Float `xml:"-"` DSCnt rrdbinary.Uint `xml:"-"` RRACnt rrdbinary.Uint `xml:"-"` - DPDStep rrdbinary.Uint `xml:"step"` + PDPStep rrdbinary.Uint `xml:"step"` Parameters [10]rrdbinary.Unival `xml:"-"` } @@ -66,10 +63,10 @@ type DSDef struct { type RRADef struct { CFName rrdbinary.String `rrdbinary:"size=20"` RowCnt rrdbinary.Uint - PDPCnt rrdBinary.Uint + PDPCnt rrdbinary.Uint } -type Timestamp struct { +type Time struct { Sec rrdbinary.Time Usec rrdbinary.Int // signed, but always >= 0 } @@ -92,24 +89,41 @@ type RRDv0004 = RRDv0003 type RRDv0003 struct { Header Header - DSDefs []DSDef - RRADefs []RRADef - LastUpdated Timestamp - PDPPrep TODO - CPDPrep TODO - RRAPtr TODO - Values []RRDValue + DSDefs []DSDef // .Header.DSCnt + RRADefs []RRADef // .Header.RRACnt + LastUpdated Time + PDPPrep []PDPPrep // .Header.DSCnt + CDPPrep []CDPPrep // .Header.DSCnt * .Header.RRACnt + RRAPtr []RRAPtr // .Header.RRACnt + Values []RRDValue // Σ .RRADefs[i].RowCnt*.Header.DsCnt } type RRDv0002 = RRDv0001 type RRDv0001 struct { Header Header - DSDefs []DSDef - RRADefs []RRADef - LastUpdated rrdbinary.Timestamp - PDPPrep TODO - CPDPrep TODO - RRAPtr TODO - Values []RRDValue + DSDefs []DSDef // .Header.DSCnt + RRADefs []RRADef // .Header.RRACnt + LastUpdated rrdbinary.Time + PDPPrep []PDPPrep // .Header.DSCnt + CDPPrep []CDPPrep // .Header.DSCnt * .Header.RRACnt + RRAPtr []RRAPtr // .Header.RRACnt + Values []RRDValue // Σ .RRADefs[i].RowCnt*.Header.DsCnt } + +func (h *Header) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + if err := e.EncodeElement(h.Version, xml.StartElement{Name: xml.Name{Local: "version", Space: XMLNS}}); err != nil { + return err + } + if err := e.EncodeElement(h.PDPStep, xml.StartElement{Name: xml.Name{Local: "step", Space: XMLNS}}); err != nil { + return err + } + return nil +} + +//var _ encoding.BinaryMarshaler = &Header{} +//var _ encoding.BinaryUnmarshaler = &Header{} + +var _ xml.Marshaler = &Header{} + +//var _ xml.Unmarshaler = &Header{} diff --git a/rrdformat/rrdbinary/errors.go b/rrdformat/rrdbinary/errors.go index 984e0a5..1e61af0 100644 --- a/rrdformat/rrdbinary/errors.go +++ b/rrdformat/rrdbinary/errors.go @@ -13,7 +13,7 @@ type BinaryError struct { ctxEOF bool } -func newBinError(msg string, ctxFile []byte, ctxStart, ctxLen int) error { +func NewBinError(msg string, ctxFile []byte, ctxStart, ctxLen int) error { if ctxStart+ctxLen > len(ctxFile) { ctxLen = len(ctxFile) - ctxStart } diff --git a/rrdformat/rrdbinary/errors_test.go b/rrdformat/rrdbinary/errors_test.go index f65929b..86ae809 100644 --- a/rrdformat/rrdbinary/errors_test.go +++ b/rrdformat/rrdbinary/errors_test.go @@ -11,7 +11,7 @@ func TestBinaryError(t *testing.T) { assert := assert.New(t) bad404 := []byte(`|<1111----ds_cnt---0000>| // 64be | R| R| D|\0| 0| 0| 0| 3|\0| |<----doublecookie----->|<0000----ds_cnt---1111>| if len(data) < restOffset+8 { - return newBinError("unexpected end of file", data, restOffset, 8) + return rrdbinary.Architecture{}, rrdbinary.NewBinError("unexpected end of file", data, restOffset, 8) } offset := map[binary.ByteOrder]int{ binary.BigEndian: restOffset, // 24 in the above diagram binary.LittleEndian: restOffset + 4, // 28 in the above diagram - }[h.ByteOrder] - if h.ByteOrder.Uint32(data[offset:]) == 0 { - h.IntWidth = 8 - h.IntAlign = 8 + }[arch.ByteOrder] + if arch.ByteOrder.Uint32(data[offset:]) == 0 { + arch.IntWidth = 8 + arch.IntAlign = 8 } else { - h.IntWidth = 4 - h.IntAlign = 4 + arch.IntWidth = 4 + arch.IntAlign = 4 } } - return nil -} - -func (h *Header) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - if err := e.EncodeElement(h.Version, xml.StartElement{Name: xml.Name{Local: "version", Space: XMLNS}}); err != nil { - return err - } - if err := e.EncodeElement(h.PDPStep, xml.StartElement{Name: xml.Name{Local: "step", Space: XMLNS}}); err != nil { - return err - } - return nil -} - -//var _ encoding.BinaryMarshaler = &Header{} -var _ encoding.BinaryUnmarshaler = &Header{} + // The above just os happens that FloatXXX >= IntXXX, so we + // can just set the Unival stuff to the Float Stuff. + arch.UnivalWidth = arch.FloatWidth // max(FloatWidth, IntWidth) + arch.UnivalAlign = arch.FloatAlign // max(FloatAlign, IntAlign) -var _ xml.Marshaler = &Header{} + // FIXME: Figure out how to sniff the sizeof(time_t). + // + // javascriptRRD assumes that it's the same as sizeof(long), + // which his historically been true, but + // - isn't true of proprietary 32-bit Unixen that are 2038-safe + // - isn't true of the Linux kernel with the x32 ABI + arch.TimeWidth = arch.IntWidth + arch.TimeAlign = arch.IntAlign -//var _ xml.Unmarshaler = &Header{} + return arch, nil +} -- cgit v1.2.3