]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dvd.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / dvd.c
index 40e8b2e1f2a31a989dad5e3bf389c9b55317b490..417cf5fcec0fad34143699161341700c004b17d1 100644 (file)
@@ -135,6 +135,7 @@ static bool do_mount_dev(DEVICE* dev, int mount, int dotimeout)
    /* If busy retry each second */
    while ((status = run_program_full_output(ocmd.c_str(), 
                        dev->max_open_wait/2, results)) != 0) {
+      /* Doesn't work with internationalisation (This is not a problem) */
       if (fnmatch("*is already mounted on", results, 0) == 0) {
          break;
       }
@@ -149,7 +150,7 @@ static bool do_mount_dev(DEVICE* dev, int mount, int dotimeout)
          continue;
       }
       Dmsg2(40, "Device %s cannot be mounted. ERR=%s\n", dev->print_name(), results);
-      Mmsg(dev->errmsg, "Device %s cannot be mounted. ERR=%s\n"
+      Mmsg(dev->errmsg, _("Device %s cannot be mounted. ERR=%s\n")
            dev->print_name(), results);
       /*
        * Now, just to be sure it is not mounted, try to read the
@@ -176,17 +177,26 @@ static bool do_mount_dev(DEVICE* dev, int mount, int dotimeout)
       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
       while (1) {
          if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
-            dev->dev_errno = ENOENT;
-            Dmsg2(29, "open_mounted_dev: failed to find suitable file in dir %s (dev=%s)\n", 
+            dev->dev_errno = EIO;
+            Dmsg2(129, "open_mounted_dev: failed to find suitable file in dir %s (dev=%s)\n", 
                   dev->device->mount_point, dev->print_name());
             break;
          }
-         count++;
+         if ((strcmp(result->d_name, ".")) && (strcmp(result->d_name, "..")) && (strcmp(result->d_name, ".keep"))) {
+            count++; /* result->d_name != ., .. or .keep (Gentoo-specific) */
+         }
+         else {
+            Dmsg2(129, "open_mounted_dev: ignoring %s in %s\n", 
+                  result->d_name, dev->device->mount_point);
+         }
       }
       free(entry);
       closedir(dp);
-      if (count > 2) {
-         mount = 1;                      /* If we got more than . and .. */
+      
+      Dmsg1(29, "open_mounted_dev: got %d files in the mount point (not counting ., .. and .keep)\n", count);
+      
+      if (count > 0) {
+         mount = 1;                      /* If we got more than ., .. and .keep */
          break;                          /*   there must be something mounted */
       }
 get_out:
@@ -199,7 +209,7 @@ get_out:
    
    dev->set_mounted(mount);              /* set/clear mounted flag */
    free_pool_memory(results);
-   /* Do not check free space when unmounting (otherwise it will mount it again) */
+   /* Do not check free space when unmounting */
    if (mount) {
       update_free_space_dev(dev);
    }
@@ -226,8 +236,10 @@ void 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_media();
-      Dmsg2(29, "update_free_space_dev: free_space=%d, free_space_errno=%d (!icmd)\n", dev->free_space, dev->free_space_errno);
+      Dmsg2(29, "update_free_space_dev: free_space=%s, free_space_errno=%d (!icmd)\n", 
+            edit_uint64(dev->free_space, ed1), dev->free_space_errno);
       return;
    }
    
@@ -246,15 +258,17 @@ void update_free_space_dev(DEVICE* dev)
          free = str_to_int64(results);
          if (free >= 0) {
             dev->free_space = free;
-            dev->free_space_errno = 1;
+            dev->free_space_errno = 0;
+            dev->set_freespace_ok();     /* have valid freespace */
             dev->set_media();
             Mmsg0(dev->errmsg, "");
             break;
          }
       }
       dev->free_space = 0;
