Debian Codegraph @

tq/custom.go

raw symbols SCIP
1 package tq
2
3 import (
4 "bufio"
5 "bytes"
6 "encoding/json"
7 "fmt"
8 "io"
9 "path/filepath"
10 "regexp"
11 "strings"
12 "time"
13
14 "github.com/git-lfs/git-lfs/v3/errors"
15 "github.com/git-lfs/git-lfs/v3/fs"
16 "github.com/git-lfs/git-lfs/v3/tools"
17 "github.com/git-lfs/git-lfs/v3/tr"
18
19 "github.com/git-lfs/git-lfs/v3/subprocess"
20 "github.com/rubyist/tracerx"
21 )
22
23 // Adapter for custom transfer via external process
24 type customAdapter struct {
25 *adapterBase
26 path string
27 args string
28 concurrent bool
29 originalConcurrency int
30 standalone bool
31 }
32
33 // Struct to capture stderr and write to trace
34 type traceWriter struct {
35 buf bytes.Buffer
36 processName string
37 }
38
39 func (t *traceWriter) Write(b []byte) (int, error) {
40 n, err := t.buf.Write(b)
41 t.Flush()
42 return n, err
43 }
44 func (t *traceWriter) Flush() {
45 var err error
46 for err == nil {
47 var s string
48 s, err = t.buf.ReadString('\n')
49 if len(s) > 0 {
50 tracerx.Printf("xfer[%v]: %v", t.processName, strings.TrimSpace(s))
51 }
52 }
53 }
54
55 type customAdapterWorkerContext struct {
56 workerNum int
57 cmd *subprocess.Cmd
58 stdout io.ReadCloser
59 bufferedOut *bufio.Reader
60 stdin io.WriteCloser
61 errTracer *traceWriter
62 }
63
64 type customAdapterInitRequest struct {
65 Event string `json:"event"`
66 Operation string `json:"operation"`
67 Remote string `json:"remote"`
68 Concurrent bool `json:"concurrent"`
69 ConcurrentTransfers int `json:"concurrenttransfers"`
70 }
71
72 func NewCustomAdapterInitRequest(
73 op string, remote string, concurrent bool, concurrentTransfers int,
74 ) *customAdapterInitRequest {
75 return &customAdapterInitRequest{"init", op, remote, concurrent, concurrentTransfers}
76 }
77
78 type customAdapterTransferRequest struct {
79 // common between upload/download
80 Event string `json:"event"`
81 Oid string `json:"oid"`
82 Size int64 `json:"size"`
83 Path string `json:"path,omitempty"`
84 Action *Action `json:"action"`
85 }
86
87 func NewCustomAdapterUploadRequest(oid string, size int64, path string, action *Action) *customAdapterTransferRequest {
88 return &customAdapterTransferRequest{"upload", oid, size, path, action}
89 }
90 func NewCustomAdapterDownloadRequest(oid string, size int64, action *Action) *customAdapterTransferRequest {
91 return &customAdapterTransferRequest{"download", oid, size, "", action}
92 }
93
94 type customAdapterTerminateRequest struct {
95 Event string `json:"event"`
96 }
97
98 func NewCustomAdapterTerminateRequest() *customAdapterTerminateRequest {
99 return &customAdapterTerminateRequest{"terminate"}
100 }
101
102 // A common struct that allows all types of response to be identified
103 type customAdapterResponseMessage struct {
104 Event string `json:"event"`
105 Error *ObjectError `json:"error"`
106 Oid string `json:"oid"`
107 Path string `json:"path,omitempty"` // always blank for upload
108 BytesSoFar int64 `json:"bytesSoFar"`
109 BytesSinceLast int `json:"bytesSinceLast"`
110 }
111
112 func (a *customAdapter) Begin(cfg AdapterConfig, cb ProgressCallback) error {
113 a.originalConcurrency = cfg.ConcurrentTransfers()
114 if a.concurrent {
115 // Use common workers impl, but downgrade workers to number of processes
116 return a.adapterBase.Begin(cfg, cb)
117 }
118
119 // If config says not to launch multiple processes, downgrade incoming value
120 return a.adapterBase.Begin(&customAdapterConfig{AdapterConfig: cfg}, cb)
121 }
122
123 func (a *customAdapter) WorkerStarting(workerNum int) (interface{}, error) {
124 // Start a process per worker
125 // If concurrent = false we have already dialled back workers to 1
126 a.Trace("xfer: starting up custom transfer process %q for worker %d", a.name, workerNum)
127 cmdName, cmdArgs := subprocess.FormatForShell(subprocess.ShellQuoteSingle(a.path), a.args)
128 cmd, err := subprocess.ExecCommand(cmdName, cmdArgs...)
129 if err != nil {
130 return nil, errors.New(tr.Tr.Get("failed to find custom transfer command %q remote: %v", a.path, err))
131 }
132 outp, err := cmd.StdoutPipe()
133 if err != nil {
134 return nil, errors.New(tr.Tr.Get("failed to get stdout for custom transfer command %q remote: %v", a.path, err))
135 }
136 inp, err := cmd.StdinPipe()
137 if err != nil {
138 return nil, errors.New(tr.Tr.Get("failed to get stdin for custom transfer command %q remote: %v", a.path, err))
139 }
140 // Capture stderr to trace
141 tracer := &traceWriter{}
142 tracer.processName = filepath.Base(a.path)
143 cmd.Stderr = tracer
144 err = cmd.Start()
145 if err != nil {
146 return nil, errors.New(tr.Tr.Get("failed to start custom transfer command %q remote: %v", a.path, err))
147 }
148 // Set up buffered reader/writer since we operate on lines
149 ctx := &customAdapterWorkerContext{workerNum, cmd, outp, bufio.NewReader(outp), inp, tracer}
150
151 // send initiate message
152 initReq := NewCustomAdapterInitRequest(
153 a.getOperationName(), a.remote, a.concurrent, a.originalConcurrency,
154 )
155 resp, err := a.exchangeMessage(ctx, initReq)
156 if err != nil {
157 a.abortWorkerProcess(ctx)
158 return nil, err
159 }
160 if resp.Error != nil {
161 a.abortWorkerProcess(ctx)
162 return nil, errors.New(tr.Tr.Get("error initializing custom adapter %q worker %d: %v", a.name, workerNum, resp.Error))
163 }
164
165 a.Trace("xfer: started custom adapter process %q for worker %d OK", a.path, workerNum)
166
167 // Save this process context and use in future callbacks
168 return ctx, nil
169 }
170
171 func (a *customAdapter) getOperationName() string {
172 if a.direction == Download {
173 return "download"
174 }
175 return "upload"
176 }
177
178 // sendMessage sends a JSON message to the custom adapter process
179 func (a *customAdapter) sendMessage(ctx *customAdapterWorkerContext, req interface{}) error {
180 b, err := json.Marshal(req)
181 if err != nil {
182 return err
183 }
184 a.Trace("xfer: Custom adapter worker %d sending message: %v", ctx.workerNum, string(b))
185 // Line oriented JSON
186 b = append(b, '\n')
187 _, err = ctx.stdin.Write(b)
188 return err
189 }
190
191 func (a *customAdapter) readResponse(ctx *customAdapterWorkerContext) (*customAdapterResponseMessage, error) {
192 line, err := ctx.bufferedOut.ReadString('\n')
193 if err != nil {
194 return nil, err
195 }
196 a.Trace("xfer: Custom adapter worker %d received response: %v", ctx.workerNum, strings.TrimSpace(line))
197 resp := &customAdapterResponseMessage{}
198 err = json.Unmarshal([]byte(line), resp)
199 return resp, err
200 }
201
202 // exchangeMessage sends a message to a process and reads a response if resp != nil
203 // Only fatal errors to communicate return an error, errors may be embedded in reply
204 func (a *customAdapter) exchangeMessage(ctx *customAdapterWorkerContext, req interface{}) (*customAdapterResponseMessage, error) {
205 err := a.sendMessage(ctx, req)
206 if err != nil {
207 return nil, err
208 }
209 return a.readResponse(ctx)
210 }
211
212 // shutdownWorkerProcess terminates gracefully a custom adapter process
213 // returns an error if it couldn't shut down gracefully (caller may abortWorkerProcess)
214 func (a *customAdapter) shutdownWorkerProcess(ctx *customAdapterWorkerContext) error {
215 defer ctx.errTracer.Flush()
216
217 a.Trace("xfer: Shutting down adapter worker %d", ctx.workerNum)
218
219 finishChan := make(chan error, 1)
220 go func() {
221 termReq := NewCustomAdapterTerminateRequest()
222 err := a.sendMessage(ctx, termReq)
223 if err != nil {
224 finishChan <- err
225 }
226 ctx.stdin.Close()
227 ctx.stdout.Close()
228 finishChan <- ctx.cmd.Wait()
229 }()
230 select {
231 case err := <-finishChan:
232 return err
233 case <-time.After(30 * time.Second):
234 return errors.New(tr.Tr.Get("timeout while shutting down worker process %d", ctx.workerNum))
235 }
236 }
237
238 // abortWorkerProcess terminates & aborts untidily, most probably breakdown of comms or internal error
239 func (a *customAdapter) abortWorkerProcess(ctx *customAdapterWorkerContext) {
240 a.Trace("xfer: Aborting worker process: %d", ctx.workerNum)
241 ctx.stdin.Close()
242 ctx.stdout.Close()
243 ctx.cmd.Process.Kill()
244 }
245 func (a *customAdapter) WorkerEnding(workerNum int, ctx interface{}) {
246 customCtx, ok := ctx.(*customAdapterWorkerContext)
247 if !ok {
248 tracerx.Printf("Context object for custom transfer %q was of the wrong type", a.name)
249 return
250 }
251
252 err := a.shutdownWorkerProcess(customCtx)
253 if err != nil {
254 tracerx.Printf("xfer: error finishing up custom transfer process %q worker %d, aborting: %v", a.path, customCtx.workerNum, err)
255 a.abortWorkerProcess(customCtx)
256 }
257 }
258
259 func (a *customAdapter) DoTransfer(ctx interface{}, t *Transfer, cb ProgressCallback, authOkFunc func()) error {
260 if ctx == nil {
261 return errors.New(tr.Tr.Get("custom transfer %q was not properly initialized, see previous errors", a.name))
262 }
263
264 customCtx, ok := ctx.(*customAdapterWorkerContext)
265 if !ok {
266 return errors.New(tr.Tr.Get("context object for custom transfer %q was of the wrong type", a.name))
267 }
268 var authCalled bool
269
270 rel, err := t.Rel(a.getOperationName())
271 if err != nil {
272 return err
273 }
274 if rel == nil && !a.standalone {
275 return errors.New(tr.Tr.Get("Object %s not found on the server.", t.Oid))
276 }
277 var req *customAdapterTransferRequest
278 if a.direction == Upload {
279 req = NewCustomAdapterUploadRequest(t.Oid, t.Size, t.Path, rel)
280 } else {
281 req = NewCustomAdapterDownloadRequest(t.Oid, t.Size, rel)
282 }
283 if err = a.sendMessage(customCtx, req); err != nil {
284 return err
285 }
286
287 // 1..N replies (including progress & one of download / upload)
288 var complete bool
289 for !complete {
290 resp, err := a.readResponse(customCtx)
291 if err != nil {
292 return err
293 }
294 var wasAuthOk bool
295 switch resp.Event {
296 case "progress":
297 // Progress
298 if resp.Oid != t.Oid {
299 return errors.New(tr.Tr.Get("unexpected OID %q in response, expecting %q", resp.Oid, t.Oid))
300 }
301 if cb != nil {
302 cb(t.Name, t.Size, resp.BytesSoFar, resp.BytesSinceLast)
303 }
304 wasAuthOk = resp.BytesSoFar > 0
305 case "complete":
306 // Download/Upload complete
307 if resp.Oid != t.Oid {
308 return errors.New(tr.Tr.Get("unexpected OID %q in response, expecting %q", resp.Oid, t.Oid))
309 }
310 if resp.Error != nil {
311 return errors.New(tr.Tr.Get("error transferring %q: %v", t.Oid, resp.Error))
312 }
313 if a.direction == Download {
314 // So we don't have to blindly trust external providers, check SHA
315 if err = tools.VerifyFileHash(t.Oid, resp.Path); err != nil {
316 return errors.New(tr.Tr.Get("downloaded file failed checks: %v", err))
317 }
318 // Move file to final location
319 if err = tools.RenameFileCopyPermissions(resp.Path, t.Path); err != nil {
320 return errors.New(tr.Tr.Get("failed to copy downloaded file: %v", err))
321 }
322 } else if a.direction == Upload {
323 if err = verifyUpload(a.apiClient, a.remote, t); err != nil {
324 return err
325 }
326 }
327 wasAuthOk = true
328 complete = true
329 default:
330 return errors.New(tr.Tr.Get("invalid message %q from custom adapter %q", resp.Event, a.name))
331 }
332 // Fall through from both progress and completion messages
333 // Call auth on first progress or success to free up other workers
334 if wasAuthOk && authOkFunc != nil && !authCalled {
335 authOkFunc()
336 authCalled = true
337 }
338 }
339
340 return nil
341 }
342
343 func newCustomAdapter(f *fs.Filesystem, name string, dir Direction, path, args string, concurrent, standalone bool) *customAdapter {
344 c := &customAdapter{newAdapterBase(f, name, dir, nil), path, args, concurrent, 3, standalone}
345 // self implements impl
346 c.transferImpl = c
347 return c
348 }
349
350 const (
351 standaloneFileName = "lfs-standalone-file"
352 )
353
354 func configureDefaultCustomAdapters(git Env, m *concreteManifest) {
355 newfunc := func(name string, dir Direction) Adapter {
356 standalone := m.standaloneTransferAgent != ""
357 return newCustomAdapter(m.fs, standaloneFileName, dir, "git-lfs", "standalone-file", false, standalone)
358 }
359 m.RegisterNewAdapterFunc(standaloneFileName, Download, true, newfunc)
360 m.RegisterNewAdapterFunc(standaloneFileName, Upload, true, newfunc)
361 }
362
363 // Initialise custom adapters based on current config
364 func configureCustomAdapters(git Env, m *concreteManifest) {
365 configureDefaultCustomAdapters(git, m)
366
367 pathRegex := regexp.MustCompile(`lfs\.((?i)customtransfer\.([^.]+))\.path`)
368 for k, _ := range git.All() {
369 match := pathRegex.FindStringSubmatch(k)
370 if match == nil {
371 continue
372 }
373
374 subSection := match[1]
375 name := match[2]
376 path, _ := git.Get(k)
377 // retrieve other values
378 args, _ := git.Get(fmt.Sprintf("lfs.%s.args", subSection))
379 concurrent := git.Bool(fmt.Sprintf("lfs.%s.concurrent", subSection), true)
380 direction, _ := git.Get(fmt.Sprintf("lfs.%s.direction", subSection))
381 if len(direction) == 0 {
382 direction = "both"
383 } else {
384 direction = strings.ToLower(direction)
385 }
386
387 // Separate closure for each since we need to capture vars above
388 newfunc := func(name string, dir Direction) Adapter {
389 standalone := m.standaloneTransferAgent != ""
390 return newCustomAdapter(m.fs, name, dir, path, args, concurrent, standalone)
391 }
392
393 if direction == "download" || direction == "both" {
394 m.RegisterNewAdapterFunc(name, Download, true, newfunc)
395 }
396 if direction == "upload" || direction == "both" {
397 m.RegisterNewAdapterFunc(name, Upload, true, newfunc)
398 }
399 }
400 }
401
402 type customAdapterConfig struct {
403 AdapterConfig
404 }
405
406 func (c *customAdapterConfig) ConcurrentTransfers() int {
407 return 1
408 }