From: Eric Bollengier Date: Thu, 6 Apr 2023 15:54:31 +0000 (+0200) Subject: Cleanup immutability/read-only code X-Git-Tag: Beta-15.0.0~193 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ab8fda3954679f3bf357cdf964977cd06503aeb;p=thirdparty%2Fbacula.git Cleanup immutability/read-only code --- diff --git a/bacula/src/stored/block_util.c b/bacula/src/stored/block_util.c index 7a173b1ae..da53d382d 100644 --- a/bacula/src/stored/block_util.c +++ b/bacula/src/stored/block_util.c @@ -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; } diff --git a/bacula/src/stored/file_dev.c b/bacula/src/stored/file_dev.c index 6026d8b8e..225a43b2e 100644 --- a/bacula/src/stored/file_dev.c +++ b/bacula/src/stored/file_dev.c @@ -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); } diff --git a/bacula/src/stored/file_dev.h b/bacula/src/stored/file_dev.h index b30e998b9..892fa7521 100644 --- a/bacula/src/stored/file_dev.h +++ b/bacula/src/stored/file_dev.h @@ -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() { };