]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dvd.c
kes Fix logic error in handling error return from mtx-changer
[bacula/bacula] / bacula / src / stored / dvd.c
index 6c826ba4c3ab1d118cfc497811d754334810f1f2..3178758b5753c5e318b1aca8ae4ca6bd42b3df28 100644 (file)
@@ -36,7 +36,6 @@ void make_mounted_dvd_filename(DEVICE *dev, POOL_MEM &archive_name)
 {
    pm_strcpy(archive_name, dev->device->mount_point);
    add_file_and_part_name(dev, archive_name);
-   dev->set_part_spooled(false);
 }
 
 void make_spooled_dvd_filename(DEVICE *dev, POOL_MEM &archive_name)
@@ -48,7 +47,6 @@ void make_spooled_dvd_filename(DEVICE *dev, POOL_MEM &archive_name)
       pm_strcpy(archive_name, working_directory);
    }
    add_file_and_part_name(dev, archive_name);
-   dev->set_part_spooled(true);
 }      
 
 static void add_file_and_part_name(DEVICE *dev, POOL_MEM &archive_name)
@@ -65,7 +63,8 @@ static void add_file_and_part_name(DEVICE *dev, POOL_MEM &archive_name)
       bsnprintf(partnumber, sizeof(partnumber), "%d", dev->part);
       pm_strcat(archive_name, partnumber);
    }
