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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
package tq
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/lfshttp"
"github.com/git-lfs/git-lfs/v3/ssh"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
)
type SSHBatchClient struct {
maxRetries int
transfer *ssh.SSHTransfer
}
func (a *SSHBatchClient) batchInternal(args []string, batchLines []string) (int, []string, []string, error) {
conn, err := a.transfer.Connection(0)
if err != nil {
return 0, nil, nil, errors.Wrap(err, tr.Tr.Get("could not get connection for batch request"))
}
conn.Lock()
defer conn.Unlock()
err = conn.SendMessageWithLines("batch", args, batchLines)
if err != nil {
return 0, nil, nil, errors.Wrap(err, tr.Tr.Get("batch request"))
}
status, args, lines, err := conn.ReadStatusWithLines()
if err != nil {
return status, nil, nil, errors.Wrap(err, tr.Tr.Get("batch response"))
}
return status, args, lines, err
}
func (a *SSHBatchClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, error) {
bRes := &BatchResponse{TransferAdapterName: "ssh"}
if len(bReq.Objects) == 0 {
return bRes, nil
}
missing := make(map[string]bool)
batchLines := make([]string, 0, len(bReq.Objects))
for _, obj := range bReq.Objects {
missing[obj.Oid] = obj.Missing
batchLines = append(batchLines, fmt.Sprintf("%s %d", obj.Oid, obj.Size))
}
tracerx.Printf("api: batch %d files", len(bReq.Objects))
requestedAt := time.Now()
args := []string{"transfer=ssh", "hash-algo=sha256"}
if bReq.Ref != nil {
args = append(args, fmt.Sprintf("refname=%s", bReq.Ref.Name))
}
status, args, lines, err := a.batchInternal(args, batchLines)
if err != nil {
return nil, err
}
if status != 200 {
msg := tr.Tr.Get("no message provided")
if len(lines) > 0 {
msg = lines[0]
}
return nil, errors.New(tr.Tr.Get("batch response: status %d from server (%s)", status, msg))
}
for _, arg := range args {
entries := strings.SplitN(arg, "=", 2)
if len(entries) < 2 {
continue
}
if entries[0] == "hash-algo" {
bRes.HashAlgorithm = entries[1]
if bRes.HashAlgorithm != "sha256" {
return nil, errors.New(tr.Tr.Get("batch response: unsupported hash algorithm: %q", entries[1]))
}
}
}
sort.Strings(lines)
for _, line := range lines {
entries := strings.Split(line, " ")
if len(entries) < 3 {
return nil, errors.New(tr.Tr.Get("batch response: malformed response: %q", line))
}
length := len(bRes.Objects)
if length == 0 || bRes.Objects[length-1].Oid != entries[0] {
bRes.Objects = append(bRes.Objects, &Transfer{Actions: make(map[string]*Action)})
}
transfer := bRes.Objects[len(bRes.Objects)-1]
transfer.Oid = entries[0]
transfer.Size, err = strconv.ParseInt(entries[1], 10, 64)
if err != nil {
return nil, errors.New(tr.Tr.Get("batch response: invalid size: %s", entries[1]))
}
if entries[2] == "noop" {
continue
}
transfer.Actions[entries[2]] = &Action{}
if len(entries) > 3 {
for _, entry := range entries[3:] {
if strings.HasPrefix(entry, "id=") {
transfer.Actions[entries[2]].Id = entry[3:]
} else if strings.HasPrefix(entry, "token=") {
transfer.Actions[entries[2]].Token = entry[6:]
} else if strings.HasPrefix(entry, "expires-in=") {
transfer.Actions[entries[2]].ExpiresIn, err = strconv.Atoi(entry[11:])
if err != nil {
return nil, errors.New(tr.Tr.Get("batch response: invalid expires-in: %s", entry))
}
} else if strings.HasPrefix(entry, "expires-at=") {
transfer.Actions[entries[2]].ExpiresAt, err = time.Parse(time.RFC3339, entry[11:])
if err != nil {
return nil, errors.New(tr.Tr.Get("batch response: invalid expires-at: %s", entry))
}
}
}
}
}
for _, obj := range bRes.Objects {
obj.Missing = missing[obj.Oid]
for _, a := range obj.Actions {
a.createdAt = requestedAt
}
}
return bRes, nil
}
func (a *SSHBatchClient) MaxRetries() int {
return a.maxRetries
}
func (a *SSHBatchClient) SetMaxRetries(n int) {
a.maxRetries = n
}
type SSHAdapter struct {
*adapterBase
ctx lfshttp.Context
transfer *ssh.SSHTransfer
}
// WorkerStarting is called when a worker goroutine starts to process jobs
// Implementations can run some startup logic here & return some context if needed
func (a *SSHAdapter) WorkerStarting(workerNum int) (interface{}, error) {
a.transfer.SetConnectionCountAtLeast(workerNum + 1)
return workerNum, nil
}
// WorkerEnding is called when a worker goroutine is shutting down
// Implementations can clean up per-worker resources here, context is as returned from WorkerStarting
func (a *SSHAdapter) WorkerEnding(workerNum int, ctx interface{}) {
}
func (a *SSHAdapter) tempDir() string {
// Shared with the basic download adapter.
d := filepath.Join(a.fs.LFSStorageDir, "incomplete")
if err := tools.MkdirAll(d, a.fs); err != nil {
return os.TempDir()
}
return d
}
// DoTransfer performs a single transfer within a worker. ctx is any context returned from WorkerStarting
func (a *SSHAdapter) DoTransfer(ctx interface{}, t *Transfer, cb ProgressCallback, authOkFunc func()) error {
if authOkFunc != nil {
authOkFunc()
}
workerNum := ctx.(int)
if a.adapterBase.direction == Upload {
return a.upload(t, workerNum, cb)
} else {
return a.download(t, workerNum, cb)
}
}
func (a *SSHAdapter) download(t *Transfer, workerNum int, cb ProgressCallback) error {
rel, err := t.Rel("download")
if err != nil {
return err
}
if rel == nil {
return errors.Errorf(tr.Tr.Get("No download action for object: %s", t.Oid))
}
// Reserve a temporary filename. We need to make sure nobody operates on the file simultaneously with us.
f, err := tools.TempFile(a.tempDir(), t.Oid, a.fs)
if err != nil {
return err
}
tmpName := f.Name()
defer func() {
if f != nil {
f.Close()
}
os.Remove(tmpName)
}()
return a.doDownload(t, workerNum, f, cb)
}
// doDownload starts a download. f is expected to be an existing file open in RW mode
func (a *SSHAdapter) doDownload(t *Transfer, workerNum int, f *os.File, cb ProgressCallback) error {
args := a.argumentsForTransfer(t, "download")
conn, err := a.transfer.Connection(workerNum)
if err != nil {
return err
}
conn.Lock()
defer conn.Unlock()
err = conn.SendMessage(fmt.Sprintf("get-object %s", t.Oid), args)
if err != nil {
return err
}
status, args, data, err := conn.ReadStatusWithData()
if err != nil {
return err
}
if status < 200 || status > 299 {
buffer := &bytes.Buffer{}
if data != nil {
io.CopyN(buffer, data, 1024)
io.Copy(io.Discard, data)
}
return errors.NewRetriableError(errors.New(tr.Tr.Get("got status %d when fetching OID %s: %s", status, t.Oid, buffer.String())))
}
var actualSize int64
seenSize := false
for _, arg := range args {
if strings.HasPrefix(arg, "size=") {
if seenSize {
return errors.NewProtocolError(tr.Tr.Get("unexpected size argument"), nil)
}
actualSize, err = strconv.ParseInt(arg[5:], 10, 64)
if err != nil || actualSize < 0 {
return errors.NewProtocolError(tr.Tr.Get("expected valid size, got %q", arg[5:]), err)
}
seenSize = true
}
}
if !seenSize {
return errors.NewProtocolError(tr.Tr.Get("no size argument seen"), nil)
}
dlfilename := f.Name()
// Wrap callback to give name context
ccb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
if cb != nil {
return cb(t.Name, totalSize, readSoFar, readSinceLast)
}
return nil
}
hasher := tools.NewHashingReader(data)
written, err := tools.CopyWithCallback(f, hasher, t.Size, ccb)
if err != nil {
return errors.Wrapf(err, tr.Tr.Get("cannot write data to temporary file %q", dlfilename))
}
if actual := hasher.Hash(); actual != t.Oid {
return errors.New(tr.Tr.Get("expected OID %s, got %s after %d bytes written", t.Oid, actual, written))
}
if err := f.Close(); err != nil {
return errors.New(tr.Tr.Get("can't close temporary file %q: %v", dlfilename, err))
}
err = tools.RenameFileCopyPermissions(dlfilename, t.Path)
if _, err2 := os.Stat(t.Path); err2 == nil {
// Target file already exists, possibly was downloaded by other git-lfs process
return nil
}
return err
}
func (a *SSHAdapter) verifyUpload(t *Transfer, workerNum int) error {
args := a.argumentsForTransfer(t, "upload")
conn, err := a.transfer.Connection(workerNum)
if err != nil {
return err
}
conn.Lock()
defer conn.Unlock()
err = conn.SendMessage(fmt.Sprintf("verify-object %s", t.Oid), args)
if err != nil {
return err
}
status, _, lines, err := conn.ReadStatusWithLines()
if err != nil {
return err
}
if status < 200 || status > 299 {
if len(lines) > 0 {
return errors.New(tr.Tr.Get("got status %d when verifying upload OID %s: %s", status, t.Oid, lines[0]))
}
return errors.New(tr.Tr.Get("got status %d when verifying upload OID %s", status, t.Oid))
}
return nil
}
func (a *SSHAdapter) doUpload(t *Transfer, workerNum int, f *os.File, cb ProgressCallback) (int, []string, []string, error) {
args := a.argumentsForTransfer(t, "upload")
// Ensure progress callbacks made while uploading
// Wrap callback to give name context
ccb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
if cb != nil {
return cb(t.Name, totalSize, readSoFar, readSinceLast)
}
return nil
}
cbr := tools.NewFileBodyWithCallback(f, t.Size, ccb)
conn, err := a.transfer.Connection(workerNum)
if err != nil {
return 0, nil, nil, err
}
conn.Lock()
defer conn.Unlock()
defer cbr.Close()
err = conn.SendMessageWithData(fmt.Sprintf("put-object %s", t.Oid), args, cbr)
if err != nil {
return 0, nil, nil, err
}
return conn.ReadStatusWithLines()
}
// upload starts an upload.
func (a *SSHAdapter) upload(t *Transfer, workerNum int, cb ProgressCallback) error {
rel, err := t.Rel("upload")
if err != nil {
return err
}
if rel == nil {
return errors.Errorf(tr.Tr.Get("No upload action for object: %s", t.Oid))
}
f, err := os.OpenFile(t.Path, os.O_RDONLY, 0644)
if err != nil {
return errors.Wrap(err, tr.Tr.Get("SSH upload"))
}
defer f.Close()
status, _, lines, err := a.doUpload(t, workerNum, f, cb)
if err != nil {
return err
}
if status < 200 || status > 299 {
// A status code of 403 likely means that an authentication token for the
// upload has expired. This can be safely retried.
if status == 403 {
err = errors.New(tr.Tr.Get("Received status %d", status))
return errors.NewRetriableError(err)
}
if status == 429 {
return errors.NewRetriableError(errors.New(tr.Tr.Get("got status %d when uploading OID %s", status, t.Oid)))
}
if len(lines) > 0 {
return errors.New(tr.Tr.Get("got status %d when uploading OID %s: %s", status, t.Oid, lines[0]))
}
return errors.New(tr.Tr.Get("got status %d when uploading OID %s", status, t.Oid))
}
return a.verifyUpload(t, workerNum)
}
func (a *SSHAdapter) argumentsForTransfer(t *Transfer, action string) []string {
args := make([]string, 0, 3)
set, ok := t.Actions[action]
if !ok {
return nil
}
args = append(args, fmt.Sprintf("size=%d", t.Size))
if set.Id != "" {
args = append(args, fmt.Sprintf("id=%s", set.Id))
}
if set.Token != "" {
args = append(args, fmt.Sprintf("token=%s", set.Token))
}
return args
}
// Begin a new batch of uploads or downloads. Call this first, followed by one
// or more Add calls. The passed in callback will receive updates on progress.
func (a *SSHAdapter) Begin(cfg AdapterConfig, cb ProgressCallback) error {
if err := a.adapterBase.Begin(cfg, cb); err != nil {
return err
}
a.ctx = a.adapterBase.apiClient.Context()
a.debugging = a.ctx.OSEnv().Bool("GIT_TRANSFER_TRACE", false)
return nil
}
func (a *SSHAdapter) Trace(format string, args ...interface{}) {
if !a.adapterBase.debugging {
return
}
tracerx.Printf(format, args...)
}
func configureSSHAdapter(m *concreteManifest) {
m.RegisterNewAdapterFunc("ssh", Upload, func(name string, dir Direction) Adapter {
a := &SSHAdapter{newAdapterBase(m.fs, name, dir, nil), nil, m.sshTransfer}
a.transferImpl = a
return a
})
m.RegisterNewAdapterFunc("ssh", Download, func(name string, dir Direction) Adapter {
a := &SSHAdapter{newAdapterBase(m.fs, name, dir, nil), nil, m.sshTransfer}
a.transferImpl = a
return a
})
}
|