2018-02-17 23:25:59 +00:00
|
|
|
// Package restic serves a remote suitable for use with restic
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2020-11-09 16:13:08 +00:00
|
|
|
"context"
|
2018-02-17 23:25:59 +00:00
|
|
|
"encoding/json"
|
2018-03-06 22:22:29 +00:00
|
|
|
"errors"
|
2021-05-02 07:56:24 +00:00
|
|
|
"fmt"
|
2018-02-17 23:25:59 +00:00
|
|
|
"net/http"
|
2018-03-06 22:22:29 +00:00
|
|
|
"os"
|
2018-02-17 23:25:59 +00:00
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2020-06-10 16:33:55 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2021-05-02 07:56:24 +00:00
|
|
|
libhttp "github.com/rclone/rclone/lib/http"
|
2021-04-20 04:50:08 +00:00
|
|
|
"github.com/rclone/rclone/lib/http/serve"
|
2023-09-04 15:32:04 +00:00
|
|
|
"github.com/rclone/rclone/lib/systemd"
|
2020-07-31 18:57:48 +00:00
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
2018-02-17 23:25:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-03-06 22:22:29 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// Options required for http server
|
|
|
|
type Options struct {
|
|
|
|
Auth libhttp.AuthConfig
|
|
|
|
HTTP libhttp.Config
|
|
|
|
Stdio bool
|
|
|
|
AppendOnly bool
|
|
|
|
PrivateRepos bool
|
|
|
|
CacheObjects bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values used for Options
|
|
|
|
var DefaultOpt = Options{
|
|
|
|
Auth: libhttp.DefaultAuthCfg(),
|
|
|
|
HTTP: libhttp.DefaultCfg(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opt is options set by command line flags
|
|
|
|
var Opt = DefaultOpt
|
2018-02-17 23:25:59 +00:00
|
|
|
|
2023-01-11 05:05:44 +00:00
|
|
|
// flagPrefix is the prefix used to uniquely identify command line flags.
|
|
|
|
// It is intentionally empty for this package.
|
|
|
|
const flagPrefix = ""
|
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
func init() {
|
2020-06-10 16:33:55 +00:00
|
|
|
flagSet := Command.Flags()
|
2023-01-11 05:05:44 +00:00
|
|
|
libhttp.AddAuthFlagsPrefix(flagSet, flagPrefix, &Opt.Auth)
|
|
|
|
libhttp.AddHTTPFlagsPrefix(flagSet, flagPrefix, &Opt.HTTP)
|
2023-07-10 17:34:10 +00:00
|
|
|
flags.BoolVarP(flagSet, &Opt.Stdio, "stdio", "", false, "Run an HTTP2 server on stdin/stdout", "")
|
|
|
|
flags.BoolVarP(flagSet, &Opt.AppendOnly, "append-only", "", false, "Disallow deletion of repository data", "")
|
|
|
|
flags.BoolVarP(flagSet, &Opt.PrivateRepos, "private-repos", "", false, "Users can only access their private repo", "")
|
|
|
|
flags.BoolVarP(flagSet, &Opt.CacheObjects, "cache-objects", "", true, "Cache listed objects", "")
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "restic remote:path",
|
|
|
|
Short: `Serve the remote for restic's REST API.`,
|
2022-12-06 12:14:25 +00:00
|
|
|
Long: `Run a basic web server to serve a remote over restic's REST backend
|
2022-06-19 17:18:14 +00:00
|
|
|
API over HTTP. This allows restic to use rclone as a data storage
|
2018-02-17 23:25:59 +00:00
|
|
|
mechanism for cloud providers that restic does not support directly.
|
|
|
|
|
2021-11-04 11:50:43 +00:00
|
|
|
[Restic](https://restic.net/) is a command-line program for doing
|
2018-02-17 23:25:59 +00:00
|
|
|
backups.
|
|
|
|
|
|
|
|
The server will log errors. Use -v to see access logs.
|
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
` + "`--bwlimit`" + ` will be respected for file transfers.
|
|
|
|
Use ` + "`--stats`" + ` to control the stats printing.
|
2018-02-17 23:25:59 +00:00
|
|
|
|
|
|
|
### Setting up rclone for use by restic ###
|
|
|
|
|
|
|
|
First [set up a remote for your chosen cloud provider](/docs/#configure).
|
|
|
|
|
|
|
|
Once you have set up the remote, check it is working with, for example
|
|
|
|
"rclone lsd remote:". You may have called the remote something other
|
|
|
|
than "remote:" - just substitute whatever you called it in the
|
|
|
|
following instructions.
|
|
|
|
|
|
|
|
Now start the rclone restic server
|
|
|
|
|
|
|
|
rclone serve restic -v remote:backup
|
|
|
|
|
|
|
|
Where you can replace "backup" in the above by whatever path in the
|
|
|
|
remote you wish to use.
|
|
|
|
|
|
|
|
By default this will serve on "localhost:8080" you can change this
|
2022-06-19 17:55:37 +00:00
|
|
|
with use of the ` + "`--addr`" + ` flag.
|
2018-02-17 23:25:59 +00:00
|
|
|
|
|
|
|
You might wish to start this server on boot.
|
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
Adding ` + "`--cache-objects=false`" + ` will cause rclone to stop caching objects
|
2020-11-09 16:13:08 +00:00
|
|
|
returned from the List call. Caching is normally desirable as it speeds
|
|
|
|
up downloading objects, saves transactions and uses very little memory.
|
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
### Setting up restic to use rclone ###
|
|
|
|
|
|
|
|
Now you can [follow the restic
|
|
|
|
instructions](http://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html#rest-server)
|
|
|
|
on setting up restic.
|
|
|
|
|
|
|
|
Note that you will need restic 0.8.2 or later to interoperate with
|
|
|
|
rclone.
|
|
|
|
|
|
|
|
For the example above you will want to use "http://localhost:8080/" as
|
|
|
|
the URL for the REST server.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
$ export RESTIC_REPOSITORY=rest:http://localhost:8080/
|
|
|
|
$ export RESTIC_PASSWORD=yourpassword
|
|
|
|
$ restic init
|
|
|
|
created restic backend 8b1a4b56ae at rest:http://localhost:8080/
|
2019-06-07 17:47:46 +00:00
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
Please note that knowledge of your password is required to access
|
|
|
|
the repository. Losing your password means that your data is
|
|
|
|
irrecoverably lost.
|
|
|
|
$ restic backup /path/to/files/to/backup
|
|
|
|
scan [/path/to/files/to/backup]
|
|
|
|
scanned 189 directories, 312 files in 0:00
|
2019-06-07 17:47:46 +00:00
|
|
|
[0:00] 100.00% 38.128 MiB / 38.128 MiB 501 / 501 items 0 errors ETA 0:00
|
2018-02-17 23:25:59 +00:00
|
|
|
duration: 0:00
|
|
|
|
snapshot 45c8fdd8 saved
|
|
|
|
|
|
|
|
#### Multiple repositories ####
|
|
|
|
|
|
|
|
Note that you can use the endpoint to host multiple repositories. Do
|
|
|
|
this by adding a directory name or path after the URL. Note that
|
|
|
|
these **must** end with /. Eg
|
|
|
|
|
|
|
|
$ export RESTIC_REPOSITORY=rest:http://localhost:8080/user1repo/
|
|
|
|
# backup user1 stuff
|
|
|
|
$ export RESTIC_REPOSITORY=rest:http://localhost:8080/user2repo/
|
|
|
|
# backup user2 stuff
|
|
|
|
|
2019-06-07 17:47:46 +00:00
|
|
|
#### Private repositories ####
|
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
The` + "`--private-repos`" + ` flag can be used to limit users to repositories starting
|
2020-05-22 11:22:52 +00:00
|
|
|
with a path of ` + "`/<username>/`" + `.
|
2024-04-05 11:27:33 +00:00
|
|
|
|
2023-01-11 05:05:44 +00:00
|
|
|
` + libhttp.Help(flagPrefix) + libhttp.AuthHelp(flagPrefix),
|
2022-11-26 22:40:49 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.40",
|
|
|
|
},
|
2018-02-17 23:25:59 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2021-05-02 07:56:24 +00:00
|
|
|
ctx := context.Background()
|
2018-02-17 23:25:59 +00:00
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f := cmd.NewFsSrc(args)
|
|
|
|
cmd.Run(false, true, command, func() error {
|
2021-05-02 07:56:24 +00:00
|
|
|
s, err := newServer(ctx, f, &Opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.opt.Stdio {
|
2018-03-06 22:22:29 +00:00
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
2022-06-08 20:54:39 +00:00
|
|
|
return errors.New("refusing to run HTTP2 server directly on a terminal, please let restic start rclone")
|
2018-03-06 22:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conn := &StdioConn{
|
|
|
|
stdin: os.Stdin,
|
|
|
|
stdout: os.Stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSrv := &http2.Server{}
|
|
|
|
opts := &http2.ServeConnOpts{
|
2021-05-02 07:56:24 +00:00
|
|
|
Handler: s.Server.Router(),
|
2018-03-06 22:22:29 +00:00
|
|
|
}
|
|
|
|
httpSrv.ServeConn(conn, opts)
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-22 16:02:17 +00:00
|
|
|
fs.Logf(s.f, "Serving restic REST API on %s", s.URLs())
|
2023-04-10 14:22:54 +00:00
|
|
|
|
2023-09-04 15:32:04 +00:00
|
|
|
defer systemd.Notify()()
|
2022-12-21 12:32:21 +00:00
|
|
|
s.Wait()
|
2023-04-10 14:22:54 +00:00
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:09:56 +00:00
|
|
|
const (
|
|
|
|
resticAPIV2 = "application/vnd.x.restic.rest.v2"
|
|
|
|
)
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
type contextRemoteType struct{}
|
2018-02-17 23:25:59 +00:00
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// ContextRemoteKey is a simple context key for storing the username of the request
|
|
|
|
var ContextRemoteKey = &contextRemoteType{}
|
2018-02-17 23:25:59 +00:00
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// WithRemote makes a remote from a URL path. This implements the backend layout
|
|
|
|
// required by restic.
|
|
|
|
func WithRemote(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var urlpath string
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
if rctx != nil && rctx.RoutePath != "" {
|
|
|
|
urlpath = rctx.RoutePath
|
|
|
|
} else {
|
|
|
|
urlpath = r.URL.Path
|
|
|
|
}
|
|
|
|
urlpath = strings.Trim(urlpath, "/")
|
|
|
|
parts := matchData.FindStringSubmatch(urlpath)
|
|
|
|
// if no data directory, layout is flat
|
|
|
|
if parts != nil {
|
|
|
|
// otherwise map
|
|
|
|
// data/2159dd48 to
|
|
|
|
// data/21/2159dd48
|
|
|
|
fileName := parts[1]
|
|
|
|
prefix := urlpath[:len(urlpath)-len(fileName)]
|
|
|
|
urlpath = prefix + fileName[:2] + "/" + fileName
|
|
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), ContextRemoteKey, urlpath)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// Middleware to ensure authenticated user is accessing their own private folder
|
|
|
|
func checkPrivate(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := chi.URLParam(r, "userID")
|
|
|
|
userID, ok := libhttp.CtxGetUser(r.Context())
|
|
|
|
if ok && user != "" && user == userID {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
}
|
|
|
|
})
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// server contains everything to run the server
|
|
|
|
type server struct {
|
|
|
|
*libhttp.Server
|
|
|
|
f fs.Fs
|
|
|
|
cache *cache
|
|
|
|
opt Options
|
|
|
|
}
|
2018-02-17 23:25:59 +00:00
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
func newServer(ctx context.Context, f fs.Fs, opt *Options) (s *server, err error) {
|
|
|
|
s = &server{
|
|
|
|
f: f,
|
|
|
|
cache: newCache(opt.CacheObjects),
|
|
|
|
opt: *opt,
|
2019-08-04 09:56:38 +00:00
|
|
|
}
|
2022-12-22 16:02:17 +00:00
|
|
|
// Don't bind any HTTP listeners if running with --stdio
|
|
|
|
if opt.Stdio {
|
|
|
|
opt.HTTP.ListenAddr = nil
|
|
|
|
}
|
2021-05-02 07:56:24 +00:00
|
|
|
s.Server, err = libhttp.NewServer(ctx,
|
|
|
|
libhttp.WithConfig(opt.HTTP),
|
|
|
|
libhttp.WithAuth(opt.Auth),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init server: %w", err)
|
2019-06-07 17:47:46 +00:00
|
|
|
}
|
2021-05-02 07:56:24 +00:00
|
|
|
router := s.Router()
|
|
|
|
s.Bind(router)
|
|
|
|
s.Server.Serve()
|
|
|
|
return s, nil
|
|
|
|
}
|
2019-06-07 17:47:46 +00:00
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
// bind helper for main Bind method
|
|
|
|
func (s *server) bind(router chi.Router) {
|
|
|
|
router.MethodFunc("GET", "/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
urlpath := chi.URLParam(r, "*")
|
|
|
|
if urlpath == "" || strings.HasSuffix(urlpath, "/") {
|
|
|
|
s.listObjects(w, r)
|
|
|
|
} else {
|
|
|
|
s.serveObject(w, r)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
2021-05-02 07:56:24 +00:00
|
|
|
})
|
|
|
|
router.MethodFunc("POST", "/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
urlpath := chi.URLParam(r, "*")
|
|
|
|
if urlpath == "" || strings.HasSuffix(urlpath, "/") {
|
|
|
|
s.createRepo(w, r)
|
|
|
|
} else {
|
|
|
|
s.postObject(w, r)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
2021-05-02 07:56:24 +00:00
|
|
|
})
|
|
|
|
router.MethodFunc("HEAD", "/*", s.serveObject)
|
|
|
|
router.MethodFunc("DELETE", "/*", s.deleteObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind restic server routes to passed router
|
|
|
|
func (s *server) Bind(router chi.Router) {
|
|
|
|
// FIXME
|
|
|
|
// if m := authX.Auth(authX.Opt); m != nil {
|
|
|
|
// router.Use(m)
|
|
|
|
// }
|
|
|
|
router.Use(
|
|
|
|
middleware.SetHeader("Accept-Ranges", "bytes"),
|
|
|
|
middleware.SetHeader("Server", "rclone/"+fs.Version),
|
|
|
|
WithRemote,
|
|
|
|
)
|
|
|
|
|
|
|
|
if s.opt.PrivateRepos {
|
|
|
|
router.Route("/{userID}", func(r chi.Router) {
|
|
|
|
r.Use(checkPrivate)
|
|
|
|
s.bind(r)
|
|
|
|
})
|
|
|
|
router.NotFound(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
s.bind(router)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 07:56:24 +00:00
|
|
|
var matchData = regexp.MustCompile("(?:^|/)data/([^/]{2,})$")
|
|
|
|
|
2020-11-09 16:13:08 +00:00
|
|
|
// newObject returns an object with the remote given either from the
|
|
|
|
// cache or directly
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) newObject(ctx context.Context, remote string) (fs.Object, error) {
|
2020-11-09 16:13:08 +00:00
|
|
|
o := s.cache.find(remote)
|
|
|
|
if o != nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
o, err := s.f.NewObject(ctx, remote)
|
|
|
|
if err != nil {
|
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
s.cache.add(remote, o)
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
// get the remote
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) serveObject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := r.Context().Value(ContextRemoteKey).(string)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-11-09 16:13:08 +00:00
|
|
|
o, err := s.newObject(r.Context(), remote)
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
2018-10-27 16:44:07 +00:00
|
|
|
fs.Debugf(remote, "%s request error: %v", r.Method, err)
|
2024-01-29 17:54:23 +00:00
|
|
|
if errors.Is(err, fs.ErrorObjectNotFound) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2018-02-17 23:25:59 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-27 16:44:07 +00:00
|
|
|
serve.Object(w, r, o)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// postObject posts an object to the repository
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) postObject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := r.Context().Value(ContextRemoteKey).(string)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.opt.AppendOnly {
|
2018-04-04 13:49:13 +00:00
|
|
|
// make sure the file does not exist yet
|
2020-11-09 16:13:08 +00:00
|
|
|
_, err := s.newObject(r.Context(), remote)
|
2018-04-04 13:49:13 +00:00
|
|
|
if err == nil {
|
|
|
|
fs.Errorf(remote, "Post request: file already exists, refusing to overwrite in append-only mode")
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
2018-08-30 15:45:41 +00:00
|
|
|
|
2018-04-04 13:49:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:42:18 +00:00
|
|
|
o, err := operations.RcatSize(r.Context(), s.f, remote, r.Body, r.ContentLength, time.Now(), nil)
|
2018-08-30 15:45:41 +00:00
|
|
|
if err != nil {
|
2019-11-18 14:13:02 +00:00
|
|
|
err = accounting.Stats(r.Context()).Error(err)
|
2018-08-30 15:45:41 +00:00
|
|
|
fs.Errorf(remote, "Post request rcat error: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
2020-11-09 16:13:08 +00:00
|
|
|
|
|
|
|
// if successfully uploaded add to cache
|
|
|
|
s.cache.add(remote, o)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete the remote
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) deleteObject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := r.Context().Value(ContextRemoteKey).(string)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.opt.AppendOnly {
|
2018-03-29 18:24:41 +00:00
|
|
|
parts := strings.Split(r.URL.Path, "/")
|
|
|
|
|
|
|
|
// if path doesn't end in "/locks/:name", disallow the operation
|
|
|
|
if len(parts) < 2 || parts[len(parts)-2] != "locks" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:13:08 +00:00
|
|
|
o, err := s.newObject(r.Context(), remote)
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(remote, "Delete request error: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
if err := o.Remove(r.Context()); err != nil {
|
2018-02-17 23:25:59 +00:00
|
|
|
fs.Errorf(remote, "Delete request remove error: %v", err)
|
2024-01-29 17:54:23 +00:00
|
|
|
if errors.Is(err, fs.ErrorObjectNotFound) {
|
2018-02-17 23:25:59 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-11-09 16:13:08 +00:00
|
|
|
|
|
|
|
// remove object from cache
|
|
|
|
s.cache.remove(remote)
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listItem is an element returned for the restic v2 list response
|
|
|
|
type listItem struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// return type for list
|
|
|
|
type listItems []listItem
|
|
|
|
|
2020-11-09 16:13:08 +00:00
|
|
|
// add an fs.Object to the listItems
|
|
|
|
func (ls *listItems) add(o fs.Object) {
|
|
|
|
*ls = append(*ls, listItem{
|
|
|
|
Name: path.Base(o.Remote()),
|
|
|
|
Size: o.Size(),
|
|
|
|
})
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listObjects lists all Objects of a given type in an arbitrary order.
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) listObjects(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := r.Context().Value(ContextRemoteKey).(string)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-03-08 10:09:56 +00:00
|
|
|
if r.Header.Get("Accept") != resticAPIV2 {
|
2021-05-02 07:56:24 +00:00
|
|
|
fs.Errorf(remote, "Restic v2 API required for List Objects")
|
|
|
|
http.Error(w, "Restic v2 API required for List Objects", http.StatusBadRequest)
|
2018-03-08 10:09:56 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-02 07:56:24 +00:00
|
|
|
fs.Debugf(remote, "list request")
|
2018-03-08 10:09:56 +00:00
|
|
|
|
2018-03-07 20:59:53 +00:00
|
|
|
// make sure an empty list is returned, and not a 'nil' value
|
|
|
|
ls := listItems{}
|
|
|
|
|
2020-11-09 16:13:08 +00:00
|
|
|
// Remove all existing values from the cache
|
|
|
|
s.cache.removePrefix(remote)
|
|
|
|
|
2018-03-18 14:26:10 +00:00
|
|
|
// if remote supports ListR use that directly, otherwise use recursive Walk
|
2019-06-17 08:34:30 +00:00
|
|
|
err := walk.ListR(r.Context(), s.f, remote, true, -1, walk.ListObjects, func(entries fs.DirEntries) error {
|
2019-01-21 16:58:27 +00:00
|
|
|
for _, entry := range entries {
|
2020-11-09 16:13:08 +00:00
|
|
|
if o, ok := entry.(fs.Object); ok {
|
|
|
|
ls.add(o)
|
|
|
|
s.cache.add(o.Remote(), o)
|
|
|
|
}
|
2019-01-21 16:58:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
if !errors.Is(err, fs.ErrorDirNotFound) {
|
2018-03-06 22:22:43 +00:00
|
|
|
fs.Errorf(remote, "list failed: %#v %T", err, err)
|
2024-01-29 17:54:23 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2018-03-06 22:22:43 +00:00
|
|
|
return
|
|
|
|
}
|
2018-02-17 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:26:58 +00:00
|
|
|
w.Header().Set("Content-Type", "application/vnd.x.restic.rest.v2")
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
err = enc.Encode(ls)
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
2018-03-18 16:26:58 +00:00
|
|
|
fs.Errorf(remote, "failed to write list: %v", err)
|
2018-02-17 23:25:59 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// createRepo creates repository directories.
|
|
|
|
//
|
|
|
|
// We don't bother creating the data dirs as rclone will create them on the fly
|
2021-05-02 07:56:24 +00:00
|
|
|
func (s *server) createRepo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := r.Context().Value(ContextRemoteKey).(string)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-02-17 23:25:59 +00:00
|
|
|
fs.Infof(remote, "Creating repository")
|
|
|
|
|
|
|
|
if r.URL.Query().Get("create") != "true" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
err := s.f.Mkdir(r.Context(), remote)
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(remote, "Create repo failed to Mkdir: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range []string{"data", "index", "keys", "locks", "snapshots"} {
|
|
|
|
dirRemote := path.Join(remote, name)
|
2019-06-17 08:34:30 +00:00
|
|
|
err := s.f.Mkdir(r.Context(), dirRemote)
|
2018-02-17 23:25:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(dirRemote, "Create repo failed to Mkdir: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|