-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
254 lines (218 loc) · 7.16 KB
/
common.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package common defines vars and constants for the sync package
package common
import (
"context"
"io"
"strings"
"time"
"github.com/micro/go-micro/metadata"
"github.com/pydio/cells/common/proto/tree"
)
// EventType represents the type of the event occurred.
type EventType string
const (
// EventCreate notifies when a new object is created
EventCreate EventType = "ObjectCreated"
// EventCreate notifies when a new object is created
EventRename EventType = "ObjectRenamed"
// EventRemove notifies when a new object is deleted
EventRemove EventType = "ObjectRemoved"
// EventAccessed notifies when an object is accessed.
EventAccessed EventType = "ObjectAccessed"
// EventAccessedRead notifies when an object is accessed (specifically read).
EventAccessedRead EventType = "ObjectAccessed:Read"
// EventAccessedStat notifies when an object is accessed (specifically stat).
EventAccessedStat EventType = "ObjectAccessed:Stat"
// EventOther notifies any other events
EventOther EventType = "ObjectOther"
// Use unique path separator everywhere
InternalPathSeparator = "/"
DefaultEtag = "00000000000000000000000000000000-1"
)
/*
type Node struct {
Path string
Leaf bool
Hash string
Uuid string
FileInfo os.FileInfo
}
*/
func AsPathSyncSource(endpoint Endpoint) (PathSyncSource, bool) {
i, ok := interface{}(endpoint).(PathSyncSource)
return i, ok
}
func AsPathSyncTarget(endpoint Endpoint) (PathSyncTarget, bool) {
i, ok := interface{}(endpoint).(PathSyncTarget)
return i, ok
}
func AsDataSyncSource(endpoint Endpoint) (DataSyncSource, bool) {
i, ok := interface{}(endpoint).(DataSyncSource)
return i, ok
}
func AsDataSyncTarget(endpoint Endpoint) (DataSyncTarget, bool) {
i, ok := interface{}(endpoint).(DataSyncTarget)
return i, ok
}
func AsVersioner(endpoint Endpoint) (Versioner, bool) {
i, ok := interface{}(endpoint).(Versioner)
return i, ok
}
func AsSessionProvider(endpoint Endpoint) (SessionProvider, bool) {
i, ok := interface{}(endpoint).(SessionProvider)
return i, ok
}
type EndpointInfo struct {
RequiresNormalization bool
RequiresFoldersRescan bool
}
type Endpoint interface {
LoadNode(ctx context.Context, path string, leaf ...bool) (node *tree.Node, err error)
GetEndpointInfo() EndpointInfo
}
type WalkNodesFunc func(path string, node *tree.Node, err error)
type PathSyncSource interface {
Endpoint
Walk(walknFc WalkNodesFunc, pathes ...string) (err error)
Watch(recursivePath string) (*WatchObject, error)
ComputeChecksum(node *tree.Node) error
}
type PathSyncTarget interface {
Endpoint
CreateNode(ctx context.Context, node *tree.Node, updateIfExists bool) (err error)
UpdateNode(ctx context.Context, node *tree.Node) (err error)
DeleteNode(ctx context.Context, path string) (err error)
MoveNode(ctx context.Context, oldPath string, newPath string) (err error)
}
type DataSyncTarget interface {
PathSyncTarget
GetWriterOn(path string, targetSize int64) (out io.WriteCloser, err error)
}
type DataSyncSource interface {
PathSyncSource
GetReaderOn(path string) (out io.ReadCloser, err error)
}
type UuidProvider interface {
LoadNodeByUuid(ctx context.Context, uuid string) (node *tree.Node, err error)
}
type UuidReceiver interface {
UpdateNodeUuid(ctx context.Context, node *tree.Node) (*tree.Node, error)
}
type Versioner interface {
Commit(node *tree.Node)
ListVersions(node *tree.Node) (versions map[int]string, lastVersion int)
}
type SessionProvider interface {
StartSession(ctx context.Context, rootNode *tree.Node) (*tree.IndexationSession, error)
FlushSession(ctx context.Context, sessionUuid string) error
FinishSession(ctx context.Context, sessionUuid string) error
}
// EventInfo contains the information of the event that occurred and the source
// IP:PORT of the client which triggerred the event.
type EventInfo struct {
Time string
Size int64
Etag string
Folder bool
Path string
PathSyncSource PathSyncSource
Type EventType
Host string
Port string
UserAgent string
OperationId string
ScanEvent bool
ScanSourceNode *tree.Node
Metadata map[string]string
}
func (e EventInfo) CreateContext(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
if e.Metadata == nil {
return ctx
} else {
return metadata.NewContext(ctx, e.Metadata)
}
}
type ProcessorEvent struct {
Type string
Data interface{}
}
type WatchObject struct {
// eventInfo will be put on this chan
EventInfoChan chan EventInfo
// errors will be put on this chan
ErrorChan chan error
// will stop the watcher goroutines
DoneChan chan bool
}
// NextEvent pops the next event from the EventInfoChan
func (w WatchObject) NextEvent() EventInfo {
return <-w.EventInfoChan
}
// NextError pops the next error from the ErrorChan
func (w WatchObject) NextError() error {
return <-w.ErrorChan
}
// Done returns a channel that unblocks when Close has been
// called
func (w WatchObject) Done() <-chan bool { return w.DoneChan }
// Events returns the chan receiving events
func (w *WatchObject) Events() chan EventInfo {
return w.EventInfoChan
}
// Errors returns the chan receiving errors
func (w *WatchObject) Errors() chan error {
return w.ErrorChan
}
// Close the watcher, will stop all goroutines
func (w *WatchObject) Close() {
close(w.DoneChan)
}
func IsIgnoredFile(path string) (ignored bool) {
return strings.HasSuffix(path, ".DS_Store") || strings.Contains(path, ".minio.sys") || strings.HasSuffix(path, "$buckets.json") || strings.HasSuffix(path, "$multiparts-session.json") || strings.HasSuffix(path, "--COMPUTE_HASH")
}
func NodeToEventInfo(ctx context.Context, path string, node *tree.Node, eventType EventType) (eventInfo EventInfo) {
timeFormatFS := "2006-01-02T15:04:05.000Z"
eventInfo = EventInfo{
Time: time.Now().UTC().Format(timeFormatFS),
Folder: !node.IsLeaf(),
Path: path,
Type: eventType,
ScanEvent: true,
ScanSourceNode: node,
}
if ctx != nil {
if meta, ok := metadata.FromContext(ctx); ok {
eventInfo.Metadata = meta
}
}
return eventInfo
}
func DirWithInternalSeparator(filePath string) string {
segments := strings.Split(filePath, InternalPathSeparator)
return strings.Join(segments[:len(segments)-1], InternalPathSeparator)
}
func NodeRequiresChecksum(node *tree.Node) bool {
return node.IsLeaf() && (node.Etag == "" || node.Etag == DefaultEtag)
}