2019-04-25 05:55:39 +02:00
|
|
|
package filestate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"path"
|
2019-05-20 20:46:00 +02:00
|
|
|
"path/filepath"
|
2019-04-25 05:55:39 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
|
2019-04-25 05:55:39 +02:00
|
|
|
"gocloud.dev/blob"
|
|
|
|
)
|
|
|
|
|
2019-05-20 20:46:00 +02:00
|
|
|
// Bucket is a wrapper around an underlying gocloud blob.Bucket. It ensures that we pass all paths
|
|
|
|
// to it normalized to forward-slash form like it requires.
|
|
|
|
type Bucket interface {
|
|
|
|
Copy(ctx context.Context, dstKey, srcKey string, opts *blob.CopyOptions) (err error)
|
|
|
|
Delete(ctx context.Context, key string) (err error)
|
|
|
|
List(opts *blob.ListOptions) *blob.ListIterator
|
|
|
|
SignedURL(ctx context.Context, key string, opts *blob.SignedURLOptions) (string, error)
|
|
|
|
ReadAll(ctx context.Context, key string) (_ []byte, err error)
|
|
|
|
WriteAll(ctx context.Context, key string, p []byte, opts *blob.WriterOptions) (err error)
|
2019-08-14 20:50:03 +02:00
|
|
|
Exists(ctx context.Context, key string) (bool, error)
|
2019-05-20 20:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrappedBucket encapsulates a true gocloud blob.Bucket, but ensures that all paths we send to it
|
|
|
|
// are appropriately normalized to use forward slashes as required by it. Without this, we may use
|
|
|
|
// filepath.join which can make paths like `c:\temp\etc`. gocloud's fileblob then converts those
|
|
|
|
// backslashes to the hex string __0x5c__, breaking things on windows completely.
|
|
|
|
type wrappedBucket struct {
|
|
|
|
bucket *blob.Bucket
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) Copy(ctx context.Context, dstKey, srcKey string, opts *blob.CopyOptions) (err error) {
|
|
|
|
return b.bucket.Copy(ctx, filepath.ToSlash(dstKey), filepath.ToSlash(srcKey), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) Delete(ctx context.Context, key string) (err error) {
|
|
|
|
return b.bucket.Delete(ctx, filepath.ToSlash(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) List(opts *blob.ListOptions) *blob.ListIterator {
|
2020-03-18 21:57:25 +01:00
|
|
|
optsCopy := *opts
|
|
|
|
optsCopy.Prefix = filepath.ToSlash(opts.Prefix)
|
|
|
|
return b.bucket.List(&optsCopy)
|
2019-05-20 20:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) SignedURL(ctx context.Context, key string, opts *blob.SignedURLOptions) (string, error) {
|
|
|
|
return b.bucket.SignedURL(ctx, filepath.ToSlash(key), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) ReadAll(ctx context.Context, key string) (_ []byte, err error) {
|
|
|
|
return b.bucket.ReadAll(ctx, filepath.ToSlash(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *wrappedBucket) WriteAll(ctx context.Context, key string, p []byte, opts *blob.WriterOptions) (err error) {
|
|
|
|
return b.bucket.WriteAll(ctx, filepath.ToSlash(key), p, opts)
|
|
|
|
}
|
|
|
|
|
2019-08-14 20:50:03 +02:00
|
|
|
func (b *wrappedBucket) Exists(ctx context.Context, key string) (bool, error) {
|
|
|
|
return b.bucket.Exists(ctx, filepath.ToSlash(key))
|
|
|
|
}
|
|
|
|
|
2019-04-25 05:55:39 +02:00
|
|
|
// listBucket returns a list of all files in the bucket within a given directory. go-cloud sorts the results by key
|
2019-05-20 20:46:00 +02:00
|
|
|
func listBucket(bucket Bucket, dir string) ([]*blob.ListObject, error) {
|
2019-04-25 05:55:39 +02:00
|
|
|
bucketIter := bucket.List(&blob.ListOptions{
|
|
|
|
Delimiter: "/",
|
|
|
|
Prefix: dir + "/",
|
|
|
|
})
|
|
|
|
|
|
|
|
files := []*blob.ListObject{}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
for {
|
|
|
|
file, err := bucketIter.Next(ctx)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not list bucket")
|
|
|
|
}
|
|
|
|
files = append(files, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:57:25 +01:00
|
|
|
// objectName returns the filename of a ListObject (an object from a bucket).
|
2019-04-25 05:55:39 +02:00
|
|
|
func objectName(obj *blob.ListObject) string {
|
|
|
|
_, filename := path.Split(obj.Key)
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeAllByPrefix deletes all objects with a given prefix (i.e. filepath)
|
2019-05-20 20:46:00 +02:00
|
|
|
func removeAllByPrefix(bucket Bucket, dir string) error {
|
2019-04-25 05:55:39 +02:00
|
|
|
files, err := listBucket(bucket, dir)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unable to list bucket objects for removal")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
err = bucket.Delete(context.TODO(), file.Key)
|
|
|
|
if err != nil {
|
|
|
|
logging.V(5).Infof("error deleting object: %v (%v) skipping", file.Key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// renameObject renames an object in a bucket. the rename requires a download/upload of the object
|
|
|
|
// due to a go-cloud API limitation
|
2019-05-20 20:46:00 +02:00
|
|
|
func renameObject(bucket Bucket, source string, dest string) error {
|
2019-04-25 05:55:39 +02:00
|
|
|
err := bucket.Copy(context.TODO(), dest, source, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "copying %s to %s", source, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bucket.Delete(context.TODO(), source)
|
|
|
|
if err != nil {
|
|
|
|
logging.V(5).Infof("error deleting source object after rename: %v (%v) skipping", source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|