2022-07-18 11:43:25 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
//go:build !noazblob
|
2020-10-25 07:18:48 +00:00
|
|
|
// +build !noazblob
|
|
|
|
|
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
2020-10-25 16:41:04 +00:00
|
|
|
"bytes"
|
2020-10-25 07:18:48 +00:00
|
|
|
"context"
|
2020-10-25 16:41:04 +00:00
|
|
|
"encoding/base64"
|
2020-10-25 07:18:48 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-10-25 16:41:04 +00:00
|
|
|
"sync"
|
2022-06-30 16:25:40 +00:00
|
|
|
"sync/atomic"
|
2020-10-25 07:18:48 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-26 10:47:12 +00:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
2022-02-20 13:43:24 +00:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
2020-10-25 07:18:48 +00:00
|
|
|
"github.com/eikenb/pipeat"
|
2022-10-07 05:38:12 +00:00
|
|
|
"github.com/google/uuid"
|
2021-02-11 18:45:52 +00:00
|
|
|
"github.com/pkg/sftp"
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/metric"
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/plugin"
|
2021-12-16 17:18:36 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
2020-10-25 07:18:48 +00:00
|
|
|
)
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
const (
|
|
|
|
azureDefaultEndpoint = "blob.core.windows.net"
|
|
|
|
)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
|
|
|
// AzureBlobFs is a Fs implementation for Azure Blob storage.
|
|
|
|
type AzureBlobFs 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
|
2022-02-20 13:43:24 +00:00
|
|
|
mountPath string
|
|
|
|
config *AzBlobFsConfig
|
|
|
|
hasContainerAccess bool
|
2022-04-25 15:34:52 +00:00
|
|
|
containerClient *azblob.ContainerClient
|
2022-02-20 13:43:24 +00:00
|
|
|
ctxTimeout time.Duration
|
|
|
|
ctxLongTimeout time.Duration
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
version.AddFeature("+azblob")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAzBlobFs returns an AzBlobFs object that allows to interact with Azure Blob storage
|
2021-03-21 18:15:47 +00:00
|
|
|
func NewAzBlobFs(connectionID, localTempDir, mountPath string, config AzBlobFsConfig) (Fs, error) {
|
|
|
|
if localTempDir == "" {
|
2021-05-27 13:38:27 +00:00
|
|
|
if tempPath != "" {
|
|
|
|
localTempDir = tempPath
|
|
|
|
} else {
|
|
|
|
localTempDir = filepath.Clean(os.TempDir())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
fs := &AzureBlobFs{
|
2020-10-25 07:18:48 +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-10-25 07:18:48 +00:00
|
|
|
ctxTimeout: 30 * time.Second,
|
2022-02-20 13:43:24 +00:00
|
|
|
ctxLongTimeout: 90 * time.Second,
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-04-28 10:55:01 +00:00
|
|
|
if err := fs.config.validate(); err != nil {
|
2020-10-25 07:18:48 +00:00
|
|
|
return fs, err
|
|
|
|
}
|
2021-03-27 18:10:27 +00:00
|
|
|
|
2022-04-02 16:32:46 +00:00
|
|
|
if err := fs.config.tryDecrypt(); err != nil {
|
2021-06-11 20:27:36 +00:00
|
|
|
return fs, err
|
|
|
|
}
|
2022-04-02 16:32:46 +00:00
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
fs.setConfigDefaults()
|
|
|
|
|
|
|
|
version := version.Get()
|
2022-02-20 13:43:24 +00:00
|
|
|
clientOptions := &azblob.ClientOptions{
|
|
|
|
Telemetry: policy.TelemetryOptions{
|
|
|
|
ApplicationID: fmt.Sprintf("SFTPGo-%v_%v", version.Version, version.CommitHash),
|
|
|
|
},
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-06-11 20:27:36 +00:00
|
|
|
if fs.config.SASURL.GetPayload() != "" {
|
2022-04-25 15:34:52 +00:00
|
|
|
parts, err := azblob.NewBlobURLParts(fs.config.SASURL.GetPayload())
|
|
|
|
if err != nil {
|
2022-04-02 16:32:46 +00:00
|
|
|
return fs, fmt.Errorf("invalid SAS URL: %w", err)
|
|
|
|
}
|
2022-08-01 19:52:32 +00:00
|
|
|
svc, err := azblob.NewServiceClientWithNoCredential(fs.config.SASURL.GetPayload(), clientOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fs, fmt.Errorf("invalid credentials: %v", err)
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if parts.ContainerName != "" {
|
|
|
|
if fs.config.Container != "" && fs.config.Container != parts.ContainerName {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fs, fmt.Errorf("container name in SAS URL %#v and container provided %#v do not match",
|
2020-10-25 07:18:48 +00:00
|
|
|
parts.ContainerName, fs.config.Container)
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
fs.config.Container = parts.ContainerName
|
2022-08-01 19:52:32 +00:00
|
|
|
fs.containerClient, err = svc.NewContainerClient("")
|
2020-10-25 07:18:48 +00:00
|
|
|
} else {
|
|
|
|
if fs.config.Container == "" {
|
|
|
|
return fs, errors.New("container is required with this SAS URL")
|
|
|
|
}
|
2022-08-01 19:52:32 +00:00
|
|
|
fs.containerClient, err = svc.NewContainerClient(fs.config.Container)
|
2022-02-20 13:43:24 +00:00
|
|
|
}
|
|
|
|
fs.hasContainerAccess = false
|
2022-04-25 15:34:52 +00:00
|
|
|
return fs, err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
credential, err := azblob.NewSharedKeyCredential(fs.config.AccountName, fs.config.AccountKey.GetPayload())
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fs, fmt.Errorf("invalid credentials: %v", err)
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
var endpoint string
|
2020-10-25 07:18:48 +00:00
|
|
|
if fs.config.UseEmulator {
|
2022-02-20 13:43:24 +00:00
|
|
|
endpoint = fmt.Sprintf("%s/%s", fs.config.Endpoint, fs.config.AccountName)
|
2020-10-25 07:18:48 +00:00
|
|
|
} else {
|
2022-02-20 13:43:24 +00:00
|
|
|
endpoint = fmt.Sprintf("https://%s.%s/", fs.config.AccountName, fs.config.Endpoint)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
svc, err := azblob.NewServiceClientWithSharedKey(endpoint, credential, clientOptions)
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fs, fmt.Errorf("invalid credentials: %v", err)
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
fs.hasContainerAccess = true
|
2022-04-25 15:34:52 +00:00
|
|
|
fs.containerClient, err = svc.NewContainerClient(fs.config.Container)
|
|
|
|
return fs, err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for the Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) Name() string {
|
2021-06-11 20:27:36 +00:00
|
|
|
if !fs.config.SASURL.IsEmpty() {
|
|
|
|
return fmt.Sprintf("Azure Blob with SAS URL, container %#v", fs.config.Container)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("Azure Blob container %#v", fs.config.Container)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionID returns the connection ID associated to this Fs implementation
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) ConnectionID() string {
|
2020-10-25 07:18:48 +00:00
|
|
|
return fs.connectionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) Stat(name string) (os.FileInfo, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
if name == "" || name == "." {
|
2022-02-20 13:43:24 +00:00
|
|
|
if fs.hasContainerAccess {
|
2020-11-05 14:03:35 +00:00
|
|
|
err := fs.checkIfBucketExists()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Now(), false))
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
if fs.config.KeyPrefix == name+"/" {
|
2021-12-16 17:18:36 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Now(), false))
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
attrs, err := fs.headObject(name)
|
|
|
|
if err == nil {
|
2022-02-20 13:43:24 +00:00
|
|
|
contentType := util.GetStringFromPointer(attrs.ContentType)
|
|
|
|
isDir := contentType == dirMimeType
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZListObjectsCompleted(nil)
|
2022-02-20 13:43:24 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, isDir,
|
|
|
|
util.GetIntFromPointer(attrs.ContentLength),
|
|
|
|
util.GetTimeFromPointer(attrs.LastModified), false))
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// now check if this is a prefix (virtual directory)
|
|
|
|
hasContents, err := fs.hasContents(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasContents {
|
2021-12-16 17:18:36 +00:00
|
|
|
return updateFileInfoModTime(fs.getStorageID(), name, NewFileInfo(name, true, 0, time.Now(), false))
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-03-26 10:47:12 +00:00
|
|
|
return nil, os.ErrNotExist
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns a FileInfo describing the named file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) Lstat(name string) (os.FileInfo, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
return fs.Stat(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens the named file for reading
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *AzureBlobFs) Open(name string, offset int64) (File, *pipeat.PipeReaderAt, func(), error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2022-04-25 15:34:52 +00:00
|
|
|
blockBlob, err := fs.containerClient.NewBlockBlobClient(name)
|
|
|
|
if err != nil {
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
2022-02-21 18:01:31 +00:00
|
|
|
|
|
|
|
err := fs.handleMultipartDownload(ctx, blockBlob, offset, w)
|
2020-10-25 07:18:48 +00:00
|
|
|
w.CloseWithError(err) //nolint:errcheck
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "download completed, path: %#v size: %v, err: %+v", name, w.GetWrittenBytes(), err)
|
2022-02-21 18:01:31 +00:00
|
|
|
metric.AZTransferCompleted(w.GetWrittenBytes(), 1, err)
|
2020-10-25 07:18:48 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil, r, cancelFn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates or opens the named file for writing
|
2020-11-17 18:36:39 +00:00
|
|
|
func (fs *AzureBlobFs) Create(name string, flag int) (File, *PipeWriter, func(), error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
r, w, err := pipeat.PipeInDir(fs.localTempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2022-04-25 15:34:52 +00:00
|
|
|
blockBlob, err := fs.containerClient.NewBlockBlobClient(name)
|
|
|
|
if err != nil {
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
p := NewPipeWriter(w)
|
2020-10-25 07:18:48 +00:00
|
|
|
headers := azblob.BlobHTTPHeaders{}
|
|
|
|
var contentType string
|
|
|
|
if flag == -1 {
|
|
|
|
contentType = dirMimeType
|
|
|
|
} else {
|
|
|
|
contentType = mime.TypeByExtension(path.Ext(name))
|
|
|
|
}
|
|
|
|
if contentType != "" {
|
2022-02-20 13:43:24 +00:00
|
|
|
headers.BlobContentType = &contentType
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
err := fs.handleMultipartUpload(ctx, r, blockBlob, &headers)
|
2020-10-25 07:18:48 +00:00
|
|
|
r.CloseWithError(err) //nolint:errcheck
|
|
|
|
p.Done(err)
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "upload completed, path: %#v, readed bytes: %v, err: %+v", name, r.GetReadedBytes(), err)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZTransferCompleted(r.GetReadedBytes(), 0, err)
|
2020-10-25 07:18:48 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil, p, cancelFn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 StartCopyFromURL call.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) Rename(source, target string) error {
|
2020-10-25 07:18:48 +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-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("cannot rename non empty directory: %#v", source)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 15:34:52 +00:00
|
|
|
dstBlob, err := fs.containerClient.NewBlockBlobClient(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcBlob, err := fs.containerClient.NewBlockBlobClient(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxLongTimeout))
|
2020-10-25 07:18:48 +00:00
|
|
|
defer cancelFn()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
resp, err := dstBlob.StartCopyFromURL(ctx, srcBlob.URL(), fs.getCopyOptions())
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZCopyObjectCompleted(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
copyStatus := azblob.CopyStatusType(util.GetStringFromPointer((*string)(resp.CopyStatus)))
|
2020-10-25 07:18:48 +00:00
|
|
|
nErrors := 0
|
2022-02-20 13:43:24 +00:00
|
|
|
for copyStatus == azblob.CopyStatusTypePending {
|
2020-10-25 07:18:48 +00:00
|
|
|
// Poll until the copy is complete.
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2022-04-25 15:34:52 +00:00
|
|
|
resp, err := dstBlob.GetProperties(ctx, &azblob.BlobGetPropertiesOptions{
|
2022-02-20 13:43:24 +00:00
|
|
|
BlobAccessConditions: &azblob.BlobAccessConditions{},
|
|
|
|
})
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
// A GetProperties failure may be transient, so allow a couple
|
|
|
|
// of them before giving up.
|
|
|
|
nErrors++
|
|
|
|
if ctx.Err() != nil || nErrors == 3 {
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZCopyObjectCompleted(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-20 13:43:24 +00:00
|
|
|
copyStatus = azblob.CopyStatusType(util.GetStringFromPointer((*string)(resp.CopyStatus)))
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
if copyStatus != azblob.CopyStatusTypeSuccess {
|
2021-03-21 18:15:47 +00:00
|
|
|
err := fmt.Errorf("copy failed with status: %s", copyStatus)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZCopyObjectCompleted(err)
|
2020-10-26 18:01:17 +00:00
|
|
|
return err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZCopyObjectCompleted(nil)
|
2022-04-25 15:34:52 +00:00
|
|
|
fs.preserveModificationTime(source, target, fi)
|
2020-10-25 07:18:48 +00:00
|
|
|
return fs.Remove(source, fi.IsDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or (empty) directory.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) Remove(name string, isDir bool) error {
|
2020-10-25 07:18:48 +00:00
|
|
|
if isDir {
|
2020-11-02 18:16:12 +00:00
|
|
|
hasContents, err := fs.hasContents(name)
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 18:16:12 +00:00
|
|
|
if hasContents {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("cannot remove non empty directory: %#v", name)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 15:34:52 +00:00
|
|
|
blobBlock, err := fs.containerClient.NewBlockBlobClient(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
_, err = blobBlock.Delete(ctx, &azblob.BlobDeleteOptions{
|
2022-02-20 13:43:24 +00:00
|
|
|
DeleteSnapshots: azblob.DeleteSnapshotsOptionTypeInclude.ToPtr(),
|
|
|
|
})
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZDeleteObjectCompleted(err)
|
2021-12-16 17:18:36 +00:00
|
|
|
if plugin.Handler.HasMetadater() && err == nil && !isDir {
|
|
|
|
if errMetadata := plugin.Handler.RemoveMetadata(fs.getStorageID(), ensureAbsPath(name)); errMetadata != nil {
|
2022-03-15 18:16:50 +00:00
|
|
|
fsLog(fs, logger.LevelWarn, "unable to remove metadata for path %#v: %+v", name, errMetadata)
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:18:48 +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 *AzureBlobFs) Mkdir(name string) error {
|
2020-10-25 07:18:48 +00:00
|
|
|
_, err := fs.Stat(name)
|
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, w, _, err := fs.Create(name, -1)
|
|
|
|
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 (*AzureBlobFs) Symlink(source, target string) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readlink returns the destination of the named symbolic link
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) Readlink(name string) (string, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return "", ErrVfsUnsupported
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) Chown(name string, uid int, gid int) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the named file to mode.
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) Chmod(name string, mode os.FileMode) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named file.
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *AzureBlobFs) Chtimes(name string, atime, mtime time.Time, isUploading bool) error {
|
|
|
|
if !plugin.Handler.HasMetadater() {
|
|
|
|
return ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
if !isUploading {
|
|
|
|
info, err := fs.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugin.Handler.SetModificationTime(fs.getStorageID(), ensureAbsPath(name),
|
|
|
|
util.GetTimeAsMsSinceEpoch(mtime))
|
2020-10-25 07:18:48 +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 (*AzureBlobFs) Truncate(name string, size int64) error {
|
2020-11-12 09:39:46 +00:00
|
|
|
return ErrVfsUnsupported
|
2020-10-25 07:18:48 +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 *AzureBlobFs) ReadDir(dirname string) ([]os.FileInfo, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
var result []os.FileInfo
|
|
|
|
// dirname must be already cleaned
|
2021-12-16 17:18:36 +00:00
|
|
|
prefix := fs.getPrefix(dirname)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
modTimes, err := getFolderModTimes(fs.getStorageID(), dirname)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
prefixes := make(map[string]bool)
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
pager := fs.containerClient.ListBlobsHierarchy("/", &azblob.ContainerListBlobsHierarchyOptions{
|
2022-02-21 18:01:31 +00:00
|
|
|
Include: []azblob.ListBlobsIncludeItem{},
|
|
|
|
Prefix: &prefix,
|
2022-02-20 13:43:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
hasNext := true
|
|
|
|
for hasNext {
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
if hasNext = pager.NextPage(ctx); hasNext {
|
|
|
|
resp := pager.PageResponse()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
for _, blobPrefix := range resp.ListBlobsHierarchySegmentResponse.Segment.BlobPrefixes {
|
2022-02-20 13:43:24 +00:00
|
|
|
name := util.GetStringFromPointer(blobPrefix.Name)
|
|
|
|
// we don't support prefixes == "/" this will be sent if a key starts with "/"
|
|
|
|
if name == "" || name == "/" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// sometime we have duplicate prefixes, maybe an Azurite bug
|
|
|
|
name = strings.TrimPrefix(name, prefix)
|
|
|
|
if _, ok := prefixes[strings.TrimSuffix(name, "/")]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, NewFileInfo(name, true, 0, time.Now(), false))
|
|
|
|
prefixes[strings.TrimSuffix(name, "/")] = true
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
for _, blobItem := range resp.ListBlobsHierarchySegmentResponse.Segment.BlobItems {
|
2022-02-20 13:43:24 +00:00
|
|
|
name := util.GetStringFromPointer(blobItem.Name)
|
|
|
|
name = strings.TrimPrefix(name, prefix)
|
|
|
|
size := int64(0)
|
|
|
|
isDir := false
|
|
|
|
modTime := time.Now()
|
|
|
|
if blobItem.Properties != nil {
|
|
|
|
size = util.GetIntFromPointer(blobItem.Properties.ContentLength)
|
|
|
|
modTime = util.GetTimeFromPointer(blobItem.Properties.LastModified)
|
|
|
|
contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
|
|
|
|
isDir = (contentType == dirMimeType)
|
|
|
|
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-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
if t, ok := modTimes[name]; ok {
|
|
|
|
modTime = util.GetTimeFromMsecSinceEpoch(t)
|
|
|
|
}
|
|
|
|
result = append(result, NewFileInfo(name, isDir, size, modTime, false))
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
err = pager.Err()
|
|
|
|
metric.AZListObjectsCompleted(err)
|
|
|
|
|
|
|
|
return result, err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 14:00:55 +00:00
|
|
|
// IsUploadResumeSupported returns true if resuming uploads is supported.
|
|
|
|
// Resuming uploads is not supported on Azure Blob
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) IsUploadResumeSupported() bool {
|
2020-10-25 07:18:48 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAtomicUploadSupported returns true if atomic upload is supported.
|
|
|
|
// Azure Blob uploads are already atomic, we don't need to upload to a temporary
|
|
|
|
// file
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) IsAtomicUploadSupported() bool {
|
2020-10-25 07:18:48 +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 (*AzureBlobFs) IsNotExist(err error) bool {
|
2020-10-25 07:18:48 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-26 10:47:12 +00:00
|
|
|
var errStorage *azblob.StorageError
|
|
|
|
if errors.As(err, &errStorage) {
|
|
|
|
return errStorage.StatusCode() == http.StatusNotFound
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 10:47:12 +00:00
|
|
|
var errResp *azcore.ResponseError
|
|
|
|
if errors.As(err, &errResp) {
|
|
|
|
return errResp.StatusCode == http.StatusNotFound
|
|
|
|
}
|
|
|
|
// os.ErrNotExist can be returned internally by fs.Stat
|
|
|
|
return errors.Is(err, os.ErrNotExist)
|
2020-10-25 07:18:48 +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 (*AzureBlobFs) IsPermission(err error) bool {
|
2020-10-25 07:18:48 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-26 10:47:12 +00:00
|
|
|
var errStorage *azblob.StorageError
|
|
|
|
if errors.As(err, &errStorage) {
|
|
|
|
statusCode := errStorage.StatusCode()
|
2022-03-15 18:16:50 +00:00
|
|
|
return statusCode == http.StatusForbidden || statusCode == http.StatusUnauthorized
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
|
2022-03-26 10:47:12 +00:00
|
|
|
var errResp *azcore.ResponseError
|
|
|
|
if errors.As(err, &errResp) {
|
|
|
|
return errResp.StatusCode == http.StatusForbidden || errResp.StatusCode == http.StatusUnauthorized
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:16:50 +00:00
|
|
|
return false
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:39:46 +00:00
|
|
|
// IsNotSupported returns true if the error indicate an unsupported operation
|
|
|
|
func (*AzureBlobFs) IsNotSupported(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return err == ErrVfsUnsupported
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
// CheckRootPath creates the specified local root directory if it does not exists
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) CheckRootPath(username string, uid int, gid int) bool {
|
2020-10-25 07:18:48 +00:00
|
|
|
// we need a local directory for temporary files
|
2021-03-21 18:15:47 +00:00
|
|
|
osFs := NewOsFs(fs.ConnectionID(), fs.localTempDir, "")
|
2020-10-25 07:18:48 +00:00
|
|
|
return osFs.CheckRootPath(username, uid, gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanRootDirContents returns the number of files contained in the bucket,
|
|
|
|
// and their size
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) ScanRootDirContents() (int, int64, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
numFiles := 0
|
|
|
|
size := int64(0)
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
pager := fs.containerClient.ListBlobsFlat(&azblob.ContainerListBlobsFlatOptions{
|
|
|
|
Prefix: &fs.config.KeyPrefix,
|
2022-02-20 13:43:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
hasNext := true
|
|
|
|
for hasNext {
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
if hasNext = pager.NextPage(ctx); hasNext {
|
|
|
|
resp := pager.PageResponse()
|
2022-04-25 15:34:52 +00:00
|
|
|
for _, blobItem := range resp.ListBlobsFlatSegmentResponse.Segment.BlobItems {
|
2022-02-20 13:43:24 +00:00
|
|
|
if blobItem.Properties != nil {
|
|
|
|
contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
|
|
|
|
isDir := (contentType == dirMimeType)
|
|
|
|
blobSize := util.GetIntFromPointer(blobItem.Properties.ContentLength)
|
|
|
|
if isDir && blobSize == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numFiles++
|
|
|
|
size += blobSize
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
err := pager.Err()
|
|
|
|
metric.AZListObjectsCompleted(err)
|
|
|
|
|
|
|
|
return numFiles, size, err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *AzureBlobFs) getFileNamesInPrefix(fsPrefix string) (map[string]bool, error) {
|
|
|
|
fileNames := make(map[string]bool)
|
|
|
|
prefix := ""
|
|
|
|
if fsPrefix != "/" {
|
|
|
|
prefix = strings.TrimPrefix(fsPrefix, "/")
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
pager := fs.containerClient.ListBlobsHierarchy("/", &azblob.ContainerListBlobsHierarchyOptions{
|
2022-02-21 18:01:31 +00:00
|
|
|
Include: []azblob.ListBlobsIncludeItem{},
|
|
|
|
Prefix: &prefix,
|
2022-02-20 13:43:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
hasNext := true
|
|
|
|
for hasNext {
|
2021-12-16 17:18:36 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
if hasNext = pager.NextPage(ctx); hasNext {
|
|
|
|
resp := pager.PageResponse()
|
2022-04-25 15:34:52 +00:00
|
|
|
for _, blobItem := range resp.ListBlobsHierarchySegmentResponse.Segment.BlobItems {
|
2022-02-20 13:43:24 +00:00
|
|
|
name := util.GetStringFromPointer(blobItem.Name)
|
|
|
|
name = strings.TrimPrefix(name, prefix)
|
|
|
|
if blobItem.Properties != nil {
|
|
|
|
contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
|
|
|
|
isDir := (contentType == dirMimeType)
|
|
|
|
if isDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fileNames[name] = true
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
err := pager.Err()
|
|
|
|
metric.AZListObjectsCompleted(err)
|
|
|
|
|
|
|
|
return fileNames, err
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckMetadata checks the metadata consistency
|
|
|
|
func (fs *AzureBlobFs) CheckMetadata() error {
|
|
|
|
return fsMetadataCheck(fs, fs.getStorageID(), fs.config.KeyPrefix)
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +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 (*AzureBlobFs) GetDirSize(dirname string) (int, int64, error) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return 0, 0, ErrVfsUnsupported
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAtomicUploadPath returns the path to use for an atomic upload.
|
|
|
|
// Azure Blob Storage uploads are already atomic, we never call this method
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) GetAtomicUploadPath(name string) string {
|
2020-10-25 07:18:48 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRelativePath returns the path for a file relative to the user's home dir.
|
|
|
|
// This is the path as seen by SFTPGo users
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) GetRelativePath(name string) string {
|
2020-10-25 07:18:48 +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-10-25 07:18:48 +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-10-25 07:18:48 +00:00
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 *AzureBlobFs) Walk(root string, walkFn filepath.WalkFunc) error {
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(root)
|
2022-04-25 15:34:52 +00:00
|
|
|
pager := fs.containerClient.ListBlobsFlat(&azblob.ContainerListBlobsFlatOptions{
|
|
|
|
Prefix: &prefix,
|
2022-02-20 13:43:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
hasNext := true
|
|
|
|
for hasNext {
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
if hasNext = pager.NextPage(ctx); hasNext {
|
|
|
|
resp := pager.PageResponse()
|
2022-04-25 15:34:52 +00:00
|
|
|
for _, blobItem := range resp.ListBlobsFlatSegmentResponse.Segment.BlobItems {
|
2022-02-20 13:43:24 +00:00
|
|
|
name := util.GetStringFromPointer(blobItem.Name)
|
|
|
|
if fs.isEqual(name, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blobSize := int64(0)
|
|
|
|
lastModified := time.Now()
|
|
|
|
isDir := false
|
|
|
|
if blobItem.Properties != nil {
|
|
|
|
contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
|
|
|
|
isDir = (contentType == dirMimeType)
|
|
|
|
blobSize = util.GetIntFromPointer(blobItem.Properties.ContentLength)
|
|
|
|
lastModified = util.GetTimeFromPointer(blobItem.Properties.LastModified)
|
|
|
|
}
|
|
|
|
err := walkFn(name, NewFileInfo(name, isDir, blobSize, lastModified, false), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
err := pager.Err()
|
|
|
|
if err != nil {
|
|
|
|
metric.AZListObjectsCompleted(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZListObjectsCompleted(nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
return walkFn(root, NewFileInfo(root, true, 0, time.Now(), false), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join joins any number of path elements into a single path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) Join(elem ...string) string {
|
2020-10-25 07:18:48 +00:00
|
|
|
return strings.TrimPrefix(path.Join(elem...), "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasVirtualFolders returns true if folders are emulated
|
2020-11-02 18:16:12 +00:00
|
|
|
func (*AzureBlobFs) HasVirtualFolders() bool {
|
2020-10-25 07:18:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvePath returns the matching filesystem path for the specified sftp path
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) 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)
|
|
|
|
}
|
|
|
|
return fs.Join(fs.config.KeyPrefix, strings.TrimPrefix(virtualPath, "/")), nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
func (fs *AzureBlobFs) headObject(name string) (azblob.BlobGetPropertiesResponse, error) {
|
2020-10-25 07:18:48 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
blobClient, err := fs.containerClient.NewBlockBlobClient(name)
|
|
|
|
if err != nil {
|
|
|
|
return azblob.BlobGetPropertiesResponse{}, err
|
|
|
|
}
|
|
|
|
resp, err := blobClient.GetProperties(ctx, &azblob.BlobGetPropertiesOptions{
|
2022-02-20 13:43:24 +00:00
|
|
|
BlobAccessConditions: &azblob.BlobAccessConditions{},
|
|
|
|
})
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZHeadObjectCompleted(err)
|
2022-02-20 13:43:24 +00:00
|
|
|
return resp, err
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
// GetMimeType returns the content type
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) GetMimeType(name string) (string, error) {
|
|
|
|
response, err := fs.headObject(name)
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
return util.GetStringFromPointer(response.ContentType), nil
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
// Close closes the fs
|
|
|
|
func (*AzureBlobFs) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-15 05:30:36 +00:00
|
|
|
// GetAvailableDiskSize returns the available size for the specified path
|
2021-02-11 18:45:52 +00:00
|
|
|
func (*AzureBlobFs) GetAvailableDiskSize(dirName string) (*sftp.StatVFS, error) {
|
|
|
|
return nil, ErrStorageSizeUnavailable
|
2020-12-25 10:14:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (*AzureBlobFs) getPrefix(name string) string {
|
|
|
|
prefix := ""
|
|
|
|
if name != "" && name != "." {
|
|
|
|
prefix = strings.TrimPrefix(name, "/")
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
func (fs *AzureBlobFs) isEqual(key string, virtualName string) bool {
|
|
|
|
if key == virtualName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if key == virtualName+"/" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if key+"/" == virtualName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) setConfigDefaults() {
|
|
|
|
if fs.config.Endpoint == "" {
|
|
|
|
fs.config.Endpoint = azureDefaultEndpoint
|
|
|
|
}
|
|
|
|
if fs.config.UploadPartSize == 0 {
|
2022-02-21 18:01:31 +00:00
|
|
|
fs.config.UploadPartSize = 5
|
|
|
|
}
|
|
|
|
if fs.config.UploadPartSize < 1024*1024 {
|
|
|
|
fs.config.UploadPartSize *= 1024 * 1024
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
if fs.config.UploadConcurrency == 0 {
|
2022-02-21 18:01:31 +00:00
|
|
|
fs.config.UploadConcurrency = 5
|
|
|
|
}
|
|
|
|
if fs.config.DownloadPartSize == 0 {
|
|
|
|
fs.config.DownloadPartSize = 5
|
|
|
|
}
|
|
|
|
if fs.config.DownloadPartSize < 1024*1024 {
|
|
|
|
fs.config.DownloadPartSize *= 1024 * 1024
|
|
|
|
}
|
|
|
|
if fs.config.DownloadConcurrency == 0 {
|
|
|
|
fs.config.DownloadConcurrency = 5
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
func (fs *AzureBlobFs) checkIfBucketExists() error {
|
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
_, err := fs.containerClient.GetProperties(ctx, &azblob.ContainerGetPropertiesOptions{})
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AZHeadContainerCompleted(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
func (fs *AzureBlobFs) hasContents(name string) (bool, error) {
|
|
|
|
result := false
|
2022-03-15 18:16:50 +00:00
|
|
|
prefix := fs.getPrefix(name)
|
2022-02-20 13:43:24 +00:00
|
|
|
|
|
|
|
maxResults := int32(1)
|
2022-04-25 15:34:52 +00:00
|
|
|
pager := fs.containerClient.ListBlobsFlat(&azblob.ContainerListBlobsFlatOptions{
|
|
|
|
MaxResults: &maxResults,
|
2022-02-20 13:43:24 +00:00
|
|
|
Prefix: &prefix,
|
|
|
|
})
|
|
|
|
|
2020-11-02 18:16:12 +00:00
|
|
|
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-02-21 18:01:31 +00:00
|
|
|
if pager.NextPage(ctx) {
|
2022-02-20 13:43:24 +00:00
|
|
|
resp := pager.PageResponse()
|
2022-04-25 15:34:52 +00:00
|
|
|
result = len(resp.ListBlobsFlatSegmentResponse.Segment.BlobItems) > 0
|
2020-11-02 18:16:12 +00:00
|
|
|
}
|
2022-02-20 13:43:24 +00:00
|
|
|
|
|
|
|
err := pager.Err()
|
|
|
|
metric.AZListObjectsCompleted(err)
|
2020-11-02 18:16:12 +00:00
|
|
|
return result, err
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2020-10-25 16:41:04 +00:00
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
func (fs *AzureBlobFs) downloadPart(ctx context.Context, blockBlob *azblob.BlockBlobClient, buf []byte,
|
2022-02-21 18:01:31 +00:00
|
|
|
w io.WriterAt, offset, count, writeOffset int64,
|
|
|
|
) error {
|
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-25 15:34:52 +00:00
|
|
|
resp, err := blockBlob.Download(ctx, &azblob.BlobDownloadOptions{
|
2022-02-21 18:01:31 +00:00
|
|
|
Offset: &offset,
|
|
|
|
Count: &count,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
body := resp.Body(&azblob.RetryReaderOptions{MaxRetryRequests: 2})
|
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
_, err = io.ReadAtLeast(body, buf, int(count))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fs.writeAtFull(w, buf, writeOffset, int(count))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
func (fs *AzureBlobFs) handleMultipartDownload(ctx context.Context, blockBlob *azblob.BlockBlobClient,
|
2022-02-21 18:01:31 +00:00
|
|
|
offset int64, writer io.WriterAt,
|
|
|
|
) error {
|
2022-04-25 15:34:52 +00:00
|
|
|
props, err := blockBlob.GetProperties(ctx, &azblob.BlobGetPropertiesOptions{
|
2022-02-21 18:01:31 +00:00
|
|
|
BlobAccessConditions: &azblob.BlobAccessConditions{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fsLog(fs, logger.LevelError, "unable to get blob properties, download aborted: %+v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
contentLength := util.GetIntFromPointer(props.ContentLength)
|
|
|
|
sizeToDownload := contentLength - offset
|
|
|
|
if sizeToDownload < 0 {
|
|
|
|
fsLog(fs, logger.LevelError, "invalid multipart download size or offset, size: %v, offset: %v, size to download: %v",
|
|
|
|
contentLength, offset, sizeToDownload)
|
|
|
|
return errors.New("the requested offset exceeds the file size")
|
|
|
|
}
|
|
|
|
if sizeToDownload == 0 {
|
|
|
|
fsLog(fs, logger.LevelDebug, "nothing to download, offset %v, content length %v", offset, contentLength)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
partSize := fs.config.DownloadPartSize
|
|
|
|
guard := make(chan struct{}, fs.config.DownloadConcurrency)
|
|
|
|
blockCtxTimeout := time.Duration(fs.config.DownloadPartSize/(1024*1024)) * time.Minute
|
|
|
|
pool := newBufferAllocator(int(partSize))
|
|
|
|
finished := false
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var errOnce sync.Once
|
2022-06-30 16:25:40 +00:00
|
|
|
var hasError int32
|
2022-02-21 18:01:31 +00:00
|
|
|
var poolError error
|
|
|
|
|
|
|
|
poolCtx, poolCancel := context.WithCancel(ctx)
|
|
|
|
defer poolCancel()
|
|
|
|
|
|
|
|
for part := 0; !finished; part++ {
|
|
|
|
start := offset
|
|
|
|
end := offset + partSize
|
|
|
|
if end >= contentLength {
|
|
|
|
end = contentLength
|
|
|
|
finished = true
|
|
|
|
}
|
|
|
|
writeOffset := int64(part) * partSize
|
|
|
|
offset = end
|
|
|
|
|
|
|
|
guard <- struct{}{}
|
2022-06-30 16:25:40 +00:00
|
|
|
if atomic.LoadInt32(&hasError) == 1 {
|
2022-02-21 18:01:31 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "pool error, download for part %v not started", part)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := pool.getBuffer()
|
|
|
|
wg.Add(1)
|
|
|
|
go func(start, end, writeOffset int64, buf []byte) {
|
|
|
|
defer func() {
|
|
|
|
pool.releaseBuffer(buf)
|
|
|
|
<-guard
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
innerCtx, cancelFn := context.WithDeadline(poolCtx, time.Now().Add(blockCtxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
count := end - start
|
|
|
|
err := fs.downloadPart(innerCtx, blockBlob, buf, writer, start, count, writeOffset)
|
|
|
|
if err != nil {
|
|
|
|
errOnce.Do(func() {
|
2022-06-30 16:25:40 +00:00
|
|
|
fsLog(fs, logger.LevelError, "multipart download error: %+v", err)
|
|
|
|
atomic.StoreInt32(&hasError, 1)
|
|
|
|
poolError = fmt.Errorf("multipart download error: %w", err)
|
2022-02-21 18:01:31 +00:00
|
|
|
poolCancel()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}(start, end, writeOffset, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(guard)
|
|
|
|
pool.free()
|
|
|
|
|
|
|
|
return poolError
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
func (fs *AzureBlobFs) handleMultipartUpload(ctx context.Context, reader io.Reader,
|
2022-04-25 15:34:52 +00:00
|
|
|
blockBlob *azblob.BlockBlobClient, httpHeaders *azblob.BlobHTTPHeaders,
|
2022-02-20 13:43:24 +00:00
|
|
|
) error {
|
2020-10-25 16:41:04 +00:00
|
|
|
partSize := fs.config.UploadPartSize
|
|
|
|
guard := make(chan struct{}, fs.config.UploadConcurrency)
|
|
|
|
blockCtxTimeout := time.Duration(fs.config.UploadPartSize/(1024*1024)) * time.Minute
|
|
|
|
|
|
|
|
// sync.Pool seems to use a lot of memory so prefer our own, very simple, allocator
|
|
|
|
// we only need to recycle few byte slices
|
|
|
|
pool := newBufferAllocator(int(partSize))
|
|
|
|
finished := false
|
|
|
|
var blocks []string
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var errOnce sync.Once
|
2022-06-30 16:25:40 +00:00
|
|
|
var hasError int32
|
2020-10-25 16:41:04 +00:00
|
|
|
var poolError error
|
|
|
|
|
|
|
|
poolCtx, poolCancel := context.WithCancel(ctx)
|
|
|
|
defer poolCancel()
|
|
|
|
|
|
|
|
for part := 0; !finished; part++ {
|
|
|
|
buf := pool.getBuffer()
|
|
|
|
|
|
|
|
n, err := fs.readFill(reader, buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
// read finished, if n > 0 we need to process the last data chunck
|
|
|
|
if n == 0 {
|
|
|
|
pool.releaseBuffer(buf)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
finished = true
|
|
|
|
} else if err != nil {
|
|
|
|
pool.releaseBuffer(buf)
|
2020-10-25 20:42:43 +00:00
|
|
|
pool.free()
|
2020-10-25 16:41:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-07 05:38:12 +00:00
|
|
|
// Block IDs are unique values to avoid issue if 2+ clients are uploading blocks
|
|
|
|
// at the same time causing CommitBlockList to get a mix of blocks from all the clients.
|
|
|
|
generatedUUID, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
pool.releaseBuffer(buf)
|
|
|
|
pool.free()
|
|
|
|
return fmt.Errorf("unable to generate block ID: %w", err)
|
|
|
|
}
|
|
|
|
blockID := base64.StdEncoding.EncodeToString([]byte(generatedUUID.String()))
|
2020-10-25 16:41:04 +00:00
|
|
|
blocks = append(blocks, blockID)
|
|
|
|
|
|
|
|
guard <- struct{}{}
|
2022-06-30 16:25:40 +00:00
|
|
|
if atomic.LoadInt32(&hasError) == 1 {
|
2022-02-21 18:01:31 +00:00
|
|
|
fsLog(fs, logger.LevelError, "pool error, upload for part %v not started", part)
|
2020-10-25 16:41:04 +00:00
|
|
|
pool.releaseBuffer(buf)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(blockID string, buf []byte, bufSize int) {
|
2022-02-21 18:01:31 +00:00
|
|
|
defer func() {
|
|
|
|
pool.releaseBuffer(buf)
|
|
|
|
<-guard
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2022-02-20 13:43:24 +00:00
|
|
|
|
|
|
|
bufferReader := &bytesReaderWrapper{
|
|
|
|
Reader: bytes.NewReader(buf[:bufSize]),
|
|
|
|
}
|
2020-10-25 16:41:04 +00:00
|
|
|
innerCtx, cancelFn := context.WithDeadline(poolCtx, time.Now().Add(blockCtxTimeout))
|
|
|
|
defer cancelFn()
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
_, err := blockBlob.StageBlock(innerCtx, blockID, bufferReader, &azblob.BlockBlobStageBlockOptions{})
|
2020-10-25 16:41:04 +00:00
|
|
|
if err != nil {
|
|
|
|
errOnce.Do(func() {
|
2022-06-30 16:25:40 +00:00
|
|
|
fsLog(fs, logger.LevelDebug, "multipart upload error: %+v", err)
|
|
|
|
atomic.StoreInt32(&hasError, 1)
|
|
|
|
poolError = fmt.Errorf("multipart upload error: %w", err)
|
2020-10-25 16:41:04 +00:00
|
|
|
poolCancel()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}(blockID, buf, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(guard)
|
|
|
|
pool.free()
|
|
|
|
|
|
|
|
if poolError != nil {
|
|
|
|
return poolError
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
commitOptions := azblob.BlockBlobCommitBlockListOptions{
|
2022-02-20 13:43:24 +00:00
|
|
|
BlobHTTPHeaders: httpHeaders,
|
|
|
|
}
|
|
|
|
if fs.config.AccessTier != "" {
|
|
|
|
commitOptions.Tier = (*azblob.AccessTier)(&fs.config.AccessTier)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := blockBlob.CommitBlockList(ctx, blocks, &commitOptions)
|
2020-10-25 16:41:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:01:31 +00:00
|
|
|
func (*AzureBlobFs) writeAtFull(w io.WriterAt, buf []byte, offset int64, count int) (int, error) {
|
|
|
|
written := 0
|
|
|
|
for written < count {
|
|
|
|
n, err := w.WriteAt(buf[written:count], offset+int64(written))
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 16:41:04 +00:00
|
|
|
// copied from rclone
|
2022-02-21 18:01:31 +00:00
|
|
|
func (*AzureBlobFs) readFill(r io.Reader, buf []byte) (n int, err error) {
|
2020-10-25 16:41:04 +00:00
|
|
|
var nn int
|
|
|
|
for n < len(buf) && err == nil {
|
|
|
|
nn, err = r.Read(buf[n:])
|
|
|
|
n += nn
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 +00:00
|
|
|
func (fs *AzureBlobFs) preserveModificationTime(source, target string, fi os.FileInfo) {
|
|
|
|
if plugin.Handler.HasMetadater() {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
err := plugin.Handler.SetModificationTime(fs.getStorageID(), ensureAbsPath(target),
|
|
|
|
util.GetTimeAsMsSinceEpoch(fi.ModTime()))
|
|
|
|
if err != nil {
|
|
|
|
fsLog(fs, logger.LevelWarn, "unable to preserve modification time after renaming %#v -> %#v: %+v",
|
|
|
|
source, target, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *AzureBlobFs) getCopyOptions() *azblob.BlobStartCopyOptions {
|
|
|
|
copyOptions := &azblob.BlobStartCopyOptions{}
|
2022-02-20 13:43:24 +00:00
|
|
|
if fs.config.AccessTier != "" {
|
|
|
|
copyOptions.Tier = (*azblob.AccessTier)(&fs.config.AccessTier)
|
|
|
|
}
|
|
|
|
return copyOptions
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (fs *AzureBlobFs) getStorageID() string {
|
|
|
|
if fs.config.Endpoint != "" {
|
|
|
|
if !strings.HasSuffix(fs.config.Endpoint, "/") {
|
|
|
|
return fmt.Sprintf("azblob://%v/%v", fs.config.Endpoint, fs.config.Container)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("azblob://%v%v", fs.config.Endpoint, fs.config.Container)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("azblob://%v", fs.config.Container)
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:43:24 +00:00
|
|
|
type bytesReaderWrapper struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bytesReaderWrapper) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 16:41:04 +00:00
|
|
|
type bufferAllocator struct {
|
|
|
|
sync.Mutex
|
|
|
|
available [][]byte
|
|
|
|
bufferSize int
|
2020-10-26 18:01:17 +00:00
|
|
|
finalized bool
|
2020-10-25 16:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBufferAllocator(size int) *bufferAllocator {
|
|
|
|
return &bufferAllocator{
|
|
|
|
bufferSize: size,
|
2020-10-26 18:01:17 +00:00
|
|
|
finalized: false,
|
2020-10-25 16:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bufferAllocator) getBuffer() []byte {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
if len(b.available) > 0 {
|
|
|
|
var result []byte
|
|
|
|
|
|
|
|
truncLength := len(b.available) - 1
|
|
|
|
result = b.available[truncLength]
|
|
|
|
|
|
|
|
b.available[truncLength] = nil
|
|
|
|
b.available = b.available[:truncLength]
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return make([]byte, b.bufferSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bufferAllocator) releaseBuffer(buf []byte) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
2020-10-26 18:01:17 +00:00
|
|
|
if b.finalized || len(buf) != b.bufferSize {
|
2020-10-25 16:41:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.available = append(b.available, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bufferAllocator) free() {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
b.available = nil
|
2020-10-26 18:01:17 +00:00
|
|
|
b.finalized = true
|
2020-10-25 16:41:04 +00:00
|
|
|
}
|