Imported Upstream version 2.5.1
[debian/amanda] / common-src / tapelist.c
index 8d39cab6b5bbdd36c9b18c94b88bd2e3388a9c4d..194db84287c5ce5ddc74db72fdeb581e00a85278 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: tapelist.c,v 1.3 2006/01/15 21:00:59 martinea Exp $
+ * $Id: tapelist.c,v 1.8 2006/06/12 15:34:48 martinea Exp $
  *
  * Support code for amidxtaped and amindexd.
  */
 /*
  * Count the number of entries in this tapelist
  */
-int num_entries (tapelist)
-tapelist_t *tapelist;
+int
+num_entries(
+    tapelist_t *       tapelist)
 {
     tapelist_t *cur_tape;
     int count = 0;
 
-    for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next) count++;
+    for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next)
+       count++;
 
+    dbprintf(("num_entries(tapelist=%p)=%d\n", tapelist, count));
     return(count);
 }
 
+void
+dump_tapelist(
+    tapelist_t *tapelist)
+{
+    tapelist_t *cur_tape;
+    int count = 0;
+    int file;
+
+    dbprintf(("dump_tapelist(%p):\n", tapelist));
+    for(cur_tape = tapelist ; cur_tape != NULL ; cur_tape = cur_tape->next) {
+       dbprintf(("  %p->next     = %p\n", cur_tape, cur_tape->next));
+       dbprintf(("  %p->label    = %s\n", cur_tape, cur_tape->label));
+       dbprintf(("  %p->isafile  = %d\n", cur_tape, cur_tape->isafile));
+       dbprintf(("  %p->numfiles = %d\n", cur_tape, cur_tape->numfiles));
+       for (file=0; file < cur_tape->numfiles; file++) {
+           dbprintf(("  %p->files[%d] = " OFF_T_FMT "\n",
+                    cur_tape, file, (OFF_T_FMT_TYPE)cur_tape->files[file]));
+       }
+       count++;
+    }
+    dbprintf(("  %p count     = %d\n", tapelist, count));
+}
+
 /*
  * Add a tape entry with the given label to the given tapelist, creating a new
  * tapelist if handed a NULL one.  Squashes duplicates.
  */
