next #5

Merged
sneak merged 14 commits from next into master 2022-12-09 00:02:35 +00:00
3 changed files with 23 additions and 23 deletions
Showing only changes of commit 6f74522513 - Show all commits

View File

@ -11,7 +11,7 @@ import (
"google.golang.org/protobuf/proto"
)
func (m *manifest) validateProto() error {
func (m *manifest) validateProtoOuter() error {
if m.pbOuter.Version != MFFileOuter_VERSION_ONE {
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)
return bork.ErrFileTruncated
}
log.Debugf("inner data size is %d", isize)
log.Dump(dat)
log.Dump(m.pbOuter.Sha256)
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) {
@ -52,30 +60,30 @@ func NewFromProto(input io.Reader) (*manifest, error) {
if err != nil {
return nil, err
}
ml := len([]byte(MAGIC))
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) {
if !validateMagic(dat) {
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()
log.Dump(dat)
// deserialize:
m.pbOuter = new(MFFileOuter)
err = proto.Unmarshal(dat, m.pbOuter)
if err != nil {
return nil, err
}
ve := m.validateProto()
ve := m.validateProtoOuter()
if ve != nil {
return nil, ve
}
// FIXME TODO deserialize inner
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 {
// FIXME scan and whatever function does the hashing should take ctx
log.Debug("manifest Scan()")
for idx, sfs := range m.sourceFS {
if sfs == nil {
return errors.New("invalid source fs")

View File

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