From a621a86cb44a18c47cfc15d427dc4f8b17fea825 Mon Sep 17 00:00:00 2001 From: Richard Scothern Date: Fri, 14 Oct 2016 16:25:37 -0700 Subject: [PATCH] Fix aliyun OSS Delete method's notion of subpaths Deleting "/a" was deleting "/a/b" but also "/ab". Signed-off-by: Richard Scothern --- registry/storage/driver/oss/oss.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/registry/storage/driver/oss/oss.go b/registry/storage/driver/oss/oss.go index 7ae703346..39f0ceb49 100644 --- a/registry/storage/driver/oss/oss.go +++ b/registry/storage/driver/oss/oss.go @@ -408,7 +408,8 @@ func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) e // Delete recursively deletes all objects stored at "path" and its subpaths. func (d *driver) Delete(ctx context.Context, path string) error { - listResponse, err := d.Bucket.List(d.ossPath(path), "", "", listMax) + ossPath := d.ossPath(path) + listResponse, err := d.Bucket.List(ossPath, "", "", listMax) if err != nil || len(listResponse.Contents) == 0 { return storagedriver.PathNotFoundError{Path: path} } @@ -416,15 +417,25 @@ func (d *driver) Delete(ctx context.Context, path string) error { ossObjects := make([]oss.Object, listMax) for len(listResponse.Contents) > 0 { + numOssObjects := len(listResponse.Contents) for index, key := range listResponse.Contents { + // Stop if we encounter a key that is not a subpath (so that deleting "/a" does not delete "/ab"). + if len(key.Key) > len(ossPath) && (key.Key)[len(ossPath)] != '/' { + numOssObjects = index + break + } ossObjects[index].Key = key.Key } - err := d.Bucket.DelMulti(oss.Delete{Quiet: false, Objects: ossObjects[0:len(listResponse.Contents)]}) + err := d.Bucket.DelMulti(oss.Delete{Quiet: false, Objects: ossObjects[0:numOssObjects]}) if err != nil { return nil } + if numOssObjects < len(listResponse.Contents) { + return nil + } + listResponse, err = d.Bucket.List(d.ossPath(path), "", "", listMax) if err != nil { return err