2020-05-23 09:58:05 +00:00
|
|
|
// +build !nogcs
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-22 20:53:04 +00:00
|
|
|
"encoding/json"
|
2021-02-22 08:13:26 +00:00
|
|
|
"errors"
|
2020-01-31 18:04:00 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-11-22 20:53:04 +00:00
|
|
|
"io/ioutil"
|
2020-09-28 19:52:18 +00:00
|
|
|
"mime"
|
2020-01-31 18:04:00 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-26 21:38:29 +00:00
|
|
|
"path/filepath"
|
2020-01-31 18:04:00 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
|
|
|
"github.com/eikenb/pipeat"
|
2021-02-11 18:45:52 +00:00
|
|
|
"github.com/pkg/sftp"
|
2020-01-31 18:04:00 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
"google.golang.org/api/option"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/metrics"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2020-01-31 18:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-11-02 18:16:12 +00:00
|
|
|
gcsDefaultFieldsSelection = []string{"Name", "Size", "Deleted", "Updated", "ContentType"}
|
2020-01-31 18:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GCSFs is a Fs implementation for Google Cloud Storage.
|
|
|
|
type GCSFs struct {
|
|
|
|
connectionID string
|
|
|
|
localTempDir string
|
2020-12-12 09:31:09 +00:00
|
|
|
config *GCSFsConfig
|
2020-01-31 18:04:00 +00:00
|
|
|
svc *storage.Client
|
|
|
|
ctxTimeout time.Duration
|
|
|
|
ctxLongTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
func init() {
|
2020-06-19 15:08:51 +00:00
|
|
|
version.AddFeature("+gcs")
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// NewGCSFs returns an GCSFs object that allows to interact with Google Cloud Storage
|
|
|
|
func NewGCSFs(connectionID, localTempDir string, config GCSFsConfig) (Fs, error) {
|
|
|
|
var err error
|
2020-11-02 18:16:12 +00:00
|
|
|
fs := &GCSFs{
|
2020-01-31 18:04:00 +00:00
|
|
|
connectionID: connectionID,
|
|
|
|
localTempDir: localTempDir,
|
2020-12-12 09:31:09 +00:00
|
|
|
config: &config,
|
2020-01-31 18:04:00 +00:00
|
|
|
ctxTimeout: 30 * time.Second,
|
|
|
|
ctxLongTimeout: 300 * time.Second,
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
if err = fs.config.Validate(fs.config.CredentialFile); err != nil {
|
2020-01-31 18:04:00 +00:00
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2020-02-19 08:41:15 +00:00
|
|
|
if fs.config.AutomaticCredentials > 0 {
|
|
|
|
fs.svc, err = storage.NewClient(ctx)
|
2020-11-30 20:46:34 +00:00
|
|
|
} else if !fs.config.Credentials.IsEmpty() {
|
|
|
|
if fs.config.Credentials.IsEncrypted() {
|
|
|
|
err = fs.config.Credentials.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
fs.svc, err = storage.NewClient(ctx, option.WithCredentialsJSON([]byte(fs.config.Credentials.GetPayload())))
|
2020-02-19 08:41:15 +00:00
|
|
|
} else {
|
2020-11-22 20:53:04 +00:00
|
|
|
var creds []byte
|
|
|
|
creds, err = ioutil.ReadFile(fs.config.CredentialFile)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
secret := kms.NewEmptySecret()
|
2020-11-22 20:53:04 +00:00
|
|
|
err = json.Unmarshal(creds, secret)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
err = secret.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
fs.svc, err = storage.NewClient(ctx, option.WithCredentialsJSON([]byte(secret.GetPayload())))
|
2020-02-19 08:41:15 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for the Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Name() string {
|
2020-10-25 07:18:48 +00:00
|
|
|
return fmt.Sprintf("GCSFs bucket %#v", fs.config.Bucket)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
// ConnectionID returns the connection ID associated to this Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) ConnectionID() string {
|
2020-01-31 18:04:00 +00:00
|
|
|
return fs.connectionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Stat(name string) (os.FileInfo, error) {
|
2021-01-23 09:32:15 +00:00
|
|
|
var result *FileInfo
|
2020-01-31 18:04:00 +00:00
|
|
|
var err error
|
2020-10-25 07:18:48 +00:00
|
|
|
if name == "" || name == "." {
|
2020-01-31 18:04:00 +00:00
|
|
|
err := fs.checkIfBucketExists()
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2020-08-22 12:52:17 +00:00
|
|
|
return NewFileInfo(name, true, 0, time.Now(), false), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
if fs.config.KeyPrefix == name+"/" {
|
2020-08-22 12:52:17 +00:00
|
|
|
return NewFileInfo(name, true, 0, time.Now(), false), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
attrs, err := fs.headObject(name)
|
|
|
|
if err == nil {
|
|
|
|
objSize := attrs.Size
|
|
|
|
objectModTime := attrs.Updated
|
|
|
|
isDir := attrs.ContentType == dirMimeType || strings.HasSuffix(attrs.Name, "/")
|
|
|
|
return NewFileInfo(name, isDir, objSize, objectModTime, false), nil
|
|
|
|
}
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
// now check if this is a prefix (virtual directory)
|
|
|
|
hasContents, err := fs.hasContents(name)
|
2021-02-22 08:13:26 +00:00
|
|
|
if err != nil {
|
2020-11-02 18:16:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-22 08:13:26 +00:00
|
|
|
if hasContents {
|
|
|
|
return NewFileInfo(name, true, 0, time.Now(), false), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2021-02-22 08:13:26 +00:00
|
|
|
return nil, errors.New("404 no such file or directory")
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Lstat(name string) (os.FileInfo, error) {
|
2020-01-31 18:04:00 +00:00
|
|
|
return fs.Stat(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens the named file for reading
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *GCSFs) Open(name string, offset int64) (File, *pipeat.PipeReaderAt, func(), error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
obj := bkt.Object(name)
|
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
2020-08-03 16:03:09 +00:00
|
|
|
objectReader, err := obj.NewRangeReader(ctx, offset, -1)
|
|
|
|
if err == nil && offset > 0 && objectReader.Attrs.ContentEncoding == "gzip" {
|
|
|
|
err = fmt.Errorf("Range request is not possible for gzip content encoding, requested offset %v", offset)
|
|
|
|
objectReader.Close()
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
cancelFn()
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
|
|
|
defer objectReader.Close()
|
|
|
|
n, err := io.Copy(w, objectReader)
|
2020-08-11 21:56:10 +00:00
|
|
|
w.CloseWithError(err) //nolint:errcheck
|
2020-01-31 18:04:00 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "download completed, path: %#v size: %v, err: %v", name, n, err)
|
|
|
|
metrics.GCSTransferCompleted(n, 1, err)
|
|
|
|
}()
|
|
|
|
return nil, r, cancelFn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates or opens the named file for writing
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *GCSFs) Create(name string, flag int) (File, *PipeWriter, func(), error) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2020-05-19 17:17:43 +00:00
|
|
|
p := NewPipeWriter(w)
|
2020-01-31 18:04:00 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
obj := bkt.Object(name)
|
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
objectWriter := obj.NewWriter(ctx)
|
2020-10-25 07:18:48 +00:00
|
|
|
var contentType string
|
|
|
|
if flag == -1 {
|
|
|
|
contentType = dirMimeType
|
|
|
|
} else {
|
|
|
|
contentType = mime.TypeByExtension(path.Ext(name))
|
|
|
|
}
|
2020-09-28 19:52:18 +00:00
|
|
|
if contentType != "" {
|
2020-09-28 20:12:46 +00:00
|
|
|
objectWriter.ObjectAttrs.ContentType = contentType
|
2020-09-28 19:52:18 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if fs.config.StorageClass != "" {
|
2020-01-31 18:04:00 +00:00
|
|
|
objectWriter.ObjectAttrs.StorageClass = fs.config.StorageClass
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
2020-11-13 17:40:18 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
n, err := io.Copy(objectWriter, r)
|
2020-11-13 17:40:18 +00:00
|
|
|
closeErr := objectWriter.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
r.CloseWithError(err) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
p.Done(err)
|
2020-01-31 18:04:00 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "upload completed, path: %#v, readed bytes: %v, err: %v", name, n, err)
|
|
|
|
metrics.GCSTransferCompleted(n, 0, err)
|
|
|
|
}()
|
2020-05-19 17:17:43 +00:00
|
|
|
return nil, p, cancelFn, nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames (moves) source to target.
|
|
|
|
// We don't support renaming non empty directories since we should
|
|
|
|
// rename all the contents too and this could take long time: think
|
|
|
|
// about directories with thousands of files, for each file we should
|
|
|
|
// execute a CopyObject call.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Rename(source, target string) error {
|
2020-01-31 18:04:00 +00:00
|
|
|
if source == target {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fi, err := fs.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2020-11-02 18:16:12 +00:00
|
|
|
hasContents, err := fs.hasContents(source)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2020-01-31 18:04:00 +00:00
|
|
|
return fmt.Errorf("Cannot rename non empty directory: %#v", source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src := fs.svc.Bucket(fs.config.Bucket).Object(source)
|
|
|
|
dst := fs.svc.Bucket(fs.config.Bucket).Object(target)
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
copier := dst.CopierFrom(src)
|
2020-11-02 18:16:12 +00:00
|
|
|
if fs.config.StorageClass != "" {
|
2020-01-31 18:04:00 +00:00
|
|
|
copier.StorageClass = fs.config.StorageClass
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
var contentType string
|
|
|
|
if fi.IsDir() {
|
|
|
|
contentType = dirMimeType
|
|
|
|
} else {
|
|
|
|
contentType = mime.TypeByExtension(path.Ext(source))
|
|
|
|
}
|
|
|
|
if contentType != "" {
|
|
|
|
copier.ContentType = contentType
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
_, err = copier.Run(ctx)
|
|
|
|
metrics.GCSCopyObjectCompleted(err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fs.Remove(source, fi.IsDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or (empty) directory.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Remove(name string, isDir bool) error {
|
2020-01-31 18:04:00 +00:00
|
|
|
if isDir {
|
2020-11-02 18:16:12 +00:00
|
|
|
hasContents, err := fs.hasContents(name)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2020-01-31 18:04:00 +00:00
|
|
|
return fmt.Errorf("Cannot remove non empty directory: %#v", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2020-11-02 18:16:12 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
err := fs.svc.Bucket(fs.config.Bucket).Object(name).Delete(ctx)
|
|
|
|
metrics.GCSDeleteObjectCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory with the specified name and default permissions
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Mkdir(name string) error {
|
2020-01-31 18:04:00 +00:00
|
|
|
_, err := fs.Stat(name)
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
_, w, _, err := fs.Create(name, -1)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates source as a symbolic link to target.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Symlink(source, target string) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
// Readlink returns the destination of the named symbolic link
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Readlink(name string) (string, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return "", ErrVfsUnsupported
|
2020-08-22 12:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Chown(name string, uid int, gid int) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the named file to mode.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Chmod(name string, mode os.FileMode) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named file.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Chtimes(name string, atime, mtime time.Time) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:54:36 +00:00
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
// Truncate by path is not supported, while truncating an opened
|
|
|
|
// file is handled inside base transfer
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Truncate(name string, size int64) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// ReadDir reads the directory named by dirname and returns
|
|
|
|
// a list of directory entries.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
2020-01-31 18:04:00 +00:00
|
|
|
var result []os.FileInfo
|
2020-02-19 08:41:15 +00:00
|
|
|
// dirname must be already cleaned
|
2020-11-02 18:16:12 +00:00
|
|
|
prefix := fs.getPrefix(dirname)
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
query := &storage.Query{Prefix: prefix, Delimiter: "/"}
|
2020-09-28 10:51:19 +00:00
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
|
|
|
|
prefixes := make(map[string]bool)
|
2020-01-31 18:04:00 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2020-11-02 18:16:12 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
it := bkt.Objects(ctx, query)
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return result, err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if attrs.Prefix != "" {
|
2020-01-31 18:04:00 +00:00
|
|
|
name, _ := fs.resolve(attrs.Prefix, prefix)
|
2020-11-02 18:16:12 +00:00
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-22 12:52:17 +00:00
|
|
|
result = append(result, NewFileInfo(name, true, 0, time.Now(), false))
|
2020-11-02 18:16:12 +00:00
|
|
|
prefixes[name] = true
|
2020-01-31 18:04:00 +00:00
|
|
|
} else {
|
|
|
|
name, isDir := fs.resolve(attrs.Name, prefix)
|
2020-11-02 18:16:12 +00:00
|
|
|
if name == "" {
|
2020-01-31 18:04:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if attrs.ContentType == dirMimeType {
|
|
|
|
isDir = true
|
|
|
|
}
|
|
|
|
if isDir {
|
|
|
|
// check if the dir is already included, it will be sent as blob prefix if it contains at least one item
|
|
|
|
if _, ok := prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prefixes[name] = true
|
|
|
|
}
|
2020-08-22 12:52:17 +00:00
|
|
|
fi := NewFileInfo(name, isDir, attrs.Size, attrs.Updated, false)
|
2020-08-11 21:56:10 +00:00
|
|
|
result = append(result, fi)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
metrics.GCSListObjectsCompleted(nil)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsUploadResumeSupported returns true if upload resume is supported.
|
|
|
|
// SFTP Resume is not supported on S3
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) IsUploadResumeSupported() bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAtomicUploadSupported returns true if atomic upload is supported.
|
|
|
|
// S3 uploads are already atomic, we don't need to upload to a temporary
|
|
|
|
// file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) IsAtomicUploadSupported() bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotExist returns a boolean indicating whether the error is known to
|
|
|
|
// report that a file or directory does not exist
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) IsNotExist(err error) bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err == storage.ErrObjectNotExist || err == storage.ErrBucketNotExist {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if e, ok := err.(*googleapi.Error); ok {
|
|
|
|
if e.Code == http.StatusNotFound {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Contains(err.Error(), "404")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPermission returns a boolean indicating whether the error is known to
|
|
|
|
// report that permission is denied.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) IsPermission(err error) bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e, ok := err.(*googleapi.Error); ok {
|
|
|
|
if e.Code == http.StatusForbidden || e.Code == http.StatusUnauthorized {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Contains(err.Error(), "403")
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:39:46 +00:00
|
|
|
// IsNotSupported returns true if the error indicate an unsupported operation
|
|
|
|
func (*GCSFs) IsNotSupported(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return err == ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// CheckRootPath creates the specified local root directory if it does not exists
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) CheckRootPath(username string, uid int, gid int) bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
// we need a local directory for temporary files
|
2020-02-23 10:30:26 +00:00
|
|
|
osFs := NewOsFs(fs.ConnectionID(), fs.localTempDir, nil)
|
2020-08-11 21:56:10 +00:00
|
|
|
return osFs.CheckRootPath(username, uid, gid)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScanRootDirContents returns the number of files contained in the bucket,
|
|
|
|
// and their size
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) ScanRootDirContents() (int, int64, error) {
|
2020-01-31 18:04:00 +00:00
|
|
|
numFiles := 0
|
|
|
|
size := int64(0)
|
|
|
|
query := &storage.Query{Prefix: fs.config.KeyPrefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxLongTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
it := bkt.Objects(ctx, query)
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
isDir := strings.HasSuffix(attrs.Name, "/") || attrs.ContentType == dirMimeType
|
2020-10-25 07:18:48 +00:00
|
|
|
if isDir && attrs.Size == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
numFiles++
|
|
|
|
size += attrs.Size
|
|
|
|
}
|
|
|
|
metrics.GCSListObjectsCompleted(nil)
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetDirSize returns the number of files and the size for a folder
|
|
|
|
// including any subfolders
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) GetDirSize(dirname string) (int, int64, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return 0, 0, ErrVfsUnsupported
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// GetAtomicUploadPath returns the path to use for an atomic upload.
|
2020-10-25 07:18:48 +00:00
|
|
|
// GCS uploads are already atomic, we never call this method for GCS
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) GetAtomicUploadPath(name string) string {
|
2020-01-31 18:04:00 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRelativePath returns the path for a file relative to the user's home dir.
|
2020-10-25 07:18:48 +00:00
|
|
|
// This is the path as seen by SFTPGo users
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) GetRelativePath(name string) string {
|
2020-01-31 18:04:00 +00:00
|
|
|
rel := path.Clean(name)
|
|
|
|
if rel == "." {
|
|
|
|
rel = ""
|
|
|
|
}
|
|
|
|
if !path.IsAbs(rel) {
|
|
|
|
rel = "/" + rel
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if fs.config.KeyPrefix != "" {
|
2020-01-31 18:04:00 +00:00
|
|
|
if !strings.HasPrefix(rel, "/"+fs.config.KeyPrefix) {
|
|
|
|
rel = "/"
|
|
|
|
}
|
|
|
|
rel = path.Clean("/" + strings.TrimPrefix(rel, "/"+fs.config.KeyPrefix))
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
// Walk walks the file tree rooted at root, calling walkFn for each file or
|
|
|
|
// directory in the tree, including root
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) Walk(root string, walkFn filepath.WalkFunc) error {
|
2020-08-11 21:56:10 +00:00
|
|
|
prefix := ""
|
2020-10-25 07:18:48 +00:00
|
|
|
if root != "" && root != "." {
|
2020-08-11 21:56:10 +00:00
|
|
|
prefix = strings.TrimPrefix(root, "/")
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &storage.Query{Prefix: prefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
walkFn(root, nil, err) //nolint:errcheck
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
it := bkt.Objects(ctx, query)
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
walkFn(root, nil, err) //nolint:errcheck
|
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
name, isDir := fs.resolve(attrs.Name, prefix)
|
|
|
|
if name == "" {
|
2020-08-11 21:56:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if attrs.ContentType == dirMimeType {
|
|
|
|
isDir = true
|
|
|
|
}
|
2020-08-22 12:52:17 +00:00
|
|
|
err = walkFn(attrs.Name, NewFileInfo(name, isDir, attrs.Size, attrs.Updated, false), nil)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2020-10-25 07:18:48 +00:00
|
|
|
return err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
walkFn(root, NewFileInfo(root, true, 0, time.Now(), false), err) //nolint:errcheck
|
2020-08-11 21:56:10 +00:00
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return err
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// Join joins any number of path elements into a single path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) Join(elem ...string) string {
|
2020-01-31 18:04:00 +00:00
|
|
|
return strings.TrimPrefix(path.Join(elem...), "/")
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:24:57 +00:00
|
|
|
// HasVirtualFolders returns true if folders are emulated
|
|
|
|
func (GCSFs) HasVirtualFolders() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
// ResolvePath returns the matching filesystem path for the specified virtual path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) ResolvePath(virtualPath string) (string, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
if !path.IsAbs(virtualPath) {
|
|
|
|
virtualPath = path.Clean("/" + virtualPath)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
return fs.Join(fs.config.KeyPrefix, strings.TrimPrefix(virtualPath, "/")), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *GCSFs) resolve(name string, prefix string) (string, bool) {
|
|
|
|
result := strings.TrimPrefix(name, prefix)
|
|
|
|
isDir := strings.HasSuffix(result, "/")
|
|
|
|
if isDir {
|
|
|
|
result = strings.TrimSuffix(result, "/")
|
|
|
|
}
|
|
|
|
return result, isDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *GCSFs) checkIfBucketExists() error {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
_, err := bkt.Attrs(ctx)
|
|
|
|
metrics.GCSHeadBucketCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) hasContents(name string) (bool, error) {
|
|
|
|
result := false
|
|
|
|
prefix := ""
|
|
|
|
if name != "" && name != "." {
|
|
|
|
prefix = strings.TrimPrefix(name, "/")
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query := &storage.Query{Prefix: prefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxLongTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
it := bkt.Objects(ctx, query)
|
|
|
|
// if we have a dir object with a trailing slash it will be returned so we set the size to 2
|
|
|
|
it.PageInfo().MaxSize = 2
|
|
|
|
for {
|
|
|
|
attrs, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
name, _ := fs.resolve(attrs.Name, prefix)
|
|
|
|
// a dir object with a trailing slash will result in an empty name
|
|
|
|
if name == "/" || name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.GCSListObjectsCompleted(err)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *GCSFs) getPrefix(name string) string {
|
|
|
|
prefix := ""
|
|
|
|
if name != "" && name != "." && name != "/" {
|
|
|
|
prefix = strings.TrimPrefix(name, "/")
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *GCSFs) headObject(name string) (*storage.ObjectAttrs, error) {
|
2020-09-28 20:12:46 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2020-09-29 07:17:08 +00:00
|
|
|
|
2020-09-28 20:12:46 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
obj := bkt.Object(name)
|
|
|
|
attrs, err := obj.Attrs(ctx)
|
2020-10-26 18:01:17 +00:00
|
|
|
metrics.GCSHeadObjectCompleted(err)
|
2020-11-02 18:16:12 +00:00
|
|
|
return attrs, err
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
// GetMimeType returns the content type
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) GetMimeType(name string) (string, error) {
|
|
|
|
attrs, err := fs.headObject(name)
|
2020-09-28 20:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return attrs.ContentType, nil
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
|
|
|
|
// Close closes the fs
|
|
|
|
func (fs *GCSFs) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-25 10:14:08 +00:00
|
|
|
|
|
|
|
// GetAvailableDiskSize return the available size for the specified path
|
2021-02-11 18:45:52 +00:00
|
|
|
func (*GCSFs) GetAvailableDiskSize(dirName string) (*sftp.StatVFS, error) {
|
|
|
|
return nil, ErrStorageSizeUnavailable
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|