[fuzz] Fix tag and ref for fx fuzz store and fetch

- Set "latest" as a ref for fx store.
- Prefix digests with "version:" for fx fetch.

Also:
- print the digest version when fx store completes
- fix an erroneous error code check in fx store
- fix permissions to allow for proper cleanup on fx store

Tested: fx fuzz store ledger; fx fuzz fetch ledger (with various versions)
Change-Id: Iafe7daf8d377f47092664982e506894e7b5a9f49
diff --git a/devshell/fuzz b/devshell/fuzz
index 4550635..880a442 100755
--- a/devshell/fuzz
+++ b/devshell/fuzz
@@ -149,9 +149,10 @@
 fetch() {
   [[ "${status}" == "STOPPED" ]] || fatal "fuzzer must be stopped to run this command"
 
-  local version="$1"
-  if [[ -z "${version}" ]] ; then
-    version="latest"
+  local version="${1:-latest}"
+  # "latest" is a ref, but digests are tags that are prefixed with "version:"
+  if [[ "${version}" != "latest" ]] ; then
+    version="version:${version}"
   fi
 
   local corpus="${staging}/${fuzzer}/corpus"
@@ -161,7 +162,7 @@
   # Get corpus from CIPD
   if ${CIPD} ls -r "${CIPD_PREFIX}" | grep -q "${fuzzer}" ; then
     ${CIPD} install "${cipd_path}" ${version} --root "${corpus}"
-    [[ $? -ne 0 ]] || fatal "failed to retrieve corpus"
+    [[ $? -eq 0 ]] || fatal "failed to retrieve corpus"
   fi
 
   # Add any upstream third-party corpora
@@ -209,7 +210,12 @@
 EOF
   # TODO: catch the error and tell user to do this
   # $ cipd auth-login  # One-time auth.
-  ${CIPD} create --pkg-def cipd.yaml -tag version:${version}
+  ${CIPD} create --pkg-def cipd.yaml -tag version:${version} -ref latest
+  echo "***"
+  echo "Successfully stored package for ${fuzzer}, version ${version}."
+  # cipd creates a .cipd directory in corpus that misses +x so it cannot be
+  # cleaned up properly in cleanup().
+  sudo chmod -R +x ${corpus}
   popd
 }