Imported Upstream version 3.1.0
[debian/amanda] / oldrecover-src / extract_list.c
index d1d89556bda5af8b6137410e6b94cfe6cffe3b21..970b88dc38091656d35249e0ce396cc99b814187 100644 (file)
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: extract_list.c,v 1.6 2006/08/24 01:57:15 paddy_s Exp $
+ * $Id$
  *
  * implements the "extract" command in amrecover
  */
 
 #include "amanda.h"
+#include "match.h"
 #include "conffile.h"
-#include "version.h"
 #include "amrecover.h"
 #include "fileheader.h"
 #include "dgram.h"
@@ -231,7 +231,7 @@ clear_tape_list(
     EXTRACT_LIST *     tape_list)
 {
     EXTRACT_LIST_ITEM *this, *next;
-    
+
 
     this = tape_list->files;
     while (this != NULL)
@@ -443,7 +443,7 @@ add_extract_item(
     /* add this in date increasing order          */
     /* because restore must be done in this order */
     /* add at begining */
-    if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0) 
+    if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)
     {
        this->next = extract_list;
        extract_list = this;
@@ -624,7 +624,7 @@ void add_file(
            path_on_disk = stralloc2("/", regex);
        }
     } else {
-       char *clean_disk_path = clean_regex(disk_path);
+       char *clean_disk_path = clean_regex(disk_path, 0);
        path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
        amfree(clean_disk_path);
     }
@@ -943,7 +943,7 @@ delete_file(
            path_on_disk = stralloc2("/", regex);
        }
     } else {
-       char *clean_disk_path = clean_regex(disk_path);
+       char *clean_disk_path = clean_regex(disk_path, 0);
        path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
        amfree(clean_disk_path);
     }
