2021-11-19 18:54:49 +00:00
|
|
|
package s3
|
2021-08-23 18:40:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-08-23 19:55:52 +00:00
|
|
|
"io"
|
2021-08-23 20:00:55 +00:00
|
|
|
"log"
|
2021-08-23 18:40:03 +00:00
|
|
|
"os"
|
2021-08-23 19:55:52 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2021-08-23 18:40:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* S3FS lifetime is limited to a single request
|
|
|
|
* Conversely, Golang's abstraction has been thought to be shared between users
|
|
|
|
* Sharing an instance between users would be very dangerous (as we would need many checks between shared values)
|
|
|
|
*/
|
|
|
|
type S3FS struct {
|
|
|
|
cache map[string]*S3Stat
|
|
|
|
mc *minio.Client
|
2021-11-20 12:42:20 +00:00
|
|
|
local string
|
2021-08-23 18:40:03 +00:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2021-11-20 12:42:20 +00:00
|
|
|
func NewS3FS(mc *minio.Client, local string) S3FS {
|
2021-08-23 18:40:03 +00:00
|
|
|
return S3FS{
|
|
|
|
cache: make(map[string]*S3Stat),
|
|
|
|
mc: mc,
|
2021-11-20 12:42:20 +00:00
|
|
|
local: local,
|
2021-08-23 18:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s S3FS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
|
|
|
|
s.ctx = ctx
|
2021-08-23 20:00:55 +00:00
|
|
|
|
|
|
|
p := NewS3Path(name)
|
|
|
|
|
2021-11-19 18:54:49 +00:00
|
|
|
if p.Class == ROOT {
|
2021-08-23 20:00:55 +00:00
|
|
|
return errors.New("Unable to create another root folder")
|
2021-11-19 18:54:49 +00:00
|
|
|
} else if p.Class == BUCKET {
|
2021-08-23 20:00:55 +00:00
|
|
|
log.Println("Creating bucket is not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:55:52 +00:00
|
|
|
f, err := NewS3File(&s, path.Join(name, ".bagage"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(f, strings.NewReader("This is a placeholder"))
|
|
|
|
return nil
|
2021-08-23 18:40:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:49 +00:00
|
|
|
func (s S3FS) OpenFile2(ctx context.Context, name string, flag int, perm os.FileMode) (*S3File, error) {
|
2021-08-23 18:40:03 +00:00
|
|
|
s.ctx = ctx
|
|
|
|
|
|
|
|
// If the file does not exist when opening it, we create a stub
|
|
|
|
if _, ok := s.cache[name]; !ok {
|
|
|
|
st := new(S3Stat)
|
|
|
|
st.fs = &s
|
|
|
|
st.path = NewS3Path(name)
|
2021-11-19 18:54:49 +00:00
|
|
|
st.path.Class = OBJECT
|
|
|
|
st.obj.Key = st.path.Key
|
2021-08-23 18:40:03 +00:00
|
|
|
st.obj.LastModified = time.Now()
|
|
|
|
s.cache[name] = st
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewS3File(&s, name)
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:49 +00:00
|
|
|
func (s S3FS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
|
|
|
|
return s.OpenFile2(ctx, name, flag, perm)
|
|
|
|
}
|
|
|
|
|
2021-08-23 18:40:03 +00:00
|
|
|
func (s S3FS) RemoveAll(ctx context.Context, name string) error {
|
2021-08-23 20:23:19 +00:00
|
|
|
//@FIXME nautilus deletes files one by one, at the end, it does not find its folder as it is "already deleted"
|
2021-08-23 18:40:03 +00:00
|
|
|
s.ctx = ctx
|
2021-08-23 20:23:19 +00:00
|
|
|
|
|
|
|
p := NewS3Path(name)
|
2021-11-19 18:54:49 +00:00
|
|
|
if p.Class == ROOT {
|
2021-08-23 20:23:19 +00:00
|
|
|
return errors.New("Unable to create another root folder")
|
2021-11-19 18:54:49 +00:00
|
|
|
} else if p.Class == BUCKET {
|
2021-08-23 20:23:19 +00:00
|
|
|
log.Println("Deleting bucket is not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:49 +00:00
|
|
|
objCh := s.mc.ListObjects(s.ctx, p.Bucket, minio.ListObjectsOptions{Prefix: p.Key, Recursive: true})
|
|
|
|
rmCh := s.mc.RemoveObjects(s.ctx, p.Bucket, objCh, minio.RemoveObjectsOptions{})
|
2021-08-23 20:23:19 +00:00
|
|
|
|
|
|
|
for rErr := range rmCh {
|
|
|
|
return rErr.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-23 18:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s S3FS) Rename(ctx context.Context, oldName, newName string) error {
|
|
|
|
s.ctx = ctx
|
2021-08-31 08:31:26 +00:00
|
|
|
|
|
|
|
po := NewS3Path(oldName)
|
|
|
|
pn := NewS3Path(newName)
|
2021-11-19 18:54:49 +00:00
|
|
|
if po.Class == ROOT || pn.Class == ROOT {
|
2021-08-31 08:31:26 +00:00
|
|
|
return errors.New("Unable to rename root folder")
|
2021-11-19 18:54:49 +00:00
|
|
|
} else if po.Class == BUCKET || pn.Class == BUCKET {
|
2021-08-31 08:31:26 +00:00
|
|
|
log.Println("Moving a bucket is not implemented yet")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check that newName is not inside oldName
|
|
|
|
if len(newName) > len(oldName) && newName[:len(oldName)] == oldName {
|
|
|
|
return errors.New("Cannot move an entity inside itself (eg. moving /data in /data/test is impossible)")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Gather all keys, copy the object, delete the original
|
2021-11-19 18:54:49 +00:00
|
|
|
objCh := s.mc.ListObjects(s.ctx, po.Bucket, minio.ListObjectsOptions{Prefix: po.Key, Recursive: true})
|
2021-08-31 08:31:26 +00:00
|
|
|
for obj := range objCh {
|
|
|
|
src := minio.CopySrcOptions{
|
2021-11-19 18:54:49 +00:00
|
|
|
Bucket: po.Bucket,
|
2021-08-31 08:31:26 +00:00
|
|
|
Object: obj.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := minio.CopyDestOptions{
|
2021-11-19 18:54:49 +00:00
|
|
|
Bucket: pn.Bucket,
|
|
|
|
Object: path.Join(pn.Key, obj.Key[len(po.Key):]),
|
2021-08-31 08:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := s.mc.CopyObject(s.ctx, dst, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:49 +00:00
|
|
|
err = s.mc.RemoveObject(s.ctx, po.Bucket, obj.Key, minio.RemoveObjectOptions{})
|
2021-09-03 08:49:05 +00:00
|
|
|
var e minio.ErrorResponse
|
|
|
|
log.Println(errors.As(err, &e))
|
|
|
|
log.Println(e)
|
|
|
|
if errors.As(err, &e) && e.StatusCode == 200 {
|
|
|
|
/* @FIXME workaround for garage's bug #98 */
|
|
|
|
} else if err != nil {
|
2021-08-31 08:31:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-23 18:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s S3FS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
|
|
|
|
s.ctx = ctx
|
|
|
|
return NewS3Stat(&s, name)
|
|
|
|
}
|