-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsyncfile.go
99 lines (91 loc) · 1.96 KB
/
syncfile.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
package putiosync
import (
"fmt"
"os"
"strings"
"github.com/cenkalti/log"
"github.com/putdotio/go-putio"
"github.com/putdotio/putio-sync/v2/internal/walker"
"golang.org/x/text/unicode/norm"
)
type iLocalFile interface {
Info() os.FileInfo
RelPath() string
FullPath() string
}
type iRemoteFile interface {
Info() os.FileInfo
RelPath() string
PutioFile() *putio.File
}
type syncFile struct {
local iLocalFile
remote iRemoteFile
state *stateType
relpath string
skip bool
}
func (f *syncFile) String() string {
flags := []byte("...")
if f.state != nil {
switch f.state.Status {
case statusSynced:
flags[0] = 'S'
case statusDownloading:
flags[0] = 'D'
case statusUploading:
flags[0] = 'U'
default:
flags[0] = '?'
}
}
if f.local != nil {
flags[1] = 'L'
}
if f.remote != nil {
flags[2] = 'R'
}
return fmt.Sprintf("%s %s", string(flags), f.relpath)
}
func groupFiles(states []stateType, localFiles []*walker.LocalFile, remoteFiles []*walker.RemoteFile) map[string]*syncFile {
m := make(map[string]*syncFile)
initSyncFile := func(relpath string) *syncFile {
relpath = norm.NFC.String(relpath)
sf, ok := m[relpath]
if ok {
return sf
}
sf = &syncFile{relpath: relpath}
m[relpath] = sf
return sf
}
for _, lf := range localFiles {
sf := initSyncFile(lf.RelPath())
sf.local = lf
}
for _, rf := range remoteFiles {
sf := initSyncFile(rf.RelPath())
sf.remote = rf
}
for _, state := range states {
sf := initSyncFile(state.relpath)
s := state
sf.state = &s
}
return m
}
func filterOutInvalidNames(syncFiles map[string]*syncFile) {
invalidNames := make([]string, 0, len(syncFiles))
for relpath := range syncFiles {
for _, s := range strings.Split(relpath, "/") {
if shouldIgnoreName(s) {
invalidNames = append(invalidNames, relpath)
break
}
}
}
for _, name := range invalidNames {
log.Warningf("Special characters in file name, skipping sync: %q", name)
delete(syncFiles, name)
}
}