restic/internal/backend/location/location.go

104 lines
2.4 KiB
Go
Raw Normal View History

2015-12-28 14:57:20 +00:00
// Package location implements parsing the restic repository location from a string.
package location
2015-12-28 14:51:24 +00:00
import (
"strings"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
2015-12-28 14:51:24 +00:00
)
2015-12-28 14:57:20 +00:00
// Location specifies the location of a repository, including the method of
// access and (possibly) credentials needed for access.
type Location struct {
2015-12-28 14:51:24 +00:00
Scheme string
Config interface{}
}
// NoPassword returns the repository location unchanged (there's no sensitive information there)
func NoPassword(s string) string {
2020-03-20 22:52:27 +00:00
return s
2015-12-28 14:51:24 +00:00
}
func isPath(s string) bool {
if strings.HasPrefix(s, "../") || strings.HasPrefix(s, `..\`) {
return true
}
if strings.HasPrefix(s, "/") || strings.HasPrefix(s, `\`) {
return true
}
if len(s) < 3 {
return false
}
// check for drive paths
drive := s[0]
if !(drive >= 'a' && drive <= 'z') && !(drive >= 'A' && drive <= 'Z') {
return false
}
if s[1] != ':' {
return false
}
if s[2] != '\\' && s[2] != '/' {
return false
}
return true
}
2015-12-28 15:42:44 +00:00
// Parse extracts repository location information from the string s. If s
// starts with a backend name followed by a colon, that backend's Parse()
// function is called. Otherwise, the local backend is used which interprets s
// as the name of a directory.
func Parse(registry *Registry, s string) (u Location, err error) {
2015-12-28 14:51:24 +00:00
scheme := extractScheme(s)
u.Scheme = scheme
factory := registry.Lookup(scheme)
if factory != nil {
u.Config, err = factory.ParseConfig(s)
2015-12-28 14:51:24 +00:00
if err != nil {
2015-12-28 14:57:20 +00:00
return Location{}, err
2015-12-28 14:51:24 +00:00
}
return u, nil
}
// if s is not a path or contains ":", it's ambiguous
if !isPath(s) && strings.ContainsRune(s, ':') {
2022-05-07 20:23:59 +00:00
return Location{}, errors.New("invalid backend\nIf the repository is in a local directory, you need to add a `local:` prefix")
}
2015-12-28 14:51:24 +00:00
u.Scheme = "local"
factory = registry.Lookup(u.Scheme)
if factory == nil {
return Location{}, errors.New("local backend not available")
}
u.Config, err = factory.ParseConfig("local:" + s)
2015-12-28 14:51:24 +00:00
if err != nil {
2015-12-28 14:57:20 +00:00
return Location{}, err
2015-12-28 14:51:24 +00:00
}
return u, nil
}
2020-03-20 22:52:27 +00:00
// StripPassword returns a displayable version of a repository location (with any sensitive information removed)
func StripPassword(registry *Registry, s string) string {
2020-03-20 22:52:27 +00:00
scheme := extractScheme(s)
factory := registry.Lookup(scheme)
if factory != nil {
return factory.StripPassword(s)
2020-03-20 22:52:27 +00:00
}
return s
}
2015-12-28 14:51:24 +00:00
func extractScheme(s string) string {
scheme, _, _ := strings.Cut(s, ":")
return scheme
2015-12-28 14:51:24 +00:00
}