// Save persists a Siegfried struct to disk (path) func (s *Siegfried) Save(path string) error { ls := persist.NewLoadSaver(nil) ls.SaveTime(s.C) s.em.Save(ls) s.cm.Save(ls) s.bm.Save(ls) s.tm.Save(ls) ls.SaveTinyUInt(len(s.ids)) for _, i := range s.ids { i.Save(ls) } if ls.Err != nil { return ls.Err } f, err := os.Create(path) if err != nil { return err } defer f.Close() _, err = f.Write(append(config.Magic(), byte(config.Version()[0]), byte(config.Version()[1]))) if err != nil { return err } z, err := flate.NewWriter(f, 1) if err != nil { return err } _, err = z.Write(ls.Bytes()) z.Close() return err }
// Load creates a Siegfried struct and loads content from path func Load(path string) (*Siegfried, error) { errOpening := "siegfried: error opening signature file; got %v\nTry running `sf -update`" errNotSig := "siegfried: not a siegfried signature file" fbuf, err := ioutil.ReadFile(path) if err != nil { return nil, fmt.Errorf(errOpening, err) } if string(fbuf[:len(config.Magic())]) != string(config.Magic()) { return nil, fmt.Errorf(errNotSig) } r := bytes.NewBuffer(fbuf[len(config.Magic()):]) rc := flate.NewReader(r) defer rc.Close() buf, err := ioutil.ReadAll(rc) if err != nil { return nil, fmt.Errorf(errOpening, err) } ls := persist.NewLoadSaver(buf) if ls.LoadString() != "siegfried" { return nil, fmt.Errorf(errNotSig) } return &Siegfried{ C: ls.LoadTime(), em: extensionmatcher.Load(ls), cm: containermatcher.Load(ls), bm: bytematcher.Load(ls), tm: textmatcher.Load(ls), ids: func() []core.Identifier { ids := make([]core.Identifier, ls.LoadTinyUInt()) for i := range ids { ids[i] = core.LoadIdentifier(ls) } return ids }(), buffers: siegreader.New(), }, ls.Err }
// Load creates a Siegfried struct and loads content from path func Load(path string) (*Siegfried, error) { errOpening := "siegfried: error opening signature file, got %v; try running `sf -update`" errNotSig := "siegfried: not a siegfried signature file; try running `sf -update`" errUpdateSig := "siegfried: signature file is incompatible with this version of sf; try running `sf -update`" fbuf, err := ioutil.ReadFile(path) if err != nil { return nil, fmt.Errorf(errOpening, err) } if len(fbuf) < len(config.Magic())+2 { return nil, fmt.Errorf(errNotSig) } if string(fbuf[:len(config.Magic())]) != string(config.Magic()) { return nil, fmt.Errorf(errNotSig) } if major, minor := fbuf[len(config.Magic())], fbuf[len(config.Magic())+1]; major < byte(config.Version()[0]) || (major == byte(config.Version()[0]) && minor < byte(config.Version()[1])) { return nil, fmt.Errorf(errUpdateSig) } r := bytes.NewBuffer(fbuf[len(config.Magic())+2:]) rc := flate.NewReader(r) defer rc.Close() buf, err := ioutil.ReadAll(rc) if err != nil { return nil, fmt.Errorf(errOpening, err) } ls := persist.NewLoadSaver(buf) return &Siegfried{ C: ls.LoadTime(), em: stringmatcher.Load(ls), mm: stringmatcher.Load(ls), cm: containermatcher.Load(ls), bm: bytematcher.Load(ls), tm: textmatcher.Load(ls), ids: func() []core.Identifier { ids := make([]core.Identifier, ls.LoadTinyUInt()) for i := range ids { ids[i] = core.LoadIdentifier(ls) } return ids }(), buffers: siegreader.New(), }, ls.Err }