switching everything over to []byte, abandoning merkletree for now

This commit is contained in:
2020-07-01 14:33:01 -04:00
parent e4ac7f70e6
commit 07bbb442ef
12 changed files with 215 additions and 82 deletions

View File

@@ -1,8 +1,10 @@
package manager
import (
"encoding/base64"
"bytes"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
@@ -110,7 +112,7 @@ func (m *Manager) AddFileToRepo(relFilePath string) error {
relFilePath = strings.TrimSpace(relFilePath) //purging any odd spaces TODO: Make sure not needed
var tmpFile database.File
filename := filepath.Base(relFilePath)
var hash [16]byte
var hash []byte
//check that the file actually exists (currently done by client/server)
// if filename, err = engine.VerifySrcFile(relFilePath); err != nil {
// //there was no source file or it was not recognisable as a file
@@ -127,11 +129,11 @@ func (m *Manager) AddFileToRepo(relFilePath string) error {
}
tmpFile = database.File{}
tmpFile.CurrentHash = hash
tmpFile.Hash = hash
tmpFile.Name = filename
tmpFile.Path = relFilePath
tmpFile.CreatedAt = time.Now()
tmpFile.Unique = base64.URLEncoding.EncodeToString([]byte(filename)) + "_" + base64.URLEncoding.EncodeToString((tmpFile.CurrentHash[:])) + "_" + strconv.FormatInt(tmpFile.CreatedAt.Unix(), 10) + "_" + filename
tmpFile.Unique = hex.EncodeToString([]byte(filename)) + "_" + hex.EncodeToString((tmpFile.Hash)) + "_" + strconv.FormatInt(tmpFile.CreatedAt.Unix(), 10) + "_" + filename
//tmpFile.BkpLocation = filepath.Join(m.SyncFolder, tmpFile.Unique)
//tmpFile.CurrentBase = tmpFile.BkpLocation
//tmpFile.Ignore = false //we can have files in the database that are ignored. TODO: This was initially added so that 'All Files' would show up as a file (its a hack as it adds a dummy to the database)
@@ -144,7 +146,7 @@ func (m *Manager) AddFileToRepo(relFilePath string) error {
return err
}
m.Info().Msgf("added file: %s at path: %s with hash: %s at time: %s", filename, relFilePath, tmpFile.CurrentHash, tmpFile.CreatedAt.String)
m.Info().Msgf("added file: %s at path: %s with hash: %s at time: %s", filename, relFilePath, tmpFile.Hash, tmpFile.CreatedAt.String)
return nil
}
@@ -163,13 +165,41 @@ func (m *Manager) prepareDatabaseForFile(tmpFile database.File) (int, error) {
}
func (m *Manager) BeginCommit(fileList []database.File, branch string) error {
func (m *Manager) BeginCommit(branch string, commitMessage string) error {
trackedFiles, err := m.FetchTrackedFiles()
if err != nil {
return err
}
var filesToDiff []database.File // Contains the list of files that have changed
for _, trackedFile := range trackedFiles {
currentFile, err := os.Stat(trackedFile.Path)
if err != nil {
fmt.Printf("unable to stat tracked file: %s error: %s\n", currentFile.Name(), err)
continue
}
currentFileHash, err := engine.UniqueFileHash(trackedFile.Path)
if err != nil {
fmt.Printf("unable to create hash for file: %s error: %s\n", currentFile.Name(), err)
continue
}
result := bytes.Compare(currentFileHash, trackedFile.Hash) // Compare the hashes of the two files
if result == 0 { //If they are equal
fmt.Printf("No changes found in file: %s when compared to file: %s\n", currentFile.Name(), trackedFile.Name)
continue
}
filesToDiff = append(filesToDiff, trackedFile)
}
diffChannel := make(chan database.DiffObject)
diffContext := context.Background()
m.WaitGroup.Add(2)
commit, err := m.dB.FetchLastCommitOnBranch(branch)
if err != nil {
m.Err(err).Msgf("unable to fetch last commit on branch, assuming first commit on branch", err)
m.Info().Msgf("unable to fetch last commit on branch, assuming first commit on branch", err)
err := engine.CreateInitialCommit(filesToDiff, commitMessage)
if err != nil {
m.Err(err).Msgf("unable to create initial commit: %s", err)
return err
}
}
return nil
}