mirror of
https://github.com/rclone/rclone.git
synced 2025-02-02 03:29:51 +01:00
union: enable passing of options to upstreams and policies #6071
This factors out the options into a sub package so they can be passed to upstreams and used in policies.
This commit is contained in:
parent
4f94b27800
commit
1d2fe0d856
16
backend/union/common/options.go
Normal file
16
backend/union/common/options.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Package common defines code common to the union and the policies
|
||||||
|
//
|
||||||
|
// These need to be defined in a separate package to avoid import loops
|
||||||
|
package common
|
||||||
|
|
||||||
|
import "github.com/rclone/rclone/fs"
|
||||||
|
|
||||||
|
// Options defines the configuration for this backend
|
||||||
|
type Options struct {
|
||||||
|
Upstreams fs.SpaceSepList `config:"upstreams"`
|
||||||
|
Remotes fs.SpaceSepList `config:"remotes"` // Deprecated
|
||||||
|
ActionPolicy string `config:"action_policy"`
|
||||||
|
CreatePolicy string `config:"create_policy"`
|
||||||
|
SearchPolicy string `config:"search_policy"`
|
||||||
|
CacheTime int `config:"cache_time"`
|
||||||
|
}
|
@ -13,6 +13,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rclone/rclone/backend/union/common"
|
||||||
"github.com/rclone/rclone/backend/union/policy"
|
"github.com/rclone/rclone/backend/union/policy"
|
||||||
"github.com/rclone/rclone/backend/union/upstream"
|
"github.com/rclone/rclone/backend/union/upstream"
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
@ -54,21 +55,11 @@ func init() {
|
|||||||
fs.Register(fsi)
|
fs.Register(fsi)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options defines the configuration for this backend
|
|
||||||
type Options struct {
|
|
||||||
Upstreams fs.SpaceSepList `config:"upstreams"`
|
|
||||||
Remotes fs.SpaceSepList `config:"remotes"` // Deprecated
|
|
||||||
ActionPolicy string `config:"action_policy"`
|
|
||||||
CreatePolicy string `config:"create_policy"`
|
|
||||||
SearchPolicy string `config:"search_policy"`
|
|
||||||
CacheTime int `config:"cache_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fs represents a union of upstreams
|
// Fs represents a union of upstreams
|
||||||
type Fs struct {
|
type Fs struct {
|
||||||
name string // name of this remote
|
name string // name of this remote
|
||||||
features *fs.Features // optional features
|
features *fs.Features // optional features
|
||||||
opt Options // options for this Fs
|
opt common.Options // options for this Fs
|
||||||
root string // the path we are working on
|
root string // the path we are working on
|
||||||
upstreams []*upstream.Fs // slice of upstreams
|
upstreams []*upstream.Fs // slice of upstreams
|
||||||
hashSet hash.Set // intersection of hash types
|
hashSet hash.Set // intersection of hash types
|
||||||
@ -808,7 +799,7 @@ func (f *Fs) Shutdown(ctx context.Context) error {
|
|||||||
// The returned Fs is the actual Fs, referenced by remote in the config
|
// The returned Fs is the actual Fs, referenced by remote in the config
|
||||||
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
|
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
|
||||||
// Parse config into Options struct
|
// Parse config into Options struct
|
||||||
opt := new(Options)
|
opt := new(common.Options)
|
||||||
err := configstruct.Set(m, opt)
|
err := configstruct.Set(m, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -836,7 +827,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
|||||||
errs := Errors(make([]error, len(opt.Upstreams)))
|
errs := Errors(make([]error, len(opt.Upstreams)))
|
||||||
multithread(len(opt.Upstreams), func(i int) {
|
multithread(len(opt.Upstreams), func(i int) {
|
||||||
u := opt.Upstreams[i]
|
u := opt.Upstreams[i]
|
||||||
upstreams[i], errs[i] = upstream.New(ctx, u, root, time.Duration(opt.CacheTime)*time.Second)
|
upstreams[i], errs[i] = upstream.New(ctx, u, root, opt)
|
||||||
})
|
})
|
||||||
var usedUpstreams []*upstream.Fs
|
var usedUpstreams []*upstream.Fs
|
||||||
var fserr error
|
var fserr error
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rclone/rclone/backend/union/common"
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/fs/cache"
|
"github.com/rclone/rclone/fs/cache"
|
||||||
"github.com/rclone/rclone/fs/fspath"
|
"github.com/rclone/rclone/fs/fspath"
|
||||||
@ -26,6 +27,7 @@ type Fs struct {
|
|||||||
fs.Fs
|
fs.Fs
|
||||||
RootFs fs.Fs
|
RootFs fs.Fs
|
||||||
RootPath string
|
RootPath string
|
||||||
|
Opt *common.Options
|
||||||
writable bool
|
writable bool
|
||||||
creatable bool
|
creatable bool
|
||||||
usage *fs.Usage // Cache the usage
|
usage *fs.Usage // Cache the usage
|
||||||
@ -61,17 +63,18 @@ type Entry interface {
|
|||||||
|
|
||||||
// New creates a new Fs based on the
|
// New creates a new Fs based on the
|
||||||
// string formatted `type:root_path(:ro/:nc)`
|
// string formatted `type:root_path(:ro/:nc)`
|
||||||
func New(ctx context.Context, remote, root string, cacheTime time.Duration) (*Fs, error) {
|
func New(ctx context.Context, remote, root string, opt *common.Options) (*Fs, error) {
|
||||||
configName, fsPath, err := fspath.SplitFs(remote)
|
configName, fsPath, err := fspath.SplitFs(remote)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
f := &Fs{
|
f := &Fs{
|
||||||
RootPath: strings.TrimRight(root, "/"),
|
RootPath: strings.TrimRight(root, "/"),
|
||||||
|
Opt: opt,
|
||||||
writable: true,
|
writable: true,
|
||||||
creatable: true,
|
creatable: true,
|
||||||
cacheExpiry: time.Now().Unix(),
|
cacheExpiry: time.Now().Unix(),
|
||||||
cacheTime: cacheTime,
|
cacheTime: time.Duration(opt.CacheTime) * time.Second,
|
||||||
usage: &fs.Usage{},
|
usage: &fs.Usage{},
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(fsPath, ":ro") {
|
if strings.HasSuffix(fsPath, ":ro") {
|
||||||
|
Loading…
Reference in New Issue
Block a user