-
Notifications
You must be signed in to change notification settings - Fork 2
/
conflict.go
117 lines (98 loc) · 2.83 KB
/
conflict.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package sync
import (
"fmt"
"log"
"path/filepath"
"reflect"
"github.com/quic-s/quics-client/pkg/db/badger"
"github.com/quic-s/quics-client/pkg/net/qclient"
"github.com/quic-s/quics-protocol/pkg/stream"
qstypes "github.com/quic-s/quics/pkg/types"
)
const (
SERVER = "SERVER"
LOCAL = "LOCAL"
)
// @URL /api/v1/conflict/download
func ConflictDownload(path string) error {
_, Afterpath := badger.SplitBeforeAfterRoot(path)
err := Conn.OpenTransaction(qstypes.CONFLICTDOWNLOAD, func(stream *stream.Stream, transactionName string, transactionID []byte) error {
log.Println("quics-client : [CONFLICTDOWNLOAD] transaction start")
UUID := badger.GetUUID()
res, err := qclient.SendConflictDownload(stream, UUID, Afterpath)
if err != nil {
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("cannot download conflict file")
}
return nil
})
if err != nil {
return err
}
log.Println("quics-client : [CONFLICTDOWNLOAD] transaction success")
return nil
}
// @URL /api/v1/conflict/list
func GetConflictList() ([]qstypes.Conflict, error) {
UUID := badger.GetUUID()
result := []qstypes.Conflict{}
err := Conn.OpenTransaction(qstypes.CONFLICTLIST, func(stream *stream.Stream, transactionName string, transactionID []byte) error {
log.Println("quics-client : [CONFLICTLIST] transaction start")
cflist, err := qclient.SendAskConflictList(stream, UUID)
if err != nil {
return err
}
result = cflist.Conflicts
return nil
})
if err != nil {
return nil, err
}
log.Println("quics-client : [CONFLICTLIST] transaction success")
return result, nil
}
// @URL /api/v1/conflict/list
func PrintCFOptions() (string, error) {
cflist, err := GetConflictList()
if err != nil {
return "", err
}
result := "\n\n"
for i, conflictFile := range cflist {
afterPath := conflictFile.AfterPath
beforePath := badger.GetBeforePathWithAfterPath(afterPath)
result += fmt.Sprintf("%d. %s\n", i+1, filepath.Join(beforePath, afterPath))
y := 1
for UUID, candidate := range conflictFile.StagingFiles {
time, err := candidate.File.ModTime.MarshalText()
if err != nil {
return "", err
}
result += fmt.Sprintf("\t(%d) Candidate > %s \n\t\tSize > %d ModTime > %s\n", y, UUID, candidate.File.Size, time)
y++
}
}
return result, nil
}
// @URL /api/v1/conflict/choose
func ChooseOne(path string, Side string) error {
UUID := badger.GetUUID()
_, AfterPath := badger.SplitBeforeAfterRoot(path)
err := Conn.OpenTransaction(qstypes.CHOOSEONE, func(stream *stream.Stream, transactionName string, transactionID []byte) error {
// Send ChooseOne
res, err := qclient.SendChooseOne(stream, UUID, AfterPath, Side)
if err != nil {
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("cannot choose one")
}
return nil
})
if err != nil {
return fmt.Errorf("[CONFLICT] ", err)
}
return nil
}