11 "github.com/git-lfs/git-lfs/errors"
12 "github.com/git-lfs/git-lfs/lfsapi"
13 "github.com/git-lfs/git-lfs/tools"
17 BasicAdapterName = "basic"
20 // Adapter for basic uploads (non resumable)
21 type basicUploadAdapter struct {
25 func (a *basicUploadAdapter) ClearTempStorage() error {
26 // Should be empty already but also remove dir
27 return os.RemoveAll(a.tempDir())
30 func (a *basicUploadAdapter) tempDir() string {
31 // Must be dedicated to this adapter as deleted by ClearTempStorage
32 d := filepath.Join(os.TempDir(), "git-lfs-basic-temp")
33 if err := os.MkdirAll(d, 0755); err != nil {
39 func (a *basicUploadAdapter) WorkerStarting(workerNum int) (interface{}, error) {
42 func (a *basicUploadAdapter) WorkerEnding(workerNum int, ctx interface{}) {
45 func (a *basicUploadAdapter) DoTransfer(ctx interface{}, t *Transfer, cb ProgressCallback, authOkFunc func()) error {
46 rel, err := t.Rel("upload")
51 return errors.Errorf("No upload action for object: %s", t.Oid)
54 req, err := a.newHTTPRequest("PUT", rel)
59 if len(req.Header.Get("Content-Type")) == 0 {
60 req.Header.Set("Content-Type", "application/octet-stream")
63 if req.Header.Get("Transfer-Encoding") == "chunked" {
64 req.TransferEncoding = []string{"chunked"}
66 req.Header.Set("Content-Length", strconv.FormatInt(t.Size, 10))
69 req.ContentLength = t.Size
71 f, err := os.OpenFile(t.Path, os.O_RDONLY, 0644)
73 return errors.Wrap(err, "basic upload")
77 // Ensure progress callbacks made while uploading
78 // Wrap callback to give name context
79 ccb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
81 return cb(t.Name, totalSize, readSoFar, readSinceLast)
86 cbr := tools.NewBodyWithCallback(f, t.Size, ccb)
87 var reader lfsapi.ReadSeekCloser = cbr
89 // Signal auth was ok on first read; this frees up other workers to start
90 if authOkFunc != nil {
91 reader = newStartCallbackReader(reader, func() error {
99 req = a.apiClient.LogRequest(req, "lfs.data.upload")
100 res, err := a.doHTTP(t, req)
102 // We're about to return a retriable error, meaning that this
103 // transfer will either be retried, or it will fail.
105 // Either way, let's decrement the number of bytes that we've
106 // read _so far_, so that the next iteration doesn't re-transfer
107 // those bytes, according to the progress meter.
108 if perr := cbr.ResetProgress(); perr != nil {
109 err = errors.Wrap(err, perr.Error())
112 return errors.NewRetriableError(err)
115 // A status code of 403 likely means that an authentication token for the
116 // upload has expired. This can be safely retried.
117 if res.StatusCode == 403 {
118 err = errors.New("http: received status 403")
119 return errors.NewRetriableError(err)
122 if res.StatusCode > 299 {
123 return errors.Wrapf(nil, "Invalid status for %s %s: %d",
125 strings.SplitN(req.URL.String(), "?", 2)[0],
130 io.Copy(ioutil.Discard, res.Body)
133 return verifyUpload(a.apiClient, a.remote, t)
136 // startCallbackReader is a reader wrapper which calls a function as soon as the
137 // first Read() call is made. This callback is only made once
138 type startCallbackReader struct {
141 lfsapi.ReadSeekCloser
144 func (s *startCallbackReader) Read(p []byte) (n int, err error) {
145 if !s.cbDone && s.cb != nil {
146 if err := s.cb(); err != nil {
151 return s.ReadSeekCloser.Read(p)
153 func newStartCallbackReader(r lfsapi.ReadSeekCloser, cb func() error) *startCallbackReader {
154 return &startCallbackReader{
160 func configureBasicUploadAdapter(m *Manifest) {
161 m.RegisterNewAdapterFunc(BasicAdapterName, Upload, func(name string, dir Direction) Adapter {
164 bu := &basicUploadAdapter{newAdapterBase(m.fs, name, dir, nil)}
165 // self implements impl
169 panic("Should never ask this func for basic download")