Imported Upstream version 2.5.1p3
[debian/amanda] / recover-src / display_commands.c
index 1797db5307ffeafcc83f8a34e5e98bd130eabc27..1a5b63876c42173cb0b63c0d873d226cea3e18b1 100644 (file)
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: display_commands.c,v 1.12.4.1.6.3 2002/10/27 21:13:25 martinea Exp $
+ * $Id: display_commands.c,v 1.22.2.1 2006/12/22 15:10:26 martinea Exp $
  *
  * implements the directory-display related commands in amrecover
  */
 
 #include "amanda.h"
 #include "amrecover.h"
+#include "util.h"
+
+DIR_ITEM *get_dir_list(void);
+DIR_ITEM *get_next_dir_item(DIR_ITEM *this);
+
+void clear_dir_list(void);
+void free_dir_item(DIR_ITEM *item);
+static int add_dir_list_item(char *date,
+                               int level,
+                               char *tape,
+                               off_t fileno,
+                               char *path);
+void list_disk_history(void);
+void suck_dir_list_from_server(void);
+void list_directory(void);
 
 static DIR_ITEM *dir_list = NULL;
 
-DIR_ITEM *get_dir_list P((void))
+DIR_ITEM *
+get_dir_list(void)
 {
     return dir_list;
 }
 
-DIR_ITEM *get_next_dir_item(this)
-DIR_ITEM *this;
+DIR_ITEM *
+get_next_dir_item(
+    DIR_ITEM * this)
 {
+    /*@ignore@*/
     return this->next;
+    /*@end@*/
 }
 
 
-void clear_dir_list P((void))
+void
+clear_dir_list(void)
 {
-    DIR_ITEM *this;
+    free_dir_item(dir_list); /* Frees all items from dir_list to end of list */
+    dir_list = NULL;
+}
 
-    if (dir_list == NULL)
-       return;
-    do
-    {
-       this = dir_list;
-       dir_list = dir_list->next;
-       amfree(this);
-    } while (dir_list != NULL);
+void
+free_dir_item(
+    DIR_ITEM * item)
+{
+    DIR_ITEM *next;
+
+    while (item != NULL) {
+       next = item->next;
+        amfree(item->date);
+        amfree(item->tape);
+        amfree(item->path);
+        amfree(item);
+       item = next;
+    }
 }
 
 /* add item to list if path not already on list */
-static int add_dir_list_item(date, level, tape, fileno, path)
-char *date;
-int level;
-char *tape;
-int fileno;
-char *path;
+static int
+add_dir_list_item(
+    char *     date,
+    int                level,
+    char *     tape,
+    off_t      fileno,
+    char *     path)
 {
-    DIR_ITEM *last;
+    DIR_ITEM *next;
 
-    dbprintf(("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \"%d\" \"%s\"\n",
-             date, level, tape, fileno, path));
+    dbprintf(("add_dir_list_item: Adding \"%s\" \"%d\" \"%s\" \""
+             OFF_T_FMT "\" \"%s\"\n",
+             date, level, tape, (OFF_T_FMT_TYPE)fileno, path));
 
-    if (dir_list == NULL)
-    {
-       dir_list = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
-       dir_list->next = NULL;
-       strncpy(dir_list->date, date, sizeof(dir_list->date)-1);
-       dir_list->date[sizeof(dir_list->date)-1] = '\0';
-       dir_list->level = level;
-       strncpy(dir_list->tape, tape, sizeof(dir_list->tape)-1);
-       dir_list->tape[sizeof(dir_list->tape)-1] = '\0';
-       dir_list->fileno = fileno;
-       strncpy(dir_list->path, path, sizeof(dir_list->path)-1);
-       dir_list->path[sizeof(dir_list->path)-1] = '\0';
-
-       return 0;
-    }
+    next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
+    memset(next, 0, sizeof(DIR_ITEM));
 
-    last = dir_list;
-    while (last->next != NULL)
-    {
-       last = last->next;
-    }
+    next->date = stralloc(date);
+    next->level = level;
+    next->tape = stralloc(tape);
+    next->fileno = fileno;
+    next->path = stralloc(path);
 
-    last->next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
-    last->next->next = NULL;
-    strncpy(last->next->date, date, sizeof(last->next->date)-1);
-    last->next->date[sizeof(last->next->date)-1] = '\0';
-    last->next->level = level;
-    strncpy(last->next->tape, tape, sizeof(last->next->tape)-1);
-    last->next->tape[sizeof(last->next->tape)-1] = '\0';
-    last->next->fileno = fileno;
-    strncpy(last->next->path, path, sizeof(last->next->path)-1);
-    last->next->path[sizeof(last->next->path)-1] = '\0';
+    next->next = dir_list;
+    dir_list = next;
 
     return 0;
 }
 
 
-void list_disk_history P((void))
+void
+list_disk_history(void)
 {
     if (converse("DHST") == -1)
        exit(1);
 }
 
 
