still working

This commit is contained in:
Jeffrey Paul 2022-12-06 21:09:06 +04:00
parent 86d724ee35
commit 6f74522513
3 changed files with 23 additions and 23 deletions

View File

@ -11,7 +11,7 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
func (m *manifest) validateProto() error { func (m *manifest) validateProtoOuter() error {
if m.pbOuter.Version != MFFileOuter_VERSION_ONE { if m.pbOuter.Version != MFFileOuter_VERSION_ONE {
return errors.New("unknown version") return errors.New("unknown version")
} }
@ -38,12 +38,20 @@ func (m *manifest) validateProto() error {
log.Debugf("truncated data, got %d expected %d", isize, m.pbOuter.Size) log.Debugf("truncated data, got %d expected %d", isize, m.pbOuter.Size)
return bork.ErrFileTruncated return bork.ErrFileTruncated
} }
log.Debugf("inner data size is %d", isize)
log.Dump(dat) log.Dump(dat)
log.Dump(m.pbOuter.Sha256) log.Dump(m.pbOuter.Sha256)
return nil return nil
}
func validateMagic(dat []byte) bool {
ml := len([]byte(MAGIC))
if len(dat) < ml {
return false
}
got := dat[0:ml]
expected := []byte(MAGIC)
return bytes.Equal(got, expected)
} }
func NewFromProto(input io.Reader) (*manifest, error) { func NewFromProto(input io.Reader) (*manifest, error) {
@ -52,30 +60,30 @@ func NewFromProto(input io.Reader) (*manifest, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ml := len([]byte(MAGIC)) if !validateMagic(dat) {
bb := bytes.NewBuffer(dat)
got := dat[0:ml]
log.Dump("got:")
log.Dump(got)
expected := []byte(MAGIC)
log.Dump("expected:")
log.Dump(expected)
if !bytes.Equal(got, expected) {
return nil, errors.New("invalid file format") return nil, errors.New("invalid file format")
} }
bb = bytes.NewBuffer(dat[ml:])
// remove magic bytes prefix:
ml := len([]byte(MAGIC))
bb := bytes.NewBuffer(dat[ml:])
dat = bb.Bytes() dat = bb.Bytes()
log.Dump(dat) log.Dump(dat)
// deserialize:
m.pbOuter = new(MFFileOuter) m.pbOuter = new(MFFileOuter)
err = proto.Unmarshal(dat, m.pbOuter) err = proto.Unmarshal(dat, m.pbOuter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ve := m.validateProto()
ve := m.validateProtoOuter()
if ve != nil { if ve != nil {
return nil, ve return nil, ve
} }
// FIXME TODO deserialize inner
return m, nil return m, nil
} }

View File

@ -158,7 +158,6 @@ func (m *manifest) addFile(p string, fi fs.FileInfo, sfsIndex int) error {
func (m *manifest) Scan() error { func (m *manifest) Scan() error {
// FIXME scan and whatever function does the hashing should take ctx // FIXME scan and whatever function does the hashing should take ctx
log.Debug("manifest Scan()")
for idx, sfs := range m.sourceFS { for idx, sfs := range m.sourceFS {
if sfs == nil { if sfs == nil {
return errors.New("invalid source fs") return errors.New("invalid source fs")

View File

@ -7,12 +7,12 @@ import (
"errors" "errors"
"time" "time"
"git.eeqj.de/sneak/mfer/internal/log"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
//go:generate protoc --go_out=. --go_opt=paths=source_relative mf.proto //go:generate protoc --go_out=. --go_opt=paths=source_relative mf.proto
// rot13("MANIFEST")
const MAGIC string = "ZNAVSRFG" const MAGIC string = "ZNAVSRFG"
func newTimestampFromTime(t time.Time) *Timestamp { func newTimestampFromTime(t time.Time) *Timestamp {
@ -24,8 +24,6 @@ func newTimestampFromTime(t time.Time) *Timestamp {
} }
func (m *manifest) generate() error { func (m *manifest) generate() error {
log.Debug("generate()")
if m.pbInner == nil { if m.pbInner == nil {
e := m.generateInner() e := m.generateInner()
if e != nil { if e != nil {
@ -51,7 +49,6 @@ func (m *manifest) generate() error {
} }
func (m *manifest) generateOuter() error { func (m *manifest) generateOuter() error {
log.Debug("generateOuter()")
if m.pbInner == nil { if m.pbInner == nil {
return errors.New("internal error") return errors.New("internal error")
} }
@ -83,18 +80,15 @@ func (m *manifest) generateOuter() error {
CompressionType: MFFileOuter_COMPRESSION_GZIP, CompressionType: MFFileOuter_COMPRESSION_GZIP,
} }
m.pbOuter = o m.pbOuter = o
log.Dump(m.pbOuter)
return nil return nil
} }
func (m *manifest) generateInner() error { func (m *manifest) generateInner() error {
log.Debug("generateInner()")
m.pbInner = &MFFile{ m.pbInner = &MFFile{
Version: MFFile_VERSION_ONE, Version: MFFile_VERSION_ONE,
CreatedAt: newTimestampFromTime(time.Now()), CreatedAt: newTimestampFromTime(time.Now()),
Files: []*MFFilePath{}, Files: []*MFFilePath{},
} }
for _, f := range m.files { for _, f := range m.files {
nf := &MFFilePath{ nf := &MFFilePath{
Path: f.path, Path: f.path,
@ -102,6 +96,5 @@ func (m *manifest) generateInner() error {
} }
m.pbInner.Files = append(m.pbInner.Files, nf) m.pbInner.Files = append(m.pbInner.Files, nf)
} }
log.Dump(m.pbInner)
return nil return nil
} }