]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix #10401 About issue when truncating immutable volume
authorEric Bollengier <eric@baculasystems.com>
Tue, 26 Sep 2023 15:06:37 +0000 (17:06 +0200)
committerEric Bollengier <eric@baculasystems.com>
Tue, 13 Feb 2024 09:36:03 +0000 (10:36 +0100)
 - The protection time was not set in with the immutability flag
 - The information message for immutability has now the retention time
 - The errno checked in open_device was ACCESS instead of PERM

bacula/src/stored/block_util.c
bacula/src/stored/dircmd.c
bacula/src/stored/file_dev.c

index 3879474c2611e09f2095cb2ae6040de417870b28..1f50854a622cfc1792fd4453e67bd275e677c9e0 100644 (file)
@@ -839,47 +839,47 @@ bool terminate_writing_volume(DCR *dcr)
    if (bstrcmp(dev->VolCatInfo.VolCatStatus, "Append")) {
       dev->setVolCatStatus("Full");
    }
-   
-   if (dev->device->set_vol_read_only) {
-      btime_t now = time(NULL);
+   char buf[128], buf2[128];
+   if (dev->device->set_vol_immutable || dev->device->set_vol_read_only) {
       uint32_t when = MAX(dev->device->min_volume_protection_time, dev->VolCatInfo.VolRetention);
-      dev->VolCatInfo.UseProtect = 1;
-      /* Set volume as immutable/read only */
-      if (dev->set_atime(dev->m_fd, dev->getVolCatName(), now + when) < 0) {
-         Jmsg(dcr->jcr, M_WARNING, 0, _("Failed to set the volume %s on device %s in atime retention, ERR=%s.\n"),
+      btime_t now = time(NULL);
+      if (dev->set_atime(-1, dev->getVolCatName(), now + when) < 0) {
+         Jmsg(dcr->jcr, M_WARNING, 0, _(" Failed to set the volume %s on device %s in atime retention, ERR=%s.\n"),
               dev->getVolCatName(), dev->print_name(), dev->errmsg);
       }
+      bstrftime(buf2, sizeof(buf2), now+when);
+      strip_trailing_junk(edit_utime(when, buf, sizeof(buf)));
+      dev->VolCatInfo.UseProtect = 1;
+   }
+
+   if (dev->device->set_vol_read_only) {
+      /* Set volume as immutable/read only */
       if (dev->set_readonly(dev->m_fd, dev->getVolCatName()) < 0) {
          berrno be;
          /* We may proceed with that but warn the user */
          Jmsg(dcr->jcr, M_WARNING, 0, _("Failed to set the volume %s on device %s in read-only, ERR=%s.\n"),
               dev->getVolCatName(), dev->print_name(), be.bstrerror());
       } else {
-         char buf[128], buf2[128];
-         strip_trailing_junk(edit_utime(when, buf, sizeof(buf)));
-         bstrftime(buf2, sizeof(buf2), now+when);
-
          Jmsg(dcr->jcr, M_INFO, 0, _("Marking Volume \"%s\" as read-only. Retention set to %s (%s).\n"),
               dev->getVolCatName(), buf2, buf);
          
          dev->VolCatInfo.Protected = 1;
          events_send_msg(dcr->jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)dcr->jcr,
-                            "Mark Volume \"%s\" as read-only. retention %s.", dev->getVolCatName(), buf);;
+                         "Mark Volume \"%s\" as read-only. Retention set to %s (%s).", dev->getVolCatName(), buf2, buf);
       }
    }
 
    if (dev->device->set_vol_immutable) {
-      dev->VolCatInfo.UseProtect = 1;
       /* Set volume as immutable */
       if (!dev->set_immutable(dev->getVolCatName(), &dev->errmsg)) {
          /* We may proceed with that but warn the user */
          Jmsg(dcr->jcr, M_WARNING, 0, _("Failed to set the volume %s on device %s as immutable, ERR=%s.\n"),
               dev->getVolCatName(), dev->print_name(), dev->errmsg);
       } else {
-         Jmsg(dcr->jcr, M_INFO, 0, _("Marking Volume \"%s\" as immutable\n"),
-              dev->getVolCatName());
+         Jmsg(dcr->jcr, M_INFO, 0, _("Marking Volume \"%s\" as immutable. Retention set to %s (%s).\n"),
+              dev->getVolCatName(), buf2, buf);
          events_send_msg(dcr->jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)dcr->jcr,
-                         "Mark Volume \"%s\" as immutable", dev->getVolCatName());;
+                         "Mark Volume \"%s\" as immutable. Retention set to %s (%s).", dev->getVolCatName(), buf2, buf);;
          dev->VolCatInfo.Protected = 1;
       }
    }
