2016-02-20 21:05:48 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2016-02-20 21:05:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
Fix REST backend HTTP keepalive
This is subtle. A combination od fast client disk (read: SSD) with lots
of files and fast network connection to restic-server would suddenly
start getting lots of "dial tcp: connect: cannot assign requested
address" errors during backup stage. Further inspection revealed that
client machine was plagued with TCP sockets in TIME_WAIT state. When
ephemeral port range was finally exhausted, no more sockets could be
opened, so restic would freak out.
To understand the magnitude of this problem, with ~18k ports and default
timeout of 60 seconds, it means more than 300 HTTP connections per
seconds were created and teared down. Yeah, restic-server is that
fast. :)
As it turns out, this behavior was product of 2 subtle issues:
1) The body of HTTP response wasn't read completely with io.ReadFull()
at the end of the Load() function. This deactivated HTTP keepalive,
so already open connections were not reused, but closed instead, and
new ones opened for every new request. io.Copy(ioutil.Discard,
resp.Body) before resp.Body.Close() remedies this.
2) Even with the above fix, somehow having MaxIdleConnsPerHost at its
default value of 2 wasn't enough to stop reconnecting. It is hard to
understand why this would be so detrimental, it could even be some
subtle Go runtime bug. Anyhow, setting this value to match the
connection limit, as set by connLimit global variable, finally nails
this ugly bug.
I fixed several other places where the response body wasn't read in
full (or at all). For example, json.NewDecoder() is also known not to
read the whole body of response.
Unfortunately, this is not over yet. :( The check command is firing up
to 40 simultaneous connections to the restic-server. Then, once again,
MaxIdleConnsPerHost is too low to support keepalive, and sockets in the
TIME_WAIT state pile up. But, as this kind of concurrency absolutely
kill the poor disk on the server side, this is a completely different
bug then.
2016-11-09 21:37:20 +00:00
|
|
|
"io/ioutil"
|
2016-02-20 21:05:48 +00:00
|
|
|
"net/http"
|
2021-07-30 08:17:28 +00:00
|
|
|
"net/textproto"
|
2016-02-20 21:05:48 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2021-05-15 22:28:12 +00:00
|
|
|
"strconv"
|
2016-02-21 15:35:25 +00:00
|
|
|
"strings"
|
2016-02-20 21:05:48 +00:00
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
|
|
|
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2016-02-20 21:05:48 +00:00
|
|
|
)
|
|
|
|
|
2017-01-22 11:32:20 +00:00
|
|
|
// make sure the rest backend implements restic.Backend
|
2018-03-13 21:30:41 +00:00
|
|
|
var _ restic.Backend = &Backend{}
|
2017-01-22 11:32:20 +00:00
|
|
|
|
2018-03-13 21:30:41 +00:00
|
|
|
// Backend uses the REST protocol to access data stored on a server.
|
|
|
|
type Backend struct {
|
2017-06-05 22:25:22 +00:00
|
|
|
url *url.URL
|
|
|
|
sem *backend.Semaphore
|
2017-06-07 17:48:32 +00:00
|
|
|
client *http.Client
|
2017-04-11 19:47:57 +00:00
|
|
|
backend.Layout
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:35 +00:00
|
|
|
// the REST API protocol version is decided by HTTP request headers, these are the constants.
|
2018-01-23 22:12:52 +00:00
|
|
|
const (
|
2018-03-13 21:22:35 +00:00
|
|
|
ContentTypeV1 = "application/vnd.x.restic.rest.v1"
|
|
|
|
ContentTypeV2 = "application/vnd.x.restic.rest.v2"
|
2018-01-23 22:12:52 +00:00
|
|
|
)
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
// Open opens the REST backend with the given config.
|
2018-03-13 21:30:41 +00:00
|
|
|
func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
|
2017-09-24 18:04:23 +00:00
|
|
|
client := &http.Client{Transport: rt}
|
2016-02-20 21:05:48 +00:00
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
sem, err := backend.NewSemaphore(cfg.Connections)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:47:57 +00:00
|
|
|
// use url without trailing slash for layout
|
|
|
|
url := cfg.URL.String()
|
|
|
|
if url[len(url)-1] == '/' {
|
|
|
|
url = url[:len(url)-1]
|
|
|
|
}
|
|
|
|
|
2018-03-13 21:30:41 +00:00
|
|
|
be := &Backend{
|
2017-06-05 22:25:22 +00:00
|
|
|
url: cfg.URL,
|
|
|
|
client: client,
|
|
|
|
Layout: &backend.RESTLayout{URL: url, Join: path.Join},
|
|
|
|
sem: sem,
|
2017-04-11 19:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:50:26 +00:00
|
|
|
// Create creates a new REST on server configured in config.
|
2020-04-10 10:08:52 +00:00
|
|
|
func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
|
2017-09-24 18:04:23 +00:00
|
|
|
be, err := Open(cfg, rt)
|
2017-03-16 20:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-10 10:08:52 +00:00
|
|
|
_, err = be.Stat(ctx, restic.Handle{Type: restic.ConfigFile})
|
2017-03-16 20:50:26 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil, errors.Fatal("config file already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
url := *cfg.URL
|
|
|
|
values := url.Query()
|
|
|
|
values.Set("create", "true")
|
|
|
|
url.RawQuery = values.Encode()
|
|
|
|
|
2017-11-25 19:56:40 +00:00
|
|
|
resp, err := be.client.Post(url.String(), "binary/octet-stream", strings.NewReader(""))
|
2017-03-16 20:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Fatalf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
// Location returns this backend's location (the server's URL).
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Location() string {
|
2016-02-20 21:05:48 +00:00
|
|
|
return b.url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores data in the backend at the handle.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
2016-02-20 21:05:48 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return backoff.Permanent(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-06-15 13:58:23 +00:00
|
|
|
// make sure that client.Post() cannot close the reader by wrapping it
|
2018-03-03 13:20:54 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, b.Filename(h), ioutil.NopCloser(rd))
|
2018-01-23 22:12:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "NewRequest")
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/octet-stream")
|
2018-03-13 21:22:35 +00:00
|
|
|
req.Header.Set("Accept", ContentTypeV2)
|
2018-01-23 22:12:52 +00:00
|
|
|
|
2018-03-08 21:14:12 +00:00
|
|
|
// explicitly set the content length, this prevents chunked encoding and
|
|
|
|
// let's the server know what's coming.
|
|
|
|
req.ContentLength = rd.Length()
|
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.GetToken()
|
2018-01-23 22:12:52 +00:00
|
|
|
resp, err := ctxhttp.Do(ctx, b.client, req)
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.ReleaseToken()
|
2016-02-20 21:05:48 +00:00
|
|
|
|
2021-01-30 18:25:04 +00:00
|
|
|
var cerr error
|
2016-02-20 21:05:48 +00:00
|
|
|
if resp != nil {
|
2021-01-30 18:25:04 +00:00
|
|
|
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
cerr = resp.Body.Close()
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-08-29 19:54:50 +00:00
|
|
|
return errors.Wrap(err, "client.Post")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2017-05-28 10:31:19 +00:00
|
|
|
return errors.Errorf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:25:04 +00:00
|
|
|
return errors.Wrap(cerr, "Close")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
// ErrIsNotExist is returned whenever the requested file does not exist on the
|
|
|
|
// server.
|
|
|
|
type ErrIsNotExist struct {
|
|
|
|
restic.Handle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrIsNotExist) Error() string {
|
|
|
|
return fmt.Sprintf("%v does not exist", e.Handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotExist returns true if the error was caused by a non-existing file.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) IsNotExist(err error) bool {
|
2017-06-15 11:40:27 +00:00
|
|
|
err = errors.Cause(err)
|
|
|
|
_, ok := err.(ErrIsNotExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
2020-10-02 23:23:34 +00:00
|
|
|
r, err := b.openReader(ctx, h, length, offset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fn(r)
|
|
|
|
if err != nil {
|
|
|
|
_ = r.Close() // ignore error here
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: readerat.ReadAt() (the fn) uses io.ReadFull() that doesn't
|
|
|
|
// wait for EOF after reading body. Due to HTTP/2 stream multiplexing
|
|
|
|
// and goroutine timings the EOF frame arrives from server (eg. rclone)
|
|
|
|
// with a delay after reading body. Immediate close might trigger
|
|
|
|
// HTTP/2 stream reset resulting in the *stream closed* error on server,
|
|
|
|
// so we wait for EOF before closing body.
|
|
|
|
var buf [1]byte
|
|
|
|
_, err = r.Read(buf[:])
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := r.Close(); err == nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
return err
|
2018-01-17 04:59:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 08:17:28 +00:00
|
|
|
// checkContentLength returns an error if the server returned a value in the
|
|
|
|
// Content-Length header in an HTTP2 connection, but closed the connection
|
|
|
|
// before any data was sent.
|
|
|
|
//
|
|
|
|
// This is a workaround for https://github.com/golang/go/issues/46071
|
|
|
|
//
|
|
|
|
// See also https://forum.restic.net/t/http2-stream-closed-connection-reset-context-canceled/3743/10
|
|
|
|
func checkContentLength(resp *http.Response) error {
|
|
|
|
// the following code is based on
|
|
|
|
// https://github.com/golang/go/blob/b7a85e0003cedb1b48a1fd3ae5b746ec6330102e/src/net/http/h2_bundle.go#L8646
|
|
|
|
|
|
|
|
if resp.ContentLength != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ProtoMajor != 2 && resp.ProtoMinor != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Header[textproto.CanonicalMIMEHeaderKey("Content-Length")]) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that if the server returned a content length and we can
|
|
|
|
// parse it, it is really zero, otherwise return an error
|
|
|
|
contentLength := resp.Header.Get("Content-Length")
|
|
|
|
cl, err := strconv.ParseUint(contentLength, 10, 63)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse Content-Length %q: %w", contentLength, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cl != 0 {
|
|
|
|
return errors.Errorf("unexpected EOF: got 0 instead of %v bytes", cl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-01-23 17:11:10 +00:00
|
|
|
debug.Log("Load %v, length %v, offset %v", h, length, offset)
|
2017-01-22 21:01:12 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return nil, backoff.Permanent(err)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if length < 0 {
|
|
|
|
return nil, errors.Errorf("invalid length %d", length)
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:47:57 +00:00
|
|
|
req, err := http.NewRequest("GET", b.Filename(h), nil)
|
2017-01-22 21:01:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "http.NewRequest")
|
|
|
|
}
|
|
|
|
|
|
|
|
byteRange := fmt.Sprintf("bytes=%d-", offset)
|
|
|
|
if length > 0 {
|
|
|
|
byteRange = fmt.Sprintf("bytes=%d-%d", offset, offset+int64(length)-1)
|
|
|
|
}
|
2018-01-23 22:12:52 +00:00
|
|
|
req.Header.Set("Range", byteRange)
|
2018-03-13 21:22:35 +00:00
|
|
|
req.Header.Set("Accept", ContentTypeV2)
|
2017-01-23 17:11:10 +00:00
|
|
|
debug.Log("Load(%v) send range %v", h, byteRange)
|
2017-01-22 21:01:12 +00:00
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.GetToken()
|
2017-06-03 15:39:57 +00:00
|
|
|
resp, err := ctxhttp.Do(ctx, b.client, req)
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.ReleaseToken()
|
2017-01-22 21:01:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if resp != nil {
|
2017-06-03 15:39:57 +00:00
|
|
|
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
_ = resp.Body.Close()
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
return nil, errors.Wrap(err, "client.Do")
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
return nil, ErrIsNotExist{h}
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:01:12 +00:00
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 206 {
|
2017-06-03 15:39:57 +00:00
|
|
|
_ = resp.Body.Close()
|
2017-05-28 10:31:19 +00:00
|
|
|
return nil, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 22:28:12 +00:00
|
|
|
// workaround https://github.com/golang/go/issues/46071
|
|
|
|
// see also https://forum.restic.net/t/http2-stream-closed-connection-reset-context-canceled/3743/10
|
2021-07-30 08:17:28 +00:00
|
|
|
err = checkContentLength(resp)
|
|
|
|
if err != nil {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
return nil, err
|
2021-05-15 22:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:01:12 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
// Stat returns information about a blob.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
|
2016-02-20 21:05:48 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return restic.FileInfo{}, backoff.Permanent(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 22:12:52 +00:00
|
|
|
req, err := http.NewRequest(http.MethodHead, b.Filename(h), nil)
|
|
|
|
if err != nil {
|
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "NewRequest")
|
|
|
|
}
|
2018-03-13 21:22:35 +00:00
|
|
|
req.Header.Set("Accept", ContentTypeV2)
|
2018-01-23 22:12:52 +00:00
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.GetToken()
|
2018-01-23 22:12:52 +00:00
|
|
|
resp, err := ctxhttp.Do(ctx, b.client, req)
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.ReleaseToken()
|
2016-02-20 21:05:48 +00:00
|
|
|
if err != nil {
|
2016-08-31 20:51:35 +00:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "client.Head")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
2016-02-20 21:05:48 +00:00
|
|
|
if err = resp.Body.Close(); err != nil {
|
2016-08-31 20:51:35 +00:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "Close")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
return restic.FileInfo{}, ErrIsNotExist{h}
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2017-05-28 10:31:19 +00:00
|
|
|
return restic.FileInfo{}, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ContentLength < 0 {
|
2016-08-31 20:51:35 +00:00
|
|
|
return restic.FileInfo{}, errors.New("negative content length")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
bi := restic.FileInfo{
|
2016-02-20 21:05:48 +00:00
|
|
|
Size: resp.ContentLength,
|
2018-01-20 18:34:38 +00:00
|
|
|
Name: h.Name,
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) {
|
2017-06-03 15:39:57 +00:00
|
|
|
_, err := b.Stat(ctx, h)
|
2016-02-20 21:05:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
2016-02-20 21:05:48 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return backoff.Permanent(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 19:47:57 +00:00
|
|
|
req, err := http.NewRequest("DELETE", b.Filename(h), nil)
|
2016-02-20 21:05:48 +00:00
|
|
|
if err != nil {
|
2016-08-29 19:54:50 +00:00
|
|
|
return errors.Wrap(err, "http.NewRequest")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
2018-03-13 21:22:35 +00:00
|
|
|
req.Header.Set("Accept", ContentTypeV2)
|
2018-01-23 22:12:52 +00:00
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.GetToken()
|
2017-06-03 15:39:57 +00:00
|
|
|
resp, err := ctxhttp.Do(ctx, b.client, req)
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.ReleaseToken()
|
2016-02-20 21:05:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-29 19:54:50 +00:00
|
|
|
return errors.Wrap(err, "client.Do")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
return ErrIsNotExist{h}
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2017-03-16 20:50:26 +00:00
|
|
|
return errors.Errorf("blob not removed, server response: %v (%v)", resp.Status, resp.StatusCode)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
_, err = io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Copy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(resp.Body.Close(), "Close")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
// List runs fn for each file in the backend which has the type t. When an
|
|
|
|
// error occurs (or fn returns an error), List stops and returns it.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
2017-04-11 19:47:57 +00:00
|
|
|
url := b.Dirname(restic.Handle{Type: t})
|
2016-02-21 15:35:25 +00:00
|
|
|
if !strings.HasSuffix(url, "/") {
|
|
|
|
url += "/"
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:12:52 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "NewRequest")
|
|
|
|
}
|
2018-03-13 21:22:35 +00:00
|
|
|
req.Header.Set("Accept", ContentTypeV2)
|
2018-01-23 22:12:52 +00:00
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.GetToken()
|
2018-01-23 22:12:52 +00:00
|
|
|
resp, err := ctxhttp.Do(ctx, b.client, req)
|
2017-06-05 22:25:22 +00:00
|
|
|
b.sem.ReleaseToken()
|
2016-02-20 21:05:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-03-08 10:22:43 +00:00
|
|
|
return errors.Wrap(err, "List")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errors.Errorf("List failed, server response: %v (%v)", resp.Status, resp.StatusCode)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 21:22:35 +00:00
|
|
|
if resp.Header.Get("Content-Type") == ContentTypeV2 {
|
2018-01-23 22:12:52 +00:00
|
|
|
return b.listv2(ctx, t, resp, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.listv1(ctx, t, resp, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// listv1 uses the REST protocol v1, where a list HTTP request (e.g. `GET
|
|
|
|
// /data/`) only returns the names of the files, so we need to issue an HTTP
|
|
|
|
// HEAD request for each file.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) listv1(ctx context.Context, t restic.FileType, resp *http.Response, fn func(restic.FileInfo) error) error {
|
2018-01-23 22:12:52 +00:00
|
|
|
debug.Log("parsing API v1 response")
|
2016-02-20 21:05:48 +00:00
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
var list []string
|
2018-01-23 22:12:52 +00:00
|
|
|
if err := dec.Decode(&list); err != nil {
|
2018-01-20 18:34:38 +00:00
|
|
|
return errors.Wrap(err, "Decode")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
for _, m := range list {
|
|
|
|
fi, err := b.Stat(ctx, restic.Handle{Name: m, Type: t})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
fi.Name = m
|
|
|
|
err = fn(fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Err()
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 22:12:52 +00:00
|
|
|
// listv2 uses the REST protocol v2, where a list HTTP request (e.g. `GET
|
|
|
|
// /data/`) returns the names and sizes of all files.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) listv2(ctx context.Context, t restic.FileType, resp *http.Response, fn func(restic.FileInfo) error) error {
|
2018-01-23 22:12:52 +00:00
|
|
|
debug.Log("parsing API v2 response")
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
var list []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
}
|
|
|
|
if err := dec.Decode(&list); err != nil {
|
|
|
|
return errors.Wrap(err, "Decode")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range list {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
fi := restic.FileInfo{
|
|
|
|
Name: item.Name,
|
|
|
|
Size: item.Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fn(fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
// Close closes all open files.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Close() error {
|
2016-02-20 21:05:48 +00:00
|
|
|
// this does not need to do anything, all open files are closed within the
|
|
|
|
// same function.
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-14 11:38:17 +00:00
|
|
|
|
|
|
|
// Remove keys for a specified backend type.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) removeKeys(ctx context.Context, t restic.FileType) error {
|
2018-01-20 18:34:38 +00:00
|
|
|
return b.List(ctx, t, func(fi restic.FileInfo) error {
|
|
|
|
return b.Remove(ctx, restic.Handle{Type: t, Name: fi.Name})
|
|
|
|
})
|
2017-10-14 11:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes all data in the backend.
|
2018-03-13 21:30:41 +00:00
|
|
|
func (b *Backend) Delete(ctx context.Context) error {
|
2017-10-14 11:38:17 +00:00
|
|
|
alltypes := []restic.FileType{
|
2020-08-16 09:16:38 +00:00
|
|
|
restic.PackFile,
|
2017-10-14 11:38:17 +00:00
|
|
|
restic.KeyFile,
|
|
|
|
restic.LockFile,
|
|
|
|
restic.SnapshotFile,
|
|
|
|
restic.IndexFile}
|
|
|
|
|
|
|
|
for _, t := range alltypes {
|
|
|
|
err := b.removeKeys(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:56:38 +00:00
|
|
|
err := b.Remove(ctx, restic.Handle{Type: restic.ConfigFile})
|
|
|
|
if err != nil && b.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2017-10-14 11:38:17 +00:00
|
|
|
}
|