]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Cleanup immutability/read-only code
authorEric Bollengier <eric@baculasystems.com>
Thu, 6 Apr 2023 15:54:31 +0000 (17:54 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:57:01 +0000 (13:57 +0200)
bacula/src/stored/block_util.c
bacula/src/stored/file_dev.c
bacula/src/stored/file_dev.h

index 7a173b1ae11709525eefd64f48c953fb6cd6e2ab..da53d382d2fb7508ccfec5d49eb8dd245db73bf8 100644 (file)
@@ -682,50 +682,6 @@ bool is_user_volume_size_reached(DCR *dcr, bool quiet)
             edit_uint64_with_commas(max_size, ed1),  dev->print_name(),
             dev->getVolCatName());
       }
-
-      if (dev->device->set_vol_read_only) {
-         btime_t now = time(NULL);
-         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"),
-                 dev->getVolCatName(), dev->print_name(), dev->errmsg);
-         }
-         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);;
-         }
-      }
-
-      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());
-            events_send_msg(dcr->jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)dcr->jcr,
-                            "Mark Volume \"%s\" as immutable", dev->getVolCatName());;
-            dev->VolCatInfo.Protected = 1;
-         }
-      }
       Dmsg4(100, "Maximum volume size %s exceeded Vol=%s device=%s.\n"
          "Marking Volume \"%s\" as Full.\n",
          edit_uint64_with_commas(max_size, ed1), dev->getVolCatName(),
@@ -882,6 +838,50 @@ 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);
+      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"),
+              dev->getVolCatName(), dev->print_name(), dev->errmsg);
+      }
+      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);;
+      }
+   }
+
+   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());
+         events_send_msg(dcr->jcr, "SJ0003", EVENTS_TYPE_VOLUME, me->hdr.name, (intptr_t)dcr->jcr,
+                         "Mark Volume \"%s\" as immutable", dev->getVolCatName());;
+         dev->VolCatInfo.Protected = 1;
+      }
+   }
 
    if (!dir_update_volume_info(dcr, false, true)) {
       Mmsg(dev->errmsg, _("Error sending Volume info to Director.\n"));
@@ -911,6 +911,7 @@ bool terminate_writing_volume(DCR *dcr)
    if (was_adata) {
       dcr->set_adata();
    }
+
    Leave(dbglvl);
    return ok;
 }
index 6026d8b8ea437e50b58374bfaaa86f7d0a37b359..225a43b2eb97135d32c70cccc495cdc5b13a6f1d 100644 (file)
@@ -216,7 +216,7 @@ bool file_dev::open_device(DCR *dcr, int omode)
                if (immutable && clear_immutable(getVolCatName(), &errmsg)) {
                   tryopen = true;
                }
-               if (readonly && set_writable(-1, getVolCatName())) {
+               if (readonly && set_writable(-1, getVolCatName()) == 0) {
                   tryopen = true;
                }
                if (tryopen) { /* It should be now possible to open the device with desired mode */
@@ -639,16 +639,13 @@ bool file_dev::is_attribute_supported(int attr)
 void file_dev::get_volume_fpath(const char *vol_name, POOLMEM **fname)
 {
    pm_strcpy(fname, dev_name);
+
    if (!IsPathSeparator((*fname)[strlen(*fname)-1])) {
       pm_strcat(fname, "/");
    }
 
    pm_strcat(fname, vol_name);
 
-   if (is_adata()) {
-      pm_strcat(fname, ADATA_EXTENSION);
-   }
-
    Dmsg1(DT_VOLUME|250, "Full volume path built: %s\n", *fname);
 }
 
index b30e998b9e0778499f52df8501edefeef512e66d..892fa75214f1ab4858acfe3465ed979b9d9d40b5 100644 (file)
@@ -25,7 +25,6 @@
 
 class file_dev : public DEVICE {
 private:
-   void get_volume_fpath(const char *vol_name, POOLMEM **buf);
    bool modify_fattr(const char *vol_name, int attr, bool set, POOLMEM **error);
    bool check_for_attr(const char *vol_name, int attr);
    bool set_fattr(const char *vol_name, int attr, POOLMEM **error);
@@ -33,6 +32,9 @@ private:
    bool append_open_needed(const char *vol_name);
    bool is_attribute_supported(int attr);
 
+protected:
+   virtual void get_volume_fpath(const char *vol_name, POOLMEM **buf);
+
 public:
 
    file_dev() { };