@@ -1509,7 +1509,7 @@ extract_files_setup(
         nread = read(tape_control_sock, buffer, sizeof(buffer));
 
        if (nread <= 0) {
-           error(_("Could not read from control socket: %s\n"), 
+           error(_("Could not read from control socket: %s\n"),
                   strerror(errno));
            /*NOTREACHED*/
         }
@@ -1535,7 +1535,7 @@ extract_files_setup(
        }
 
        amfree(our_feature_string);
-    
+
        line = get_security();
 
        send_to_tape_server(tape_data_sock, line);
@@ -1599,9 +1599,9 @@ extract_files_child(
     EXTRACT_LIST *     elist)
 {
     int save_errno;
-    int extra_params = 0;
-    int i,j=0;
-    char **restore_args = NULL;
+    int   i;
+    guint j;
+    GPtrArray *argv_ptr = g_ptr_array_new();
     int files_off_tape;
     EXTRACT_LIST_ITEM *fn;
     enum dumptypes dumptype = IS_UNKNOWN;
@@ -1609,7 +1609,7 @@ extract_files_child(
     dumpfile_t file;
     size_t len_program;
     char *cmd = NULL;
-    int passwd_field = -1;
+    guint passwd_field = 999999999;
 #ifdef SAMBA_CLIENT
     char *domain = NULL, *smbpass = NULL;
 #endif
@@ -1659,109 +1659,75 @@ extract_files_child(
 
     /* form the arguments to restore */
     files_off_tape = length_of_tape_list(elist);
-    switch (dumptype) {
-    case IS_SAMBA:
-#ifdef SAMBA_CLIENT
-       extra_params = 10;
-       break;
-#endif
-    case IS_TAR:
-    case IS_GNUTAR:
-        extra_params = 4;
-        break;
-    case IS_SAMBA_TAR:
-        extra_params = 3;
-        break;
-    case IS_UNKNOWN:
-    case IS_DUMP:
-#ifdef AIX_BACKUP
-        extra_params = 2;
-#else
-#if defined(XFSDUMP)
-       if (strcmp(file.program, XFSDUMP) == 0) {
-            extra_params = 4 + files_off_tape;
-       } else
-#endif
-       {
-        extra_params = 4;
-       }
-#endif
-       break;
-    }
-
-    restore_args = (char **)alloc((size_t)((extra_params + files_off_tape + 1)
-                                 * sizeof(char *)));
     switch(dumptype) {
     case IS_SAMBA:
 #ifdef SAMBA_CLIENT
-       restore_args[j++] = stralloc("smbclient");
-       smbpass = findpass(file.disk, &domain);
-       if (smbpass) {
-            restore_args[j++] = stralloc(file.disk);
-           passwd_field=j;
-           restore_args[j++] = stralloc("-U");
-           restore_args[j++] = smbpass;
-           if (domain) {
-               restore_args[j++] = stralloc("-W");
-               restore_args[j++] = stralloc(domain);
-           } else
-               extra_params -= 2;
-       } else
-           extra_params -= 6;
-       restore_args[j++] = stralloc("-d0");
-       restore_args[j++] = stralloc("-Tx");
-       restore_args[j++] = stralloc("-");      /* data on stdin */
-       break;
+       g_ptr_array_add(argv_ptr, stralloc("smbclient"));
+       smbpass = findpass(file.disk, &domain);
+       if (smbpass) {
+           g_ptr_array_add(argv_ptr, stralloc(file.disk));
+           g_ptr_array_add(argv_ptr, stralloc("-U"));
+           passwd_field = argv_ptr->len;
+           g_ptr_array_add(argv_ptr, stralloc(smbpass));
+           if (domain) {
+               g_ptr_array_add(argv_ptr, stralloc("-W"));
+               g_ptr_array_add(argv_ptr, stralloc(domain));
+           }
+       }
+       g_ptr_array_add(argv_ptr, stralloc("-d0"));
+       g_ptr_array_add(argv_ptr, stralloc("-Tx"));
+       g_ptr_array_add(argv_ptr, stralloc("-"));       /* data on stdin */
+       break;
 #endif
     case IS_TAR:
     case IS_GNUTAR:
-       restore_args[j++] = stralloc("tar");
-       restore_args[j++] = stralloc("--numeric-owner");
-       restore_args[j++] = stralloc("-xpGvf");
-       restore_args[j++] = stralloc("-");      /* data on stdin */
+       g_ptr_array_add(argv_ptr, stralloc("tar"));
+       g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
+       g_ptr_array_add(argv_ptr, stralloc("-xpGvf"));
+       g_ptr_array_add(argv_ptr, stralloc("-"));       /* data on stdin */
        break;
     case IS_SAMBA_TAR:
-       restore_args[j++] = stralloc("tar");
-       restore_args[j++] = stralloc("-xpvf");
-       restore_args[j++] = stralloc("-");      /* data on stdin */
+       g_ptr_array_add(argv_ptr, stralloc("tar"));
+       g_ptr_array_add(argv_ptr, stralloc("-xpvf"));
+       g_ptr_array_add(argv_ptr, stralloc("-"));       /* data on stdin */
        break;
     case IS_UNKNOWN:
     case IS_DUMP:
-        restore_args[j++] = stralloc("restore");
+       g_ptr_array_add(argv_ptr, stralloc("restore"));
 #ifdef AIX_BACKUP
-        restore_args[j++] = stralloc("-xB");
+       g_ptr_array_add(argv_ptr, stralloc("-xB"));
 #else
 #if defined(XFSDUMP)
        if (strcmp(file.program, XFSDUMP) == 0) {
-            restore_args[j++] = stralloc("-v");
-            restore_args[j++] = stralloc("silent");
+           g_ptr_array_add(argv_ptr, stralloc("-v"));
+           g_ptr_array_add(argv_ptr, stralloc("silent"));
        } else
 #endif
 #if defined(VDUMP)
        if (strcmp(file.program, VDUMP) == 0) {
-            restore_args[j++] = stralloc("xf");
-            restore_args[j++] = stralloc("-"); /* data on stdin */
+           g_ptr_array_add(argv_ptr, stralloc("xf"));
+           g_ptr_array_add(argv_ptr, stralloc("-"));   /* data on stdin */
        } else
 #endif
        {
-        restore_args[j++] = stralloc("xbf");
-        restore_args[j++] = stralloc("2");     /* read in units of 1K */
-        restore_args[j++] = stralloc("-");     /* data on stdin */
+       g_ptr_array_add(argv_ptr, stralloc("xbf"));
+       g_ptr_array_add(argv_ptr, stralloc("2")); /* read in units of 1K */
+       g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
        }
 #endif
     }
-  
+
     for (i = 0, fn = elist->files; i < files_off_tape; i++, fn = fn->next)
     {
        switch (dumptype) {
-       case IS_TAR:
-       case IS_GNUTAR:
-       case IS_SAMBA_TAR:
-       case IS_SAMBA:
+       case IS_TAR:
+       case IS_GNUTAR:
+       case IS_SAMBA_TAR:
+       case IS_SAMBA:
            if (strcmp(fn->path, "/") == 0)
-               restore_args[j++] = stralloc(".");
+               g_ptr_array_add(argv_ptr, stralloc("."));
            else
-               restore_args[j++] = stralloc2(".", fn->path);
+               g_ptr_array_add(argv_ptr, stralloc2(".", fn->path));
            break;
        case IS_UNKNOWN:
        case IS_DUMP:
@@ -1771,28 +1737,28 @@ extract_files_child(
                 * xfsrestore needs a -s option before each file to be
                 * restored, and also wants them to be relative paths.
                 */
-               restore_args[j++] = stralloc("-s");
-               restore_args[j++] = stralloc(fn->path + 1);
+               g_ptr_array_add(argv_ptr, stralloc("-s"));
+               g_ptr_array_add(argv_ptr, stralloc(fn->path + 1));
            } else
 #endif
            {
-           restore_args[j++] = stralloc(fn->path);
+               g_ptr_array_add(argv_ptr, stralloc(fn->path));
            }
-       }
+       }
     }
 #if defined(XFSDUMP)
     if (strcmp(file.program, XFSDUMP) == 0) {
-       restore_args[j++] = stralloc("-");
-       restore_args[j++] = stralloc(".");
+       g_ptr_array_add(argv_ptr, stralloc("-"));
+       g_ptr_array_add(argv_ptr, stralloc("."));
     }
 #endif
-    restore_args[j] = NULL;
+    g_ptr_array_add(argv_ptr, NULL);
 
     switch (dumptype) {
     case IS_SAMBA:
 #ifdef SAMBA_CLIENT
-       cmd = stralloc(SAMBA_CLIENT);
-       break;
+       cmd = stralloc(SAMBA_CLIENT);
+       break;
 #else
        /* fall through to ... */
 #endif
@@ -1805,28 +1771,28 @@ extract_files_child(
 #else
        cmd = stralloc(GNUTAR);
 #endif
-       break;
+       break;
     case IS_UNKNOWN:
     case IS_DUMP:
        cmd = NULL;
 #if defined(DUMP)
        if (strcmp(file.program, DUMP) == 0) {
-           cmd = stralloc(RESTORE);
+           cmd = stralloc(RESTORE);
        }
 #endif
 #if defined(VDUMP)
        if (strcmp(file.program, VDUMP) == 0) {
-           cmd = stralloc(VRESTORE);
+           cmd = stralloc(VRESTORE);
        }
 #endif
 #if defined(VXDUMP)
        if (strcmp(file.program, VXDUMP) == 0) {
-           cmd = stralloc(VXRESTORE);
+           cmd = stralloc(VXRESTORE);
        }
 #endif
 #if defined(XFSDUMP)
        if (strcmp(file.program, XFSDUMP) == 0) {
-           cmd = stralloc(XFSRESTORE);
+           cmd = stralloc(XFSRESTORE);
        }
 #endif
        if (cmd == NULL) {
@@ -1837,20 +1803,17 @@ extract_files_child(
     }
     if (cmd) {
         dbprintf(_("Exec'ing %s with arguments:\n"), cmd);
-       for (i = 0; i < j; i++) {
-           if( i == passwd_field)
+       for (j = 0; j < argv_ptr->len - 1; j++) {
+           if( j == passwd_field)
                dbprintf("\tXXXXX\n");
            else
-               dbprintf("\t%s\n", restore_args[i]);
+               dbprintf("\t%s\n", (char *)g_ptr_array_index(argv_ptr, j));
        }
        safe_fd(-1, 0);
-        (void)execv(cmd, restore_args);
+        (void)execv(cmd, (char **)argv_ptr->pdata);
        /* only get here if exec failed */
        save_errno = errno;
-       for (i = 0; i < j; i++) {
-           amfree(restore_args[i]);
-       }
-       amfree(restore_args);
+       g_ptr_array_free_full(argv_ptr);
        errno = save_errno;
         perror(_("amrecover couldn't exec"));
         g_fprintf(stderr, _(" problem executing %s\n"), cmd);
@@ -1923,21 +1886,21 @@ writer_intermediary(
        timeout.tv_sec = READ_TIMEOUT;
        timeout.tv_usec = 0;
        FD_COPY(&readset, &selectset);
-        
+
        nfound = select(max_fd, &selectset, NULL, NULL,
                        &timeout);
        if(nfound < 0) {
            g_fprintf(stderr,_("select error: %s\n"), strerror(errno));
            break;
        }
-        
+
        if (nfound == 0) { /* timeout */
            g_fprintf(stderr, _("timeout waiting %d seconds for restore\n"),
                    READ_TIMEOUT);
            g_fprintf(stderr, _("increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n"));
            break;
        }
-        
+
        if(FD_ISSET(ctl_fd, &selectset)) {
            bytes_read = read(ctl_fd, buffer, sizeof(buffer)-1);
            switch(bytes_read) {
@@ -1950,14 +1913,14 @@ writer_intermediary(
                     FD_CLR(ctl_fd, &readset);
                 }
                 break;
-                
+
             case  0:
                 FD_CLR(ctl_fd, &readset);
                 break;
-                
+
             default: {
                 char desired_tape[MAX_TAPE_LABEL_BUF];
-                
+
                 buffer[bytes_read] = '\0';
                 /* if prompted for a tape, relay said prompt to the user */
                 if(sscanf(buffer, "FEEDME %132s\n", desired_tape) == 1) {
@@ -1967,14 +1930,14 @@ writer_intermediary(
                         g_printf(_("Please insert tape %s. Continue? [Y|n]: "),
                                desired_tape);
                         fflush(stdout);
-                        
+
                         input = agets(stdin); /* strips \n */
-                        if (strcasecmp("", input) == 0|| 
-                            strcasecmp("y", input) == 0|| 
+                        if (strcasecmp("", input) == 0||
+                            strcasecmp("y", input) == 0||
                             strcasecmp("yes", input) == 0) {
                             send_to_tape_server(tape_control_sock, "OK");
                             done = 1;
-                        } else if (strcasecmp("n", input) == 0|| 
+                        } else if (strcasecmp("n", input) == 0||
                                    strcasecmp("no", input) == 0) {
                             send_to_tape_server(tape_control_sock, "ERROR");
                             /* Abort!
@@ -1986,7 +1949,7 @@ writer_intermediary(
                 } else {
                     g_fprintf(stderr, _("Strange message from tape server: %s"), buffer);
                    break;
-                }    
+                }
              }
             }
         }
@@ -2004,11 +1967,11 @@ writer_intermediary(
                     FD_CLR(data_fd, &readset);
                 }
                 break;
-                
+
             case  0:
                 FD_CLR(data_fd, &readset);
                 break;
-                
+
             default:
                 /*
                  * spit what we got from the server to the child
@@ -2117,7 +2080,7 @@ extract_files(void)
            }
            else
                g_printf("                               ");
-           tlist = unmarshal_tapelist_str(elist->tape); 
+           tlist = unmarshal_tapelist_str(elist->tape);
            for( ; tlist != NULL; tlist = tlist->next)
                g_printf(" %s", tlist->label);
            g_printf("\n");
@@ -2135,7 +2098,7 @@ extract_files(void)
            }
            else
                g_printf("                               ");
-           tlist = unmarshal_tapelist_str(elist->tape); 
+           tlist = unmarshal_tapelist_str(elist->tape);
            for( ; tlist != NULL; tlist = tlist->next)
                g_printf(" %s", tlist->label);
            g_printf("\n");
@@ -2163,7 +2126,7 @@ extract_files(void)
        if(elist->tape[0]=='/') {
            dump_device_name = newstralloc(dump_device_name, elist->tape);
            g_printf(_("Extracting from file "));
-           tlist = unmarshal_tapelist_str(dump_device_name); 
+           tlist = unmarshal_tapelist_str(dump_device_name);
            for( ; tlist != NULL; tlist = tlist->next)
                g_printf(" %s", tlist->label);
            g_printf("\n");
@@ -2172,7 +2135,7 @@ extract_files(void)
        else {
            g_printf(_("Extracting files using tape drive %s on host %s.\n"),
                   tape_device_name, tape_server_name);
-           tlist = unmarshal_tapelist_str(elist->tape); 
+           tlist = unmarshal_tapelist_str(elist->tape);
            g_printf(_("Load tape %s now\n"), tlist->label);
            amfree(tlist);
            otc = okay_to_continue(1,1,0);