|
|
|
@@ -22,14 +22,14 @@ import (
|
|
|
|
|
// TODO: Be able to cancel a diff creation (for instance if the user resaves). Does this work? Should we block
|
|
|
|
|
// creating diffs within 5 minutes of creating one? Cancelling is probably better at this point.
|
|
|
|
|
// it might be nice to inform the user when diffs build up
|
|
|
|
|
func manageFileDiffing(ctx context.Context, subject, object, diffStorageLocation string, fs bool, diffChannel chan database.DiffObject, wg *sync.WaitGroup) error {
|
|
|
|
|
func manageFileDiffing(ctx context.Context, target, diffobject, commitHashPath string, diffChannel chan database.DiffObject, wg *sync.WaitGroup) error {
|
|
|
|
|
|
|
|
|
|
var subjectHash, objectHash [16]byte
|
|
|
|
|
var targetHash, diffobjectHash [16]byte
|
|
|
|
|
var err error
|
|
|
|
|
if subjectHash, err = UniqueFileHash(subject); err != nil {
|
|
|
|
|
if targetHash, err = UniqueFileHash(target); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if objectHash, err = UniqueFileHash(object); err != nil {
|
|
|
|
|
if diffobjectHash, err = UniqueFileHash(diffobject); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -37,30 +37,21 @@ func manageFileDiffing(ctx context.Context, subject, object, diffStorageLocation
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(messages chan<- database.DiffObject) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
var dO database.DiffObject
|
|
|
|
|
//doing this on routine to not lose anytime... does it change anything?
|
|
|
|
|
dO.Description = ""
|
|
|
|
|
dO.Subject = object
|
|
|
|
|
dO.Object = subject
|
|
|
|
|
//dO.Description = ""
|
|
|
|
|
dO.Target = target
|
|
|
|
|
dO.DiffObject = diffobject
|
|
|
|
|
dO.StartTime = diffTime
|
|
|
|
|
dO.SubjectHash = objectHash //TODO: these being the wrong way round is a legacy thing. Swapping them needs testing, but should be fine
|
|
|
|
|
dO.ObjectHash = subjectHash
|
|
|
|
|
dO.TargetHash = targetHash //TODO: these being the wrong way round is a legacy thing. Swapping them needs testing, but should be fine
|
|
|
|
|
dO.DiffObjectHash = diffobjectHash
|
|
|
|
|
fmt.Println("creating diff object now")
|
|
|
|
|
if diff, err := binaryDiff(ctx, &dO, diffStorageLocation, fs); err != nil { //binaryDiff
|
|
|
|
|
if diff, err := binaryDiff(ctx, &dO, commitHashPath); err != nil { //binaryDiff
|
|
|
|
|
fmt.Println("error from binary diff ", err)
|
|
|
|
|
dO.E = err
|
|
|
|
|
} else {
|
|
|
|
|
dO.Diff = &diff
|
|
|
|
|
dO.Diff = &diff //Storing it in memory as a complete failure //TODO: Remove this at some point
|
|
|
|
|
}
|
|
|
|
|
// ssStruct := <-ssChannel
|
|
|
|
|
// fmt.Printf("received over ssChannel %+v\r\n", ssStruct)
|
|
|
|
|
// if ssStruct.ScreenshotError != nil {
|
|
|
|
|
// fmt.Println("screenshot failed, ", ssStruct.ScreenshotError)
|
|
|
|
|
// } else {
|
|
|
|
|
// fmt.Println("diff reeived screenshot ", ssStruct.Screenshot)
|
|
|
|
|
// dO.Screenshot = ssStruct.Screenshot
|
|
|
|
|
// }
|
|
|
|
|
elapsed := time.Since(diffTime)
|
|
|
|
|
dO.Message = "elapsed time:" + elapsed.String()
|
|
|
|
|
messages <- dO
|
|
|
|
@@ -69,7 +60,7 @@ func manageFileDiffing(ctx context.Context, subject, object, diffStorageLocation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//run instead of binaryDiff to turn it off
|
|
|
|
|
func dryrun(ctx context.Context, dO *database.DiffObject, diffStorageLocation string, fs bool) ([]byte, error) {
|
|
|
|
|
func dryrun(ctx context.Context, dO *database.DiffObject, commitHash string) ([]byte, error) {
|
|
|
|
|
return []byte{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -80,41 +71,24 @@ func dryrun(ctx context.Context, dO *database.DiffObject, diffStorageLocation st
|
|
|
|
|
// 2. Whether to save diffs in both directions
|
|
|
|
|
// 3. Creates a diff object that contains any necessary metadata about the diff files
|
|
|
|
|
// subject is the file that changed, object is file on record
|
|
|
|
|
func binaryDiff(ctx context.Context, dO *database.DiffObject, diffStorageLocation string, fs bool) ([]byte, error) {
|
|
|
|
|
func binaryDiff(ctx context.Context, dO *database.DiffObject, commitHashPath string) ([]byte, error) {
|
|
|
|
|
var fileName string
|
|
|
|
|
_, fileName = filepath.Split(dO.Subject) // dirPath
|
|
|
|
|
dO.Watching = fileName
|
|
|
|
|
// var sub io.Reader
|
|
|
|
|
// if sub, err = os.Open(dO.Subject); err != nil {
|
|
|
|
|
// return []byte{}, err
|
|
|
|
|
// }
|
|
|
|
|
// var obj io.Reader
|
|
|
|
|
// if obj, err = os.Open(dO.Object); err != nil {
|
|
|
|
|
// return []byte{}, err
|
|
|
|
|
// }
|
|
|
|
|
_, fileName = filepath.Split(dO.Target) // dirPath
|
|
|
|
|
startTime := strconv.FormatInt(dO.StartTime.Unix(), 10)
|
|
|
|
|
if fs { //if the wish is to store to the filesystem
|
|
|
|
|
dO.DiffPath = filepath.Join(diffStorageLocation, fileName+"_"+startTime+"_"+dO.Description) + "_diff.patch"
|
|
|
|
|
if writeDiff, err := os.Create(dO.DiffPath); err != nil {
|
|
|
|
|
dO.DiffPath = filepath.Join(commitHashPath, fileName+"_"+startTime) + "_diff.patch"
|
|
|
|
|
if writeDiff, err := os.Create(dO.DiffPath); err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else if deltaBytes, err := fdeltaDiff(ctx, dO.Target, dO.DiffObject); err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else {
|
|
|
|
|
bytesWritten, err := writeDiff.Write(deltaBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else if deltaBytes, err := fdeltaDiff(ctx, dO.Subject, dO.Object); err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else {
|
|
|
|
|
if bytesWritten, err := writeDiff.Write(deltaBytes); err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else {
|
|
|
|
|
dO.DiffSize = int64(bytesWritten)
|
|
|
|
|
return []byte{}, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { //if we actually want the bytes we have to set fs to false (can do this above.)
|
|
|
|
|
if deltaBytes, err := fdeltaDiff(ctx, dO.Subject, dO.Object); err != nil {
|
|
|
|
|
return []byte{}, err
|
|
|
|
|
} else {
|
|
|
|
|
dO.DiffSize = int64(len(deltaBytes))
|
|
|
|
|
return deltaBytes, nil
|
|
|
|
|
}
|
|
|
|
|
dO.DiffSize = int64(bytesWritten)
|
|
|
|
|
return []byte{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//sub is the original
|
|
|
|
|