2021-03-31 14:55:22 +03:00
|
|
|
// Copyright © 2021 Vasily Ovchinnikov <vasily@remerge.io>.
|
|
|
|
//
|
|
|
|
// The code in this file is derived from afero fork github.com/Zatte/afero by Mikael Rapp
|
|
|
|
// licensed under Apache License 2.0.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package gcsfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultFileMode = 0755
|
2021-04-12 21:01:12 +03:00
|
|
|
gsPrefix = "gs://"
|
2021-03-31 14:55:22 +03:00
|
|
|
)
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
// Fs is a Fs implementation that uses functions provided by google cloud storage
|
|
|
|
type Fs struct {
|
2021-04-12 21:01:12 +03:00
|
|
|
ctx context.Context
|
|
|
|
client stiface.Client
|
|
|
|
separator string
|
|
|
|
|
|
|
|
buckets map[string]stiface.BucketHandle
|
2021-03-31 14:55:22 +03:00
|
|
|
rawGcsObjects map[string]*GcsFile
|
|
|
|
|
|
|
|
autoRemoveEmptyFolders bool //trigger for creating "virtual folders" (not required by GCSs)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFs(ctx context.Context, client stiface.Client) *Fs {
|
2021-04-12 21:01:12 +03:00
|
|
|
return NewGcsFsWithSeparator(ctx, client, "/")
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFsWithSeparator(ctx context.Context, client stiface.Client, folderSep string) *Fs {
|
|
|
|
return &Fs{
|
2021-03-31 14:55:22 +03:00
|
|
|
ctx: ctx,
|
2021-04-12 21:01:12 +03:00
|
|
|
client: client,
|
2021-03-31 14:55:22 +03:00
|
|
|
separator: folderSep,
|
|
|
|
rawGcsObjects: make(map[string]*GcsFile),
|
|
|
|
|
|
|
|
autoRemoveEmptyFolders: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// normSeparators will normalize all "\\" and "/" to the provided separator
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) normSeparators(s string) string {
|
2021-03-31 14:55:22 +03:00
|
|
|
return strings.Replace(strings.Replace(s, "\\", fs.separator, -1), "/", fs.separator, -1)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) ensureTrailingSeparator(s string) string {
|
2021-03-31 14:55:22 +03:00
|
|
|
if len(s) > 0 && !strings.HasSuffix(s, fs.separator) {
|
|
|
|
return s + fs.separator
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) ensureNoLeadingSeparator(s string) string {
|
2021-03-31 14:55:22 +03:00
|
|
|
if len(s) > 0 && strings.HasPrefix(s, fs.separator) {
|
2021-04-12 21:01:12 +03:00
|
|
|
s = s[len(fs.separator):]
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
2021-04-12 21:01:12 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
func ensureNoPrefix(s string) string {
|
|
|
|
if len(s) > 0 && strings.HasPrefix(s, gsPrefix) {
|
|
|
|
return s[len(gsPrefix):]
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
func validateName(s string) error {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return ErrNoBucketInName
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splits provided name into bucket name and path
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) splitName(name string) (bucketName string, path string) {
|
2021-04-12 21:01:12 +03:00
|
|
|
splitName := strings.Split(name, fs.separator)
|
|
|
|
|
|
|
|
return splitName[0], strings.Join(splitName[1:], fs.separator)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) getBucket(name string) (stiface.BucketHandle, error) {
|
2021-04-12 21:01:12 +03:00
|
|
|
bucket := fs.buckets[name]
|
|
|
|
if bucket == nil {
|
|
|
|
bucket = fs.client.Bucket(name)
|
|
|
|
_, err := bucket.Attrs(fs.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bucket, nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) getObj(name string) (stiface.ObjectHandle, error) {
|
2021-04-12 21:01:12 +03:00
|
|
|
bucketName, path := fs.splitName(name)
|
|
|
|
|
|
|
|
bucket, err := fs.getBucket(bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Object(path), nil
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Name() string { return "GcsFs" }
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Create(name string) (*GcsFile, error) {
|
2021-04-12 21:01:12 +03:00
|
|
|
name = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(name)))
|
|
|
|
if err := validateName(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
if !fs.autoRemoveEmptyFolders {
|
|
|
|
baseDir := filepath.Base(name)
|
|
|
|
if stat, err := fs.Stat(baseDir); err != nil || !stat.IsDir() {
|
|
|
|
err = fs.MkdirAll(baseDir, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
obj, err := fs.getObj(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
w := obj.NewWriter(fs.ctx)
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file := NewGcsFile(fs.ctx, fs, obj, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0, name)
|
|
|
|
|
|
|
|
fs.rawGcsObjects[name] = file
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Mkdir(name string, _ os.FileMode) error {
|
2021-04-12 21:01:12 +03:00
|
|
|
name = fs.ensureNoLeadingSeparator(fs.ensureTrailingSeparator(fs.normSeparators(ensureNoPrefix(name))))
|
|
|
|
if err := validateName(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-05 19:38:36 +03:00
|
|
|
// folder creation logic has to additionally check for folder name presence
|
|
|
|
bucketName, path := fs.splitName(name)
|
|
|
|
if bucketName == "" {
|
|
|
|
return ErrNoBucketInName
|
|
|
|
}
|
|
|
|
if path == "" {
|
|
|
|
// the API would throw "googleapi: Error 400: No object name, required", but this one is more consistent
|
|
|
|
return ErrEmptyObjectName
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
obj, err := fs.getObj(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
w := obj.NewWriter(fs.ctx)
|
|
|
|
return w.Close()
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) MkdirAll(path string, perm os.FileMode) error {
|
2021-04-12 21:01:12 +03:00
|
|
|
path = fs.ensureNoLeadingSeparator(fs.ensureTrailingSeparator(fs.normSeparators(ensureNoPrefix(path))))
|
|
|
|
if err := validateName(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-05 19:38:36 +03:00
|
|
|
// folder creation logic has to additionally check for folder name presence
|
|
|
|
bucketName, splitPath := fs.splitName(path)
|
|
|
|
if bucketName == "" {
|
|
|
|
return ErrNoBucketInName
|
|
|
|
}
|
|
|
|
if splitPath == "" {
|
|
|
|
// the API would throw "googleapi: Error 400: No object name, required", but this one is more consistent
|
|
|
|
return ErrEmptyObjectName
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
root := ""
|
|
|
|
folders := strings.Split(path, fs.separator)
|
|
|
|
for i, f := range folders {
|
|
|
|
if f == "" && i != 0 {
|
|
|
|
continue // it's the last item - it should be empty
|
|
|
|
}
|
|
|
|
//Don't force a delimiter prefix
|
|
|
|
if root != "" {
|
|
|
|
root = root + fs.separator + f
|
|
|
|
} else {
|
2021-08-05 19:38:36 +03:00
|
|
|
// we have to have at least bucket name + folder name to create successfully
|
2021-03-31 14:55:22 +03:00
|
|
|
root = f
|
2021-08-05 19:38:36 +03:00
|
|
|
continue
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := fs.Mkdir(root, perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Open(name string) (*GcsFile, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
return fs.OpenFile(name, os.O_RDONLY, 0)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) OpenFile(name string, flag int, fileMode os.FileMode) (*GcsFile, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
var file *GcsFile
|
|
|
|
var err error
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
name = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(name)))
|
|
|
|
if err = validateName(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
f, found := fs.rawGcsObjects[name]
|
2021-03-31 14:55:22 +03:00
|
|
|
if found {
|
2021-04-12 21:01:12 +03:00
|
|
|
file = NewGcsFileFromOldFH(flag, fileMode, f.resource)
|
2021-03-31 14:55:22 +03:00
|
|
|
} else {
|
2021-04-12 21:01:12 +03:00
|
|
|
var obj stiface.ObjectHandle
|
|
|
|
obj, err = fs.getObj(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file = NewGcsFile(fs.ctx, fs, obj, flag, fileMode, name)
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if flag == os.O_RDONLY {
|
|
|
|
_, err = file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag&os.O_TRUNC != 0 {
|
|
|
|
err = file.resource.obj.Delete(fs.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fs.Create(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag&os.O_APPEND != 0 {
|
|
|
|
_, err = file.Seek(0, 2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag&os.O_CREATE != 0 {
|
|
|
|
_, err = file.Stat()
|
|
|
|
if err == nil { // the file actually exists
|
|
|
|
return nil, syscall.EPERM
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.WriteString("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Remove(name string) error {
|
2021-04-12 21:01:12 +03:00
|
|
|
name = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(name)))
|
|
|
|
if err := validateName(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
obj, err := fs.getObj(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
info, err := fs.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(fs.rawGcsObjects, name)
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
// it's a folder, we ha to check its contents - it cannot be removed, if not empty
|
|
|
|
var dir *GcsFile
|
|
|
|
dir, err = fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var infos []os.FileInfo
|
|
|
|
infos, err = dir.Readdir(0)
|
|
|
|
if len(infos) > 0 {
|
|
|
|
return syscall.ENOTEMPTY
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's an empty folder, we can continue
|
|
|
|
name = fs.ensureTrailingSeparator(name)
|
2021-04-12 21:01:12 +03:00
|
|
|
obj, err = fs.getObj(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
return obj.Delete(fs.ctx)
|
|
|
|
}
|
|
|
|
return obj.Delete(fs.ctx)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) RemoveAll(path string) error {
|
2021-04-12 21:01:12 +03:00
|
|
|
path = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(path)))
|
|
|
|
if err := validateName(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
pathInfo, err := fs.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !pathInfo.IsDir() {
|
|
|
|
return fs.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir *GcsFile
|
|
|
|
dir, err = fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []os.FileInfo
|
|
|
|
infos, err = dir.Readdir(0)
|
|
|
|
for _, info := range infos {
|
2021-04-12 21:01:12 +03:00
|
|
|
nameToRemove := fs.normSeparators(info.Name())
|
|
|
|
err = fs.RemoveAll(path + fs.separator + nameToRemove)
|
2021-03-31 14:55:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.Remove(path)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Rename(oldName, newName string) error {
|
2021-04-12 21:01:12 +03:00
|
|
|
oldName = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(oldName)))
|
|
|
|
if err := validateName(oldName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
newName = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(newName)))
|
|
|
|
if err := validateName(newName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
src, err := fs.getObj(oldName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dst, err := fs.getObj(newName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = dst.CopierFrom(src).Run(fs.ctx); err != nil {
|
2021-03-31 14:55:22 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(fs.rawGcsObjects, oldName)
|
|
|
|
return src.Delete(fs.ctx)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Stat(name string) (os.FileInfo, error) {
|
2021-04-12 21:01:12 +03:00
|
|
|
name = fs.ensureNoLeadingSeparator(fs.normSeparators(ensureNoPrefix(name)))
|
|
|
|
if err := validateName(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
return newFileInfo(name, fs, defaultFileMode)
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Chmod(_ string, _ os.FileMode) error {
|
2021-03-31 14:55:22 +03:00
|
|
|
return errors.New("method Chmod is not implemented in GCS")
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Chtimes(_ string, _, _ time.Time) error {
|
2021-03-31 14:55:22 +03:00
|
|
|
return errors.New("method Chtimes is not implemented. Create, Delete, Updated times are read only fields in GCS and set implicitly")
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *Fs) Chown(_ string, _, _ int) error {
|
2021-03-31 14:55:22 +03:00
|
|
|
return errors.New("method Chown is not implemented for GCS")
|
|
|
|
}
|