Skip to content

Commit

Permalink
Add invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
dmage committed Feb 13, 2024
1 parent efb129e commit e49be1f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions registry/storage/driver/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ func (d *driver) List(ctx context.Context, path string) ([]string, error) {
path = ""
}

// Invairant: path is not "/".

blobs, err := d.listBlobs(ctx, path)
if err != nil {
return blobs, err
Expand Down Expand Up @@ -400,13 +402,19 @@ func directDescendants(blobs []string, prefix string) []string {
}

func (d *driver) listBlobs(ctx context.Context, virtPath string) ([]string, error) {
// Invairant: virtPath is not "/"

if virtPath != "" && !strings.HasSuffix(virtPath, "/") { // containerify the path
virtPath += "/"
}

// Invairant: virtPath is "" or ends with "/"

out := []string{}
listPrefix := d.blobName(virtPath)

// Invairant: listPrefix starts with a "/" and ends with a "/" (or is "/")

pager := d.client.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{
Prefix: &listPrefix,
})
Expand All @@ -425,6 +433,7 @@ func (d *driver) listBlobs(ctx context.Context, virtPath string) ([]string, erro
// In those cases, there is no root prefix to replace and we must actually add a "/" to all
// results in order to keep them as valid paths as recognized by storagedriver.PathRegexp
if !strings.HasPrefix(name, "/") { // add leading '/'
// Assertion: Unreachable code, as the name starts with listPrefix and listPrefix starts with a "/".
name = "/" + name
}
out = append(out, name)
Expand All @@ -435,6 +444,8 @@ func (d *driver) listBlobs(ctx context.Context, virtPath string) ([]string, erro
}

func (d *driver) blobName(path string) string {
// Invariant: path is any string.

// avoid returning an empty blob name.
// this will happen when rootDirectory is unset, and path == "/",
// which is what we get from the storage driver health check Stat call.
Expand All @@ -445,6 +456,8 @@ func (d *driver) blobName(path string) string {
trimmedRoot := strings.TrimRight(d.rootDirectory, "/")
trimmedPath := strings.TrimLeft(path, "/")
return trimmedRoot + "/" + trimmedPath

// Invairant: the returned string starts with a "/".
}

func is404(err error) bool {
Expand Down

0 comments on commit e49be1f

Please sign in to comment.