-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.go
202 lines (181 loc) · 5.14 KB
/
transfer.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
package backup
import (
"context"
"errors"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/0x07cf-dev/go-backup/internal/logger"
rc_fs "github.com/rclone/rclone/fs"
rc_fspath "github.com/rclone/rclone/fs/fspath"
rc_ops "github.com/rclone/rclone/fs/operations"
rc_sync "github.com/rclone/rclone/fs/sync"
)
func initFs(ctx context.Context, path string) (rc_fs.Fs, error) {
newFs, err := rc_fs.NewFs(ctx, path)
if err != nil {
return nil, err
}
// Make remote dir if not found
if _, err := newFs.List(ctx, ""); errors.Is(err, rc_fs.ErrorDirNotFound) {
if err = rc_ops.Mkdir(ctx, newFs, ""); err != nil {
return nil, err
}
logger.Infof("Created dir on remote: '%s'", path)
}
return newFs, nil
}
func (session *BackupSession) uploadPath(path string, wg *sync.WaitGroup, errCh chan BackupError, simulate bool) {
defer wg.Done()
// Mutex lock
session.mu.Lock()
defer session.mu.Unlock()
if _, ok := session.processed[path]; ok {
logger.Warnf("Path already processed: '%s'", path)
return
}
session.processed[path] = true
if simulate {
// totally valid human-readable errors
errorChance := 0
if rand.Float32() < float32(errorChance) {
var syllables []string
vowels := []rune{'a', 'e', 'i', 'o', 'u'}
consonants := []rune{'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'}
for _, c := range consonants {
for _, v := range vowels {
syllables = append(syllables, string(c)+string(v))
}
}
randomString := ""
for i := 0; i < 12; i++ {
randomString += syllables[rand.Intn(len(syllables))]
if rand.Intn(3) == 0 {
randomString += " "
}
}
logger.Infof("Simulating error: '%s'", path)
simulation := []BackupErrorCode{GenericError, UploadError}
rnd := (rand.Intn(len(simulation)) * rand.Intn(len(simulation))) % len(simulation)
errCh <- simulation[rnd].Error(path, randomString)
return
}
}
currFile, err := os.Stat(path)
if err != nil {
errCh <- PathError.Error(path, err.Error())
return
}
absPath, err := filepath.Abs(path)
if err != nil {
errCh <- PathError.Error(path, err.Error())
return
}
parent, _, err := rc_fspath.Split(path)
if err != nil {
errCh <- PathError.Error(path, err.Error())
return
}
// This is a naive approach that simply copies the files/dirs over, overwriting.
if currFile.IsDir() {
// Upload directory to remote
remotePath, err := session.getRemotePath(absPath)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
// Destination and source filesystems
destFs, err := initFs(session.context, remotePath)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
srcFs, err := initFs(session.context, absPath)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
// Upload
if !simulate {
if err = rc_sync.CopyDir(session.context,
destFs, // Upload dir destination: remoteRoot/hostname/sourceFileName.any
srcFs, // Upload dir source: user-defined
true, // Upload empty source dirs?
); err != nil {
errCh <- UploadError.Error(path, err.Error())
return
} else {
logger.Infof("Upload dir: '%s' ---> '%s'", path, remotePath)
}
} else {
logger.Infof("Would upload dir: '%s' ---> '%s'", path, remotePath)
}
} else {
// Upload file to remote
remotePath, err := session.getRemotePath(parent)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
// Destination and source filesystems
destFs, err := initFs(session.context, remotePath)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
srcFs, err := initFs(session.context, parent)
if err != nil {
errCh <- UploadError.Error(path, err.Error())
return
}
// Upload
if !simulate {
if err = rc_ops.CopyFile(
session.context,
destFs, // Upload file destination: remoteRoot/hostname/sourceFileName.any
srcFs, // Upload file source: user-defined
currFile.Name(),
currFile.Name(),
); err != nil {
errCh <- UploadError.Error(path, err.Error())
return
} else {
logger.Infof("Upload file: '%s' ---> '%s'", path, remotePath)
}
} else {
logger.Infof("Would upload file: '%s' ---> '%s'", path, remotePath)
}
}
}
// not gonna happen for a while
func (session *BackupSession) downloadPath(path string, wg *sync.WaitGroup, errCh chan BackupError, simulate bool) {
t0 := time.Now()
logger.Infof("DOWNLOADED! (%v)\n", time.Since(t0))
}
func (session *BackupSession) getRemotePath(path string) (string, error) {
// Remote needs to end with ':'
remote := session.Opts.Remote
// Append colon to a non-empty, non-path remote if it doesn't have it
if remote != "" && !filepath.IsAbs(remote) && !strings.HasSuffix(remote, ":") {
remote += ":"
}
illegal, err := regexp.Compile(`[|<>?:*"]`)
if err != nil {
return "", err
}
cleanPath := illegal.ReplaceAllString(path, "")
cleanPath = rc_fspath.JoinRootPath(
remote,
filepath.Join(
session.Opts.RemoteRoot,
session.Machine.Hostname,
cleanPath,
),
)
logger.Debugf("Parsed path: '%s' ---> '%s'", path, cleanPath)
return cleanPath, nil
}