2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
//go:build !nogcs
|
2020-05-23 09:58:05 +00:00
|
|
|
// +build !nogcs
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
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"
|
2024-02-17 18:49:34 +00:00
|
|
|
"strconv"
|
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"
|
2023-10-24 17:14:33 +00:00
|
|
|
"github.com/rs/xid"
|
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
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
2024-02-17 18:49:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2020-01-31 18:04:00 +00:00
|
|
|
)
|
|
|
|
|
2022-03-04 17:46:17 +00:00
|
|
|
const (
|
2022-03-15 18:16:50 +00:00
|
|
|
defaultGCSPageSize = 5000
|
2022-03-04 17:46:17 +00:00
|
|
|
)
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
var (
|
2024-02-17 18:49:34 +00:00
|
|
|
gcsDefaultFieldsSelection = []string{"Name", "Size", "Deleted", "Updated", "ContentType", "Metadata"}
|
2020-01-31 18:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GCSFs is a Fs implementation for Google Cloud Storage.
|
|
|
|
type GCSFs struct {
|
2021-03-21 18:15:47 +00:00
|
|
|
connectionID string
|
|
|
|
localTempDir string
|
|
|
|
// if not empty this fs is mouted as virtual folder in the specified path
|
|
|
|
mountPath 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
|
2021-03-21 18:15:47 +00:00
|
|
|
func NewGCSFs(connectionID, localTempDir, mountPath string, config GCSFsConfig) (Fs, error) {
|
|
|
|
if localTempDir == "" {
|
2023-10-17 16:06:52 +00:00
|
|
|
localTempDir = getLocalTempDir()
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
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,
|
2022-04-02 16:32:46 +00:00
|
|
|
mountPath: getMountPath(mountPath),
|
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,
|
|
|
|
}
|
2022-04-28 10:55:01 +00:00
|
|
|
if err = fs.config.validate(); 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)
|
2022-04-28 10:55:01 +00:00
|
|
|
} else {
|
2021-03-27 18:10:27 +00:00
|
|
|
err = fs.config.Credentials.TryDecrypt()
|
|
|
|
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
|
|
|
}
|
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 {
|
2022-10-10 17:34:15 +00:00
|
|
|
return fmt.Sprintf("%s bucket %q", gcsfsName, 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) {
|
2022-08-11 06:31:51 +00:00
|
|
|
if name == "" || name == "/" || name == "." {
|
2024-02-17 11:30:47 +00:00
|
|
|
return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
if fs.config.KeyPrefix == name+"/" {
|
2024-02-17 11:30:47 +00:00
|
|
|
return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
return fs.getObjectStat(name)
|
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
|
2024-01-24 18:59:50 +00:00
|
|
|
func (fs *GCSFs) Open(name string, offset int64) (File, PipeReader, 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
|
|
|
|
}
|
2023-08-12 16:51:47 +00:00
|
|
|
p := NewPipeReader(r)
|
|
|
|
if readMetadata > 0 {
|
|
|
|
attrs, err := fs.headObject(name)
|
|
|
|
if err != nil {
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
p.setMetadata(attrs.Metadata)
|
|
|
|
}
|
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())
|
2020-08-03 16:03:09 +00:00
|
|
|
objectReader, err := obj.NewRangeReader(ctx, offset, -1)
|
|
|
|
if err == nil && offset > 0 && objectReader.Attrs.ContentEncoding == "gzip" {
|
2023-08-12 16:51:47 +00:00
|
|
|
err = fmt.Errorf("range request is not possible for gzip content encoding, requested offset %d", offset)
|
2020-08-03 16:03:09 +00:00
|
|
|
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()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
n, err := io.Copy(w, objectReader)
|
2020-08-11 21:56:10 +00:00
|
|
|
w.CloseWithError(err) //nolint:errcheck
|
2023-02-27 18:02:43 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "download completed, path: %q size: %v, err: %+v", name, n, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.GCSTransferCompleted(n, 1, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
}()
|
2023-08-12 16:51:47 +00:00
|
|
|
return nil, p, cancelFn, nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates or opens the named file for writing
|
2023-10-22 14:30:04 +00:00
|
|
|
func (fs *GCSFs) Create(name string, flag, checks int) (File, PipeWriter, func(), error) {
|
2023-04-06 16:22:09 +00:00
|
|
|
if checks&CheckParentDir != 0 {
|
|
|
|
_, err := fs.Stat(path.Dir(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2023-10-24 17:14:33 +00:00
|
|
|
var partialFileName string
|
|
|
|
var attrs *storage.ObjectAttrs
|
|
|
|
var statErr error
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
obj := bkt.Object(name)
|
2023-10-24 17:14:33 +00:00
|
|
|
|
2022-12-18 10:51:46 +00:00
|
|
|
if flag == -1 {
|
|
|
|
obj = obj.If(storage.Conditions{DoesNotExist: true})
|
|
|
|
} else {
|
2023-10-24 17:14:33 +00:00
|
|
|
attrs, statErr = fs.headObject(name)
|
2022-12-18 10:51:46 +00:00
|
|
|
if statErr == nil {
|
|
|
|
obj = obj.If(storage.Conditions{GenerationMatch: attrs.Generation})
|
|
|
|
} else if fs.IsNotExist(statErr) {
|
|
|
|
obj = obj.If(storage.Conditions{DoesNotExist: true})
|
|
|
|
} else {
|
|
|
|
fsLog(fs, logger.LevelWarn, "unable to set precondition for %q, stat err: %v", name, statErr)
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
2023-10-24 17:14:33 +00:00
|
|
|
|
|
|
|
var p PipeWriter
|
|
|
|
var objectWriter *storage.Writer
|
|
|
|
if checks&CheckResume != 0 {
|
|
|
|
if statErr != nil {
|
|
|
|
cancelFn()
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
return nil, nil, nil, fmt.Errorf("unable to resume %q stat error: %w", name, statErr)
|
|
|
|
}
|
|
|
|
p = newPipeWriterAtOffset(w, attrs.Size)
|
|
|
|
partialFileName = fs.getTempObject(name)
|
|
|
|
partialObj := bkt.Object(partialFileName)
|
|
|
|
partialObj = partialObj.If(storage.Conditions{DoesNotExist: true})
|
|
|
|
objectWriter = partialObj.NewWriter(ctx)
|
|
|
|
} else {
|
|
|
|
p = NewPipeWriter(w)
|
|
|
|
objectWriter = obj.NewWriter(ctx)
|
|
|
|
}
|
|
|
|
|
2022-12-18 10:51:46 +00:00
|
|
|
if fs.config.UploadPartSize > 0 {
|
|
|
|
objectWriter.ChunkSize = int(fs.config.UploadPartSize) * 1024 * 1024
|
|
|
|
}
|
|
|
|
if fs.config.UploadPartMaxTime > 0 {
|
|
|
|
objectWriter.ChunkRetryDeadline = time.Duration(fs.config.UploadPartMaxTime) * time.Second
|
|
|
|
}
|
2023-10-25 17:05:37 +00:00
|
|
|
fs.setWriterAttrs(objectWriter, flag, name)
|
2023-10-22 14:09:30 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
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
|
|
|
|
}
|
2023-10-24 17:14:33 +00:00
|
|
|
if err == nil && partialFileName != "" {
|
|
|
|
partialObject := bkt.Object(partialFileName)
|
|
|
|
partialObject = partialObject.If(storage.Conditions{GenerationMatch: objectWriter.Attrs().Generation})
|
|
|
|
err = fs.composeObjects(ctx, obj, partialObject)
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
r.CloseWithError(err) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
p.Done(err)
|
2023-02-27 18:02:43 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "upload completed, path: %q, acl: %q, readed bytes: %v, err: %+v",
|
2021-11-15 20:57:41 +00:00
|
|
|
name, fs.config.ACL, n, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.GCSTransferCompleted(n, 0, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
}()
|
2023-10-22 14:09:30 +00:00
|
|
|
|
2023-10-25 17:05:37 +00:00
|
|
|
if uploadMode&8 != 0 {
|
|
|
|
return nil, p, nil, nil
|
|
|
|
}
|
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.
|
2023-01-06 11:33:50 +00:00
|
|
|
func (fs *GCSFs) Rename(source, target string) (int, int64, error) {
|
2020-01-31 18:04:00 +00:00
|
|
|
if source == target {
|
2023-01-06 11:33:50 +00:00
|
|
|
return -1, -1, nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2023-04-06 16:22:09 +00:00
|
|
|
_, err := fs.Stat(path.Dir(target))
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, err
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
fi, err := fs.getObjectStat(source)
|
2020-01-31 18:04:00 +00:00
|
|
|
if err != nil {
|
2023-01-06 11:33:50 +00:00
|
|
|
return -1, -1, err
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
return fs.renameInternal(source, target, fi, 0)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("cannot remove non empty directory: %q", name)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2021-06-24 17:36:01 +00:00
|
|
|
if !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2022-12-18 10:51:46 +00:00
|
|
|
obj := fs.svc.Bucket(fs.config.Bucket).Object(name)
|
|
|
|
attrs, statErr := fs.headObject(name)
|
|
|
|
if statErr == nil {
|
|
|
|
obj = obj.If(storage.Conditions{GenerationMatch: attrs.Generation})
|
|
|
|
} else {
|
|
|
|
fsLog(fs, logger.LevelWarn, "unable to set precondition for deleting %q, stat err: %v",
|
|
|
|
name, statErr)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-12-18 10:51:46 +00:00
|
|
|
err := obj.Delete(ctx)
|
|
|
|
if isDir && fs.IsNotExist(err) {
|
2021-06-24 17:36:01 +00:00
|
|
|
// we can have directories without a trailing "/" (created using v2.1.0 and before)
|
2023-10-24 17:14:33 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2021-06-24 17:36:01 +00:00
|
|
|
err = fs.svc.Bucket(fs.config.Bucket).Object(strings.TrimSuffix(name, "/")).Delete(ctx)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.GCSDeleteObjectCompleted(err)
|
2020-01-31 18:04:00 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-24 17:14:24 +00:00
|
|
|
return fs.mkdirInternal(name)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates source as a symbolic link to target.
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) Symlink(_, _ 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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) Readlink(_ 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.
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) Chown(_ string, _ int, _ 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.
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) Chmod(_ string, _ 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.
|
2024-02-17 18:49:34 +00:00
|
|
|
func (fs *GCSFs) Chtimes(name string, _, mtime time.Time, isUploading bool) error {
|
|
|
|
if isUploading {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
obj := fs.svc.Bucket(fs.config.Bucket).Object(name)
|
|
|
|
attrs, err := fs.headObject(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj = obj.If(storage.Conditions{MetagenerationMatch: attrs.Metageneration})
|
|
|
|
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
metadata := attrs.Metadata
|
|
|
|
if metadata == nil {
|
|
|
|
metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
metadata[lastModifiedField] = strconv.FormatInt(mtime.UnixMilli(), 10)
|
|
|
|
|
|
|
|
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
_, err = obj.Update(ctx, objectAttrsToUpdate)
|
|
|
|
|
|
|
|
return err
|
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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) Truncate(_ string, _ 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.
|
2024-02-15 19:53:56 +00:00
|
|
|
func (fs *GCSFs) ReadDir(dirname string) (DirLister, error) {
|
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-01-31 18:04:00 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
return &gcsDirLister{
|
|
|
|
bucket: bkt,
|
|
|
|
query: query,
|
|
|
|
timeout: fs.ctxTimeout,
|
|
|
|
prefix: prefix,
|
|
|
|
prefixes: make(map[string]bool),
|
|
|
|
}, nil
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 14:00:55 +00:00
|
|
|
// IsUploadResumeSupported returns true if resuming uploads is supported.
|
|
|
|
// Resuming uploads is not supported on GCS
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*GCSFs) IsUploadResumeSupported() bool {
|
2020-01-31 18:04:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:09:30 +00:00
|
|
|
// IsConditionalUploadResumeSupported returns if resuming uploads is supported
|
|
|
|
// for the specified size
|
2023-10-24 17:14:33 +00:00
|
|
|
func (*GCSFs) IsConditionalUploadResumeSupported(_ int64) bool {
|
|
|
|
return true
|
2023-10-22 14:09:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return false
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:16:50 +00:00
|
|
|
return false
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-05-16 16:08:14 +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) {
|
2023-01-06 11:33:50 +00:00
|
|
|
return fs.GetDirSize(fs.config.KeyPrefix)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetDirSize returns the number of files and the size for a folder
|
|
|
|
// including any subfolders
|
2023-01-06 11:33:50 +00:00
|
|
|
func (fs *GCSFs) GetDirSize(dirname string) (int, int64, error) {
|
|
|
|
prefix := fs.getPrefix(dirname)
|
|
|
|
numFiles := 0
|
|
|
|
size := int64(0)
|
|
|
|
|
|
|
|
query := &storage.Query{Prefix: prefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
|
2024-02-18 09:12:53 +00:00
|
|
|
iteratePage := func(nextPageToken string) (string, error) {
|
|
|
|
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)
|
|
|
|
pager := iterator.NewPager(it, defaultGCSPageSize, nextPageToken)
|
2023-01-06 11:33:50 +00:00
|
|
|
|
|
|
|
var objects []*storage.ObjectAttrs
|
|
|
|
pageToken, err := pager.NextPage(&objects)
|
|
|
|
if err != nil {
|
2024-02-18 09:12:53 +00:00
|
|
|
return pageToken, err
|
2023-01-06 11:33:50 +00:00
|
|
|
}
|
|
|
|
for _, attrs := range objects {
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
isDir := strings.HasSuffix(attrs.Name, "/") || attrs.ContentType == dirMimeType
|
|
|
|
if isDir && attrs.Size == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numFiles++
|
|
|
|
size += attrs.Size
|
|
|
|
}
|
2024-02-18 09:12:53 +00:00
|
|
|
return pageToken, nil
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
|
2024-02-18 09:12:53 +00:00
|
|
|
pageToken := ""
|
|
|
|
for {
|
|
|
|
pageToken, err = iteratePage(pageToken)
|
|
|
|
if err != nil {
|
|
|
|
metric.GCSListObjectsCompleted(err)
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
fsLog(fs, logger.LevelDebug, "scan in progress for %q, files: %d, size: %d", dirname, numFiles, size)
|
2023-01-06 11:33:50 +00:00
|
|
|
if pageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
metric.GCSListObjectsCompleted(nil)
|
|
|
|
return numFiles, size, err
|
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
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) GetAtomicUploadPath(_ 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))
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.mountPath != "" {
|
|
|
|
rel = path.Join(fs.mountPath, rel)
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
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 {
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(root)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
query := &storage.Query{Prefix: prefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
walkFn(root, nil, err) //nolint:errcheck
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-18 09:12:53 +00:00
|
|
|
iteratePage := func(nextPageToken string) (string, error) {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2024-02-18 09:12:53 +00:00
|
|
|
bkt := fs.svc.Bucket(fs.config.Bucket)
|
|
|
|
it := bkt.Objects(ctx, query)
|
|
|
|
pager := iterator.NewPager(it, defaultGCSPageSize, nextPageToken)
|
2022-03-04 17:46:17 +00:00
|
|
|
|
|
|
|
var objects []*storage.ObjectAttrs
|
|
|
|
pageToken, err := pager.NextPage(&objects)
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
walkFn(root, nil, err) //nolint:errcheck
|
2024-02-18 09:12:53 +00:00
|
|
|
return pageToken, err
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2022-03-04 17:46:17 +00:00
|
|
|
for _, attrs := range objects {
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name, isDir := fs.resolve(attrs.Name, prefix, attrs.ContentType)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
2024-02-18 09:12:53 +00:00
|
|
|
objectModTime := attrs.Updated
|
|
|
|
if val := getLastModified(attrs.Metadata); val > 0 {
|
|
|
|
objectModTime = util.GetTimeFromMsecSinceEpoch(val)
|
|
|
|
}
|
|
|
|
err = walkFn(attrs.Name, NewFileInfo(name, isDir, attrs.Size, objectModTime, false), nil)
|
2022-03-04 17:46:17 +00:00
|
|
|
if err != nil {
|
2024-02-18 09:12:53 +00:00
|
|
|
return pageToken, err
|
2022-03-04 17:46:17 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2024-02-18 09:12:53 +00:00
|
|
|
return pageToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pageToken := ""
|
|
|
|
for {
|
|
|
|
pageToken, err = iteratePage(pageToken)
|
|
|
|
if err != nil {
|
|
|
|
metric.GCSListObjectsCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
2022-03-04 17:46:17 +00:00
|
|
|
if pageToken == "" {
|
|
|
|
break
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:59:13 +00:00
|
|
|
walkFn(root, NewFileInfo(root, true, 0, time.Unix(0, 0), false), err) //nolint:errcheck
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.GCSListObjectsCompleted(err)
|
2020-08-11 21:56:10 +00:00
|
|
|
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) {
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.mountPath != "" {
|
|
|
|
virtualPath = strings.TrimPrefix(virtualPath, fs.mountPath)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-01-07 15:28:46 +00:00
|
|
|
// CopyFile implements the FsFileCopier interface
|
2023-03-22 18:02:54 +00:00
|
|
|
func (fs *GCSFs) CopyFile(source, target string, _ int64) error {
|
2023-01-07 15:28:46 +00:00
|
|
|
return fs.copyFileInternal(source, target)
|
|
|
|
}
|
|
|
|
|
2022-03-04 17:46:17 +00:00
|
|
|
func (fs *GCSFs) resolve(name, prefix, contentType string) (string, bool) {
|
2020-01-31 18:04:00 +00:00
|
|
|
result := strings.TrimPrefix(name, prefix)
|
|
|
|
isDir := strings.HasSuffix(result, "/")
|
|
|
|
if isDir {
|
|
|
|
result = strings.TrimSuffix(result, "/")
|
|
|
|
}
|
2022-03-04 17:46:17 +00:00
|
|
|
if contentType == dirMimeType {
|
|
|
|
isDir = true
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
return result, isDir
|
|
|
|
}
|
|
|
|
|
2023-04-06 16:22:09 +00:00
|
|
|
// getObjectStat returns the stat result
|
2023-01-06 11:33:50 +00:00
|
|
|
func (fs *GCSFs) getObjectStat(name string) (os.FileInfo, error) {
|
2021-06-24 17:36:01 +00:00
|
|
|
attrs, err := fs.headObject(name)
|
|
|
|
if err == nil {
|
|
|
|
objSize := attrs.Size
|
|
|
|
objectModTime := attrs.Updated
|
2024-02-17 18:49:34 +00:00
|
|
|
if val := getLastModified(attrs.Metadata); val > 0 {
|
|
|
|
objectModTime = util.GetTimeFromMsecSinceEpoch(val)
|
|
|
|
}
|
2021-06-24 17:36:01 +00:00
|
|
|
isDir := attrs.ContentType == dirMimeType || strings.HasSuffix(attrs.Name, "/")
|
2024-02-17 11:30:47 +00:00
|
|
|
return NewFileInfo(name, isDir, objSize, objectModTime, false), nil
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
|
|
|
if !fs.IsNotExist(err) {
|
2023-01-06 11:33:50 +00:00
|
|
|
return nil, err
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
|
|
|
// now check if this is a prefix (virtual directory)
|
|
|
|
hasContents, err := fs.hasContents(name)
|
|
|
|
if err != nil {
|
2023-01-06 11:33:50 +00:00
|
|
|
return nil, err
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
|
|
|
if hasContents {
|
2024-02-17 11:30:47 +00:00
|
|
|
return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
|
|
|
// finally check if this is an object with a trailing /
|
|
|
|
attrs, err = fs.headObject(name + "/")
|
|
|
|
if err != nil {
|
2023-01-06 11:33:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-17 18:49:34 +00:00
|
|
|
objectModTime := attrs.Updated
|
|
|
|
if val := getLastModified(attrs.Metadata); val > 0 {
|
|
|
|
objectModTime = util.GetTimeFromMsecSinceEpoch(val)
|
|
|
|
}
|
|
|
|
return NewFileInfo(name, true, attrs.Size, objectModTime, false), nil
|
2023-01-06 11:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 17:05:37 +00:00
|
|
|
func (fs *GCSFs) setWriterAttrs(objectWriter *storage.Writer, flag int, name string) {
|
|
|
|
var contentType string
|
|
|
|
if flag == -1 {
|
|
|
|
contentType = dirMimeType
|
|
|
|
} else {
|
|
|
|
contentType = mime.TypeByExtension(path.Ext(name))
|
|
|
|
}
|
2023-10-22 14:09:30 +00:00
|
|
|
if contentType != "" {
|
|
|
|
objectWriter.ObjectAttrs.ContentType = contentType
|
|
|
|
}
|
|
|
|
if fs.config.StorageClass != "" {
|
|
|
|
objectWriter.ObjectAttrs.StorageClass = fs.config.StorageClass
|
|
|
|
}
|
|
|
|
if fs.config.ACL != "" {
|
|
|
|
objectWriter.PredefinedACL = fs.config.ACL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 17:14:33 +00:00
|
|
|
func (fs *GCSFs) composeObjects(ctx context.Context, dst, partialObject *storage.ObjectHandle) error {
|
|
|
|
fsLog(fs, logger.LevelDebug, "start object compose for partial file %q, destination %q",
|
|
|
|
partialObject.ObjectName(), dst.ObjectName())
|
|
|
|
composer := dst.ComposerFrom(dst, partialObject)
|
|
|
|
if fs.config.StorageClass != "" {
|
|
|
|
composer.StorageClass = fs.config.StorageClass
|
|
|
|
}
|
|
|
|
if fs.config.ACL != "" {
|
|
|
|
composer.PredefinedACL = fs.config.ACL
|
|
|
|
}
|
|
|
|
contentType := mime.TypeByExtension(path.Ext(dst.ObjectName()))
|
|
|
|
if contentType != "" {
|
|
|
|
composer.ContentType = contentType
|
|
|
|
}
|
|
|
|
_, err := composer.Run(ctx)
|
|
|
|
fsLog(fs, logger.LevelDebug, "object compose for %q finished, err: %v", dst.ObjectName(), err)
|
|
|
|
|
|
|
|
delCtx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
2023-10-22 14:09:30 +00:00
|
|
|
defer cancelFn()
|
|
|
|
|
2023-10-24 17:14:33 +00:00
|
|
|
errDelete := partialObject.Delete(delCtx)
|
|
|
|
metric.GCSDeleteObjectCompleted(errDelete)
|
|
|
|
fsLog(fs, logger.LevelDebug, "deleted partial file %q after composing with %q, err: %v",
|
|
|
|
partialObject.ObjectName(), dst.ObjectName(), errDelete)
|
2023-10-22 14:09:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:33:50 +00:00
|
|
|
func (fs *GCSFs) copyFileInternal(source, target string) error {
|
|
|
|
src := fs.svc.Bucket(fs.config.Bucket).Object(source)
|
|
|
|
dst := fs.svc.Bucket(fs.config.Bucket).Object(target)
|
|
|
|
attrs, statErr := fs.headObject(target)
|
|
|
|
if statErr == nil {
|
|
|
|
dst = dst.If(storage.Conditions{GenerationMatch: attrs.Generation})
|
|
|
|
} else if fs.IsNotExist(statErr) {
|
|
|
|
dst = dst.If(storage.Conditions{DoesNotExist: true})
|
|
|
|
} else {
|
|
|
|
fsLog(fs, logger.LevelWarn, "unable to set precondition for rename, target %q, stat err: %v",
|
|
|
|
target, statErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxLongTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
copier := dst.CopierFrom(src)
|
|
|
|
if fs.config.StorageClass != "" {
|
|
|
|
copier.StorageClass = fs.config.StorageClass
|
|
|
|
}
|
|
|
|
if fs.config.ACL != "" {
|
|
|
|
copier.PredefinedACL = fs.config.ACL
|
|
|
|
}
|
|
|
|
contentType := mime.TypeByExtension(path.Ext(source))
|
|
|
|
if contentType != "" {
|
|
|
|
copier.ContentType = contentType
|
|
|
|
}
|
|
|
|
_, err := copier.Run(ctx)
|
|
|
|
metric.GCSCopyObjectCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
func (fs *GCSFs) renameInternal(source, target string, fi os.FileInfo, recursion int) (int, int64, error) {
|
2023-01-06 11:33:50 +00:00
|
|
|
var numFiles int
|
|
|
|
var filesSize int64
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
if renameMode == 0 {
|
|
|
|
hasContents, err := fs.hasContents(source)
|
|
|
|
if err != nil {
|
|
|
|
return numFiles, filesSize, err
|
|
|
|
}
|
|
|
|
if hasContents {
|
|
|
|
return numFiles, filesSize, fmt.Errorf("cannot rename non empty directory: %q", source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := fs.mkdirInternal(target); err != nil {
|
|
|
|
return numFiles, filesSize, err
|
|
|
|
}
|
|
|
|
if renameMode == 1 {
|
2024-02-15 19:53:56 +00:00
|
|
|
files, size, err := doRecursiveRename(fs, source, target, fs.renameInternal, recursion)
|
|
|
|
numFiles += files
|
|
|
|
filesSize += size
|
2023-01-06 11:33:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return numFiles, filesSize, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := fs.copyFileInternal(source, target); err != nil {
|
|
|
|
return numFiles, filesSize, err
|
|
|
|
}
|
|
|
|
numFiles++
|
|
|
|
filesSize += fi.Size()
|
|
|
|
}
|
|
|
|
err := fs.Remove(source, fi.IsDir())
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
err = nil
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
return numFiles, filesSize, err
|
2021-06-24 17:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 17:14:24 +00:00
|
|
|
func (fs *GCSFs) mkdirInternal(name string) error {
|
|
|
|
if !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
2023-04-06 16:22:09 +00:00
|
|
|
_, w, _, err := fs.Create(name, -1, 0)
|
2022-11-24 17:14:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *GCSFs) hasContents(name string) (bool, error) {
|
|
|
|
result := false
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(name)
|
2020-11-02 18:16:12 +00:00
|
|
|
query := &storage.Query{Prefix: prefix}
|
|
|
|
err := query.SetAttrSelection(gcsDefaultFieldsSelection)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2022-03-04 17:46:17 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
2020-11-02 18:16:12 +00:00
|
|
|
defer cancelFn()
|
2022-03-04 17:46:17 +00:00
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
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
|
2022-03-04 17:46:17 +00:00
|
|
|
pager := iterator.NewPager(it, 2, "")
|
|
|
|
|
|
|
|
var objects []*storage.ObjectAttrs
|
|
|
|
_, err = pager.NextPage(&objects)
|
|
|
|
if err != nil {
|
|
|
|
metric.GCSListObjectsCompleted(err)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attrs := range objects {
|
|
|
|
name, _ := fs.resolve(attrs.Name, prefix, attrs.ContentType)
|
2020-11-02 18:16:12 +00:00
|
|
|
// a dir object with a trailing slash will result in an empty name
|
|
|
|
if name == "/" || name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-03-04 17:46:17 +00:00
|
|
|
metric.GCSListObjectsCompleted(nil)
|
2020-11-02 18:16:12 +00:00
|
|
|
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)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.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
|
|
|
|
2022-05-15 05:30:36 +00:00
|
|
|
// GetAvailableDiskSize returns the available size for the specified path
|
2023-03-22 18:02:54 +00:00
|
|
|
func (*GCSFs) GetAvailableDiskSize(_ string) (*sftp.StatVFS, error) {
|
2021-02-11 18:45:52 +00:00
|
|
|
return nil, ErrStorageSizeUnavailable
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
|
2023-10-24 17:14:33 +00:00
|
|
|
func (*GCSFs) getTempObject(name string) string {
|
|
|
|
dir := filepath.Dir(name)
|
|
|
|
guid := xid.New().String()
|
|
|
|
return filepath.Join(dir, ".sftpgo-partial."+guid+"."+filepath.Base(name))
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
type gcsDirLister struct {
|
|
|
|
baseDirLister
|
|
|
|
bucket *storage.BucketHandle
|
|
|
|
query *storage.Query
|
|
|
|
timeout time.Duration
|
|
|
|
nextPageToken string
|
|
|
|
noMorePages bool
|
|
|
|
prefix string
|
|
|
|
prefixes map[string]bool
|
|
|
|
metricUpdated bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcsDirLister) resolve(name, contentType string) (string, bool) {
|
|
|
|
result := strings.TrimPrefix(name, l.prefix)
|
|
|
|
isDir := strings.HasSuffix(result, "/")
|
|
|
|
if isDir {
|
|
|
|
result = strings.TrimSuffix(result, "/")
|
|
|
|
}
|
|
|
|
if contentType == dirMimeType {
|
|
|
|
isDir = true
|
|
|
|
}
|
|
|
|
return result, isDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcsDirLister) Next(limit int) ([]os.FileInfo, error) {
|
|
|
|
if limit <= 0 {
|
|
|
|
return nil, errInvalidDirListerLimit
|
|
|
|
}
|
|
|
|
if len(l.cache) >= limit {
|
|
|
|
return l.returnFromCache(limit), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.noMorePages {
|
|
|
|
if !l.metricUpdated {
|
|
|
|
l.metricUpdated = true
|
|
|
|
metric.GCSListObjectsCompleted(nil)
|
|
|
|
}
|
|
|
|
return l.returnFromCache(limit), io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(l.timeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
it := l.bucket.Objects(ctx, l.query)
|
|
|
|
paginator := iterator.NewPager(it, defaultGCSPageSize, l.nextPageToken)
|
|
|
|
var objects []*storage.ObjectAttrs
|
|
|
|
|
|
|
|
pageToken, err := paginator.NextPage(&objects)
|
|
|
|
if err != nil {
|
|
|
|
metric.GCSListObjectsCompleted(err)
|
|
|
|
return l.cache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attrs := range objects {
|
|
|
|
if attrs.Prefix != "" {
|
|
|
|
name, _ := l.resolve(attrs.Prefix, attrs.ContentType)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := l.prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.cache = append(l.cache, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
|
|
|
|
l.prefixes[name] = true
|
|
|
|
} else {
|
|
|
|
name, isDir := l.resolve(attrs.Name, attrs.ContentType)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !attrs.Deleted.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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 := l.prefixes[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.prefixes[name] = true
|
|
|
|
}
|
2024-02-17 18:49:34 +00:00
|
|
|
modTime := attrs.Updated
|
|
|
|
if val := getLastModified(attrs.Metadata); val > 0 {
|
|
|
|
modTime = util.GetTimeFromMsecSinceEpoch(val)
|
|
|
|
}
|
|
|
|
l.cache = append(l.cache, NewFileInfo(name, isDir, attrs.Size, modTime, false))
|
2024-02-15 19:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.nextPageToken = pageToken
|
|
|
|
l.noMorePages = (l.nextPageToken == "")
|
|
|
|
|
|
|
|
return l.returnFromCache(limit), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcsDirLister) Close() error {
|
|
|
|
clear(l.prefixes)
|
|
|
|
return l.baseDirLister.Close()
|
|
|
|
}
|