MCPcopy
hub / github.com/git-lfs/git-lfs / download

Method download

tq/basic_download.go:115–263  ·  view source on GitHub ↗

download starts or resumes and download. dlFile is expected to be an existing file open in RW mode

(t *Transfer, cb ProgressCallback, authOkFunc func(), dlFile *os.File, fromByte int64, hash hash.Hash)

Source from the content-addressed store, hash-verified

113
114// download starts or resumes and download. dlFile is expected to be an existing file open in RW mode
115func (a *basicDownloadAdapter) download(t *Transfer, cb ProgressCallback, authOkFunc func(), dlFile *os.File, fromByte int64, hash hash.Hash) error {
116 rel, err := t.Rel("download")
117 if err != nil {
118 return err
119 }
120 if rel == nil {
121 return errors.New(tr.Tr.Get("Object %s not found on the server.", t.Oid))
122 }
123
124 req, err := a.newHTTPRequest("GET", rel)
125 if err != nil {
126 return err
127 }
128
129 if fromByte > 0 {
130 // We could just use a start byte, but since we know the length be specific
131 req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", fromByte, t.Size-1))
132 }
133
134 req = a.apiClient.LogRequest(req, "lfs.data.download")
135 res, err := a.makeRequest(t, req)
136 if err != nil {
137 if res == nil {
138 // We encountered a network or similar error which caused us
139 // to not receive a response at all.
140 return errors.NewRetriableError(err)
141 }
142
143 // Special-case status code 416 () - fall back
144 if fromByte > 0 && dlFile != nil && res.StatusCode == 416 {
145 tracerx.Printf("xfer: server rejected resume download request for %q from byte %d; re-downloading from start", t.Oid, fromByte)
146 if _, err := dlFile.Seek(0, io.SeekStart); err != nil {
147 return err
148 }
149 if err := dlFile.Truncate(0); err != nil {
150 return err
151 }
152 return a.download(t, cb, authOkFunc, dlFile, 0, nil)
153 }
154
155 // Special-cae status code 429 - retry after certain time
156 if res.StatusCode == 429 {
157 retLaterErr := errors.NewRetriableLaterError(err, res.Header.Get("Retry-After"))
158 if retLaterErr != nil {
159 return retLaterErr
160 }
161 }
162
163 return errors.NewRetriableError(err)
164 }
165
166 defer res.Body.Close()
167
168 // Range request must return 206 & content range to confirm
169 if fromByte > 0 {
170 rangeRequestOk := false
171 var failReason string
172 // check 206 and Content-Range, fall back if either not as expected

Callers 1

DoTransferMethod · 0.95

Calls 15

makeRequestMethod · 0.95
HashMethod · 0.95
NewFunction · 0.92
NewRetriableErrorFunction · 0.92
NewRetriableLaterErrorFunction · 0.92
NewRetriableReaderFunction · 0.92
NewHashingReaderFunction · 0.92
CopyWithCallbackFunction · 0.92
WrapFunction · 0.92
advanceCallbackProgressFunction · 0.85

Tested by

no test coverage detected