vold: Clean up asec command response and add support for 'StorageBusy'

Signed-off-by: San Mehat <san@google.com>
diff --git a/Android.mk b/Android.mk
index 9036f78..bdb2325 100644
--- a/Android.mk
+++ b/Android.mk
@@ -24,7 +24,8 @@
                   geom_mbr_enc.c                       \
                   Fat.cpp                              \
                   Loop.cpp                             \
-                  Devmapper.cpp
+                  Devmapper.cpp                        \
+                  ResponseCode.cpp
 
 LOCAL_MODULE:= vold
 
diff --git a/CommandListener.cpp b/CommandListener.cpp
index c87198e..f47d66c 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -88,21 +88,8 @@
     if (!rc) {
         cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
     } else {
-        /*
-         * Failed
-         */
-        if (errno == ENODEV) {
-            rc = ResponseCode::OpFailedNoMedia;
-        } else if (errno == ENODATA) {
-            rc = ResponseCode::OpFailedMediaBlank;
-        } else if (errno == EIO) {
-            rc = ResponseCode::OpFailedMediaCorrupt;
-        } else if (errno == EBUSY) {
-            rc = ResponseCode::OpFailedVolBusy;
-        } else {
-            LOGW("Returning OperationFailed - no handler for errno %d", errno);
-            rc = ResponseCode::OperationFailed;
-        }
+        int erno = errno;
+        rc = ResponseCode::convertFromErrno();
         cli->sendMsg(rc, "volume operation failed", true);
     }
 
@@ -223,7 +210,6 @@
             }
         }
         closedir(d);
-        cli->sendMsg(ResponseCode::CommandOkay, "ASEC listing complete", false);
     } else if (!strcmp(argv[1], "create")) {
         if (argc != 7) {
             cli->sendMsg(ResponseCode::CommandSyntaxError,
@@ -232,21 +218,13 @@
         }
 
         unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
-        if (vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]))) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Container creation failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Container created", false);
-        }
+        rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]));
     } else if (!strcmp(argv[1], "finalize")) {
         if (argc != 3) {
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
             return 0;
         }
-        if (vm->finalizeAsec(argv[2])) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Container finalize failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Container finalized", false);
-        }
+        rc = vm->finalizeAsec(argv[2]);
     } else if (!strcmp(argv[1], "destroy")) {
         if (argc < 3) {
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
@@ -256,25 +234,14 @@
         if (argc > 3 && !strcmp(argv[3], "force")) {
             force = true;
         }
-        if (vm->destroyAsec(argv[2], force)) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Container destroy failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Container destroyed", false);
-        }
+        rc = vm->destroyAsec(argv[2], force);
     } else if (!strcmp(argv[1], "mount")) {
         if (argc != 5) {
             cli->sendMsg(ResponseCode::CommandSyntaxError,
                     "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
             return 0;
         }
-
-        int rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
-
-        if (rc < 0) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Mount failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Mount succeeded", false);
-        }
+        rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
     } else if (!strcmp(argv[1], "unmount")) {
         if (argc < 3) {
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
@@ -284,22 +251,14 @@
         if (argc > 3 && !strcmp(argv[3], "force")) {
             force = true;
         }
-        if (vm->unmountAsec(argv[2], force)) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Container unmount failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Container unmounted", false);
-        }
+        rc = vm->unmountAsec(argv[2], force);
     } else if (!strcmp(argv[1], "rename")) {
         if (argc != 4) {
             cli->sendMsg(ResponseCode::CommandSyntaxError,
                     "Usage: asec rename <old_id> <new_id>", false);
             return 0;
         }
-        if (vm->renameAsec(argv[2], argv[3])) {
-            cli->sendMsg(ResponseCode::OperationFailed, "Container rename failed", true);
-        } else {
-            cli->sendMsg(ResponseCode::CommandOkay, "Container renamed", false);
-        }
+        rc = vm->renameAsec(argv[2], argv[3]);
     } else if (!strcmp(argv[1], "path")) {
         if (argc != 3) {
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
@@ -312,9 +271,17 @@
         } else {
             cli->sendMsg(ResponseCode::AsecPathResult, path, false);
         }
+        return 0;
     } else {
         cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
     }
 
+    if (!rc) {
+        cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
+    } else {
+        rc = ResponseCode::convertFromErrno();
+        cli->sendMsg(rc, "asec operation failed", true);
+    }
+
     return 0;
 }
diff --git a/ResponseCode.cpp b/ResponseCode.cpp
new file mode 100644
index 0000000..d0410b6
--- /dev/null
+++ b/ResponseCode.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#define LOG_TAG "Vold"
+
+#include <cutils/log.h>
+
+#include "ResponseCode.h"
+
+int ResponseCode::convertFromErrno() {
+   if (errno == ENODEV) {
+        return(ResponseCode::OpFailedNoMedia);
+    } else if (errno == ENODATA) {
+        return(ResponseCode::OpFailedMediaBlank);
+    } else if (errno == EIO) {
+        return(ResponseCode::OpFailedMediaCorrupt);
+    } else if (errno == EBUSY) {
+        return(ResponseCode::OpFailedStorageBusy);
+    }
+
+    LOGW("Returning OperationFailed - no handler for errno %d", errno);
+    return(ResponseCode::OperationFailed);
+}
diff --git a/ResponseCode.h b/ResponseCode.h
index 3508f81..31d6482 100644
--- a/ResponseCode.h
+++ b/ResponseCode.h
@@ -40,7 +40,7 @@
     static const int OpFailedMediaBlank       = 402;
     static const int OpFailedMediaCorrupt     = 403;
     static const int OpFailedVolNotMounted    = 404;
-    static const int OpFailedVolBusy          = 405;
+    static const int OpFailedStorageBusy      = 405;
 
     // 500 series - The command was not accepted and the requested
     // action did not take place.
@@ -59,5 +59,7 @@
     static const int VolumeDiskInserted            = 630;
     static const int VolumeDiskRemoved             = 631;
     static const int VolumeBadRemoval              = 632;
+
+    static int convertFromErrno();
 };
 #endif