index d9bea49d981e9618a950fae58a5efa2aeed6c6cf..ebbcec0cbe7a0cb69edfaa56097038bb726605ab 100644 (file)
@@ -1360,6 +1360,19 @@ static bool volumeprotect_cmd(JCR *jcr)
          /* Everything can be  done without a lock because we don't touch anything
           * inside the device structure
           */
+         char buf[128], buf2[128];
+         if (dev->device->set_vol_immutable || dev->device->set_vol_read_only) {
+            uint32_t when = MAX(dev->device->min_volume_protection_time, retention);
+            btime_t now = time(NULL);
+            if (dev->set_atime(-1, volume, now + when) < 0) {
+               berrno be;
+               Mmsg(tmp, _(" Failed to set the volume %s on device %s in atime retention, ERR=%s.\n"),
+                    volume, dev->print_name(), be.bstrerror());
+            }
+            pm_strcpy(tmp, "");
+            bstrftime(buf2, sizeof(buf2), now+when);
+            strip_trailing_junk(edit_utime(when, buf, sizeof(buf)));
+         }
          if (dev->device->set_vol_immutable) {
             /* Set volume as immutable */
             if (!dev->set_immutable(volume, tmp.handle())) {
@@ -1367,32 +1380,21 @@ static bool volumeprotect_cmd(JCR *jcr)
                dir->fsend(_("3900 Unable to set immutable flag %s\n"), tmp.c_str());
 
             } else {
-               dir->fsend(_("3000 Mark volume \"%s\" as immutable\n"), volume);
+               dir->fsend(_("3000 Mark volume \"%s\" as immutable. Retention set to %s (%s).\n"), volume, buf2, buf);
                events_send_msg(jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)jcr,
-                               "Mark volume \"%s\" as immutable", volume);
+                               "Mark volume \"%s\" as immutable, retention %s (%s)", volume, buf2, buf);
             }
 
          } else if (dev->device->set_vol_read_only) {
             /* Set volume as immutable/read only */
-            pm_strcpy(tmp, "");
-            uint32_t when = MAX(dev->device->min_volume_protection_time, retention);
-            btime_t now = time(NULL);
-            if (dev->set_atime(-1, volume, now + when) < 0) {
-               berrno be;
-               Mmsg(tmp, _(" Failed to set the volume %s on device %s in atime retention, ERR=%s.\n"),
-                    volume, dev->print_name(), be.bstrerror());
-            }
             if (dev->set_readonly(-1, volume) < 0) {
                berrno be;
                /* We may proceed with that but warn the user */
                dir->fsend(_("3900 Failed to set the volume %s on device %s in read-only, ERR=%s.%s\n"),
                           volume, dev->print_name(), be.bstrerror(), tmp.c_str());
             } else {
-               char buf[128], buf2[128];
                dir->fsend(_("3000 Marking volume \"%s\" as read-only. Retention set to %s (%s).\n"),
-                          volume,
-                          bstrftime(buf2, sizeof(buf2), now+when),
-                          strip_trailing_junk(edit_utime(when, buf, sizeof(buf))));
+                          volume,buf2, buf);
                events_send_msg(jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)jcr,
                                "Mark volume \"%s\" as read-only, retention %s (%s)", volume, buf2, buf);
             }
index ef749b52026fb03721160a0af7654f875b18fac6..892de4f9a0412d67e9fe15cccc87b577a4653f88 100644 (file)
@@ -202,7 +202,7 @@ bool file_dev::open_device(DCR *dcr, int omode)
    /* Use system open() */
    if ((m_fd = ::open(archive_name.c_str(), mode|O_CLOEXEC|append, 0640)) < 0) {
       /* Open may fail if we want to write to the Immutable volume */
-      if (errno == EACCES && use_protect()) {
+      if ((errno == EACCES || errno == EPERM) && use_protect()) {
          bool immutable = check_for_immutable(getVolCatName());
          bool readonly = check_for_read_only(-1, getVolCatName());
          Dmsg3(DT_VOLUME|40, "volume=%s immutable=%d readonly=%d\n", getVolCatName(), immutable, readonly);