From 31a4fefbe0a44377f75888284c9be0a3bec2a017 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Thu, 10 May 2018 12:46:27 -0700 Subject: Make cachePolicy int to avoid string comparison PiperOrigin-RevId: 196157086 Change-Id: Ia7f7ffe1bf486b21ef8091e2e8ef9a9faf733dfc --- pkg/sentry/fs/gofer/fs.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'pkg/sentry/fs/gofer') diff --git a/pkg/sentry/fs/gofer/fs.go b/pkg/sentry/fs/gofer/fs.go index 0a1a49bbd..a8a3ec19d 100644 --- a/pkg/sentry/fs/gofer/fs.go +++ b/pkg/sentry/fs/gofer/fs.go @@ -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) } -- cgit v1.2.3