-      dev->free_space_errno = -EPIPE;
-      Mmsg1(dev->errmsg, "Cannot run free space command (%s)\n", results);
+      dev->free_space_errno = EPIPE;
+      dev->clear_freespace_ok();         /* no valid freespace */
+      Mmsg1(dev->errmsg, _("Cannot run free space command (%s)\n"), results);
       
       if (--timeout > 0) {
          Dmsg4(40, "Cannot get free space on device %s. free_space=%s, "
@@ -265,7 +279,7 @@ void update_free_space_dev(DEVICE* dev)
          continue;
       }
 
-      dev->dev_errno = -dev->free_space_errno;
+      dev->dev_errno = dev->free_space_errno;
       Dmsg4(40, "Cannot get free space on device %s. free_space=%s, "
          "free_space_errno=%d ERR=%s\n",
             dev->print_name(), edit_uint64(dev->free_space, ed1),
@@ -274,21 +288,51 @@ void update_free_space_dev(DEVICE* dev)
    }
    
    free_pool_memory(results);
-   Dmsg3(29, "update_free_space_dev: free_space=%s, free_space_errno=%d have_media=%d\n", 
-      edit_uint64(dev->free_space, ed1), dev->free_space_errno, dev->have_media());
+   Dmsg4(29, "update_free_space_dev: free_space=%s freespace_ok=%d free_space_errno=%d have_media=%d\n", 
+      edit_uint64(dev->free_space, ed1), !!dev->is_freespace_ok(), dev->free_space_errno, !!dev->have_media());
    sm_check(__FILE__, __LINE__, false);
    return;
 }
 
 /*
  * Write a part (Vol, Vol.1, ...) from the spool to the DVD   
- * This MUST only be called from open_next_part. Otherwise the part number
- * is not updated.
+ * This routine does not update the part number, so normally, you
+ *  should call open_next_part()
+ * It is also called from truncate_dvd_dev to "blank" the medium, as
+ *  well as from block.c when the DVD is full to write the last part.
  */
