From 2fdb68950369e81163e965e57f8a783ede3b4ee3 Mon Sep 17 00:00:00 2001 From: mpl Date: Thu, 21 Aug 2014 20:29:36 +0200 Subject: [PATCH] devcam: add the -wipecache option Change-Id: Ia3149361b3b210fd8e3b5fe42147018d3ab4e6fd --- dev/devcam/camget.go | 5 +++++ dev/devcam/cammount.go | 5 +++++ dev/devcam/camput.go | 5 +++++ dev/devcam/camtool.go | 5 +++++ dev/devcam/devcam.go | 8 +++++--- dev/devcam/env.go | 13 +++++++++++++ dev/devcam/server.go | 5 +++++ 7 files changed, 43 insertions(+), 3 deletions(-) diff --git a/dev/devcam/camget.go b/dev/devcam/camget.go index 9f7607351..f1cc33a33 100644 --- a/dev/devcam/camget.go +++ b/dev/devcam/camget.go @@ -79,6 +79,11 @@ func (c *getCmd) RunCommand(args []string) error { } } c.env.SetCamdevVars(c.altkey) + // wipeCacheDir needs to be called after SetCamdevVars, because that is + // where CAMLI_CACHE_DIR is defined. + if *wipeCache { + c.env.wipeCacheDir() + } cmdBin := filepath.Join("bin", "camget") cmdArgs := []string{ diff --git a/dev/devcam/cammount.go b/dev/devcam/cammount.go index e89e7b1a0..4e049e8ec 100644 --- a/dev/devcam/cammount.go +++ b/dev/devcam/cammount.go @@ -92,6 +92,11 @@ func (c *mountCmd) RunCommand(args []string) error { } } c.env.SetCamdevVars(c.altkey) + // wipeCacheDir needs to be called after SetCamdevVars, because that is + // where CAMLI_CACHE_DIR is defined. + if *wipeCache { + c.env.wipeCacheDir() + } tryUnmount(mountpoint) if err := os.Mkdir(mountpoint, 0700); err != nil && !os.IsExist(err) { diff --git a/dev/devcam/camput.go b/dev/devcam/camput.go index ab7f2b5fb..dda1dfdc5 100644 --- a/dev/devcam/camput.go +++ b/dev/devcam/camput.go @@ -77,6 +77,11 @@ func (c *putCmd) RunCommand(args []string) error { } } c.env.SetCamdevVars(c.altkey) + // wipeCacheDir needs to be called after SetCamdevVars, because that is + // where CAMLI_CACHE_DIR is defined. + if *wipeCache { + c.env.wipeCacheDir() + } blobserver := "http://localhost:" + c.port + c.path if c.tls { diff --git a/dev/devcam/camtool.go b/dev/devcam/camtool.go index 79bcd9e68..4eeb5ed9f 100644 --- a/dev/devcam/camtool.go +++ b/dev/devcam/camtool.go @@ -66,6 +66,11 @@ func (c *toolCmd) RunCommand(args []string) error { } } c.env.SetCamdevVars(c.altkey) + // wipeCacheDir needs to be called after SetCamdevVars, because that is + // where CAMLI_CACHE_DIR is defined. + if *wipeCache { + c.env.wipeCacheDir() + } cmdBin := filepath.Join("bin", "camtool") return runExec(cmdBin, args, c.env) diff --git a/dev/devcam/devcam.go b/dev/devcam/devcam.go index 87beef215..894344ba9 100644 --- a/dev/devcam/devcam.go +++ b/dev/devcam/devcam.go @@ -36,9 +36,10 @@ import ( ) var ( - noBuild = flag.Bool("nobuild", false, "do not rebuild anything") - race = flag.Bool("race", false, "build with race detector") - quiet, _ = strconv.ParseBool(os.Getenv("CAMLI_QUIET")) + noBuild = flag.Bool("nobuild", false, "do not rebuild anything") + race = flag.Bool("race", false, "build with race detector") + quiet, _ = strconv.ParseBool(os.Getenv("CAMLI_QUIET")) + wipeCache = flag.Bool("wipecache", false, "wipe the cache directory. Server cache with devcam server, client cache otherwise.") // Whether to build the subcommand with sqlite support. This only // concerns the server subcommand, which sets it to serverCmd.sqlite. withSqlite bool @@ -246,6 +247,7 @@ func build(path string) error { func main() { cmdmain.CheckCwd = checkCamliSrcRoot + if err := checkModtime(); err != nil { log.Printf("Skipping freshness check: %v", err) } diff --git a/dev/devcam/env.go b/dev/devcam/env.go index 4d4518fe4..6d2bfdac8 100644 --- a/dev/devcam/env.go +++ b/dev/devcam/env.go @@ -153,3 +153,16 @@ func setCamdevVarsFor(e *Env, altkey bool) { setenv("CAMLI_PUBKEY_BLOBREF", pubKeyRef.String()) setenv("CAMLI_KV_VERIFY", "true") } + +func (e *Env) wipeCacheDir() { + cacheDir, _ := e.m["CAMLI_CACHE_DIR"] + if cacheDir == "" { + log.Fatal("Could not wipe cache dir, CAMLI_CACHE_DIR not defined") + } + if err := os.RemoveAll(cacheDir); err != nil { + log.Fatalf("Could not remove cache dir %v: %v", cacheDir, err) + } + if err := os.MkdirAll(cacheDir, 0700); err != nil { + log.Fatalf("Could not recreate cache dir %v: %v", cacheDir, err) + } +} diff --git a/dev/devcam/server.go b/dev/devcam/server.go index eb5dd3cdc..ea969eae5 100644 --- a/dev/devcam/server.go +++ b/dev/devcam/server.go @@ -494,6 +494,11 @@ func (c *serverCmd) RunCommand(args []string) error { if err := c.setEnvVars(); err != nil { return fmt.Errorf("Could not setup the env vars: %v", err) } + // wipeCacheDir needs to be called after setEnvVars, because that is where + // CAMLI_CACHE_DIR is defined. + if *wipeCache { + c.env.wipeCacheDir() + } if err := c.setupIndexer(); err != nil { return fmt.Errorf("Could not setup the indexer: %v", err) }