s3: Correct layout handle url/path

This commit is contained in:
Alexander Neumann 2017-04-17 20:06:06 +02:00
parent b942f61272
commit 16fd1c2352
2 changed files with 36 additions and 2 deletions

View file

@ -18,13 +18,29 @@ var s3LayoutPaths = map[restic.FileType]string{
restic.KeyFile: "key",
}
// join calls Join with the first empty elements removed.
func (l *S3Layout) join(url string, items ...string) string {
for len(items) > 0 && items[0] == "" {
items = items[1:]
}
path := l.Join(items...)
if path == "" || path[0] != '/' {
if url != "" && url[len(url)-1] != '/' {
url += "/"
}
}
return url + path
}
// Dirname returns the directory path for a given file type and name.
func (l *S3Layout) Dirname(h restic.Handle) string {
if h.Type == restic.ConfigFile {
return l.URL + l.Join(l.Path, "/")
}
return l.URL + l.Join(l.Path, "/", s3LayoutPaths[h.Type]) + "/"
return l.join(l.URL, l.Path, s3LayoutPaths[h.Type]) + "/"
}
// Filename returns a path to a file, including its name.
@ -35,7 +51,7 @@ func (l *S3Layout) Filename(h restic.Handle) string {
name = "config"
}
return l.URL + l.Join(l.Path, "/", s3LayoutPaths[h.Type], name)
return l.join(l.URL, l.Path, s3LayoutPaths[h.Type], name)
}
// Paths returns all directory names

View file

@ -190,6 +190,24 @@ func TestCloudLayoutURLs(t *testing.T) {
"https://hostname.foo:1234/prefix/repo/config",
"https://hostname.foo:1234/prefix/repo/",
},
{
&S3Layout{URL: "", Path: "", Join: path.Join},
restic.Handle{Type: restic.DataFile, Name: "foobar"},
"data/foobar",
"data/",
},
{
&S3Layout{URL: "", Path: "", Join: path.Join},
restic.Handle{Type: restic.LockFile, Name: "foobar"},
"lock/foobar",
"lock/",
},
{
&S3Layout{URL: "", Path: "/", Join: path.Join},
restic.Handle{Type: restic.ConfigFile, Name: "foobar"},
"/config",
"/",
},
}
for _, test := range tests {