-static bool dvd_write_part(DCR *dcr) 
+bool dvd_write_part(DCR *dcr) 
 {
    DEVICE *dev = dcr->dev;
+   POOL_MEM archive_name(PM_FNAME);
+   
+   /* Don't write empty part files.
+    * This is only useful when growisofs does not support write beyond
+    * the 4GB boundary.
+    * Example :
+    *   - 3.9 GB on the volume, dvd-freespace reports 0.4 GB free
+    *   - Write 0.2 GB on the volume, Bacula thinks it could still
+    *     append data, it creates a new empty part.
+    *   - dvd-freespace reports 0 GB free, as the 4GB boundary has
+    *     been crossed
+    *   - Bacula thinks he must finish to write to the device, so it
+    *     tries to write the last part (0-byte), but dvd-writepart fails...
+    *
+    * There is one exception: when recycling a volume, we write a blank part
+    * file, so, then, we need to accept to write it.
+    */
+   if ((dev->part_size == 0) && (dev->part > 0)) {
+      Dmsg2(29, "dvd_write_part: device is %s, won't write blank part %d\n", dev->print_name(), dev->part);
+      /* Delete spool file */
+      make_spooled_dvd_filename(dev, archive_name);
+      unlink(archive_name.c_str());
+      Dmsg1(29, "unlink(%s)\n", archive_name.c_str());
+      sm_check(__FILE__, __LINE__, false);
+      return true;
+   }
+   
    POOL_MEM ocmd(PM_FNAME);
+   POOL_MEM results(PM_MESSAGE);
    char* icmd;
    int status;
    int timeout;
@@ -301,38 +345,50 @@ static bool dvd_write_part(DCR *dcr)
    edit_device_codes_dev(dev, ocmd, icmd);
       
    /*
-    * Wait at most the time a maximum size part is written in DVD 0.5x speed
-    * FIXME: Minimum speed should be in device configuration 
+    * original line follows
+    * timeout = dev->max_open_wait + (dev->max_part_size/(1350*1024/2));
+    * I modified this for a longer timeout; pre-formatting, blanking and
+    * writing can take quite a while
     */
-   timeout = dev->max_open_wait + (dev->max_part_size/(1350*1024/2));
-   
+
+   /* Explanation of the timeout value, when writing the first part,
+    *  by Arno Lehmann :
+    * 9 GB, write speed 1x: 6990 seconds (almost 2 hours...)
+    * Overhead: 900 seconds (starting, initializing, finalizing,probably 
+    *   reloading 15 minutes)
+    * Sum: 15780.
+    * A reasonable last-exit timeout would be 16000 seconds. Quite long - 
+    * almost 4.5 hours, but hopefully, that timeout will only ever be needed 
+    * in case of a serious emergency.
+    */
+
+   if (dev->part == 0)
+      timeout = 16000;
+   else
+      timeout = dev->max_open_wait + (dev->part_size/(1350*1024/4));
+
    Dmsg2(29, "dvd_write_part: cmd=%s timeout=%d\n", ocmd.c_str(), timeout);
       
-{
-   POOL_MEM results(PM_MESSAGE);
-   sm_check(__FILE__, __LINE__, false);
    status = run_program_full_output(ocmd.c_str(), timeout, results.c_str());
-   sm_check(__FILE__, __LINE__, false);
    if (status != 0) {
-      Mmsg1(dev->errmsg, "Error while writing current part to the DVD: %s"
+      Mmsg1(dev->errmsg, _("Error while writing current part to the DVD: %s")
             results.c_str());
       Dmsg1(000, "%s", dev->errmsg);
       dev->dev_errno = EIO;
+      mark_volume_in_error(dcr);
+      sm_check(__FILE__, __LINE__, false);
       return false;
+   } else {
+      dev->num_parts++;            /* there is now one more part on DVD */
    }
-   sm_check(__FILE__, __LINE__, false);
-}
 
-{
-   POOL_MEM archive_name(PM_FNAME);
    /* Delete spool file */
    make_spooled_dvd_filename(dev, archive_name);
    unlink(archive_name.c_str());
    Dmsg1(29, "unlink(%s)\n", archive_name.c_str());
    sm_check(__FILE__, __LINE__, false);
-}
    
-   /* growisofs umount the device, so remount it (it will update the free space) */
+   /* growisofs umounted the device, so remount it (it will update the free space) */
    dev->clear_mounted();
    mount_dev(dev, 1);
    Jmsg(dcr->jcr, M_INFO, 0, _("Remaining free space %s on %s\n"), 
@@ -346,18 +402,18 @@ static bool dvd_write_part(DCR *dcr)
  *  - Increment part number 
  *  - Reopen the device
  */
-int open_next_part(DCR *dcr)
+int dvd_open_next_part(DCR *dcr)
 {
    DEVICE *dev = dcr->dev;
 
-   Dmsg5(29, "Enter: ==== open_next_part part=%d npart=%d dev=%s vol=%s mode=%d\n", 
+   Dmsg6(29, "Enter: ==== open_next_part part=%d npart=%d dev=%s vol=%s mode=%d file_addr=%d\n", 
       dev->part, dev->num_parts, dev->print_name(),
-         dev->VolCatInfo.VolCatName, dev->openmode);
+         dev->VolCatInfo.VolCatName, dev->openmode, dev->file_addr);
    if (!dev->is_dvd()) {
       Dmsg1(000, "Device %s is not dvd!!!!\n", dev->print_name()); 
       return -1;
    }
-      
+   
    /* When appending, do not open a new part if the current is empty */
    if (dev->can_append() && (dev->part >= dev->num_parts) && 
        (dev->part_size == 0)) {
@@ -378,6 +434,7 @@ int open_next_part(DCR *dcr)
     */
    if (dev->is_dvd() && (dev->part >= dev->num_parts) && dev->can_append()) {
       if (!dvd_write_part(dcr)) {
+         Dmsg0(29, "Error in dvd_write part.\n");
          return -1;
       }
    }
@@ -410,8 +467,7 @@ int open_next_part(DCR *dcr)
          }
       }
 
-      Dmsg2(100, "Set npart=%d to part=%d\n", dev->num_parts, dev->part);
-      dev->num_parts = dev->part;
+      Dmsg2(100, "num_parts=%d part=%d\n", dev->num_parts, dev->part);
       dev->VolCatInfo.VolCatParts = dev->part;
       make_spooled_dvd_filename(dev, archive_name);   /* makes spool name */
       
@@ -428,6 +484,13 @@ int open_next_part(DCR *dcr)
          }
       }
    }
+   /* KES.  It seems to me that this if should not be
+    *  needed. If num_parts represents what is on the DVD
+    *  we should only need to change it when writing a part
+    *  to the DVD.
+    * NB. As dvd_write_part increments dev->num_parts, I also
+    *  think it is not needed.
+    */
    if (dev->num_parts < dev->part) {
       Dmsg2(100, "Set npart=%d to part=%d\n", dev->num_parts, dev->part);
       dev->num_parts = dev->part;
@@ -436,10 +499,16 @@ int open_next_part(DCR *dcr)
    Dmsg2(50, "Call dev->open(vol=%s, mode=%d\n", dev->VolCatInfo.VolCatName, 
          dev->openmode);
    /* Open next part */
+   
+   int append = dev->can_append();
    if (dev->open(dcr, dev->openmode) < 0) {
       return -1;
    } 
    dev->set_labeled();          /* all next parts are "labeled" */
+   if (append && (dev->part == dev->num_parts)) { /* If needed, set the append flag back */
+      dev->set_append();
+   }
+   
    return dev->fd;
 }
 
