Skip to content

Commit

Permalink
Merge pull request #113 from tombuildsstuff/bugfix/double-slash-in-path
Browse files Browse the repository at this point in the history
bugfix: path fixes for files/file, retryfunc for directory deletion
  • Loading branch information
manicminer authored Mar 26, 2024
2 parents a3d2864 + 369a2c1 commit a1ba466
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
30 changes: 30 additions & 0 deletions storage/2023-11-03/file/directories/delete.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package directories

import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)

type DeleteResponse struct {
Expand All @@ -32,6 +37,30 @@ func (c Client) Delete(ctx context.Context, shareName, path string) (result Dele
return
}

// Retry the directory deletion if the directory is not empty (deleted files take a little while to disappear)
retryFunc := func(resp *http.Response, _ *odata.OData) (bool, error) {
if resp != nil {
if response.WasStatusCode(resp, http.StatusConflict) {
// TODO: move this error response parsing to a common helper function
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("could not parse response body")
}
resp.Body.Close()
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
res := ErrorResponse{}
if err = xml.Unmarshal(respBody, &res); err != nil {
return false, err
}
resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
if res.Code != nil {
return strings.Contains(*res.Code, "DirectoryNotEmpty"), nil
}
}
}
return false, nil
}

opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
Expand All @@ -40,6 +69,7 @@ func (c Client) Delete(ctx context.Context, shareName, path string) (result Dele
HttpMethod: http.MethodDelete,
OptionsObject: directoriesOptions{},
Path: fmt.Sprintf("/%s/%s", shareName, path),
RetryFunc: retryFunc,
}

req, err := c.Client.NewRequest(ctx, opts)
Expand Down
9 changes: 9 additions & 0 deletions storage/2023-11-03/file/directories/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package directories

import "encoding/xml"

type ErrorResponse struct {
XMLName xml.Name `xml:"Error"`
Code *string `xml:"Code"`
Message *string `xml:"Message"`
}
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c Client) Delete(ctx context.Context, shareName, path, fileName string) (r
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/metadata_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c Client) SetMetaData(ctx context.Context, shareName, path, fileName strin
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/properties_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c Client) GetProperties(ctx context.Context, shareName, path, fileName str
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/properties_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c Client) SetProperties(ctx context.Context, shareName, path, fileName str
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/range_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c Client) PutByteRange(ctx context.Context, shareName, path, fileName stri
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
10 changes: 7 additions & 3 deletions storage/2023-11-03/file/files/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func NewFileID(accountId accounts.AccountId, shareName, directoryPath, fileName
}

func (b FileId) ID() string {
return fmt.Sprintf("%s/%s/%s/%s", b.AccountId.ID(), b.ShareName, b.DirectoryPath, b.FileName)
path := ""
if b.DirectoryPath != "" {
path = fmt.Sprintf("%s/", b.DirectoryPath)
}
return fmt.Sprintf("%s/%s/%s%s", b.AccountId.ID(), b.ShareName, path, b.FileName)
}

func (b FileId) String() string {
Expand Down Expand Up @@ -72,8 +76,8 @@ func ParseFileID(input, domainSuffix string) (*FileId, error) {

path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) < 3 {
return nil, fmt.Errorf("expected the path to contain at least 3 segments but got %d", len(segments))
if len(segments) < 2 {
return nil, fmt.Errorf("expected the path to contain at least 2 segments but got %d", len(segments))
}
shareName := segments[0]
directoryPath := strings.Join(segments[1:len(segments)-1], "/")
Expand Down

0 comments on commit a1ba466

Please sign in to comment.