Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve restic: fix repositories named data - fixes #6086

kaii99823661bb817
diff --git a/cmd/serve/restic/restic.go b/cmd/serve/restic/restic.go
index e72801b3b..61f6a42cc 100644
--- a/cmd/serve/restic/restic.go
+++ b/cmd/serve/restic/restic.go
@@ -411,7 +411,8 @@ func (s *server) Bind(router chi.Router) {
}
}
-var matchData = regexp.MustCompile("(?:^|/)data/([^/]{2,})$")
+// Restic pack names are lowercase hexadecimal storage IDs.
+var matchData = regexp.MustCompile("(?:^|/)data/([0-9a-f]{2,})$")
// newObject returns an object with the remote given either from the
// cache or directly
diff --git a/cmd/serve/restic/restic_test.go b/cmd/serve/restic/restic_test.go
index 851e542be..0fa27bc79 100644
--- a/cmd/serve/restic/restic_test.go
+++ b/cmd/serve/restic/restic_test.go
@@ -10,6 +10,8 @@ import (
"net/http/httptest"
"os"
"os/exec"
+ "path/filepath"
+ "strings"
"testing"
_ "github.com/rclone/rclone/backend/all"
@@ -106,6 +108,17 @@ func TestMakeRemote(t *testing.T) {
{"/data/12", "data/12/12"},
{"/data/123", "data/12/123"},
{"/data/123/", "data/12/123"},
+ {"/data/abcdef", "data/ab/abcdef"},
+ {"/data/config", "data/config"},
+ {"/data/data/", "data/data"},
+ {"/data/index/", "data/index"},
+ {"/data/keys/", "data/keys"},
+ {"/data/locks/", "data/locks"},
+ {"/data/snapshots/", "data/snapshots"},
+ {"/data/data/abcdef", "data/data/ab/abcdef"},
+ {"/repo/data/config", "repo/data/config"},
+ {"/repo/data/keys/", "repo/data/keys"},
+ {"/repo/data/data/abcdef", "repo/data/data/ab/abcdef"},
{"/keys", "keys"},
{"/keys/1", "keys/1"},
{"/keys/12", "keys/12"},
@@ -123,6 +136,46 @@ func TestMakeRemote(t *testing.T) {
}
}
+func TestResticDataRepository(t *testing.T) {
+ for _, repo := range []string{"data", "repo/data"} {
+ t.Run(repo, func(t *testing.T) {
+ tempdir := t.TempDir()
+ f := cmd.NewFsSrc([]string{tempdir})
+ opt := newOpt()
+ s, err := newServer(context.Background(), f, &opt)
+ require.NoError(t, err)
+ handler := s.server.Router().ServeHTTP
+ checkRequest(t, handler, newRequest(t, "POST", "/"+repo+"/?create=true", nil),
+ []wantFunc{wantCode(http.StatusOK)})
+
+ const id = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
+ for _, object := range []struct {
+ url, remote string
+ }{
+ {"config", "config"},
+ {"keys/" + id, "keys/" + id},
+ {"data/" + id, "data/ab/" + id},
+ } {
+ url := "/" + repo + "/" + object.url
+ checkRequest(t, handler, newRequest(t, "POST", url, strings.NewReader("test content")),
+ []wantFunc{wantCode(http.StatusOK)})
+ checkRequest(t, handler, newRequest(t, "GET", url, nil),
+ []wantFunc{wantCode(http.StatusOK), wantBody("test content")})
+ content, err := os.ReadFile(filepath.Join(tempdir, repo, object.remote))
+ require.NoError(t, err)
+ assert.Equal(t, "test content", string(content))
+ }
+
+ for _, dir := range []string{"keys", "data"} {
+ checkRequest(t, handler, newRequest(t, "GET", "/"+repo+"/"+dir+"/", nil),
+ []wantFunc{wantCode(http.StatusOK), func(t testing.TB, res *httptest.ResponseRecorder) {
+ assert.JSONEq(t, `[{"name":"`+id+`","size":12}]`, res.Body.String())
+ }})
+ }
+ })
+ }
+}
+
type listErrorFs struct {
fs.Fs
}