forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_s3_fs_watch.go
175 lines (147 loc) · 4.61 KB
/
client_s3_fs_watch.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
/*
* 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 endpoints
import (
"context"
"log"
"os"
"path/filepath"
"strings"
"github.com/pydio/cells/data/source/sync/lib/common"
"github.com/pydio/minio-go"
"github.com/rjeczalik/notify"
)
type S3ClientFSWatch struct {
S3Client
FSRootPath string
}
func NewS3ClientFSWatch(ctx context.Context, url string, key string, secret string, bucket string, rootPath string, fsRootPath string) (*S3ClientFSWatch, error) {
log.Print("FS client watch")
mc, e := minio.New(url, key, secret, false)
mc.SetAppInfo(UserAgentAppName, UserAgentVersion)
if e != nil {
return nil, e
}
s3Client, e := NewS3Client(ctx, url, key, secret, bucket, rootPath)
if e != nil {
return nil, e
}
return &S3ClientFSWatch{
S3Client: *s3Client,
FSRootPath: fsRootPath,
}, e
}
func (c *S3ClientFSWatch) Watch(recursivePath string) (*common.WatchObject, error) {
eventChan := make(chan common.EventInfo)
errorChan := make(chan error)
doneChan := make(chan bool)
// Make the channel buffered to ensure no event is dropped. Notify will drop
// an event if the receiver is not able to keep up the sending pace.
in, out := PipeChan(1000)
var fsEvents []notify.Event
fsEvents = append(fsEvents, EventTypeAll...)
recursivePath = c.denormalize(recursivePath)
if e := notify.Watch(filepath.Join(c.FSRootPath, recursivePath)+"...", in, fsEvents...); e != nil {
return nil, e
}
// wait for doneChan to close the watcher, eventChan and errorChan
go func() {
<-doneChan
notify.Stop(in)
log.Println("Closing event channel for " + c.FSRootPath)
close(eventChan)
close(errorChan)
}()
// Get fsnotify notifications for events and errors, and sent them
// using eventChan and errorChan
go func() {
for event := range out {
if common.IsIgnoredFile(event.Path()) {
continue
}
eventInfo, eventError := c.fsEventToEventInfo(event)
if eventError != nil {
log.Println("Sending event error for ", event, eventError, eventInfo)
errorChan <- eventError
} else if eventInfo.Path != "" {
//log.Println("Sending event info for " + c.RootPath)
eventChan <- eventInfo
}
}
}()
return &common.WatchObject{
EventInfoChan: eventChan,
ErrorChan: errorChan,
DoneChan: doneChan,
}, nil
}
// Transforms an OS notify event to a standard pydio EventInfo.
func (c *S3ClientFSWatch) fsEventToEventInfo(event notify.EventInfo) (common.EventInfo, error) {
var i os.FileInfo
var empty common.EventInfo
eventPath := strings.TrimPrefix(CanonicalPath(event.Path()), c.FSRootPath+"/")
normalizedPath := c.normalize(eventPath)
log.Print(eventPath, " - ", event.Path(), " - ", c.FSRootPath)
if isEventType(EventTypeCreate, event.Event()) || isEventType(EventTypeWrite, event.Event()) {
var e error
i, e = c.Stat(eventPath)
if e != nil {
log.Println("Ignoring Create/Write event on non-existing file")
return empty, nil
}
return common.EventInfo{
Time: now(),
Size: i.Size(),
Folder: i.IsDir(),
Path: normalizedPath,
Type: common.EventCreate,
PathSyncSource: c,
}, nil
} else if isEventType(EventTypeRename, event.Event()) {
var e error
i, e = c.Stat(eventPath)
if e != nil {
return common.EventInfo{
Time: now(),
Path: normalizedPath,
Type: common.EventRemove,
PathSyncSource: c,
}, nil
}
return common.EventInfo{
Time: now(),
Size: i.Size(),
Folder: i.IsDir(),
Path: normalizedPath,
Type: common.EventRename,
PathSyncSource: c,
}, nil
} else if isEventType(EventTypeDelete, event.Event()) {
return common.EventInfo{
Time: now(),
Path: normalizedPath,
Type: common.EventRemove,
PathSyncSource: c,
}, nil
} else {
// Ignore other events
return empty, nil
}
}