Imported Upstream version 3.3.0
[debian/amanda] / server-src / tapefile.c
index 9a9cf3d5f4498b9ec0f0d53c5ebee516d680e1e5..b4212e5894238b47bc4d49e9801938ba26f4d78e 100644 (file)
@@ -29,6 +29,7 @@
  * routines to read and write the amanda active tape list
  */
 #include "amanda.h"
+#include "match.h"
 #include "tapefile.h"
 #include "conffile.h"
 
@@ -51,7 +52,13 @@ read_tapelist(
 
     tape_list = NULL;
     if((tapef = fopen(tapefile,"r")) == NULL) {
-       return 1;
+       if (errno == ENOENT) {
+           /* no tapelist is equivalent to an empty tapelist */
+           return 0;
+       } else {
+           g_debug("Error opening '%s': %s", tapefile, strerror(errno));
+           return 1;
+       }
     }
 
     while((line = agets(tapef)) != NULL) {
@@ -95,6 +102,12 @@ write_tapelist(
        g_fprintf(tapef, "%s %s", tp->datestamp, tp->label);
        if(tp->reuse) g_fprintf(tapef, " reuse");
        else g_fprintf(tapef, " no-reuse");
+       if (tp->barcode)
+           g_fprintf(tapef, " BARCODE:%s", tp->barcode);
+       if (tp->meta)
+           g_fprintf(tapef, " META:%s", tp->meta);
+       if (tp->comment)
+           g_fprintf(tapef, " #%s", tp->comment);
        g_fprintf(tapef, "\n");
     }
 
@@ -125,7 +138,7 @@ clear_tapelist(void)
 
 tape_t *
 lookup_tapelabel(
-    char *label)
+    const char *label)
 {
     tape_t *tp;
 
@@ -174,6 +187,15 @@ lookup_nb_tape(void)
     return pos;
 }
 
+
+char *
+get_last_reusable_tape_label(
+     int skip)
+{
+    tape_t *tp = lookup_last_reusable_tape(skip);
+    return (tp != NULL) ? tp->label : NULL;
+}
+
 tape_t *
 lookup_last_reusable_tape(
      int skip)
@@ -258,7 +280,8 @@ remove_tapelabel(
 tape_t *
 add_tapelabel(
     char *datestamp,
-    char *label)
+    char *label,
+    char *comment)
 {
     tape_t *cur, *new;
 
@@ -270,6 +293,7 @@ add_tapelabel(
     new->position = 0;
     new->reuse = 1;
     new->label = stralloc(label);
+    new->comment = comment? stralloc(comment) : NULL;
 
     new->prev  = NULL;
     if(tape_list != NULL) tape_list->prev = new;
@@ -362,10 +386,46 @@ parse_tapeline(
 
     skip_whitespace(s, ch);
     tp->reuse = 1;
-    if(strncmp_const(s - 1, "reuse") == 0)
+    if(strncmp_const(s - 1, "reuse") == 0) {
        tp->reuse = 1;
-    if(strncmp_const(s - 1, "no-reuse") == 0)
+       s1 = s - 1;
+       skip_non_whitespace(s, ch);
+       s[-1] = '\0';
+       skip_whitespace(s, ch);
+    }
+    if(strncmp_const(s - 1, "no-reuse") == 0) {
        tp->reuse = 0;
+       s1 = s - 1;
+       skip_non_whitespace(s, ch);
+       s[-1] = '\0';
+       skip_whitespace(s, ch);
+    }
+
+    if (strncmp_const(s - 1, "BARCODE:") == 0) {
+       s1 = s - 1 + 8;
+       skip_non_whitespace(s, ch);
+       s[-1] = '\0';
+       skip_whitespace(s, ch);
+       tp->barcode = stralloc(s1);
+    } else {
+       tp->barcode = NULL;
+    }
+
+    if (strncmp_const(s - 1, "META:") == 0) {
+       s1 = s - 1 + 5;
+       skip_non_whitespace(s, ch);
+       s[-1] = '\0';
+       skip_whitespace(s, ch);
+       tp->meta = stralloc(s1);
+    } else {
+       tp->meta = NULL;
+    }
+
+    if (*(s - 1) == '#') {
+       tp->comment = stralloc(s); /* skip leading '#' */
+    } else {
+       tp->comment = NULL;
+    }
 
     return tp;
 }
@@ -444,12 +504,12 @@ stamp2time(
     return mktime(tm);
 }
 
-void
-print_new_tapes(
-    FILE *output,
-    int   nb)
+char *
+list_new_tapes(
+    int nb)
 {
     tape_t *lasttp, *iter;
+    char *result = NULL;
 
     /* Find latest reusable new tape */
     lasttp = lookup_tapepos(lookup_nb_tape());
@@ -469,23 +529,36 @@ print_new_tapes(
        }
 
        if(c == 1) {
-           g_fprintf(output,
-                     _("The next new tape already labelled is: %s.\n"),
-                     lasttp->label);
+           result = g_strdup_printf(
+                       _("The next new tape already labelled is: %s."),
+                       lasttp->label);
        } else {
-           g_fprintf(output,
-                     _("The next %d new tapes already labelled are: %s"),
-                     c, lasttp->label);
+           result = g_strdup_printf(
+                       _("The next %d new tapes already labelled are: %s"),
+                       c, lasttp->label);
            iter = lasttp->prev;
            c--;
            while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
                if (iter->reuse) {
-                   g_fprintf(output, ", %s", iter->label);
+                   result = vstrextend(&result, ", ", iter->label, NULL);
                    c--;
                }
                iter = iter->prev;
            }
-           g_fprintf(output, ".\n");
        }
     }
+    return result;
+}
+
+void
+print_new_tapes(
+    FILE *output,
+    int   nb)
+{
+    char *result = list_new_tapes(nb);
+
+    if (result) {
+       g_fprintf(output,"%s\n", result);
+       amfree(result);
+    }
 }