Make cachePolicy int to avoid string comparison

PiperOrigin-RevId: 196157086
Change-Id: Ia7f7ffe1bf486b21ef8091e2e8ef9a9faf733dfc
This commit is contained in:
Fabricio Voznika
2018-05-10 12:47:15 -07:00
committed by Shentubot
parent 9d91c44d77
commit 31a4fefbe0
+19 -12
View File
@@ -57,20 +57,26 @@ const (
)
// cachePolicy is a 9p cache policy.
type cachePolicy string
type cachePolicy int
const (
// Use virtual file system cache.
cacheAll cachePolicy = "fscache"
// TODO: fully support cache=none.
cacheNone cachePolicy = "none"
cacheNone cachePolicy = iota
// defaultCache is cacheAll. Note this diverges from the 9p Linux
// client whose default is "none". See TODO above.
defaultCache = cacheAll
// Use virtual file system cache.
cacheAll
)
func parseCachePolicy(policy string) (cachePolicy, error) {
switch policy {
case "fscache":
return cacheAll, nil
case "none":
return cacheNone, nil
}
return cacheNone, fmt.Errorf("unsupported cache mode: %s", policy)
}
// defaultAname is the default attach name.
const defaultAname = "/"
@@ -206,11 +212,12 @@ func options(data string) (opts, error) {
// Parse the cache policy. Reject unsupported policies.
o.policy = cacheAll
if cp, ok := options[cacheKey]; ok {
if cachePolicy(cp) != cacheAll && cachePolicy(cp) != cacheNone {
return o, fmt.Errorf("unsupported cache mode: 'cache=%s'", cp)
if policy, ok := options[cacheKey]; ok {
cp, err := parseCachePolicy(policy)
if err != nil {
return o, err
}
o.policy = cachePolicy(cp)
o.policy = cp
delete(options, cacheKey)
}