starting to write the manager library

This commit is contained in:
2020-06-18 17:22:42 -04:00
parent 55561d8667
commit 8c01cdbcf4
10 changed files with 709 additions and 27 deletions

View File

@@ -3,7 +3,7 @@ package engine
import (
"fmt"
logger "github.com/apsdehal/go-logger"
"github.com/rs/zerolog"
)
// The watcher is responsible for not only seeing when a file changes,
@@ -12,7 +12,7 @@ import (
// * copying any versions and keeping them safe (even if temporary)
// * creating the diff of the file, in both directions if necessary
// * storing the details in the database
func NewPatcher(logger *logger.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER string) (Patcher, error) {
func NewPatcher(logger *zerolog.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER string) (Patcher, error) {
p := Patcher{
logger,
KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER,
@@ -25,28 +25,26 @@ func NewPatcher(logger *logger.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, TH
// last save is the file you want to get.
func (p *Patcher) PatchFromFile(filePath, patchPath, restorePath string) error {
if subject, err := openFile(filePath); err != nil {
return fmt.Errorf("error on subject file: ", err)
return fmt.Errorf("error on subject file: %s", err)
} else if patch, err := openFile(patchPath); err != nil {
return fmt.Errorf("error on patch file: ", err)
return fmt.Errorf("error on patch file: %s", err)
} else {
return p.applyPatch(subject, patch, restorePath)
}
return nil
}
//applyPatch actively applies the patch to the subject. This could eventually
// be upgraded for different patching algorithms
func (p *Patcher) applyPatch(subject, patch []byte, restorePath string) error {
if delta, err := decompressDelta(patch); err != nil {
return fmt.Errorf("error decompressing delta", err)
return fmt.Errorf("error decompressing delta %s", err)
} else {
if appliedBytes, err := applyPatchToFile(subject, delta); err != nil {
return fmt.Errorf("error applying delta to original file", err)
return fmt.Errorf("error applying delta to original file %s", err)
} else if err := writeFile(restorePath, appliedBytes); err != nil {
return fmt.Errorf("error writing patchedFile", err)
} else {
return nil
return fmt.Errorf("error writing patchedFile %s", err)
}
return nil
}
return nil
}