-void suck_dir_list_from_server P((void))
+void
+suck_dir_list_from_server(void)
 {
     char *cmd = NULL;
     char *err = NULL;
     int i;
     char *l = NULL;
-    char *date, *date_undo, date_undo_ch = '\0';
-    int level, fileno;
+    char *date;
+    int level = 0;
+    off_t fileno = (off_t)-1;
     char *tape, *tape_undo, tape_undo_ch = '\0';
-    char *dir, *dir_undo, dir_undo_ch = '\0';
+    char *dir, *qdir;
     char *disk_path_slash = NULL;
     char *disk_path_slash_dot = NULL;
     char *s;
     int ch;
+    char *qdisk_path;
 
     if (disk_path == NULL) {
        printf("Directory must be set before getting listing\n");
@@ -143,7 +156,9 @@ void suck_dir_list_from_server P((void))
 
     clear_dir_list();
 
-    cmd = stralloc2("OLSD ", disk_path);
+    qdisk_path = quote_string(disk_path);
+    cmd = stralloc2("OLSD ", qdisk_path);
+    amfree(qdisk_path);
     if (send_command(cmd) == -1) {
        amfree(cmd);
        amfree(disk_path_slash);
@@ -165,7 +180,7 @@ void suck_dir_list_from_server P((void))
     disk_path_slash_dot = stralloc2(disk_path_slash, ".");
     amfree(cmd);
     amfree(err);
-    date_undo = tape_undo = dir_undo = NULL;
+    tape_undo = NULL;
     /* skip the last line -- duplicate of the preamble */
     while ((i = get_reply_line()) != 0)
     {
@@ -177,8 +192,7 @@ void suck_dir_list_from_server P((void))
        if(err) {
            if(cmd == NULL) {
                if(tape_undo) *tape_undo = tape_undo_ch;
-               if(dir_undo) *dir_undo = dir_undo_ch;
-               date_undo = tape_undo = dir_undo = NULL;
+               tape_undo = NULL;
                cmd = stralloc(l);      /* save for the error report */
            }
            continue;                   /* throw the rest of the lines away */
@@ -204,9 +218,7 @@ void suck_dir_list_from_server P((void))
        }
        date = s - 1;
        skip_non_whitespace(s, ch);
-       date_undo = s - 1;
-       date_undo_ch = *date_undo;
-       *date_undo = '\0';
+       *(s - 1) = '\0';
 
        skip_whitespace(s, ch);
        if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
@@ -226,16 +238,17 @@ void suck_dir_list_from_server P((void))
        tape_undo_ch = *tape_undo;
        *tape_undo = '\0';
 
-       if(am_has_feature(their_features, fe_amindexd_fileno_in_OLSD)) {
+       if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_OLSD)) {
            skip_whitespace(s, ch);
-           if(ch == '\0' || sscanf(s - 1, "%d", &fileno) != 1) {
+           if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
+                                   (OFF_T_FMT_TYPE *)&fileno) != 1) {
                err = "bad reply: cannot parse fileno field";
                continue;
            }
            skip_integer(s, ch);
        }
        else {
-           fileno = -1;
+           fileno = (off_t)-1;
        }
 
        skip_whitespace(s, ch);
@@ -243,13 +256,16 @@ void suck_dir_list_from_server P((void))
            err = "bad reply: missing directory field";
            continue;
        }
-       dir = s - 1;
+       qdir = s - 1;
+       dir = unquote_string(qdir);
 
        /* add a '.' if it a the entry for the current directory */
-       if(strcmp(disk_path,dir)==0 || strcmp(disk_path_slash,dir)==0) {
-           dir = disk_path_slash_dot;
+       if((strcmp(disk_path,dir)==0) || (strcmp(disk_path_slash,dir)==0)) {
+           amfree(dir);
+           dir = stralloc(disk_path_slash_dot);
        }
        add_dir_list_item(date, level, tape, fileno, dir);
+       amfree(dir);
     }
     amfree(disk_path_slash_dot);
     amfree(disk_path_slash);
@@ -259,23 +275,26 @@ void suck_dir_list_from_server P((void))
        if(*err) {
            puts(err);
        }
-       puts(cmd);
+       if (cmd)
+           puts(cmd);
        clear_dir_list();
     }
     amfree(cmd);
 }
 
 
-void list_directory P((void))
+void
+list_directory(void)
 {
     size_t i;
     DIR_ITEM *item;
     FILE *fp;
     char *pager;
     char *pager_command;
+    char *quoted;
 
     if (disk_path == NULL) {
-       printf("Must select a disk before listing files\n");
+       printf("Must select a disk before listing files; use the setdisk command.\n");
        return;
     }
 
@@ -297,7 +316,10 @@ void list_directory P((void))
     i = strlen(disk_path);
     if (i != 1)
        i++;                            /* so disk_path != "/" */
-    for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item))
-       fprintf(fp, "%s %s\n", item->date, item->path+i);
+    for (item = get_dir_list(); item != NULL; item=get_next_dir_item(item)) {
+       quoted = quote_string(item->path + i);
+       fprintf(fp, "%s %s\n", item->date, quoted);
+       amfree(quoted);
+    }
     apclose(fp);
 }