-   Dmsg1(400, "Exit add_file_part_name: arch=%s\n", archive_name.c_str());
+   Dmsg2(400, "Exit add_file_part_name: arch=%s, part=%d\n",
+                  archive_name.c_str(), dev->part);
 }  
 
 /* Mount the device.
@@ -138,7 +137,7 @@ static bool do_mount_dvd(DEVICE* dev, int mount, int dotimeout)
    Dmsg1(20, "Run mount prog=%s\n", ocmd.c_str());
    while ((status = run_program_full_output(ocmd.c_str(), 
                        dev->max_open_wait/2, results)) != 0) {
-      Dmsg2(99, "Mount status=%d result=%s\n", status, results);
+      Dmsg2(20, "Mount status=%d result=%s\n", status, results);
       /* Doesn't work with internationalization (This is not a problem) */
       if (mount && fnmatch("*is already mounted on*", results, 0) == 0) {
          break;
@@ -261,6 +260,10 @@ bool update_free_space_dev(DEVICE* dev)
    char ed1[50];
    bool ok = false;
    int status;
+
+   if (!dev->is_dvd() || dev->is_freespace_ok()) {
+      return true;
+   }
    
    /* The device must be mounted in order to dvd-freespace to work */
    mount_dvd(dev, 1);
@@ -271,7 +274,7 @@ bool update_free_space_dev(DEVICE* dev)
    if (!icmd) {
       dev->free_space = 0;
       dev->free_space_errno = 0;
-      dev->clear_freespace_ok();   /* No valid freespace */
+      dev->clear_freespace_ok();              /* No valid freespace */
       dev->clear_media();
       Dmsg2(29, "ERROR: update_free_space_dev: free_space=%s, free_space_errno=%d (!icmd)\n", 
             edit_uint64(dev->free_space, ed1), dev->free_space_errno);
@@ -292,6 +295,7 @@ bool update_free_space_dev(DEVICE* dev)
       berrno be;
       Dmsg1(20, "Run freespace prog=%s\n", ocmd.c_str());
       status = run_program_full_output(ocmd.c_str(), dev->max_open_wait/2, results);
+      Dmsg2(20, "Freespace status=%d result=%s\n", status, results);
       if (status == 0) {
          free = str_to_int64(results);
          Dmsg1(400, "Free space program run: Freespace=%s\n", results);
@@ -352,6 +356,8 @@ bool dvd_write_part(DCR *dcr)
    DEVICE *dev = dcr->dev;
    POOL_MEM archive_name(PM_FNAME);
    
+   dev->clear_freespace_ok();             /* need to update freespace */
+
    /* Don't write empty part files.
     * This is only useful when growisofs does not support write beyond
     * the 4GB boundary.
@@ -373,6 +379,7 @@ bool dvd_write_part(DCR *dcr)
       /* Delete spool file */
       make_spooled_dvd_filename(dev, archive_name);
       unlink(archive_name.c_str());
+      dev->set_part_spooled(false);
       Dmsg1(29, "unlink(%s)\n", archive_name.c_str());
       sm_check(__FILE__, __LINE__, false);
       return true;
@@ -409,14 +416,17 @@ bool dvd_write_part(DCR *dcr)
     * in case of a serious emergency.
     */
 
-   if (dev->part == 1)
+   if (dev->part == 1) {
       timeout = 16000;
-   else
+   } else {
       timeout = dev->max_open_wait + (dev->part_size/(1350*1024/4));
+   }
 
-   Dmsg2(20, "dvd_write_part: cmd=%s timeout=%d\n", ocmd.c_str(), timeout);
+   Dmsg2(20, "Write part: cmd=%s timeout=%d\n", ocmd.c_str(), timeout);
    status = run_program_full_output(ocmd.c_str(), timeout, results.c_str());
+   Dmsg2(20, "Write part status=%d result=%s\n", status, results.c_str());
 
+   dev->truncated_dvd = false;
    if (status != 0) {
       Jmsg2(dcr->jcr, M_FATAL, 0, _("Error writing part %d to the DVD: ERR=%s\n"),
          dev->part, results.c_str());
@@ -426,23 +436,20 @@ bool dvd_write_part(DCR *dcr)
       dev->dev_errno = EIO;
       mark_volume_in_error(dcr);
       sm_check(__FILE__, __LINE__, false);
-      dev->truncated_dvd = false;
       return false;
    }
    Jmsg(dcr->jcr, M_INFO, 0, _("Part %d written to DVD.\n"), dev->part);
    Dmsg2(400, "dvd_write_part: Part %d written to DVD\nResults: %s\n",
             dev->part, results.c_str());
     
-   if (dev->truncated_dvd) {
-      dev->truncated_dvd = false;      /* turn off flag */
-   } else {                            /* DVD part written */
-      dev->num_dvd_parts++;            /* there is now one more part on DVD */
-      dev->VolCatInfo.VolCatParts = dev->num_dvd_parts;
-   }
+   dev->num_dvd_parts++;            /* there is now one more part on DVD */
+   dev->VolCatInfo.VolCatParts = dev->num_dvd_parts;
+   Dmsg1(000, "Update num_parts=%d\n", dev->num_dvd_parts);
 
    /* Delete spool file */
    make_spooled_dvd_filename(dev, archive_name);
    unlink(archive_name.c_str());
+   dev->set_part_spooled(false);
    Dmsg1(29, "unlink(%s)\n", archive_name.c_str());
    sm_check(__FILE__, __LINE__, false);
    
@@ -464,7 +471,6 @@ bool dvd_write_part(DCR *dcr)
 int dvd_open_next_part(DCR *dcr)
 {
    DEVICE *dev = dcr->dev;
-   VOLUME_LABEL VolHdr;
 
    Dmsg6(29, "Enter: == open_next_part part=%d npart=%d dev=%s vol=%s mode=%d file_addr=%d\n", 
       dev->part, dev->num_dvd_parts, dev->print_name(),
@@ -481,14 +487,7 @@ int dvd_open_next_part(DCR *dcr)
       return dev->fd;
    }
 
-   /*              
-    * Note, when we close, the Volume header is zeroed. Since
-    *  we reopen, but are in fact opening the same volume without
-    *  re-reading the Volume header, we simply save and restore it.
-    */
-   memcpy(&VolHdr, &dev->VolHdr, sizeof(VolHdr));
-   dev->close();                      /* close current part */
-   memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
+   dev->close_part(dcr);               /* close current part */
    
    /*
     * If we have a spooled part open, write it to the
@@ -507,6 +506,7 @@ int dvd_open_next_part(DCR *dcr)
    dev->part++;
    Dmsg2(29, "Inc part=%d num_dvd_parts=%d\n", dev->part, dev->num_dvd_parts);
 
+   /* Are we working on a part past what is written in the DVD? */
    if (dev->num_dvd_parts < dev->part) {
       POOL_MEM archive_name(PM_FNAME);
       struct stat buf;
@@ -514,35 +514,39 @@ int dvd_open_next_part(DCR *dcr)
        * First check what is on DVD.  If our part is there, we
        *   are in trouble, so bail out.
        * NB: This is however not a problem if we are writing the first part.
-       * It simply means that we are overriding an existing volume...
+       * It simply means that we are over writing an existing volume...
        */
       if (dev->num_dvd_parts > 0) {
          make_mounted_dvd_filename(dev, archive_name);   /* makes dvd name */
          Dmsg1(100, "Check if part on DVD: %s\n", archive_name.c_str());
          if (stat(archive_name.c_str(), &buf) == 0) {
             /* bad news bail out */
+            dev->set_part_spooled(false);
             Mmsg1(&dev->errmsg, _("Next Volume part already exists on DVD. Cannot continue: %s\n"),
                archive_name.c_str());
             return -1;
          }
       }
 
+#ifdef neeeded
       Dmsg2(400, "num_dvd_parts=%d part=%d\n", dev->num_dvd_parts, dev->part);
       make_spooled_dvd_filename(dev, archive_name);   /* makes spool name */
       
       /* Check if the next part exists in spool directory . */
-      Dmsg1(100, "Check if part on spool: $s\n", archive_name.c_str());
+      Dmsg1(100, "Check if part on spool: %s\n", archive_name.c_str());
       if ((stat(archive_name.c_str(), &buf) == 0) || (errno != ENOENT)) {
-         Dmsg1(29, "open_next_part %s is in the way, deleting it...\n", archive_name.c_str());
+         Dmsg1(29, "======= Part %s is in the way, deleting it...\n", archive_name.c_str());
          /* Then try to unlink it */
          if (unlink(archive_name.c_str()) < 0) {
             berrno be;
+            dev->set_part_spooled(false);
             dev->dev_errno = errno;
             Mmsg2(dev->errmsg, _("open_next_part can't unlink existing part %s, ERR=%s\n"), 
                    archive_name.c_str(), be.strerror());
             return -1;
          }
       }
+#endif
    }
 
    Dmsg2(400, "Call dev->open(vol=%s, mode=%d)\n", dcr->VolCatInfo.VolCatName, 
@@ -552,8 +556,6 @@ int dvd_open_next_part(DCR *dcr)
    if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
       return -1;
    } 
-   /* Restore Volume header record */
-   memcpy(&dev->VolHdr, &VolHdr, sizeof(dev->VolHdr));
    dev->set_labeled();                   /* all next parts are "labeled" */
    
    return dev->fd;
@@ -567,20 +569,12 @@ int dvd_open_next_part(DCR *dcr)
 int dvd_open_first_part(DCR *dcr, int mode)
 {
    DEVICE *dev = dcr->dev;
-   VOLUME_LABEL VolHdr;
 
    Dmsg5(29, "Enter: ==== open_first_part dev=%s Vol=%s mode=%d num_dvd_parts=%d append=%d\n", dev->print_name(), 
          dev->VolCatInfo.VolCatName, dev->openmode, dev->num_dvd_parts, dev->can_append());
 
 
-   /*              
-    * Note, when we close, the Volume header is zeroed. Since
-    *  we reopen, but are in fact opening the same volume without
-    *  re-reading the Volume header, we simply save and restore it.
-    */
-   memcpy(&VolHdr, &dev->VolHdr, sizeof(VolHdr));
-   dev->close();
-   memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
+   dev->close_part(dcr);
 
    Dmsg2(400, "Call dev->open(vol=%s, mode=%d)\n", dcr->VolCatInfo.VolCatName, 
          mode);
@@ -588,9 +582,6 @@ int dvd_open_first_part(DCR *dcr, int mode)
    dev->part = 1;
    dev->part_start = 0;
 
-
-   /* Restore Volume header record */
-   memcpy(&dev->VolHdr, &VolHdr, sizeof(dev->VolHdr));
    if (dev->open(dcr, mode) < 0) {
       Dmsg0(400, "open dev() failed\n");
       return -1;
@@ -740,7 +731,8 @@ bool dvd_close_job(DCR *dcr)
    JCR *jcr = dcr->jcr;
    bool ok = true;
 
-   /* If the device is a dvd and WritePartAfterJob
+   /*
+    * If the device is a dvd and WritePartAfterJob
     * is set to yes, open the next part, so, in case of a device
     * that requires mount, it will be written to the device.
     */
@@ -768,8 +760,7 @@ bool truncate_dvd(DCR *dcr)
 {
    DEVICE* dev = dcr->dev;
 
-   dev->close();
-   memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
+   dev->close_part(dcr);
 
    if (!unmount_dvd(dev, 1)) {
       Dmsg0(400, "truncate_dvd: Failed to unmount DVD\n");
@@ -801,8 +792,7 @@ bool truncate_dvd(DCR *dcr)
       return false;
    }
    
-   dev->close();
-   memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
+   dev->close_part(dcr);
    
    Dmsg0(400, "truncate_dvd: Opening first part (2)...\n");
    
@@ -853,7 +843,7 @@ bool check_can_write_on_non_blank_dvd(DCR *dcr)
    struct dirent *entry, *result;
    int name_max;
    int count = 0;
-   bool matched = false;
+   bool matched = true;
    struct stat filestat;
       
    name_max = pathconf(".", _PC_NAME_MAX);
@@ -894,7 +884,7 @@ bool check_can_write_on_non_blank_dvd(DCR *dcr)
                   filename.c_str(), be.strerror());
                return false;
             }
-            Dmsg2(99, "check_can_write_on_non_blank_dvd: size of %s is %d\n", 
+            Dmsg2(99, "check_can_write_on_non_blank_dvd: size of %s is %lld\n", 
                filename.c_str(), filestat.st_size);
             matched = filestat.st_size == 0;
          }