Add 'adb restore' to parallel 'adb backup'

It won't actually do anything until the 'bu' tool and framework are
updated to respond properly, but this is the adb side of the
necessary infrastructure: we copy the tarfile into the socket pointed
at the device, using the existing mechanisms.

Change-Id: Ic3b5779ade256bd1ad989a94b0685f7b1a7d59d2
diff --git a/adb/backup_service.c b/adb/backup_service.c
index 1e55efc..2e6e754 100644
--- a/adb/backup_service.c
+++ b/adb/backup_service.c
@@ -23,17 +23,28 @@
 #include "adb.h"
 
 /* returns the data socket passing the backup data here for forwarding */
-int backup_service(char* args) {
+int backup_service(BackupOperation op, char* args) {
     pid_t pid;
     int s[2];
+    char* operation;
+    int socketnum;
 
-    D("backup_service(%s)\n", args);
+    // Command string and choice of stdin/stdout for the pipe depend on our invocation
+    if (op == BACKUP) {
+        operation = "backup";
+        socketnum = STDOUT_FILENO;
+    } else {
+        operation = "restore";
+        socketnum = STDIN_FILENO;
+    }
+
+    D("backup_service(%s, %s)\n", operation, args);
 
     // set up the pipe from the subprocess to here
     // parent will read s[0]; child will write s[1]
     if (adb_socketpair(s)) {
-        D("can't create backup socketpair\n");
-        fprintf(stderr, "unable to create backup socketpair\n");
+        D("can't create backup/restore socketpair\n");
+        fprintf(stderr, "unable to create backup/restore socketpair\n");
         return -1;
     }
 
@@ -41,8 +52,8 @@
     pid = fork();
     if (pid < 0) {
         // failure
-        D("can't fork for backup\n");
-        fprintf(stderr, "unable to fork for backup\n");
+        D("can't fork for %s\n", operation);
+        fprintf(stderr, "unable to fork for %s\n", operation);
         adb_close(s[0]);
         adb_close(s[1]);
         return -1;
@@ -52,37 +63,37 @@
     if (pid == 0) {
         char* p;
         int argc;
-        char** backup_args;
+        char** bu_args;
 
         // child -- actually run the backup here
-        argc = 2; // room for the basic 'bu' argv[0] and 'backup' argv[1]
+        argc = 2; // room for the basic 'bu' argv[0] and '[operation]' argv[1]
         for (p = (char*)args; p && *p; ) {
             argc++;
             while (*p && *p != ':') p++;
             if (*p == ':') p++;
         }
 
-        backup_args = (char**) alloca(argc*sizeof(char*) + 1);
-        backup_args[0] = "bu";
-        backup_args[1] = "backup";
+        bu_args = (char**) alloca(argc*sizeof(char*) + 1);
+        bu_args[0] = "bu";
+        bu_args[1] = operation;
         argc = 2;   // run through again to build the argv array
-        for (p = (char*)args; *p; ) {
-            backup_args[argc++] = p;
+        for (p = (char*)args; p && *p; ) {
+            bu_args[argc++] = p;
             while (*p && *p != ':') p++;
             if (*p == ':') {
                 *p = 0;
                 p++;
             }
         }
-        backup_args[argc] = NULL;
+        bu_args[argc] = NULL;
 
         // Close the half of the socket that we don't care about, route 'bu's console
         // to the output socket, and off we go
         adb_close(s[0]);
-        dup2(s[1], STDOUT_FILENO);
+        dup2(s[1], socketnum);
 
         // off we go
-        execvp("/system/bin/bu", (char * const *)backup_args);
+        execvp("/system/bin/bu", (char * const *)bu_args);
         // oops error - close up shop and go home
         fprintf(stderr, "Unable to exec 'bu', bailing\n");
         exit(-1);