From f5c4fcc250939dd9e5524a6a40297864eaadb744 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 4 Dec 2023 10:21:29 +0200 Subject: [PATCH] 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 --- lib/backup/azremote/azblob.go | 8 ++++---- lib/backup/common/fs.go | 10 +++++++++- lib/backup/gcsremote/gcs.go | 10 ++++++---- lib/backup/s3remote/s3.go | 11 ++++++----- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/backup/azremote/azblob.go b/lib/backup/azremote/azblob.go index 2e9b5bf07..b2f559829 100644 --- a/lib/backup/azremote/azblob.go +++ b/lib/backup/azremote/azblob.go @@ -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() diff --git a/lib/backup/common/fs.go b/lib/backup/common/fs.go index 42c051556..74fbf9080 100644 --- a/lib/backup/common/fs.go +++ b/lib/backup/common/fs.go @@ -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) } diff --git a/lib/backup/gcsremote/gcs.go b/lib/backup/gcsremote/gcs.go index ce7ece62a..bca2c4fac 100644 --- a/lib/backup/gcsremote/gcs.go +++ b/lib/backup/gcsremote/gcs.go @@ -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 { diff --git a/lib/backup/s3remote/s3.go b/lib/backup/s3remote/s3.go index 45bc8cfc1..fc37e86ed 100644 --- a/lib/backup/s3remote/s3.go +++ b/lib/backup/s3remote/s3.go @@ -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),