From c363d219548064ac54807fd37e383e406811cbf1 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:11:19 -0500 Subject: [PATCH 1/8] First cut of Swift v2 --- src/duplicacy_swiftstorage.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index da5eeed..83ac32a 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -15,6 +15,7 @@ import ( type SwiftStorage struct { StorageBase + ctx *context.Context connection *swift.Connection container string storageDir string @@ -106,6 +107,8 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw arguments["protocol"] = "https" } + ctx := context.Context() + // Please refer to https://godoc.org/github.com/ncw/swift#Connection connection := swift.Connection{ Domain: arguments["domain"], @@ -129,17 +132,18 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw TrustId: arguments["trust_id"], } - err = connection.Authenticate() + err = connection.Authenticate(ctx) if err != nil { return nil, err } - _, _, err = connection.Container(container) + _, _, err = connection.Container(ctx, container) if err != nil { return nil, err } storage = &SwiftStorage{ + context: &ctx, connection: &connection, container: container, storageDir: storageDir, @@ -168,7 +172,7 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str options.Delimiter = '/' } - objects, err := storage.connection.ObjectsAll(storage.container, &options) + objects, err := storage.connection.ObjectsAll(storage.ctx, storage.container, &options) if err != nil { return nil, nil, err } @@ -190,12 +194,12 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str // DeleteFile deletes the file or directory at 'filePath'. func (storage *SwiftStorage) DeleteFile(threadIndex int, filePath string) (err error) { - return storage.connection.ObjectDelete(storage.container, storage.storageDir+filePath) + return storage.connection.ObjectDelete(storage.ctx, storage.container, storage.storageDir+filePath) } // MoveFile renames the file. func (storage *SwiftStorage) MoveFile(threadIndex int, from string, to string) (err error) { - return storage.connection.ObjectMove(storage.container, storage.storageDir+from, + return storage.connection.ObjectMove(storage.ctx, storage.container, storage.storageDir+from, storage.container, storage.storageDir+to) } @@ -207,7 +211,7 @@ func (storage *SwiftStorage) CreateDirectory(threadIndex int, dir string) (err e // GetFileInfo returns the information about the file or directory at 'filePath'. func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) { - object, _, err := storage.connection.Object(storage.container, storage.storageDir+filePath) + object, _, err := storage.connection.Object(storage.ctx, storage.container, storage.storageDir+filePath) if err != nil { if err == swift.ObjectNotFound { @@ -223,7 +227,7 @@ func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exis // DownloadFile reads the file at 'filePath' into the chunk. func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) { - file, _, err := storage.connection.ObjectOpen(storage.container, storage.storageDir+filePath, false, nil) + file, _, err := storage.connection.ObjectOpen(storage.ctx, storage.container, storage.storageDir+filePath, false, nil) if err != nil { return err } @@ -234,7 +238,7 @@ func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chun // UploadFile writes 'content' to the file at 'filePath'. func (storage *SwiftStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) { reader := CreateRateLimitedReader(content, storage.UploadRateLimit/storage.threads) - _, err = storage.connection.ObjectPut(storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil) + _, err = storage.connection.ObjectPut(storage.ctx, storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil) return err } From 934c2515cc594d0fbccd722bbbc1fe9bc7361c47 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:23:42 -0500 Subject: [PATCH 2/8] Import context --- src/duplicacy_swiftstorage.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 83ac32a..8a90905 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -1,10 +1,11 @@ -// Copyright (c) Acrosync LLC. All rights reserved. +n// Copyright (c) Acrosync LLC. All rights reserved. // Free for personal use and commercial trial // Commercial use requires per-user licenses available from https://duplicacy.com package duplicacy import ( + "context" "strconv" "strings" "time" From 041ba944c475005dde7b501f7496ae38aa2f6d53 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:24:31 -0500 Subject: [PATCH 3/8] Typo --- src/duplicacy_swiftstorage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 8a90905..0bfb44e 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -1,4 +1,4 @@ -n// Copyright (c) Acrosync LLC. All rights reserved. +// Copyright (c) Acrosync LLC. All rights reserved. // Free for personal use and commercial trial // Commercial use requires per-user licenses available from https://duplicacy.com From 1661caeb9240aead114ad8cd0eb9242fb5021b12 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:37:11 -0500 Subject: [PATCH 4/8] More fixups --- src/duplicacy_swiftstorage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 0bfb44e..700a4ac 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -16,7 +16,7 @@ import ( type SwiftStorage struct { StorageBase - ctx *context.Context + ctx context.Context connection *swift.Connection container string storageDir string @@ -144,7 +144,7 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw } storage = &SwiftStorage{ - context: &ctx, + ctx: ctx, connection: &connection, container: container, storageDir: storageDir, From 95b1227d934e16a58c7f287702ce8f1c2282e7b6 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:48:01 -0500 Subject: [PATCH 5/8] Use empty context --- src/duplicacy_swiftstorage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 700a4ac..0a92267 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -108,7 +108,7 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw arguments["protocol"] = "https" } - ctx := context.Context() + ctx := context.TODO // Please refer to https://godoc.org/github.com/ncw/swift#Connection connection := swift.Connection{ From 0590daff8595510f834714c0c2390d339e71479d Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:53:34 -0500 Subject: [PATCH 6/8] More dev --- src/duplicacy_swiftstorage.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 0a92267..d82aa1d 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -108,7 +108,8 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw arguments["protocol"] = "https" } - ctx := context.TODO + ctx, cancel := context.WithDeadline(context.Background(), time.Millisecond) + defer cancel() // Please refer to https://godoc.org/github.com/ncw/swift#Connection connection := swift.Connection{ From 590d3b1b5b82cf82987c4b0d17a18fa41d960a43 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Sat, 4 Dec 2021 10:56:47 -0500 Subject: [PATCH 7/8] More development --- src/duplicacy_swiftstorage.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index d82aa1d..8ed7947 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -108,7 +108,8 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw arguments["protocol"] = "https" } - ctx, cancel := context.WithDeadline(context.Background(), time.Millisecond) + // This context ends up expired, which is fine. + ctx, cancel := context.WithDeadline(context.Background(), time.Now()) defer cancel() // Please refer to https://godoc.org/github.com/ncw/swift#Connection From 4743c7ba0d93de02727daf41e866a385fa07bcc3 Mon Sep 17 00:00:00 2001 From: Mark Feit Date: Fri, 17 Dec 2021 10:09:36 -0500 Subject: [PATCH 8/8] DOn't cancel the context and use a sane deadline. --- src/duplicacy_swiftstorage.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/duplicacy_swiftstorage.go b/src/duplicacy_swiftstorage.go index 8ed7947..f4dfcae 100644 --- a/src/duplicacy_swiftstorage.go +++ b/src/duplicacy_swiftstorage.go @@ -108,9 +108,7 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw arguments["protocol"] = "https" } - // This context ends up expired, which is fine. - ctx, cancel := context.WithDeadline(context.Background(), time.Now()) - defer cancel() + ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) // Please refer to https://godoc.org/github.com/ncw/swift#Connection connection := swift.Connection{