2020-02-28 14:44:15 +00:00
|
|
|
package vfscommon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-11 17:48:09 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-02-28 14:44:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CacheMode controls the functionality of the cache
|
|
|
|
type CacheMode byte
|
|
|
|
|
|
|
|
// CacheMode options
|
|
|
|
const (
|
|
|
|
CacheModeOff CacheMode = iota // cache nothing - return errors for writes which can't be satisfied
|
2020-10-13 21:49:58 +00:00
|
|
|
CacheModeMinimal // cache only the minimum, e.g. read/write opens
|
2020-02-28 14:44:15 +00:00
|
|
|
CacheModeWrites // cache all files opened with write intent
|
|
|
|
CacheModeFull // cache all files opened in any mode
|
|
|
|
)
|
|
|
|
|
|
|
|
var cacheModeToString = []string{
|
|
|
|
CacheModeOff: "off",
|
|
|
|
CacheModeMinimal: "minimal",
|
|
|
|
CacheModeWrites: "writes",
|
|
|
|
CacheModeFull: "full",
|
|
|
|
}
|
|
|
|
|
|
|
|
// String turns a CacheMode into a string
|
|
|
|
func (l CacheMode) String() string {
|
|
|
|
if l >= CacheMode(len(cacheModeToString)) {
|
|
|
|
return fmt.Sprintf("CacheMode(%d)", l)
|
|
|
|
}
|
|
|
|
return cacheModeToString[l]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a CacheMode
|
|
|
|
func (l *CacheMode) Set(s string) error {
|
|
|
|
for n, name := range cacheModeToString {
|
|
|
|
if s != "" && name == s {
|
|
|
|
*l = CacheMode(n)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 20:54:39 +00:00
|
|
|
return fmt.Errorf("unknown cache mode level %q", s)
|
2020-02-28 14:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
|
|
|
func (l *CacheMode) Type() string {
|
|
|
|
return "CacheMode"
|
|
|
|
}
|
2020-12-11 17:48:09 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON
|
|
|
|
func (l *CacheMode) UnmarshalJSON(in []byte) error {
|
|
|
|
return fs.UnmarshalJSONFlag(in, l, func(i int64) error {
|
|
|
|
if i < 0 || i >= int64(len(cacheModeToString)) {
|
2022-06-08 20:54:39 +00:00
|
|
|
return fmt.Errorf("unknown cache mode level %d", i)
|
2020-12-11 17:48:09 +00:00
|
|
|
}
|
|
|
|
*l = CacheMode(i)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|