From 59e3c459a5cb5a291fe29cddf4b7f93eebfa76ef Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 16 Jun 2019 23:30:26 +0200 Subject: [PATCH] Add special case: File added locally and remotely Signed-off-by: Knut Ahlers --- sync/execute.go | 41 +++++++++++++++++++++++++++++++++++++++++ sync/logic.go | 5 ++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/sync/execute.go b/sync/execute.go index 2d9a5f2..65f5d0f 100644 --- a/sync/execute.go +++ b/sync/execute.go @@ -1,11 +1,52 @@ package sync import ( + "crypto/sha256" + "github.com/pkg/errors" "github.com/Luzifer/cloudbox/providers" ) +func (s *Sync) addBothCreated(fileName string) error { + // Use forced sha256 to ensure lesser chance for collision + var hashMethod = sha256.New() + + local, err := s.local.GetFile(fileName) + if err != nil { + return errors.Wrap(err, "Unable to retrieve file from local") + } + + remote, err := s.remote.GetFile(fileName) + if err != nil { + return errors.Wrap(err, "Unable to retrieve file from remote") + } + + localSum, err := local.Checksum(hashMethod) + if err != nil { + return errors.Wrap(err, "Unable to get checksum from local file") + } + + remoteSum, err := remote.Checksum(hashMethod) + if err != nil { + return errors.Wrap(err, "Unable to get checksum from remote file") + } + + if localSum != remoteSum { + return errors.New("Checksums differ") + } + + if err := s.setDBFileInfo(sideLocal, local.Info()); err != nil { + return errors.Wrap(err, "Unable to update DB info for local file") + } + + if err := s.setDBFileInfo(sideRemote, remote.Info()); err != nil { + return errors.Wrap(err, "Unable to update DB info for remote file") + } + + return nil +} + func (s *Sync) deleteFile(on providers.CloudProvider, fileName string) error { if err := on.DeleteFile(fileName); err != nil { return errors.Wrap(err, "Unable to delete file") diff --git a/sync/logic.go b/sync/logic.go index b7662bd..22b2ff1 100644 --- a/sync/logic.go +++ b/sync/logic.go @@ -21,7 +21,10 @@ func (s *Sync) decideAction(syncState *state, fileName string) error { case change.HasAll(ChangeLocalAdd, ChangeRemoteAdd): // Special case: Both are added, check they are the same file or leave this to manual resolve logger.Debug("File added locally as well as remotely") - // TODO: Handle special case + + if err := s.addBothCreated(fileName); err != nil { + logger.WithError(err).Error("Unable to add locally as well as remotely added file") + } case change.HasAll(ChangeLocalDelete, ChangeRemoteDelete): // Special case: Both vanished, we just need to clean up the sync cache