@@ -447,12 +516,12 @@ int open_next_part(DCR *dcr)
  *  - Close the fd
  *  - Reopen the device
  */
-int open_first_part(DCR *dcr, int mode)
+int dvd_open_first_part(DCR *dcr, int mode)
 {
    DEVICE *dev = dcr->dev;
 
-   Dmsg3(29, "Enter: ==== open_first_part dev=%s Vol=%s mode=%d\n", dev->print_name(), 
-         dev->VolCatInfo.VolCatName, dev->openmode);
+   Dmsg5(29, "Enter: ==== open_first_part dev=%s Vol=%s mode=%d num_parts=%d append=%d\n", dev->print_name(), 
+         dev->VolCatInfo.VolCatName, dev->openmode, dev->num_parts, dev->can_append());
 
    if (dev->fd >= 0) {
       close(dev->fd);
@@ -465,11 +534,16 @@ int open_first_part(DCR *dcr, int mode)
    
    Dmsg2(50, "Call dev->open(vol=%s, mode=%d)\n", dcr->VolCatInfo.VolCatName, 
          mode);
+   int append = dev->can_append();
    if (dev->open(dcr, mode) < 0) {
       Dmsg0(50, "open dev() failed\n");
       return -1;
    }
-   Dmsg1(50, "Leave open_first_part state=%s\n", dev->is_open()?"open":"not open");
+   if (append && (dev->part == dev->num_parts)) { /* If needed, set the append flag back */
+      dev->set_append();
+   }
+   Dmsg2(50, "Leave open_first_part state=%s append=%d\n", dev->is_open()?"open":"not open", dev->can_append());
+   
    return dev->fd;
 }
 
@@ -479,6 +553,7 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
 {
    DCR *dcr;
    off_t pos;
+   char ed1[50], ed2[50];
    
    Dmsg3(100, "Enter lseek_dev fd=%d part=%d nparts=%d\n", dev->fd,
       dev->part, dev->num_parts);
@@ -490,19 +565,19 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
    dcr = (DCR *)dev->attached_dcrs->first();  /* any dcr will do */
    switch(whence) {
    case SEEK_SET:
-      Dmsg1(100, "lseek_dev SEEK_SET to %d\n", (int)offset);
+      Dmsg2(100, "lseek_dev SEEK_SET to %s (part_start=%s)\n",
+         edit_uint64(offset, ed1), edit_uint64(dev->part_start, ed2));
       if ((uint64_t)offset >= dev->part_start) {
-         offset -= dev->part_start; /* adjust for start of this part */
-         if (offset == 0 || (uint64_t)offset < dev->part_size) {
+         if (((uint64_t)offset == dev->part_start) || ((uint64_t)offset < (dev->part_start+dev->part_size))) {
             /* We are staying in the current part, just seek */
-            if ((pos = lseek(dev->fd, offset, SEEK_SET)) < 0) {
+            if ((pos = lseek(dev->fd, offset-dev->part_start, SEEK_SET)) < 0) {
                return pos;
             } else {
                return pos + dev->part_start;
             }
          } else {
             /* Load next part, and start again */
-            if (open_next_part(dcr) < 0) {
+            if (dvd_open_next_part(dcr) < 0) {
                Dmsg0(100, "lseek_dev failed while trying to open the next part\n");
                return -1;
             }
@@ -515,7 +590,7 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
           * so just load the first one, and seek again
           * until the right one is loaded
           */
-         if (open_first_part(dcr, dev->openmode) < 0) {
+         if (dvd_open_first_part(dcr, dev->openmode) < 0) {
             Dmsg0(100, "lseek_dev failed while trying to open the first part\n");
             return -1;
          }
@@ -523,20 +598,20 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
       }
       break;
    case SEEK_CUR:
-      Dmsg1(100, "lseek_dev SEEK_CUR to %d\n", (int)offset);
+      Dmsg1(100, "lseek_dev SEEK_CUR to %s\n", edit_uint64(offset, ed1));
       if ((pos = lseek(dev->fd, (off_t)0, SEEK_CUR)) < 0) {
          return pos;   
       }
       pos += dev->part_start;
       if (offset == 0) {
-         Dmsg1(100, "lseek_dev SEEK_CUR returns %d\n", pos);
+         Dmsg1(100, "lseek_dev SEEK_CUR returns %s\n", edit_uint64(pos, ed1));
          return pos;
       } else { /* Not used in Bacula, but should work */
          return lseek_dev(dev, pos, SEEK_SET);
       }
       break;
    case SEEK_END:
-      Dmsg1(100, "lseek_dev SEEK_END to %d\n", (int)offset);
+      Dmsg1(100, "lseek_dev SEEK_END to %s\n", edit_uint64(offset, ed1));
       /*
        * Bacula does not use offsets for SEEK_END
        *  Also, Bacula uses seek_end only when it wants to
@@ -545,7 +620,8 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
        *  itself is read-only (as currently implemented).
        */
       if (offset > 0) { /* Not used by bacula */
-         Dmsg1(100, "lseek_dev SEEK_END called with an invalid offset %d\n", (int)offset);
+         Dmsg1(100, "lseek_dev SEEK_END called with an invalid offset %s\n", 
+            edit_uint64(offset, ed1));
          errno = EINVAL;
          return -1;
       }
@@ -556,7 +632,8 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
          if ((pos = lseek(dev->fd, (off_t)0, SEEK_END)) < 0) {
             return pos;   
          } else {
-            Dmsg1(100, "lseek_dev SEEK_END returns %d\n", pos + dev->part_start);
+            Dmsg1(100, "lseek_dev SEEK_END returns %s\n", 
+                  edit_uint64(pos + dev->part_start, ed1));
             return pos + dev->part_start;
          }
       } else {
@@ -569,19 +646,19 @@ off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
           */
          int modesave = dev->openmode;
          /* Works because num_parts > 0. */
-         if (open_first_part(dcr, OPEN_READ_ONLY) < 0) {
+         if (dvd_open_first_part(dcr, OPEN_READ_ONLY) < 0) {
             Dmsg0(100, "lseek_dev failed while trying to open the first part\n");
             return -1;
          }
          if (dev->num_parts > 0) {
             while (dev->part < (dev->num_parts-1)) {
-               if (open_next_part(dcr) < 0) {
+               if (dvd_open_next_part(dcr) < 0) {
                   Dmsg0(100, "lseek_dev failed while trying to open the next part\n");
                   return -1;
                }
             }
             dev->openmode = modesave;
-            if (open_next_part(dcr) < 0) {
+            if (dvd_open_next_part(dcr) < 0) {
                Dmsg0(100, "lseek_dev failed while trying to open the next part\n");
                return -1;
             }
@@ -619,8 +696,7 @@ bool dvd_close_job(DCR *dcr)
          NB: No! If you call dvd_write_part, the part number is not updated.
          You must open the next part, it will automatically write the part and
          update the part number. */
-      if (ok && (open_next_part(dcr) < 0)) {
-//      if (ok && !dvd_write_part(dcr)) {
+      if (ok && (dvd_open_next_part(dcr) < 0)) {
          Jmsg2(jcr, M_FATAL, 0, _("Unable to write part %s: ERR=%s\n"),
                dev->print_name(), strerror_dev(dev));
          dev->dev_errno = EIO;
@@ -632,12 +708,135 @@ bool dvd_close_job(DCR *dcr)
    return ok;
 }
 
+bool truncate_dvd_dev(DCR *dcr) {
+   DEVICE* dev = dcr->dev;
+
+   /* Set num_parts to zero (on disk) */
+   dev->num_parts = 0;
+   dcr->VolCatInfo.VolCatParts = 0;
+   dev->VolCatInfo.VolCatParts = 0;
+   
+   Dmsg0(100, "truncate_dvd_dev: Opening first part (1)...\n");
+   
+   dev->truncating = true;
+   if (dvd_open_first_part(dcr, OPEN_READ_WRITE) < 0) {
+      Dmsg0(100, "truncate_dvd_dev: Error while opening first part (1).\n");
+      dev->truncating = false;
+      return false;
+   }
+   dev->truncating = false;
+
+   Dmsg0(100, "truncate_dvd_dev: Truncating...\n");
+
+   /* If necessary, truncate it. */
+   if (ftruncate(dev->fd, 0) != 0) {
+      berrno be;
+      Mmsg2(dev->errmsg, _("Unable to truncate device %s. ERR=%s\n"), 
+         dev->print_name(), be.strerror());
+      return false;
+   }
+   
+   close(dev->fd);
+   dev->fd = -1;
+   dev->clear_opened();
+   
+   Dmsg0(100, "truncate_dvd_dev: Opening first part (2)...\n");
+   
+   if (!dvd_write_part(dcr)) {
+      Dmsg0(100, "truncate_dvd_dev: Error while writing to DVD.\n");
+      return false;
+   }
+   
+   /* Set num_parts to zero (on disk) */
+   dev->num_parts = 0;
+   dcr->VolCatInfo.VolCatParts = 0;
+   dev->VolCatInfo.VolCatParts = 0;
+   
+   if (dvd_open_first_part(dcr, OPEN_READ_WRITE) < 0) {
+      Dmsg0(100, "truncate_dvd_dev: Error while opening first part (2).\n");
+      return false;
+   }
+
+   return true;
+}
+
+/* Checks if we can write on a non-blank DVD: meaning that it just have been
+ * truncated (there is only one zero-sized file on the DVD, with the right
+ * volume name). */
+bool check_can_write_on_non_blank_dvd(DCR *dcr) {
+   DEVICE* dev = dcr->dev;
+   DIR* dp;
+   struct dirent *entry, *result;
+   int name_max;
+   int count = 0;
+   int matched = 0; /* We found an empty file with the right name. */
+   struct stat filestat;
+      
+   name_max = pathconf(".", _PC_NAME_MAX);
+   if (name_max < 1024) {
+      name_max = 1024;
+   }
+   
+   if (!(dp = opendir(dev->device->mount_point))) {
+      berrno be;
+      dev->dev_errno = errno;
+      Dmsg3(29, "check_can_write_on_non_blank_dvd: failed to open dir %s (dev=%s), ERR=%s\n", 
+            dev->device->mount_point, dev->print_name(), be.strerror());
+      return false;
+   }
+   
+   entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
+   while (1) {
+      if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
+         dev->dev_errno = EIO;
+         Dmsg2(129, "check_can_write_on_non_blank_dvd: failed to find suitable file in dir %s (dev=%s)\n", 
+               dev->device->mount_point, dev->print_name());
+         break;
+      }
+      else {
+         Dmsg2(99, "check_can_write_on_non_blank_dvd: found %s (versus %s)\n", 
+               result->d_name, dev->VolCatInfo.VolCatName);
+         if (strcmp(result->d_name, dev->VolCatInfo.VolCatName) == 0) {
+            /* Found the file, checking it is empty */
+            POOL_MEM filename(PM_FNAME);
+            pm_strcpy(filename, dev->device->mount_point);
+            if (filename.c_str()[strlen(filename.c_str())-1] != '/') {
+               pm_strcat(filename, "/");
+            }
+            pm_strcat(filename, dev->VolCatInfo.VolCatName);
+            if (stat(filename.c_str(), &filestat) < 0) {
+               berrno be;
+               dev->dev_errno = errno;
+               Dmsg2(29, "check_can_write_on_non_blank_dvd: cannot stat file (file=%s), ERR=%s\n", 
+                  filename.c_str(), be.strerror());
+               return false;
+            }
+            Dmsg2(99, "check_can_write_on_non_blank_dvd: size of %s is %d\n", 
+               filename.c_str(), filestat.st_size);
+            matched = (filestat.st_size == 0);
+         }
+      }
+      count++;
+   }
+   free(entry);
+   closedir(dp);
+   
+   Dmsg2(29, "check_can_write_on_non_blank_dvd: got %d files in the mount point (matched=%d)\n", count, matched);
+   
+   if (count != 3) {
+      /* There is more than 3 files (., .., and the volume file) */
+      return false;
+   }
+   
+   return matched;
+}
 
 /*
  * Edit codes into (Un)MountCommand, Write(First)PartCommand
  *  %% = %
  *  %a = archive device name
  *  %e = erase (set if cannot mount and first part)
+ *  %n = part number
  *  %m = mount point
  *  %v = last part name
  *
@@ -665,7 +864,7 @@ static void edit_device_codes_dev(DEVICE* dev, POOL_MEM &omsg, const char *imsg)
             str = dev->dev_name;
             break;
          case 'e':
-            if (dev->part == 0 && !dev->is_mounted()) {
+            if (dev->num_parts == 0) {
                str = "1";
             } else {
                str = "0";