Skip to content

Commit

Permalink
Use lock to prevent simultaneous file access
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrus committed Jun 20, 2023
1 parent 9054e67 commit 3d8b9f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions openstack/images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"regexp"
"strings"

Check failure on line 17 in openstack/images_image_v2.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/members"
"github.com/gophercloud/utils/terraform/mutexkv"

Check failure on line 22 in openstack/images_image_v2.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/ulikunitz/xz"
)

Expand Down Expand Up @@ -86,7 +88,7 @@ func resourceImagesImageV2FileProps(filename string) (int64, string, error) {
return filesize, filechecksum, nil
}

func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.ResourceData, mutexKV *mutexkv.MutexKV) (string, error) {
if filename := d.Get("local_file_path").(string); filename != "" {
return filename, nil
}
Expand All @@ -101,7 +103,12 @@ func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.Reso
return "", fmt.Errorf("unable to create dir %s: %s", dir, err)
}

filename := filepath.Join(dir, fmt.Sprintf("%x.img", md5.Sum([]byte(furl))))
// calculate the hashsum and create a lock to prevent simultaneous file access
md5sum := fmt.Sprintf("%x", md5.Sum([]byte(furl)))
mutexKV.Lock(md5sum)
defer mutexKV.Unlock(md5sum)

filename := filepath.Join(dir, fmt.Sprintf("%s.img", md5sum))
// a cleanup func to delete a failed file
delFile := func() {
if err := os.Remove(filename); err != nil {
Expand All @@ -115,6 +122,7 @@ func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.Reso
}

// check if the file size is zero
// it could be a leftoever from older provider versions
if info != nil {
if info.Size() != 0 {
log.Printf("[DEBUG] File exists %s", filename)
Expand Down
2 changes: 1 addition & 1 deletion openstack/resource_openstack_images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func resourceImagesImageV2Create(ctx context.Context, d *schema.ResourceData, me
var imgFile *os.File

// downloading/getting image file props
imgFilePath, err = resourceImagesImageV2File(imageClient, d)
imgFilePath, err = resourceImagesImageV2File(imageClient, d, config.MutexKV)
if err != nil {
return diag.Errorf("Error opening file for Image: %s", err)
}
Expand Down

0 comments on commit 3d8b9f1

Please sign in to comment.