static void add_file_and_part_name(DEVICE *dev, POOL_MEM &archive_name)
{
char partnumber[20];
+
if (archive_name.c_str()[strlen(archive_name.c_str())-1] != '/') {
pm_strcat(archive_name, "/");
}
icmd = dev->device->unmount_command;
}
+ dev->clear_freespace_ok();
dev->edit_mount_codes(ocmd, icmd);
Dmsg2(200, "do_mount_dvd: cmd=%s mounted=%d\n", ocmd.c_str(), !!dev->is_mounted());
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);
+ Dmsg2(500, "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);
DEVICE *dev = dcr->dev;
POOL_MEM archive_name(PM_FNAME);
- dev->clear_freespace_ok(); /* need to update freespace */
-
- /* Don't write empty part files.
+ /*
+ * Don't write empty part files.
* This is only useful when growisofs does not support write beyond
* the 4GB boundary.
* Example :
* - Bacula thinks he must finish to write to the device, so it
* tries to write the last part (0-byte), but dvd-writepart fails...
*
- * ***FIXME**** we cannot write a blank part!!!!!!!
* 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) {
+ if (dev->part_size == 0 && !dev->truncating) {
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());
dev->set_part_spooled(false);
- Dmsg1(29, "unlink(%s)\n", archive_name.c_str());
+ Dmsg1(29, "========= unlink(%s)\n", archive_name.c_str());
sm_check(__FILE__, __LINE__, false);
return true;
}
int timeout;
char ed1[50];
+ dev->clear_freespace_ok(); /* need to update freespace */
+
sm_check(__FILE__, __LINE__, false);
Dmsg3(29, "dvd_write_part: device is %s, part is %d, is_mounted=%d\n", dev->print_name(), dev->part, dev->is_mounted());
icmd = dev->device->write_part_command;
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;
+ dev->blank_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());
Mmsg1(dev->errmsg, _("Error while writing current part to the DVD: %s"),
results.c_str());
- Dmsg1(000, "%s\n", dev->errmsg);
+ Dmsg1(100, "%s\n", dev->errmsg);
dev->dev_errno = EIO;
- mark_volume_in_error(dcr);
+ if (!dev->truncating) {
+ mark_volume_in_error(dcr);
+ }
sm_check(__FILE__, __LINE__, false);
return false;
}
dev->num_dvd_parts++; /* there is now one more part on DVD */
dev->VolCatInfo.VolCatParts = dev->num_dvd_parts;
dcr->VolCatInfo.VolCatParts = dev->num_dvd_parts;
- Dmsg1(000, "Update num_parts=%d\n", dev->num_dvd_parts);
+ Dmsg1(100, "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());
+ Dmsg1(29, "========= unlink(%s)\n", archive_name.c_str());
sm_check(__FILE__, __LINE__, false);
/* growisofs umounted the device, so remount it (it will update the free space) */
dev->part, dev->num_dvd_parts, dev->print_name(),
dev->VolCatInfo.VolCatName, dev->openmode, dev->file_addr);
if (!dev->is_dvd()) {
- Dmsg1(000, "Device %s is not dvd!!!!\n", dev->print_name());
+ Dmsg1(100, "Device %s is not dvd!!!!\n", dev->print_name());
return -1;
}
* - Close the fd
* - Reopen the device
*/
-int dvd_open_first_part(DCR *dcr, int mode)
+static bool dvd_open_first_part(DCR *dcr, int mode)
{
DEVICE *dev = dcr->dev;
if (dev->open(dcr, mode) < 0) {
Dmsg0(400, "open dev() failed\n");
- return -1;
+ return false;
}
Dmsg2(400, "Leave open_first_part state=%s append=%d\n", dev->is_open()?"open":"not open", dev->can_append());
- return dev->fd;
+ return true;
}
-/* Protected version of lseek, which opens the right part if necessary */
-off_t lseek_dev(DEVICE *dev, off_t offset, int whence)
+/*
+ * Do an lseek on a DVD handling all the different parts
+ */
+off_t lseek_dvd(DCR *dcr, off_t offset, int whence)
{
- DCR *dcr;
+ DEVICE *dev = dcr->dev;
off_t pos;
char ed1[50], ed2[50];
- Dmsg5(400, "Enter lseek_dev fd=%d off=%s w=%d part=%d nparts=%d\n", dev->fd,
+ Dmsg5(400, "Enter lseek_dvd fd=%d off=%s w=%d part=%d nparts=%d\n", dev->fd,
edit_int64(offset, ed1), whence, dev->part, dev->num_dvd_parts);
- if (!dev->is_dvd()) {
- Dmsg0(400, "Using sys lseek\n");
- return lseek(dev->fd, offset, whence);
- }
- dcr = (DCR *)dev->attached_dcrs->first(); /* any dcr will do */
switch(whence) {
case SEEK_SET:
- Dmsg2(400, "lseek_dev SEEK_SET to %s (part_start=%s)\n",
+ Dmsg2(400, "lseek_dvd SEEK_SET to %s (part_start=%s)\n",
edit_int64(offset, ed1), edit_uint64(dev->part_start, ed2));
if ((uint64_t)offset >= dev->part_start) {
if ((uint64_t)offset == dev->part_start ||
/* Load next part, and start again */
Dmsg0(100, "lseek open next part\n");
if (dvd_open_next_part(dcr) < 0) {
- Dmsg0(400, "lseek_dev failed while trying to open the next part\n");
+ Dmsg0(400, "lseek_dvd failed while trying to open the next part\n");
return -1;
}
Dmsg2(100, "Recurse lseek after open next part=%d num_part=%d\n",
dev->part, dev->num_dvd_parts);
- return lseek_dev(dev, offset, SEEK_SET);
+ return lseek_dvd(dcr, offset, SEEK_SET);
}
} else {
/*
* until the right one is loaded
*/
Dmsg0(100, "lseek open first part\n");
- if (dvd_open_first_part(dcr, dev->openmode) < 0) {
- Dmsg0(400, "lseek_dev failed while trying to open the first part\n");
+ if (!dvd_open_first_part(dcr, dev->openmode)) {
+ Dmsg0(400, "lseek_dvd failed while trying to open the first part\n");
return -1;
}
Dmsg2(100, "Recurse lseek after open first part=%d num_part=%d\n",
dev->part, dev->num_dvd_parts);
- return lseek_dev(dev, offset, SEEK_SET);
+ return lseek_dvd(dcr, offset, SEEK_SET); /* system lseek */
}
break;
case SEEK_CUR:
- Dmsg1(400, "lseek_dev SEEK_CUR to %s\n", edit_int64(offset, ed1));
+ Dmsg1(400, "lseek_dvd SEEK_CUR to %s\n", edit_int64(offset, ed1));
if ((pos = lseek(dev->fd, (off_t)0, SEEK_CUR)) < 0) {
Dmsg0(400, "Seek error.\n");
return pos;
}
pos += dev->part_start;
if (offset == 0) {
- Dmsg1(400, "lseek_dev SEEK_CUR returns %s\n", edit_uint64(pos, ed1));
+ Dmsg1(400, "lseek_dvd SEEK_CUR returns %s\n", edit_uint64(pos, ed1));
return pos;
} else {
- Dmsg1(400, "do lseek_dev SEEK_SET %s\n", edit_uint64(pos, ed1));
- return lseek_dev(dev, pos, SEEK_SET);
+ Dmsg1(400, "do lseek_dvd SEEK_SET %s\n", edit_uint64(pos, ed1));
+ return lseek_dvd(dcr, pos, SEEK_SET);
}
break;
case SEEK_END:
- Dmsg1(400, "lseek_dev SEEK_END to %s\n", edit_int64(offset, ed1));
+ Dmsg1(400, "lseek_dvd SEEK_END to %s\n", edit_int64(offset, ed1));
/*
* Bacula does not use offsets for SEEK_END
* Also, Bacula uses seek_end only when it wants to
* itself is read-only (as currently implemented).
*/
if (offset > 0) { /* Not used by bacula */
- Dmsg1(400, "lseek_dev SEEK_END called with an invalid offset %s\n",
+ Dmsg1(400, "lseek_dvd SEEK_END called with an invalid offset %s\n",
edit_uint64(offset, ed1));
errno = EINVAL;
return -1;
if ((pos = lseek(dev->fd, (off_t)0, SEEK_END)) < 0) {
return pos;
} else {
- Dmsg1(400, "lseek_dev SEEK_END returns %s\n",
+ Dmsg1(400, "lseek_dvd SEEK_END returns %s\n",
edit_uint64(pos + dev->part_start, ed1));
return pos + dev->part_start;
}
* (useful for DVDs)
*/
int modesave = dev->openmode;
- if (dvd_open_first_part(dcr, OPEN_READ_ONLY) < 0) {
- Dmsg0(400, "lseek_dev failed while trying to open the first part\n");
+ if (!dvd_open_first_part(dcr, OPEN_READ_ONLY)) {
+ Dmsg0(400, "lseek_dvd failed while trying to open the first part\n");
return -1;
}
if (dev->num_dvd_parts > 0) {
while (dev->part < dev->num_dvd_parts) {
if (dvd_open_next_part(dcr) < 0) {
- Dmsg0(400, "lseek_dev failed while trying to open the next part\n");
+ Dmsg0(400, "lseek_dvd failed while trying to open the next part\n");
return -1;
}
}
dev->openmode = modesave;
if (dvd_open_next_part(dcr) < 0) {
- Dmsg0(400, "lseek_dev failed while trying to open the next part\n");
+ Dmsg0(400, "lseek_dvd failed while trying to open the next part\n");
return -1;
}
}
- return lseek_dev(dev, 0, SEEK_END);
+ return lseek_dvd(dcr, 0, SEEK_END);
}
break;
default:
return ok;
}
+void dvd_remove_empty_part(DCR *dcr)
+{
+ DEVICE *dev = dcr->dev;
+
+ /* Remove the last part file if it is empty */
+ if (dev->is_dvd() && dev->num_dvd_parts > 0) {
+ struct stat statp;
+ uint32_t part_save = dev->part;
+ POOL_MEM archive_name(PM_FNAME);
+ int status;
+
+ dev->part = dev->num_dvd_parts;
+ make_spooled_dvd_filename(dev, archive_name);
+ /* Check that the part file is empty */
+ status = stat(archive_name.c_str(), &statp);
+ if (status == 0 && statp.st_size == 0) {
+ Dmsg3(100, "Unlink empty part in close call make_dvd_filename. part=%d num=%d vol=%s\n",
+ part_save, dev->num_dvd_parts, dev->VolCatInfo.VolCatName);
+ Dmsg1(100, "unlink(%s)\n", archive_name.c_str());
+ unlink(archive_name.c_str());
+ if (part_save == dev->part) {
+ dev->set_part_spooled(false); /* no spooled part left */
+ }
+ } else if (status < 0) {
+ if (part_save == dev->part) {
+ dev->set_part_spooled(false); /* spool doesn't exit */
+ }
+ }
+ dev->part = part_save; /* restore part number */
+ }
+}
+
bool truncate_dvd(DCR *dcr)
{
DEVICE* dev = dcr->dev;
+ dev->clear_freespace_ok(); /* need to update freespace */
dev->close_part(dcr);
if (!unmount_dvd(dev, 1)) {
return false;
}
+ /* If necessary, delete its spool file. */
+ if (dev->is_part_spooled()) {
+ POOL_MEM archive_name(PM_FNAME);
+ /* Delete spool file */
+ make_spooled_dvd_filename(dev, archive_name);
+ unlink(archive_name.c_str());
+ dev->set_part_spooled(false);
+ }
+
/* Set num_dvd_parts to zero (on disk) */
+ dev->part = 0;
dev->num_dvd_parts = 0;
dcr->VolCatInfo.VolCatParts = 0;
dev->VolCatInfo.VolCatParts = 0;
Dmsg0(400, "truncate_dvd: Opening first part (1)...\n");
dev->truncating = true;
- if (dvd_open_first_part(dcr, CREATE_READ_WRITE) < 0) {
+ /* This creates a zero length spool file and sets part=1 */
+ if (!dvd_open_first_part(dcr, CREATE_READ_WRITE)) {
Dmsg0(400, "truncate_dvd: Error while opening first part (1).\n");
dev->truncating = false;
return false;
}
- Dmsg0(400, "truncate_dvd: Truncating...\n");
-
- /* If necessary, truncate it spool file. */
- if (ftruncate(dev->fd, 0) != 0) {
- berrno be;
- Mmsg2(dev->errmsg, _("Unable to truncate device %s. ERR=%s\n"),
- dev->print_name(), be.strerror());
- dev->truncating = false;
- return false;
- }
-
dev->close_part(dcr);
Dmsg0(400, "truncate_dvd: Opening first part (2)...\n");
/*
- * Now actually truncate the DVD
- * This is really kludgy, why not an argument or a separate
- * subroutine? KES
+ * Now actually truncate the DVD which is done by writing
+ * a zero length part to the DVD/
*/
if (!dvd_write_part(dcr)) {
Dmsg0(400, "truncate_dvd: Error while writing to DVD.\n");
dev->truncating = false;
/* Set num_dvd_parts to zero (on disk) */
+ dev->part = 0;
dev->num_dvd_parts = 0;
dcr->VolCatInfo.VolCatParts = 0;
dev->VolCatInfo.VolCatParts = 0;
+ /* Clear the size of the volume */
+ dev->VolCatInfo.VolCatBytes = 0;
+ dcr->VolCatInfo.VolCatBytes = 0;
/* Update catalog */
if (!dir_update_volume_info(dcr, false)) {
return false;
}
- if (dvd_open_first_part(dcr, OPEN_READ_WRITE) < 0) {
- Dmsg0(400, "truncate_dvd: 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).
+/*
+ * 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).
*
* Note! Normally if we can mount the device, which should be the case
* when we get here, it is not a blank DVD. Hence we check if
- * there is a zero length file with the right name, in which case
- * we allow it.
- * This seems terribly kludgie to me. KES
+ * if all files are of zero length (i.e. no data), in which case we allow it.
+ *
*/
bool check_can_write_on_non_blank_dvd(DCR *dcr)
{
DIR* dp;
struct dirent *entry, *result;
int name_max;
- int count = 0;
- bool matched = true;
struct stat filestat;
+ bool ok = true;
name_max = pathconf(".", _PC_NAME_MAX);
if (name_max < 1024) {
}
entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
- while (1) {
+ for ( ;; ) {
if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
dev->dev_errno = EIO;
Dmsg2(129, "check_can_write_on_non_blank_dvd: no more files in dir %s (dev=%s)\n",
} 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 */
+ if (strcmp(result->d_name, ".") && strcmp(result->d_name, "..") &&
+ strcmp(result->d_name, ".keep")) {
+ /* Found a 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);
+ pm_strcat(filename, result->d_name);
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;
+ ok = false;
+ break;
}
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;
+ if (filestat.st_size != 0) {
+ ok = false;
+ break;
+ }
}
}
- count++;
}
free(entry);
closedir(dp);
- if (count > 3) {
- /* There are more than 3 files (., .., and the volume file) */
- Dmsg1(29, "Cannot write on blank DVD too many files %d greater than 3\n", count);
- return false;
+ Dmsg1(29, "OK can_write_on_non_blank_dvd: OK=%d\n", ok);
+ return ok;
+}
+
+/*
+ * Mount a DVD device, then scan to find out how many parts
+ * there are.
+ */
+int find_num_dvd_parts(DCR *dcr)
+{
+ DEVICE *dev = dcr->dev;
+ int num_parts = 0;
+
+ if (!dev->is_dvd()) {
+ return 0;
}
- Dmsg2(29, "OK can_write_on_non_blank_dvd: got %d files in the mount point (matched=%d)\n", count, matched);
- return matched;
+ if (dev->mount(1)) {
+ DIR* dp;
+ struct dirent *entry, *result;
+ int name_max;
+ int len = strlen(dcr->VolCatInfo.VolCatName);
+
+ /* Now count the number of parts */
+ 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, "find_num_dvd_parts: failed to open dir %s (dev=%s), ERR=%s\n",
+ dev->device->mount_point, dev->print_name(), be.strerror());
+ goto get_out;
+ }
+
+ entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
+
+ Dmsg1(100, "Looking for Vol=%s\n", dcr->VolCatInfo.VolCatName);
+ for ( ;; ) {
+ int flen;
+ bool ignore;
+ if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
+ dev->dev_errno = EIO;
+ Dmsg2(129, "find_num_dvd_parts: failed to find suitable file in dir %s (dev=%s)\n",
+ dev->device->mount_point, dev->print_name());
+ break;
+ }
+ flen = strlen(result->d_name);
+ ignore = true;
+ if (flen >= len) {
+ result->d_name[len] = 0;
+ if (strcmp(dcr->VolCatInfo.VolCatName, result->d_name) == 0) {
+ num_parts++;
+ Dmsg1(100, "find_num_dvd_parts: found part: %s\n", result->d_name);
+ ignore = false;
+ }
+ }
+ if (ignore) {
+ Dmsg2(129, "find_num_dvd_parts: ignoring %s in %s\n",
+ result->d_name, dev->device->mount_point);
+ }
+ }
+ free(entry);
+ closedir(dp);
+ Dmsg1(29, "find_num_dvd_parts = %d\n", num_parts);
+ }
+
+get_out:
+ dev->set_freespace_ok();
+ if (dev->is_mounted()) {
+ dev->unmount(0);
+ }
+ return num_parts;
}