]> git.gag.com Git - debian/amanda/blobdiff - tape-src/tapetype.c
Imported Upstream version 2.5.0p2
[debian/amanda] / tape-src / tapetype.c
index 9c955a3b8fca234cebf10abed0d5b3f5cab15f1e..dd4758187b91b398b3cb48265c3bc71dcefbe222 100644 (file)
@@ -25,7 +25,7 @@
  *                        University of Maryland at College Park
  */
 /*
- * $Id: tapetype.c,v 1.3.2.3.4.3.2.9 2003/11/28 12:34:52 martinea Exp $
+ * $Id: tapetype.c,v 1.24 2006/03/10 11:56:06 martinea Exp $
  *
  * tests a tape in a given tape unit and prints a tapetype entry for
  * it.  */
@@ -45,6 +45,7 @@ static int blockkb = 32;
 static int blocksize;
 
 static char *randombytes = (char *) NULL;
+static char *prandombytes = (char *) NULL;
 
 #if USE_RAND
 /* If the C library does not define random(), try to use rand() by
@@ -74,6 +75,7 @@ static void allocrandombytes() {
     } else {
       randombytes = p;                         /* alloc already on boundary */
     }
+    prandombytes = p;
   }
 }
 
@@ -146,8 +148,9 @@ void usage()
 {
   fputs("usage: ", stderr);
   fputs(sProgName, stderr);
-  fputs(" -h", stderr);
+  fputs(" [-h]", stderr);
   fputs(" [-c]", stderr);
+  fputs(" [-o]", stderr);
   fputs(" [-b blocksize]", stderr);
   fputs(" [-e estsize]", stderr);
   fputs(" [-f tapedev]", stderr);
@@ -161,6 +164,7 @@ void help()
   fputs("\
   -h                   display this message\n\
   -c                   run hardware compression detection test only\n\
+  -o                   overwrite amanda tape\n\
   -b blocksize         record block size (default: 32k)\n\
   -e estsize           estimated tape size (default: 1g == 1024m)\n\
   -f tapedev           tape device name (default: $TAPE)\n\
@@ -307,6 +311,12 @@ int main(argc, argv)
   time_t now;
   int hwcompr = 0;
   int comprtstonly = 0;
+  int overwrite_label = 0;
+  int is_labeled = 0;
+  char *result;
+  char *datestamp = NULL;
+  char *label = NULL;
+
 
   if ((sProgName = strrchr(*argv, '/')) == NULL) {
     sProgName = *argv;
@@ -314,11 +324,14 @@ int main(argc, argv)
     sProgName++;
   }
 
+  /* Don't die when child closes pipe */
+  signal(SIGPIPE, SIG_IGN);
+
   estsize = 1024 * 1024;                       /* assume 1 GByte for now */
   tapedev = getenv("TAPE");
   typename = "unknown-tapetype";
 
-  while ((ch = getopt(argc, argv, "b:e:f:t:hc")) != EOF) {
+  while ((ch = getopt(argc, argv, "b:e:f:t:hco")) != EOF) {
     switch (ch) {
     case 'b':
       blockkb = strtol(optarg, &suffix, 0);
@@ -357,6 +370,9 @@ int main(argc, argv)
       help();
       return 1;
       break;
+    case 'o':
+      overwrite_label=1;
+      break;
     default:
       fprintf(stderr, "%s: unknown option \'%c\'\n", sProgName, ch);
       /* fall through to ... */
@@ -373,6 +389,43 @@ int main(argc, argv)
     return 1;
   }
 
+/* verifier tape */
+
+
+  fd = tape_open(tapedev, O_RDONLY);
+  if (fd == -1) {
+    fprintf(stderr, "%s: could not open %s: %s\n",
+           sProgName, tapedev, strerror(errno));
+    return 1;
+  }
+
+  if((result = tapefd_rdlabel(fd, &datestamp, &label)) == NULL) {
+    is_labeled = 1;
+  }
+  else if (strcmp(result,"not an amanda tape") == 0) {
+    is_labeled = 2;
+  }
+
+  if(tapefd_rewind(fd) == -1) {
+    fprintf(stderr, "%s: could not rewind %s: %s\n",
+           sProgName, tapedev, strerror(errno));
+    tapefd_close(fd);
+    return 1;
+  }
+
+  tapefd_close(fd);
+
+  if(is_labeled == 1 && overwrite_label == 0) {
+    fprintf(stderr, "%s: The tape is an amanda tape, use -o to overwrite the tape\n",
+           sProgName);
+    return 1;
+  }
+  else if(is_labeled == 2 && overwrite_label == 0) {
+    fprintf(stderr, "%s: The tape is already used, use -o to overwrite the tape\n",
+           sProgName);
+    return 1;
+  }
+
   fd = tape_open(tapedev, O_RDWR);
   if (fd == -1) {
     fprintf(stderr, "%s: could not open %s: %s\n",
@@ -462,7 +515,7 @@ int main(argc, argv)
   /*
    * Inform about estimated time needed to run the remaining of this program
    */
-  fprintf(stderr, "Estimated time to write 2 * %d Mbyte: ", estsize / 1024);
+  fprintf(stderr, "Estimated time to write 2 * %lu Mbyte: ", (unsigned long) (estsize / 1024));
   pass1time = (time_t)(2.0 * pass2time * estsize / (1.0 * pass0size * blockkb));
        /* avoid overflow and underflow by doing math in floating point */
   fprintf(stderr, "%ld sec = ", pass1time);
@@ -551,14 +604,17 @@ int main(argc, argv)
   if (tapefd_rewind(fd) == -1) {
     fprintf(stderr, "%s: could not rewind %s: %s\n",
            sProgName, tapedev, strerror(errno));
+    free(randombytes);
     return 1;
   }
 
   if (tapefd_close(fd) == -1) {
     fprintf(stderr, "%s: could not close %s: %s\n",
            sProgName, tapedev, strerror(errno));
+    free(randombytes);
     return 1;
   }
 
+  free(randombytes);
   return 0;
 }