github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/union/policy/eprand.go (about)

     1  package policy
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  	"time"
     7  
     8  	"github.com/rclone/rclone/backend/union/upstream"
     9  	"github.com/rclone/rclone/fs"
    10  )
    11  
    12  func init() {
    13  	registerPolicy("eprand", &EpRand{})
    14  }
    15  
    16  // EpRand stands for existing path, random
    17  // Calls epall and then randomizes. Returns one candidate.
    18  type EpRand struct {
    19  	EpAll
    20  }
    21  
    22  func (p *EpRand) rand(upstreams []*upstream.Fs) *upstream.Fs {
    23  	rand.Seed(time.Now().Unix())
    24  	return upstreams[rand.Intn(len(upstreams))]
    25  }
    26  
    27  func (p *EpRand) randEntries(entries []upstream.Entry) upstream.Entry {
    28  	rand.Seed(time.Now().Unix())
    29  	return entries[rand.Intn(len(entries))]
    30  }
    31  
    32  // Action category policy, governing the modification of files and directories
    33  func (p *EpRand) Action(ctx context.Context, upstreams []*upstream.Fs, path string) ([]*upstream.Fs, error) {
    34  	upstreams, err := p.EpAll.Action(ctx, upstreams, path)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return []*upstream.Fs{p.rand(upstreams)}, nil
    39  }
    40  
    41  // ActionEntries is ACTION category policy but receiving a set of candidate entries
    42  func (p *EpRand) ActionEntries(entries ...upstream.Entry) ([]upstream.Entry, error) {
    43  	entries, err := p.EpAll.ActionEntries(entries...)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return []upstream.Entry{p.randEntries(entries)}, nil
    48  }
    49  
    50  // Create category policy, governing the creation of files and directories
    51  func (p *EpRand) Create(ctx context.Context, upstreams []*upstream.Fs, path string) ([]*upstream.Fs, error) {
    52  	upstreams, err := p.EpAll.Create(ctx, upstreams, path)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return []*upstream.Fs{p.rand(upstreams)}, nil
    57  }
    58  
    59  // CreateEntries is CREATE category policy but receiving a set of candidate entries
    60  func (p *EpRand) CreateEntries(entries ...upstream.Entry) ([]upstream.Entry, error) {
    61  	entries, err := p.EpAll.CreateEntries(entries...)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return []upstream.Entry{p.randEntries(entries)}, nil
    66  }
    67  
    68  // Search category policy, governing the access to files and directories
    69  func (p *EpRand) Search(ctx context.Context, upstreams []*upstream.Fs, path string) (*upstream.Fs, error) {
    70  	if len(upstreams) == 0 {
    71  		return nil, fs.ErrorObjectNotFound
    72  	}
    73  	upstreams, err := p.epall(ctx, upstreams, path)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return p.rand(upstreams), nil
    78  }
    79  
    80  // SearchEntries is SEARCH category policy but receiving a set of candidate entries
    81  func (p *EpRand) SearchEntries(entries ...upstream.Entry) (upstream.Entry, error) {
    82  	if len(entries) == 0 {
    83  		return nil, fs.ErrorObjectNotFound
    84  	}
    85  	return p.randEntries(entries), nil
    86  }