| 1 | package tq |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/config" |
| 10 | "github.com/git-lfs/git-lfs/v3/fs" |
| 11 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 12 | "github.com/git-lfs/git-lfs/v3/lfshttp" |
| 13 | "github.com/git-lfs/git-lfs/v3/ssh" |
| 14 | "github.com/git-lfs/git-lfs/v3/tr" |
| 15 | "github.com/rubyist/tracerx" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | defaultMaxRetries = 8 |
| 20 | defaultMaxRetryDelay = 10 |
| 21 | defaultMaxRetryTime = 300 |
| 22 | ) |
| 23 | |
| 24 | type Manifest interface { |
| 25 | APIClient() *lfsapi.Client |
| 26 | MaxRetries() int |
| 27 | MaxRetryDelay() int |
| 28 | MaxRetryTime() int |
| 29 | ConcurrentTransfers() int |
| 30 | IsStandaloneTransfer() bool |
| 31 | batchClient() BatchClient |
| 32 | GetAdapterNames(dir Direction) []string |
| 33 | GetDownloadAdapterNames() []string |
| 34 | GetUploadAdapterNames() []string |
| 35 | getAdapterNames(adapters map[string]NewAdapterFunc) []string |
| 36 | RegisterNewAdapterFunc(name string, dir Direction, custom bool, f NewAdapterFunc) |
| 37 | NewAdapterOrDefault(name string, dir Direction) Adapter |
| 38 | NewAdapter(name string, dir Direction) Adapter |
| 39 | NewDownloadAdapter(name string) Adapter |
| 40 | NewUploadAdapter(name string) Adapter |
| 41 | isCustomAdapter(name, operation string) bool |
| 42 | Upgrade() *concreteManifest |
| 43 | Upgraded() bool |
| 44 | } |
| 45 | |
| 46 | type lazyManifest struct { |
| 47 | f *fs.Filesystem |
| 48 | apiClient *lfsapi.Client |
| 49 | operation string |
| 50 | remote string |
| 51 | mu sync.Mutex |
| 52 | m *concreteManifest |
| 53 | } |
| 54 | |
| 55 | func newLazyManifest(f *fs.Filesystem, apiClient *lfsapi.Client, operation, remote string) *lazyManifest { |
| 56 | return &lazyManifest{ |
| 57 | f: f, |
| 58 | apiClient: apiClient, |
| 59 | operation: operation, |
| 60 | remote: remote, |
| 61 | m: nil, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func (m *lazyManifest) APIClient() *lfsapi.Client { |
| 66 | return m.Upgrade().APIClient() |
| 67 | } |
| 68 | |
| 69 | func (m *lazyManifest) MaxRetries() int { |
| 70 | return m.Upgrade().MaxRetries() |
| 71 | } |
| 72 | |
| 73 | func (m *lazyManifest) MaxRetryDelay() int { |
| 74 | return m.Upgrade().MaxRetryDelay() |
| 75 | } |
| 76 | |
| 77 | func (m *lazyManifest) MaxRetryTime() int { |
| 78 | return m.Upgrade().MaxRetryTime() |
| 79 | } |
| 80 | |
| 81 | func (m *lazyManifest) ConcurrentTransfers() int { |
| 82 | return m.Upgrade().ConcurrentTransfers() |
| 83 | } |
| 84 | |
| 85 | func (m *lazyManifest) IsStandaloneTransfer() bool { |
| 86 | return m.Upgrade().IsStandaloneTransfer() |
| 87 | } |
| 88 | |
| 89 | func (m *lazyManifest) batchClient() BatchClient { |
| 90 | return m.Upgrade().batchClient() |
| 91 | } |
| 92 | |
| 93 | func (m *lazyManifest) GetAdapterNames(dir Direction) []string { |
| 94 | return m.Upgrade().GetAdapterNames(dir) |
| 95 | } |
| 96 | |
| 97 | func (m *lazyManifest) GetDownloadAdapterNames() []string { |
| 98 | return m.Upgrade().GetDownloadAdapterNames() |
| 99 | } |
| 100 | |
| 101 | func (m *lazyManifest) GetUploadAdapterNames() []string { |
| 102 | return m.Upgrade().GetUploadAdapterNames() |
| 103 | } |
| 104 | |
| 105 | func (m *lazyManifest) getAdapterNames(adapters map[string]NewAdapterFunc) []string { |
| 106 | return m.Upgrade().getAdapterNames(adapters) |
| 107 | } |
| 108 | |
| 109 | func (m *lazyManifest) RegisterNewAdapterFunc(name string, dir Direction, custom bool, f NewAdapterFunc) { |
| 110 | m.Upgrade().RegisterNewAdapterFunc(name, dir, custom, f) |
| 111 | } |
| 112 | |
| 113 | func (m *lazyManifest) NewAdapterOrDefault(name string, dir Direction) Adapter { |
| 114 | return m.Upgrade().NewAdapterOrDefault(name, dir) |
| 115 | } |
| 116 | |
| 117 | func (m *lazyManifest) NewAdapter(name string, dir Direction) Adapter { |
| 118 | return m.Upgrade().NewAdapter(name, dir) |
| 119 | } |
| 120 | |
| 121 | func (m *lazyManifest) NewDownloadAdapter(name string) Adapter { |
| 122 | return m.Upgrade().NewDownloadAdapter(name) |
| 123 | } |
| 124 | |
| 125 | func (m *lazyManifest) NewUploadAdapter(name string) Adapter { |
| 126 | return m.Upgrade().NewUploadAdapter(name) |
| 127 | } |
| 128 | |
| 129 | func (m *lazyManifest) isCustomAdapter(name, operation string) bool { |
| 130 | return m.Upgrade().isCustomAdapter(name, operation) |
| 131 | } |
| 132 | |
| 133 | func (m *lazyManifest) Upgrade() *concreteManifest { |
| 134 | m.mu.Lock() |
| 135 | defer m.mu.Unlock() |
| 136 | if m.m == nil { |
| 137 | m.m = newConcreteManifest(m.f, m.apiClient, m.operation, m.remote) |
| 138 | } |
| 139 | return m.m |
| 140 | } |
| 141 | |
| 142 | func (m *lazyManifest) Upgraded() bool { |
| 143 | m.mu.Lock() |
| 144 | defer m.mu.Unlock() |
| 145 | return m.m != nil |
| 146 | } |
| 147 | |
| 148 | type concreteManifest struct { |
| 149 | // maxRetries is the maximum number of retries a single object can |
| 150 | // attempt to make before it will be dropped. maxRetryDelay is the maximum |
| 151 | // time in seconds to wait between retry attempts when using backoff. |
| 152 | maxRetries int |
| 153 | maxRetryDelay int |
| 154 | maxRetryTime int |
| 155 | concurrentTransfers int |
| 156 | basicTransfersOnly bool |
| 157 | standaloneTransferAgent string |
| 158 | tusTransfersAllowed bool |
| 159 | downloadAdapterFuncs map[string]NewAdapterFunc |
| 160 | uploadAdapterFuncs map[string]NewAdapterFunc |
| 161 | customDownloadAdapters map[string]bool |
| 162 | customUploadAdapters map[string]bool |
| 163 | fs *fs.Filesystem |
| 164 | apiClient *lfsapi.Client |
| 165 | sshTransfer *ssh.SSHTransfer |
| 166 | batchClientAdapter BatchClient |
| 167 | mu sync.Mutex |
| 168 | } |
| 169 | |
| 170 | func (m *concreteManifest) APIClient() *lfsapi.Client { |
| 171 | return m.apiClient |
| 172 | } |
| 173 | |
| 174 | func (m *concreteManifest) MaxRetries() int { |
| 175 | return m.maxRetries |
| 176 | } |
| 177 | |
| 178 | func (m *concreteManifest) MaxRetryDelay() int { |
| 179 | return m.maxRetryDelay |
| 180 | } |
| 181 | |
| 182 | func (m *concreteManifest) MaxRetryTime() int { |
| 183 | return m.maxRetryTime |
| 184 | } |
| 185 | |
| 186 | func (m *concreteManifest) ConcurrentTransfers() int { |
| 187 | return m.concurrentTransfers |
| 188 | } |
| 189 | |
| 190 | func (m *concreteManifest) IsStandaloneTransfer() bool { |
| 191 | return m.standaloneTransferAgent != "" |
| 192 | } |
| 193 | |
| 194 | func (m *concreteManifest) batchClient() BatchClient { |
| 195 | if r := m.MaxRetries(); r > 0 { |
| 196 | m.batchClientAdapter.SetMaxRetries(r) |
| 197 | } |
| 198 | return m.batchClientAdapter |
| 199 | } |
| 200 | |
| 201 | func (m *concreteManifest) Upgrade() *concreteManifest { |
| 202 | return m |
| 203 | } |
| 204 | |
| 205 | func (m *concreteManifest) Upgraded() bool { |
| 206 | return true |
| 207 | } |
| 208 | |
| 209 | func NewManifest(f *fs.Filesystem, apiClient *lfsapi.Client, operation, remote string) Manifest { |
| 210 | return newLazyManifest(f, apiClient, operation, remote) |
| 211 | } |
| 212 | |
| 213 | func newConcreteManifest(f *fs.Filesystem, apiClient *lfsapi.Client, operation, remote string) *concreteManifest { |
| 214 | sshTransfer := apiClient.SSHTransfer(operation, remote) |
| 215 | useSSHMultiplexing := false |
| 216 | if sshTransfer != nil { |
| 217 | useSSHMultiplexing = sshTransfer.IsMultiplexingEnabled() |
| 218 | } |
| 219 | |
| 220 | m := &concreteManifest{ |
| 221 | fs: f, |
| 222 | apiClient: apiClient, |
| 223 | batchClientAdapter: &tqClient{Client: apiClient}, |
| 224 | downloadAdapterFuncs: make(map[string]NewAdapterFunc), |
| 225 | uploadAdapterFuncs: make(map[string]NewAdapterFunc), |
| 226 | customDownloadAdapters: make(map[string]bool), |
| 227 | customUploadAdapters: make(map[string]bool), |
| 228 | sshTransfer: sshTransfer, |
| 229 | } |
| 230 | |
| 231 | var tusAllowed bool |
| 232 | if git := apiClient.GitEnv(); git != nil { |
| 233 | if v := git.Int("lfs.transfer.maxretries", 0); v > 0 { |
| 234 | m.maxRetries = v |
| 235 | } |
| 236 | if v := git.Int("lfs.transfer.maxretrydelay", -1); v > -1 { |
| 237 | m.maxRetryDelay = v |
| 238 | } |
| 239 | if v := git.Int("lfs.transfer.maxretrytime", 0); v > 0 { |
| 240 | m.maxRetryTime = v |
| 241 | } |
| 242 | if v := git.Int("lfs.concurrenttransfers", 0); v > 0 { |
| 243 | m.concurrentTransfers = v |
| 244 | } |
| 245 | m.basicTransfersOnly = git.Bool("lfs.basictransfersonly", false) |
| 246 | m.standaloneTransferAgent = findStandaloneTransfer( |
| 247 | apiClient, operation, remote, |
| 248 | ) |
| 249 | tusAllowed = git.Bool("lfs.tustransfers", false) |
| 250 | configureCustomAdapters(git, m) |
| 251 | } |
| 252 | |
| 253 | if m.maxRetries < 1 { |
| 254 | m.maxRetries = defaultMaxRetries |
| 255 | } |
| 256 | if m.maxRetryDelay < 1 { |
| 257 | m.maxRetryDelay = defaultMaxRetryDelay |
| 258 | } |
| 259 | if m.maxRetryTime < 1 { |
| 260 | m.maxRetryTime = defaultMaxRetryTime |
| 261 | } |
| 262 | |
| 263 | if m.concurrentTransfers < 1 { |
| 264 | m.concurrentTransfers = lfshttp.DefaultConcurrentTransfers() |
| 265 | } |
| 266 | |
| 267 | if sshTransfer != nil { |
| 268 | // Multiple concurrent transfers are not supported |
| 269 | // when SSH multiplexing is disabled. |
| 270 | if !useSSHMultiplexing { |
| 271 | m.concurrentTransfers = 1 |
| 272 | } |
| 273 | |
| 274 | m.batchClientAdapter = &SSHBatchClient{ |
| 275 | maxRetries: m.maxRetries, |
| 276 | transfer: sshTransfer, |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | configureBasicDownloadAdapter(m) |
| 281 | configureBasicUploadAdapter(m) |
| 282 | if tusAllowed { |
| 283 | configureTusAdapter(m) |
| 284 | } |
| 285 | configureSSHAdapter(m) |
| 286 | |
| 287 | if m.IsStandaloneTransfer() { |
| 288 | if !m.isCustomAdapter(m.standaloneTransferAgent, operation) { |
| 289 | tracerx.Printf("standalone agent %q is not a registered custom transfer adapter; ignoring", m.standaloneTransferAgent) |
| 290 | m.standaloneTransferAgent = "" |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return m |
| 295 | } |
| 296 | |
| 297 | func findDefaultStandaloneTransfer(url string) string { |
| 298 | if strings.HasPrefix(url, "file://") { |
| 299 | return standaloneFileName |
| 300 | } |
| 301 | return "" |
| 302 | } |
| 303 | |
| 304 | func findStandaloneTransfer(client *lfsapi.Client, operation, remote string) string { |
| 305 | if operation == "" || remote == "" { |
| 306 | v, _ := client.GitEnv().Get("lfs.standalonetransferagent") |
| 307 | return v |
| 308 | } |
| 309 | |
| 310 | ep := client.Endpoints.Endpoint(operation, remote) |
| 311 | uc := config.NewURLConfig(client.GitEnv()) |
| 312 | v, ok := uc.Get("lfs", ep.Url, "standalonetransferagent") |
| 313 | if !ok { |
| 314 | return findDefaultStandaloneTransfer(ep.Url) |
| 315 | } |
| 316 | |
| 317 | return v |
| 318 | } |
| 319 | |
| 320 | // GetAdapterNames returns a list of the names of adapters available to be created |
| 321 | func (m *concreteManifest) GetAdapterNames(dir Direction) []string { |
| 322 | switch dir { |
| 323 | case Upload: |
| 324 | return m.GetUploadAdapterNames() |
| 325 | case Download: |
| 326 | return m.GetDownloadAdapterNames() |
| 327 | } |
| 328 | return nil |
| 329 | } |
| 330 | |
| 331 | // GetDownloadAdapterNames returns a list of the names of download adapters available to be created |
| 332 | func (m *concreteManifest) GetDownloadAdapterNames() []string { |
| 333 | return m.getAdapterNames(m.downloadAdapterFuncs) |
| 334 | } |
| 335 | |
| 336 | // GetUploadAdapterNames returns a list of the names of upload adapters available to be created |
| 337 | func (m *concreteManifest) GetUploadAdapterNames() []string { |
| 338 | return m.getAdapterNames(m.uploadAdapterFuncs) |
| 339 | } |
| 340 | |
| 341 | // getAdapterNames returns a list of the names of adapters available to be created |
| 342 | func (m *concreteManifest) getAdapterNames(adapters map[string]NewAdapterFunc) []string { |
| 343 | if m.basicTransfersOnly { |
| 344 | return []string{BasicAdapterName} |
| 345 | } |
| 346 | |
| 347 | m.mu.Lock() |
| 348 | defer m.mu.Unlock() |
| 349 | |
| 350 | ret := make([]string, 0, len(adapters)) |
| 351 | for n := range adapters { |
| 352 | ret = append(ret, n) |
| 353 | } |
| 354 | return ret |
| 355 | } |
| 356 | |
| 357 | // RegisterNewTransferAdapterFunc registers a new function for creating upload |
| 358 | // or download adapters. If a function with that name & direction is already |
| 359 | // registered, it is overridden |
| 360 | func (m *concreteManifest) RegisterNewAdapterFunc(name string, dir Direction, custom bool, f NewAdapterFunc) { |
| 361 | m.mu.Lock() |
| 362 | defer m.mu.Unlock() |
| 363 | |
| 364 | var wasCustom bool |
| 365 | switch dir { |
| 366 | case Upload: |
| 367 | wasCustom = m.customUploadAdapters[name] |
| 368 | |
| 369 | m.uploadAdapterFuncs[name] = f |
| 370 | m.customUploadAdapters[name] = custom |
| 371 | case Download: |
| 372 | wasCustom = m.customDownloadAdapters[name] |
| 373 | |
| 374 | m.downloadAdapterFuncs[name] = f |
| 375 | m.customDownloadAdapters[name] = custom |
| 376 | } |
| 377 | |
| 378 | if wasCustom && !custom { |
| 379 | fmt.Fprintln(os.Stderr, tr.Tr.Get("warning: custom %s transfer adapter %q ignored due to conflict with standard adapter", dir, name)) |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Create a new adapter by name and direction; default to BasicAdapterName if doesn't exist |
| 384 | func (m *concreteManifest) NewAdapterOrDefault(name string, dir Direction) Adapter { |
| 385 | if len(name) == 0 { |
| 386 | name = BasicAdapterName |
| 387 | } |
| 388 | |
| 389 | a := m.NewAdapter(name, dir) |
| 390 | if a == nil { |
| 391 | tracerx.Printf("Defaulting to basic transfer adapter since %q did not exist", name) |
| 392 | a = m.NewAdapter(BasicAdapterName, dir) |
| 393 | } |
| 394 | return a |
| 395 | } |
| 396 | |
| 397 | // Create a new adapter by name and direction, or nil if doesn't exist |
| 398 | func (m *concreteManifest) NewAdapter(name string, dir Direction) Adapter { |
| 399 | m.mu.Lock() |
| 400 | defer m.mu.Unlock() |
| 401 | |
| 402 | switch dir { |
| 403 | case Upload: |
| 404 | if u, ok := m.uploadAdapterFuncs[name]; ok { |
| 405 | return u(name, dir) |
| 406 | } |
| 407 | case Download: |
| 408 | if d, ok := m.downloadAdapterFuncs[name]; ok { |
| 409 | return d(name, dir) |
| 410 | } |
| 411 | } |
| 412 | return nil |
| 413 | } |
| 414 | |
| 415 | // Create a new download adapter by name, or BasicAdapterName if doesn't exist |
| 416 | func (m *concreteManifest) NewDownloadAdapter(name string) Adapter { |
| 417 | return m.NewAdapterOrDefault(name, Download) |
| 418 | } |
| 419 | |
| 420 | // Create a new upload adapter by name, or BasicAdapterName if doesn't exist |
| 421 | func (m *concreteManifest) NewUploadAdapter(name string) Adapter { |
| 422 | return m.NewAdapterOrDefault(name, Upload) |
| 423 | } |
| 424 | |
| 425 | func (m *concreteManifest) isCustomAdapter(name, operation string) bool { |
| 426 | switch operation { |
| 427 | case "upload": |
| 428 | return m.customUploadAdapters[name] |
| 429 | case "download": |
| 430 | return m.customDownloadAdapters[name] |
| 431 | } |
| 432 | return false |
| 433 | } |
| 434 | |
| 435 | // Env is any object with a config.Environment interface. |
| 436 | type Env interface { |
| 437 | Get(key string) (val string, ok bool) |
| 438 | GetAll(key string) []string |
| 439 | Bool(key string, def bool) (val bool) |
| 440 | Int(key string, def int) (val int) |
| 441 | All() map[string][]string |
| 442 | } |