lib/backup: consistently use path.Join() when constructing paths for s3, gs and azblob

E.g. replace `fs.Dir + filePath` with `path.Join(fs.Dir, filePath)`

The fs.Dir is guaranteed to end with slash - see Init() functions.
The filePath may start with slash. If it starts with slash, then `fs.Dir + filePath` constructs
an incorrect path with double slashes.
path.Join() properly substitutes duplicate slashes with a single slash in this case.

While at it, also substitute incorrect usage of filepath.Join() with path.Join()
for constructing paths to object storage systems, which expect forward slashes in paths.
filepath.Join() substittues forward slashes with backslashes on Windows, so this may break
creating or managing backups from Windows.

This is a follow-up for 0399367be602b577baf6a872ca81bf0f99ba401b
Updates https://github.com/VictoriaMetrics/VictoriaMetrics-enterprise/pull/719
This commit is contained in:
Aliaksandr Valialkin 2023-12-04 10:21:29 +02:00
parent 2992682f6c
commit 2f4dc2aff1
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
4 changed files with 25 additions and 14 deletions

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"path"
"strings"
"time"
@ -272,7 +273,7 @@ func (fs *FS) DeleteFile(filePath string) error {
return nil
}
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
return fs.delete(path)
}
@ -333,7 +334,7 @@ func (fs *FS) deleteObject(path string) error {
//
// The file is overwritten if it exists.
func (fs *FS) CreateFile(filePath string, data []byte) error {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
bc := fs.clientForPath(path)
ctx := context.Background()
@ -350,8 +351,7 @@ func (fs *FS) CreateFile(filePath string, data []byte) error {
// HasFile returns true if filePath exists at fs.
func (fs *FS) HasFile(filePath string) (bool, error) {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
bc := fs.clientForPath(path)
ctx := context.Background()

View file

@ -45,15 +45,23 @@ type RemoteFS interface {
// UploadPart must upload part p from r to RemoteFS.
UploadPart(p Part, r io.Reader) error
// DeleteFile deletes filePath at RemoteFS
// DeleteFile deletes filePath at RemoteFS.
//
// filePath must use / as directory delimiters.
DeleteFile(filePath string) error
// CreateFile creates filePath at RemoteFS and puts data into it.
//
// filePath must use / as directory delimiters.
CreateFile(filePath string, data []byte) error
// HasFile returns true if filePath exists at RemoteFS.
//
// filePath must use / as directory delimiters.
HasFile(filePath string) (bool, error)
// ReadFile returns file contents at the given filePath.
//
// filePath must use / as directory delimiters.
ReadFile(filePath string) ([]byte, error)
}

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"path"
"strings"
"time"
@ -211,7 +212,7 @@ func (fs *FS) object(p common.Part) *storage.ObjectHandle {
//
// The function does nothing if the filePath doesn't exists.
func (fs *FS) DeleteFile(filePath string) error {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
return fs.delete(path)
}
@ -265,7 +266,7 @@ func (fs *FS) deleteObject(path string) error {
//
// The file is overwritten if it exists.
func (fs *FS) CreateFile(filePath string, data []byte) error {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
o := fs.bkt.Object(path)
ctx := context.Background()
w := o.NewWriter(ctx)
@ -286,7 +287,7 @@ func (fs *FS) CreateFile(filePath string, data []byte) error {
// HasFile returns ture if filePath exists at fs.
func (fs *FS) HasFile(filePath string) (bool, error) {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
o := fs.bkt.Object(path)
ctx := context.Background()
_, err := o.Attrs(ctx)
@ -301,7 +302,8 @@ func (fs *FS) HasFile(filePath string) (bool, error) {
// ReadFile returns the content of filePath at fs.
func (fs *FS) ReadFile(filePath string) ([]byte, error) {
o := fs.bkt.Object(fs.Dir + filePath)
path := path.Join(fs.Dir, filePath)
o := fs.bkt.Object(path)
ctx := context.Background()
r, err := o.NewReader(ctx)
if err != nil {

View file

@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"path"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
@ -53,7 +54,7 @@ type FS struct {
// Path to S3 configs file.
ConfigFilePath string
// GCS bucket to use.
// S3 bucket to use.
Bucket string
// Directory in the bucket to write to.
@ -292,7 +293,7 @@ func (fs *FS) DeleteFile(filePath string) error {
return nil
}
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
return fs.delete(path)
}
@ -344,7 +345,7 @@ func (fs *FS) deleteObjectWithVersions(path string) error {
//
// The file is overwritten if it already exists.
func (fs *FS) CreateFile(filePath string, data []byte) error {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
sr := &statReader{
r: bytes.NewReader(data),
}
@ -367,7 +368,7 @@ func (fs *FS) CreateFile(filePath string, data []byte) error {
// HasFile returns true if filePath exists at fs.
func (fs *FS) HasFile(filePath string) (bool, error) {
path := fs.Dir + filePath
path := path.Join(fs.Dir, filePath)
input := &s3.GetObjectInput{
Bucket: aws.String(fs.Bucket),
Key: aws.String(path),
@ -387,7 +388,7 @@ func (fs *FS) HasFile(filePath string) (bool, error) {
// ReadFile returns the content of filePath at fs.
func (fs *FS) ReadFile(filePath string) ([]byte, error) {
p := fs.Dir + filePath
p := path.Join(fs.Dir, filePath)
input := &s3.GetObjectInput{
Bucket: aws.String(fs.Bucket),
Key: aws.String(p),