-tapelist_t *append_to_tapelist (tapelist, label, file, isafile)
-tapelist_t *tapelist;
-char *label;
-int file, isafile;
+tapelist_t *
+append_to_tapelist(
+    tapelist_t *tapelist,
+    char *     label,
+    off_t      file,
+    int                isafile)
 {
     tapelist_t *new_tape, *cur_tape;
     int c;
 
+    dbprintf(("append_to_tapelist(tapelist=%p, label='%s', , file="
+               OFF_T_FMT ", isafile=%d)\n",
+               tapelist, label, (OFF_T_FMT_TYPE)file, isafile));
+
     /* see if we have this tape already, and if so just add to its file list */
-    for(cur_tape = tapelist; cur_tape; cur_tape = cur_tape->next){
-       if(!strcmp(label, cur_tape->label)){
+    for(cur_tape = tapelist; cur_tape; cur_tape = cur_tape->next) {
+       if(strcmp(label, cur_tape->label) == 0) {
            int d_idx = 0;
-           int *newfiles;
-           if(file >= 0){
-               newfiles = alloc(sizeof(int) * (cur_tape->numfiles + 1));
-               for(c = 0; c < cur_tape->numfiles ; c++){
-                   if(cur_tape->files[c] > file && c == d_idx){
+           off_t *newfiles;
+
+           if(file >= (off_t)0) {
+               newfiles = alloc(SIZEOF(*newfiles) *
+                                (cur_tape->numfiles + 1));
+               for(c = 0; c < cur_tape->numfiles ; c++) {
+                   if(cur_tape->files[c] > file && c == d_idx) {
                        newfiles[d_idx] = file;
                        d_idx++;
                    }
                    newfiles[d_idx] = cur_tape->files[c];
                    d_idx++;
                }
-               if(c == d_idx) newfiles[d_idx] = file;
+               if(c == d_idx)
+                   newfiles[d_idx] = file;
                cur_tape->numfiles++;
                amfree(cur_tape->files);
                cur_tape->files = newfiles;
            }
+           dump_tapelist(tapelist);
            return(tapelist);
        }
     }
 
-    new_tape = alloc(sizeof(tapelist_t));
-    memset(new_tape, 0, sizeof(tapelist_t));
+    new_tape = alloc(SIZEOF(tapelist_t));
+    memset(new_tape, 0, SIZEOF(tapelist_t));
     new_tape->label = stralloc(label);
-    if(file >= 0){
-       new_tape->files = alloc(sizeof(int));
+    if(file >= (off_t)0){
+       new_tape->files = alloc(SIZEOF(*(new_tape->files)));
        new_tape->files[0] = file;
        new_tape->numfiles = 1;
        new_tape->isafile = isafile;
@@ -95,21 +131,24 @@ int file, isafile;
     /* first instance of anything, start our tapelist with it */
     if(!tapelist){
        tapelist = new_tape;
-       return(tapelist);
+    } else {
+       /* new tape, tack it onto the end of the list */
+       cur_tape = tapelist;
+       while (cur_tape->next != NULL)
+           cur_tape = cur_tape->next;
+       cur_tape->next = new_tape;
     }
 
-    /* new tape, tack it onto the end of the list */
-    for(cur_tape = tapelist; cur_tape->next; cur_tape = cur_tape->next);
-    cur_tape->next = new_tape;
-
+    dump_tapelist(tapelist);
     return(tapelist);
 }
 
 /*
  * Backslash-escape all of the commas (and backslashes) in a label string.
  */
-char *escape_label (label)
-char *label;
+char *
+escape_label(
+    char *     label)
 {
     char *cooked_str, *temp_str;
     int s_idx = 0, d_idx = 0;
@@ -139,8 +178,9 @@ char *label;
 /*
  * Strip out any escape characters (backslashes)
  */
-char *unescape_label(label)
-char *label;
+char *
+unescape_label(
+    char *     label)
 {
     char *cooked_str, *temp_str;
     int s_idx = 0, d_idx = 0, prev_esc = 0;
@@ -171,9 +211,10 @@ char *label;
 /*
  * Convert a tapelist into a parseable string of tape labels and file numbers.
  */
-char *marshal_tapelist (tapelist, do_escape)
-tapelist_t *tapelist;
-int do_escape;
+char *
+marshal_tapelist(
+    tapelist_t *tapelist,
+    int                do_escape)
 {
     tapelist_t *cur_tape;
     char *str = NULL;
@@ -188,13 +229,18 @@ int do_escape;
 
        for(c = 0; c < cur_tape->numfiles ; c++){
            char num_str[NUM_STR_SIZE];
-           snprintf(num_str, sizeof(num_str), "%d", cur_tape->files[c]);
-           if(!files_str) files_str = stralloc(num_str);
-           else files_str = vstralloc(files_str, ",", num_str, NULL);
+           snprintf(num_str, SIZEOF(num_str), OFF_T_FMT,
+                       (OFF_T_FMT_TYPE)cur_tape->files[c]);
+           if (!files_str)
+               files_str = stralloc(num_str);
+           else
+               vstrextend(&files_str, ",", num_str, NULL);
        }
 
-       if(!str) str = vstralloc(esc_label, ":", files_str, NULL);
-       else str = vstralloc(str, ";", esc_label, ":", files_str, NULL);
+       if (!str)
+           str = vstralloc(esc_label, ":", files_str, NULL);
+       else
+           vstrextend(&str, ";", esc_label, ":", files_str, NULL);
 
        amfree(esc_label);
        amfree(files_str);
@@ -207,11 +253,13 @@ int do_escape;
  * Convert a previously str-ified and escaped list of tapes back into a
  * tapelist structure.
  */
-tapelist_t *unmarshal_tapelist_str (tapelist_str)
-char *tapelist_str;
+tapelist_t *
+unmarshal_tapelist_str(
+    char *     tapelist_str)
 {
     char *temp_label, *temp_filenum;
-    int l_idx, n_idx, input_length;
+    int l_idx, n_idx;
+    size_t input_length;
     tapelist_t *tapelist = NULL;
 
     if(!tapelist_str) return(NULL);
@@ -226,32 +274,38 @@ char *tapelist_str;
        memset(temp_label, '\0', input_length+1);
         l_idx = 0;
        while(*tapelist_str != ':' && *tapelist_str != '\0'){
-           if(*tapelist_str == '\\') *tapelist_str++; /* skip escapes */
+           if(*tapelist_str == '\\')
+               tapelist_str++; /* skip escapes */
            temp_label[l_idx] = *tapelist_str;
-           if(*tapelist_str == '\0') break; /* bad format, should kvetch */
-           *tapelist_str++;
+           if(*tapelist_str == '\0')
+               break; /* bad format, should kvetch */
+           tapelist_str++;
            l_idx++;
        }
-       if(*tapelist_str != '\0') *tapelist_str++;
-       tapelist = append_to_tapelist(tapelist, temp_label, -1, 0);
+       if(*tapelist_str != '\0')
+           tapelist_str++;
+       tapelist = append_to_tapelist(tapelist, temp_label, (off_t)-1, 0);
 
        /* now read the list of file numbers */
        while(*tapelist_str != ';' && *tapelist_str != '\0'){
-           int filenum = -1;
+           off_t filenum;
+
            memset(temp_filenum, '\0', input_length+1);
            n_idx = 0;
            while(*tapelist_str != ';' && *tapelist_str != ',' &&
                    *tapelist_str != '\0'){
                temp_filenum[n_idx] = *tapelist_str; 
-               *tapelist_str++;
+               tapelist_str++;
                n_idx++;
            }
-           filenum = atoi(temp_filenum);
+           filenum = OFF_T_ATOI(temp_filenum);
 
            tapelist = append_to_tapelist(tapelist, temp_label, filenum, 0);
-           if(*tapelist_str != '\0' && *tapelist_str != ';') *tapelist_str++;
+           if(*tapelist_str != '\0' && *tapelist_str != ';')
+               tapelist_str++;
        }
-       if(*tapelist_str != '\0') *tapelist_str++;
+       if(*tapelist_str != '\0')
+           tapelist_str++;
 
     } while(*tapelist_str != '\0');
 
@@ -264,18 +318,18 @@ char *tapelist_str;
 /*
  * Free up a list of tapes
  */
-void free_tapelist (tapelist)
-tapelist_t *tapelist;
+void
+free_tapelist(
+    tapelist_t *       tapelist)
 {
     tapelist_t *cur_tape;
     tapelist_t *prev = NULL;
 
     for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next){
-       if(cur_tape->label) amfree(cur_tape->label);
-       if(cur_tape->files) amfree(cur_tape->files);
-       if(prev) amfree(prev);
+       amfree(cur_tape->label);
+       amfree(cur_tape->files);
+       amfree(prev);
        prev = cur_tape;
     }
-    if(prev) amfree(prev);
-
+    amfree(prev);
 }